X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=inline;f=lib%2Freport.php;h=f06e8ca9d7c54a85bdf45342d9bfbe22845cebea;hb=refs%2Ftags%2Fuat-2013-11-07;hp=05ca904ce99fd06196722c3f7e3e5c0f210180d9;hpb=ea8e72aabd56f371b4f54b6e464d8bceb049cfb5;p=readifood.git
diff --git a/lib/report.php b/lib/report.php
index 05ca904..f06e8ca 100644
--- a/lib/report.php
+++ b/lib/report.php
@@ -29,15 +29,9 @@
$oldest = date('Y-m-d', $date);
$then = $date;
- echo "\n";
- for ($date = $then; $date <= $now; $date += 86400) {
- option("from", date('Y-m-d', $date), date('j F Y', $date), $from);
- }
- echo " to \n";
- for ($date = $then; $date <= $now; $date += 86400) {
- option("to", date('Y-m-d', $date), date('j F Y', $date), $to);
- }
- echo " \n";
+ datepicker("from", $from, true, null, false, "to");
+ echo " to ";
+ datepicker("to", $to, true, "from", false);
submit("show_reports", "Show");
end_form();
@@ -88,23 +82,83 @@
/* Fill in missing weeks. XXX */
for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) {
echo "
\n";
- printf(" 0 Week %d \n", $missing_week);
+ printf(" 0 Week %d \n", $missing_week);
echo " \n";
}
echo "\n";
- printf(" %d Week %d \n", $row->getCount(), $week);
+ printf(" %d Week %d \n", $row->getCount(), $week);
echo " \n";
$last_week = $week;
}
echo "\n";
- echo " $total Total \n";
+ echo " $total Total \n";
+ echo " \n";
+ 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(" %d %s \n", $count, htmlspecialchars($postcode));
+ echo " \n";
+ }
+ echo "\n";
+ echo " $total Total \n";
echo " \n";
echo "
\n";
}
- function show_size_report(&$order_ids) {
+ function show_contents_report(&$order_ids, $parcel_size, $grand_total) {
+ global $parcel_sizes, $parcel_contents;
+
+ $total = 0;
+ for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+ $q->where(sprintf("parcel & %d", $parcel_size));
+ $q->where(sprintf("parcel & %d", (1 << $i)));
+ $contents = $q->find();
+ $total += count($contents);
+ echo "\n";
+ printf(" %d %s \n", count($contents), htmlspecialchars($parcel_contents[$i]));
+ echo " \n";
+ }
+
+ /* No special contents. */
+ echo "\n";
+ printf(" %d %s no special contents \n", $grand_total - $total, htmlspecialchars($parcel_sizes[$parcel_size >> 1]));
+ echo " \n";
+ }
+
+ function show_parcel_report(&$order_ids) {
global $parcel_sizes;
- echo "Orders by parcel size \n";
+ echo "Orders by parcel type \n";
$q = new OrderQuery;
$q->filterById($order_ids);
@@ -117,12 +171,13 @@
foreach ($rows as $row) {
echo "\n";
$requester = get_contact_by_id($row->getRequesterId());
- printf(" %d %s \n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
+ printf(" %d %s \n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
$total += $row->getCount();
echo " \n";
+ show_contents_report($order_ids, $row->getSize(), $row->getCount());
}
echo "\n";
- echo " $total Total \n";
+ echo " $total Total \n";
echo " \n";
echo "\n";
}
@@ -141,12 +196,12 @@
foreach ($rows as $row) {
echo "\n";
$requester = get_contact_by_id($row->getRequesterId());
- printf(" %d %s \n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+ printf(" %d %s \n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
$total += $row->getCount();
echo " \n";
}
echo "\n";
- echo " $total Total \n";
+ echo " $total Total \n";
echo " \n";
echo "\n";
}
@@ -177,7 +232,8 @@
}
show_order_report($order_state_ids);
- show_size_report($order_ids);
+ show_postcode_report($order_ids);
+ show_parcel_report($order_ids);
show_requester_report($order_ids);
}