Compact delivery details.
[readifood.git] / lib / report.php
index b2bc16d..5ed0fae 100644 (file)
     end_form();
   }
 
-  function check_report_dates($from, $to) {
-    list($y, $m, $d) = explode('-', $from);
-    if (! checkdate($m, $d, $y)) {
-      echo "<p>Invalid report start date!</p>\n";
-      return false;
-    }
-    $start = mktime(0, 0, 0, $m, $d, $y);
-
-    list($y, $m, $d) = explode('-', $to);
-    if (! checkdate($m, $d, $y)) {
-      echo "<p>Invalid report end date!</p>\n";
-      return false;
-    }
-    $end = mktime(0, 0, 0, $m, $d, $y);
-
-    if ($end < $start) {
-      echo "<p>Report end date is earlier than start date!</p>\n";
-      return false;
-    }
-
-    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++) {
     echo "</table>\n";
   }
 
+  function show_beneficiary_report(&$order_ids) {
+    global $parcel_sizes;
+    echo "<h3>Unique beneficiaries</h3>\n";
+
+    $q = new OrderQuery;
+    $q->filterById($order_ids);
+    $q->withColumn(sprintf("parcel & %d", (1 << count($parcel_sizes)) - 1), 'size');
+    $q->withColumn('count(distinct beneficiary_id)', 'count');
+    $q->addGroupByColumn('size')->addAscendingOrderByColumn('size');
+    $rows = $q->find();
+    $total = 0;
+    echo "<table class=\"report\">\n";
+    foreach ($rows as $row) {
+      echo "<tr>\n";
+      /* This is only correct if we assume the sizes are 1 (single), 2 (couple) and 4 (family). */
+      $count = $row->getSize() * $row->getCount();
+      printf("  <td align=\"right\">%d</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
+      if ($row->getSize() > 1) {
+        echo "<tr class=\"small\">\n";
+        printf("  <td align=\"right\">%d</td><td>individuals%s</td>\n", $count, $row->getSize() > 2 ? ' (estimated)' : '');
+        echo "</tr>\n";
+      }
+      $total += $count;
+      echo "</tr>\n";
+    }
+    echo "<tr>\n";
+    echo "  <td align=\"right\" class=\"strong\">$total</td><td class=\"strong\">Total individuals (estimated)</td>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+  }
+
   function show_requester_report(&$order_ids) {
     echo "<h3>Orders by referrer</h3>\n";
 
   }
 
   function show_reports($from, $to) {
-    if (! check_report_dates($from, $to)) return;
+    if (! check_dates('report', $from, $to)) return;
 
     echo "<p>Showing reports for the period <strong>$from</strong> to <strong>$to</strong>.</p>\n";
 
     $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);
+    $dups = array();
     foreach ($order_states as $order_state) {
-      $order_ids[] = $order_state->getOrderId();
-      $order_state_ids[] = $order_state->getId();
+      $order_id = $order_state->getOrderId();
+      $order_ids[] = $order_id;
+      if (! $dups[$order_id]) $order_state_ids[] = $order_state->getId();
+      $dups[$order_id] = true;
     }
     $q = new OrderQuery;
     $q->filterById($order_ids);
       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_beneficiary_report($order_ids);
     show_requester_report($order_ids);
   }