Handle missing weeks at the start of the report period. 2014-04-10
authorIain Patterson <me@iain.cx>
Thu, 10 Apr 2014 15:29:18 +0000 (11:29 -0400)
committerIain Patterson <me@iain.cx>
Thu, 10 Apr 2014 15:29:18 +0000 (11:29 -0400)
If there were no orders during the first week(s) of a report period we
were incorrectly treating the first week in which there were orders as
week 1.

We now determine the week offset based on the yearweek of the period
start date and correctly include one or more weeks with zero orders in
the output.

lib/report.php

index c58864d..999bbc5 100644 (file)
     return true;
   }
 
-  function show_order_report(&$order_state_ids) {
+  function show_order_report($from, &$order_state_ids) {
     echo "<h3>Orders by week</h3>\n";
 
+    /* Handle missing weeks at the start of the range. */
+    $dbh = Propel::getConnection();
+    $sth = $dbh->prepare("select yearweek(:from)");
+    $sth->execute(array(':from' => $from));
+    list($first_week) = $sth->fetch();
+    $year_offset = substr($first_week, 0, 4);
+    $week_offset = substr($first_week, 4, 2) - 1;
+
     $q = new OrderStateQuery;
     $q->filterById($order_state_ids);
     $q->withColumn('yearweek(updated)', 'week');
     $q->withColumn('count(*)', 'count');
     $q->addGroupByColumn('week')->orderByUpdated();
     $rows = $q->find();
-    $year_offset = 0;
-    $week_offset = 0;
     $week = 1;
     $last_week = 0;
     $total = 0;
         Beware that week 201401 comes after 201352.
       */
       $yearweek = $row->getWeek();
-      if (! $week_offset) {
-        $year_offset = substr($yearweek, 0, 4);
-        $week_offset = substr($yearweek, 4, 2) - 1;
-      }
-      else {
-        $y = substr($yearweek, 0, 4);
-        $w = substr($yearweek, 4, 2);
-        $week = (($y - $year_offset) * 52) + ($w - $week_offset);
-      }
+      $y = substr($yearweek, 0, 4);
+      $w = substr($yearweek, 4, 2);
+      $week = (($y - $year_offset) * 52) + ($w - $week_offset);
       $total += $row->getCount();
       /* Fill in missing weeks. XXX */
       for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) {
       return;
     }
 
-    show_order_report($order_state_ids);
+    show_order_report($from, $order_state_ids);
     show_postcode_report($order_ids);
     show_parcel_report($order_ids);
     show_requester_report($order_ids);