X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=lib%2Freport.php;h=999bbc50c4e3c78aa73c1255d6fc421b4b91acf0;hb=refs%2Ftags%2Fuat-2016-03-29;hp=d8699d22691eedb88e5e6feac975652d3e34155d;hpb=d2336ce5f06b0e2762149edce0d7253437914f2b;p=readifood.git
diff --git a/lib/report.php b/lib/report.php
index d8699d2..999bbc5 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();
@@ -66,24 +60,36 @@
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();
- $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();
+ $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++) {
@@ -225,9 +231,12 @@
$sth = $dbh->prepare("select * from OrderState o where updated=(select min(updated) from OrderState where order_id=o.order_id and state & " . $GLOBALS['STATE_DELIVERED'] . ") and updated between '$from' and '$to'");
$sth->execute();
$order_states = OrderStatePeer::populateObjects($sth);
+ $dups = array();
foreach ($order_states as $order_state) {
- $order_ids[] = $order_state->getOrderId();
- $order_state_ids[] = $order_state->getId();
+ $order_id = $order_state->getOrderId();
+ $order_ids[] = $order_id;
+ if (! $dups[$order_id]) $order_state_ids[] = $order_state->getId();
+ $dups[$order_id] = true;
}
$q = new OrderQuery;
$q->filterById($order_ids);
@@ -237,7 +246,7 @@
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_requester_report($order_ids);