Report orders by beneficiary postcode.
[readifood.git] / lib / report.php
index 4a632f8..d8699d2 100644 (file)
     return true;
   }
 
+  function show_order_report(&$order_state_ids) {
+    echo "<h3>Orders by week</h3>\n";
+
+    $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 "<table class=\"report\">\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;
+      $total += $row->getCount();
+      /* Fill in missing weeks. XXX */
+      for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) {
+        echo "<tr>\n";
+        printf("  <td align=\"right\">0</td><td>Week %d</td>\n", $missing_week);
+        echo "</tr>\n";
+      }
+      echo "<tr>\n";
+      printf("  <td align=\"right\">%d</td><td>Week %d</td>\n", $row->getCount(), $week);
+      echo "</tr>\n";
+      $last_week = $week;
+    }
+    echo "<tr>\n";
+    echo "  <td align=\"right\" class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+  }
+
+  function show_postcode_report(&$order_ids) {
+    echo "<h3>Orders by postcode</h3>\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 "<table class=\"report\">\n";
+    foreach ($postcodes as $postcode => $count) {
+      echo "<tr>\n";
+      printf("  <td align=\"right\">%d</td><td>%s</td>\n", $count, htmlspecialchars($postcode));
+      echo "</tr>\n";
+    }
+    echo "<tr>\n";
+    echo "  <td align=\"right\" class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+  }
+
+  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 "<tr class=\"small\">\n";
+      printf("  <td align=\"right\">%d</td><td>%s</td>\n", count($contents), htmlspecialchars($parcel_contents[$i]));
+      echo "</tr>\n";
+    }
+
+    /* No special contents. */
+    echo "<tr class=\"small\">\n";
+    printf("  <td align=\"right\">%d</td><td>%s no special contents</td>\n", $grand_total - $total, htmlspecialchars($parcel_sizes[$parcel_size >> 1]));
+    echo "</tr>\n";
+  }
+
+  function show_parcel_report(&$order_ids) {
+    global $parcel_sizes;
+    echo "<h3>Orders by parcel type</h3>\n";
+
+    $q = new OrderQuery;
+    $q->filterById($order_ids);
+    $q->withColumn(sprintf("parcel & %d", (1 << count($parcel_sizes)) - 1), 'size');
+    $q->withColumn('count(*)', 'count');
+    $q->addGroupByColumn('size')->addAscendingOrderByColumn('size');
+    $rows = $q->find();
+    $total = 0;
+    echo "<table class=\"report\">\n";
+    foreach ($rows as $row) {
+      echo "<tr>\n";
+      $requester = get_contact_by_id($row->getRequesterId());
+      printf("  <td align=\"right\">%d</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($parcel_sizes[$row->getSize() >> 1]));
+      $total += $row->getCount();
+      echo "</tr>\n";
+      show_contents_report($order_ids, $row->getSize(), $row->getCount());
+    }
+    echo "<tr>\n";
+    echo "  <td align=\"right\" class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+  }
+
   function show_requester_report(&$order_ids) {
     echo "<h3>Orders by referrer</h3>\n";
 
     $q->withColumn('count(*)', 'count');
     $q->groupByRequesterId()->addDescendingOrderByColumn('count');
     $rows = $q->find();
-    echo "<table>\n";
+    $total = 0;
+    echo "<table class=\"report\">\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()));
+      printf("  <td align=\"right\">%d</td><td>%s</td>\n", $row->getCount(), htmlspecialchars($requester->getDisplayname()));
+      $total += $row->getCount();
       echo "</tr>\n";
     }
+    echo "<tr>\n";
+    echo "  <td align=\"right\" class=\"strong\">$total</td><td class=\"strong\">Total</td>\n";
+    echo "</tr>\n";
     echo "</table>\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();
+    foreach ($order_states as $order_state) {
+      $order_ids[] = $order_state->getOrderId();
+      $order_state_ids[] = $order_state->getId();
+    }
     $q = new OrderQuery;
     $q->filterById($order_ids);
 
       return;
     }
 
+    show_order_report($order_state_ids);
+    show_postcode_report($order_ids);
+    show_parcel_report($order_ids);
     show_requester_report($order_ids);
   }