X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Freport.php;h=5ed0fae69aba30974005c206cb060df333faf324;hb=refs%2Ftags%2F2022-10-31;hp=c58864dc770b18ea0669116926a2c8f010836096;hpb=7cbf38d98e84961ccab6cd8213e322fa775a2b9e;p=readifood.git
diff --git a/lib/report.php b/lib/report.php
index c58864d..5ed0fae 100644
--- a/lib/report.php
+++ b/lib/report.php
@@ -37,40 +37,23 @@
end_form();
}
- function check_report_dates($from, $to) {
- list($y, $m, $d) = explode('-', $from);
- if (! checkdate($m, $d, $y)) {
- echo "
Invalid report start date!
\n";
- return false;
- }
- $start = mktime(0, 0, 0, $m, $d, $y);
-
- list($y, $m, $d) = explode('-', $to);
- if (! checkdate($m, $d, $y)) {
- echo "Invalid report end date!
\n";
- return false;
- }
- $end = mktime(0, 0, 0, $m, $d, $y);
-
- if ($end < $start) {
- echo "Report end date is earlier than start date!
\n";
- return false;
- }
-
- return true;
- }
-
- function show_order_report(&$order_state_ids) {
+ function show_order_report($from, &$order_state_ids) {
echo "Orders by week
\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;
@@ -81,15 +64,9 @@
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++) {
@@ -194,6 +171,37 @@
echo "\n";
}
+ function show_beneficiary_report(&$order_ids) {
+ global $parcel_sizes;
+ echo "Unique beneficiaries
\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 "\n";
+ foreach ($rows as $row) {
+ echo "\n";
+ /* This is only correct if we assume the sizes are 1 (single), 2 (couple) and 4 (family). */
+ $count = $row->getSize() * $row->getCount();
+ printf(" %d | %s | \n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
+ if ($row->getSize() > 1) {
+ echo "
\n";
+ printf(" %d | individuals%s | \n", $count, $row->getSize() > 2 ? ' (estimated)' : '');
+ echo "
\n";
+ }
+ $total += $count;
+ echo "\n";
+ }
+ echo "\n";
+ echo " $total | Total individuals (estimated) | \n";
+ echo "
\n";
+ echo "
\n";
+ }
+
function show_requester_report(&$order_ids) {
echo "Orders by referrer
\n";
@@ -219,7 +227,7 @@
}
function show_reports($from, $to) {
- if (! check_report_dates($from, $to)) return;
+ if (! check_dates('report', $from, $to)) return;
echo "Showing reports for the period $from to $to.
\n";
@@ -246,9 +254,10 @@
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);
}