From d2336ce5f06b0e2762149edce0d7253437914f2b Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Tue, 24 Sep 2013 09:13:26 +0100 Subject: [PATCH] Report orders by beneficiary postcode. --- lib/report.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/report.php b/lib/report.php index 2209d52..d8699d2 100644 --- a/lib/report.php +++ b/lib/report.php @@ -102,6 +102,44 @@ echo "\n"; } + function show_postcode_report(&$order_ids) { + echo "

Orders by postcode

\n"; + + /* + No regex replace support in MySQL so we'll have to retrieve all records + and group the postcodes ourselves. + */ + $q = new OrderQuery; + $q->filterById($order_ids); + $q->join("Beneficiary"); + /* No foreign key so we need to list the two tables. */ + $q->join("Beneficiary.Address"); + /* Not a FoodOrder column so we need to ask for it explicitly. */ + $q->withColumn('upper(postcode)', 'postcode'); + $rows = $q->find(); + + $total = 0; + $postcodes = array(); + foreach ($rows as $row) { + $postcode = preg_replace('/\s*[0-9][A-Z]+$/', '', trim($row->getPostcode())); + if (! $postcode) $postcode = "Unknown"; + $postcodes[$postcode]++; + $total++; + } + ksort($postcodes); + + echo "\n"; + foreach ($postcodes as $postcode => $count) { + echo "\n"; + printf(" \n", $count, htmlspecialchars($postcode)); + echo "\n"; + } + echo "\n"; + echo " \n"; + echo "\n"; + echo "
%d%s
$totalTotal
\n"; + } + function show_contents_report(&$order_ids, $parcel_size, $grand_total) { global $parcel_sizes, $parcel_contents; @@ -200,6 +238,7 @@ } show_order_report($order_state_ids); + show_postcode_report($order_ids); show_parcel_report($order_ids); show_requester_report($order_ids); } -- 2.20.1