Report orders by week.
authorIain Patterson <me@iain.cx>
Mon, 5 Aug 2013 17:37:14 +0000 (18:37 +0100)
committerIain Patterson <me@iain.cx>
Wed, 11 Sep 2013 14:54:27 +0000 (10:54 -0400)
lib/report.php

index 4a632f8..2fabab4 100644 (file)
     return true;
   }
 
+  function show_order_report(&$order_state_ids) {
+    echo "<h3>Orders by week</h3>\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 "<table>\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 "<tr>\n";
+        printf("  <td>0</td><td>Week %d</td>\n", $missing_week);
+        echo "</tr>\n";
+      }
+      echo "<tr>\n";
+      printf("  <td>%d</td><td>Week %d</td>\n", $row->getCount(), $week);
+      echo "</tr>\n";
+      $last_week = $week;
+    }
+    echo "<tr>\n";
+    echo "  <td class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+  }
+
   function show_requester_report(&$order_ids) {
     echo "<h3>Orders by referrer</h3>\n";
 
     $q->withColumn('count(*)', 'count');
     $q->groupByRequesterId()->addDescendingOrderByColumn('count');
     $rows = $q->find();
+    $total = 0;
     echo "<table>\n";
     /* XXX Join! */
     foreach ($rows as $row) {
       echo "<tr>\n";
       $requester = get_contact_by_id($row->getRequesterId());
-      printf("  <td>%d</td><td>&nbsp;</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+      printf("  <td>%d</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+      $total += $row->getCount();
       echo "</tr>\n";
     }
+    echo "<tr>\n";
+    echo "  <td class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
     echo "</table>\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();
+    foreach ($order_states as $order_state) {
+      $order_ids[] = $order_state->getOrderId();
+      $order_state_ids[] = $order_state->getId();
+    }
     $q = new OrderQuery;
     $q->filterById($order_ids);
 
       return;
     }
 
+    show_order_report($order_state_ids);
     show_requester_report($order_ids);
   }