From 097fa2613bbc37d9573fda5095cc17e58fe19ed9 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Sun, 2 Sep 2018 18:05:02 -0400 Subject: [PATCH] Show unique beneficiary report. Count unique recipients of at least one parcel of each size during the report period. For example if a beneficiary received three "Couple" sized parcels we would count two individuals. Family units are averaged at four individuals since we do not record their actual size anywhere. --- lib/report.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/report.php b/lib/report.php index aa9e6e5..5ed0fae 100644 --- a/lib/report.php +++ b/lib/report.php @@ -171,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(" \n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1])); + if ($row->getSize() > 1) { + echo "\n"; + printf(" \n", $count, $row->getSize() > 2 ? ' (estimated)' : ''); + echo "\n"; + } + $total += $count; + echo "\n"; + } + echo "\n"; + echo " \n"; + echo "\n"; + echo "
%d%s
%dindividuals%s
$totalTotal individuals (estimated)
\n"; + } + function show_requester_report(&$order_ids) { echo "

Orders by referrer

\n"; @@ -226,6 +257,7 @@ 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); } -- 2.7.4