Show unique beneficiary report.
[readifood.git] / lib / report.php
index aa9e6e5..5ed0fae 100644 (file)
     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";
 
     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);
   }