\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";
+ }
+
function show_reports($from, $to) {
if (! check_report_dates($from, $to)) return;
echo "
Showing reports for the period $from to $to.
\n";
+
+ /* Get orders. */
+ $order_ids = array();
+ $order_state_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();
+ $order_state_ids[] = $order_state->getId();
+ }
+ $q = new OrderQuery;
+ $q->filterById($order_ids);
+
+ if (! count($order_ids)) {
+ echo "