X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Freport.php;h=b2bc16d2d4aa759974569439a498687c5b2f3797;hb=e4e05bf5dad3c8e514c5a939867758170ba36dd7;hp=2209d528f65c26707e95df5280e12ad8979536b0;hpb=6c0392caed936af1c4f3fba0c49beda59011f7d5;p=readifood.git diff --git a/lib/report.php b/lib/report.php index 2209d52..b2bc16d 100644 --- a/lib/report.php +++ b/lib/report.php @@ -29,15 +29,9 @@ $oldest = date('Y-m-d', $date); $then = $date; - echo " to \n"; + datepicker("from", $from, true, null, false, "to"); + echo " to "; + datepicker("to", $to, true, "from", false); submit("show_reports", "Show"); end_form(); @@ -75,15 +69,27 @@ $q->withColumn('count(*)', 'count'); $q->addGroupByColumn('week')->orderByUpdated(); $rows = $q->find(); + $year_offset = 0; $week_offset = 0; $week = 1; $last_week = 0; $total = 0; echo "\n"; foreach ($rows as $row) { - /* Convert week of year to date range. */ - if (! $week_offset) $week_offset = $row->getWeek() - 1; - else $week = $row->getWeek() - $week_offset; + /* + Convert week of year to date range. + 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); + } $total += $row->getCount(); /* Fill in missing weeks. XXX */ for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) { @@ -102,6 +108,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 +244,7 @@ } show_order_report($order_state_ids); + show_postcode_report($order_ids); show_parcel_report($order_ids); show_requester_report($order_ids); }