X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=lib%2Freport.php;h=2209d528f65c26707e95df5280e12ad8979536b0;hb=4aa19b269d7630781d67a924c0b91a0965fa4862;hp=e74ac07b34aa16a74adbf762130ebd5a8051213e;hpb=8ed7023cec6e6c090aa4c0776f5b30cad8f59c11;p=readifood.git
diff --git a/lib/report.php b/lib/report.php
index e74ac07..2209d52 100644
--- a/lib/report.php
+++ b/lib/report.php
@@ -66,10 +66,142 @@
return true;
}
+ function show_order_report(&$order_state_ids) {
+ echo "
Orders by week
\n";
+
+ $q = new OrderStateQuery;
+ $q->filterById($order_state_ids);
+ $q->withColumn('yearweek(updated)', 'week');
+ $q->withColumn('count(*)', 'count');
+ $q->addGroupByColumn('week')->orderByUpdated();
+ $rows = $q->find();
+ $week_offset = 0;
+ $week = 1;
+ $last_week = 0;
+ $total = 0;
+ echo "\n";
+ foreach ($rows as $row) {
+ /* Convert week of year to date range. */
+ if (! $week_offset) $week_offset = $row->getWeek() - 1;
+ else $week = $row->getWeek() - $week_offset;
+ $total += $row->getCount();
+ /* Fill in missing weeks. XXX */
+ for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) {
+ echo "\n";
+ printf(" 0 | Week %d | \n", $missing_week);
+ echo "
\n";
+ }
+ echo "\n";
+ printf(" %d | Week %d | \n", $row->getCount(), $week);
+ echo "
\n";
+ $last_week = $week;
+ }
+ echo "\n";
+ echo " $total | Total | \n";
+ echo "
\n";
+ echo "
\n";
+ }
+
+ function show_contents_report(&$order_ids, $parcel_size, $grand_total) {
+ global $parcel_sizes, $parcel_contents;
+
+ $total = 0;
+ for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+ $q->where(sprintf("parcel & %d", $parcel_size));
+ $q->where(sprintf("parcel & %d", (1 << $i)));
+ $contents = $q->find();
+ $total += count($contents);
+ echo "\n";
+ printf(" %d | %s | \n", count($contents), htmlspecialchars($parcel_contents[$i]));
+ echo "
\n";
+ }
+
+ /* No special contents. */
+ echo "\n";
+ printf(" %d | %s no special contents | \n", $grand_total - $total, htmlspecialchars($parcel_sizes[$parcel_size >> 1]));
+ echo "
\n";
+ }
+
+ function show_parcel_report(&$order_ids) {
+ global $parcel_sizes;
+ echo "Orders by parcel type
\n";
+
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+ $q->withColumn(sprintf("parcel & %d", (1 << count($parcel_sizes)) - 1), 'size');
+ $q->withColumn('count(*)', 'count');
+ $q->addGroupByColumn('size')->addAscendingOrderByColumn('size');
+ $rows = $q->find();
+ $total = 0;
+ echo "\n";
+ foreach ($rows as $row) {
+ echo "\n";
+ $requester = get_contact_by_id($row->getRequesterId());
+ printf(" %d | %s | \n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
+ $total += $row->getCount();
+ echo "
\n";
+ show_contents_report($order_ids, $row->getSize(), $row->getCount());
+ }
+ echo "\n";
+ echo " $total | Total | \n";
+ echo "
\n";
+ echo "
\n";
+ }
+
+ function show_requester_report(&$order_ids) {
+ echo "Orders by referrer
\n";
+
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+ $q->withColumn('count(*)', 'count');
+ $q->groupByRequesterId()->addDescendingOrderByColumn('count');
+ $rows = $q->find();
+ $total = 0;
+ echo "\n";
+ /* XXX Join! */
+ foreach ($rows as $row) {
+ echo "\n";
+ $requester = get_contact_by_id($row->getRequesterId());
+ printf(" %d | %s | \n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+ $total += $row->getCount();
+ echo "
\n";
+ }
+ echo "\n";
+ echo " $total | Total | \n";
+ echo "
\n";
+ echo "
\n";
+ }
+
function show_reports($from, $to) {
if (! check_report_dates($from, $to)) return;
echo "Showing reports for the period $from to $to.
\n";
+
+ /* Get orders. */
+ $order_ids = array();
+ $order_state_ids = array();
+ /* XXX: Order 51 changed to state delivered in May then updated in June. */
+ $dbh = Propel::getConnection();
+ $sth = $dbh->prepare("select * from OrderState o where updated=(select min(updated) from OrderState where order_id=o.order_id and state & " . $GLOBALS['STATE_DELIVERED'] . ") and updated between '$from' and '$to'");
+ $sth->execute();
+ $order_states = OrderStatePeer::populateObjects($sth);
+ foreach ($order_states as $order_state) {
+ $order_ids[] = $order_state->getOrderId();
+ $order_state_ids[] = $order_state->getId();
+ }
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+
+ if (! count($order_ids)) {
+ echo "No results!
\n";
+ return;
+ }
+
+ show_order_report($order_state_ids);
+ show_parcel_report($order_ids);
+ show_requester_report($order_ids);
}
if (count($parameters)) {