Report orders by requester.
[readifood.git] / lib / report.php
index e74ac07..4a632f8 100644 (file)
     return true;
   }
 
+  function show_requester_report(&$order_ids) {
+    echo "<h3>Orders by referrer</h3>\n";
+
+    $q = new OrderQuery;
+    $q->filterById($order_ids);
+    $q->withColumn('count(*)', 'count');
+    $q->groupByRequesterId()->addDescendingOrderByColumn('count');
+    $rows = $q->find();
+    echo "<table>\n";
+    /* XXX Join! */
+    foreach ($rows as $row) {
+      echo "<tr>\n";
+      $requester = get_contact_by_id($row->getRequesterId());
+      printf("  <td>%d</td><td>&nbsp;</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+      echo "</tr>\n";
+    }
+    echo "</table>\n";
+  }
+
   function show_reports($from, $to) {
     if (! check_report_dates($from, $to)) return;
 
     echo "<p>Showing reports for the period <strong>$from</strong> to <strong>$to</strong>.</p>\n";
+
+    /* Get orders. */
+    $order_ids = array();
+    /* XXX: Order 51 changed to state delivered in May then updated in June. */
+    $dbh = Propel::getConnection();
+    $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);
+    foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
+    $q = new OrderQuery;
+    $q->filterById($order_ids);
+
+    if (! count($order_ids)) {
+      echo "<p>No results!</p>\n";
+      return;
+    }
+
+    show_requester_report($order_ids);
   }
 
   if (count($parameters)) {