Allow placing recurring orders.
authorIain Patterson <me@iain.cx>
Wed, 10 Apr 2013 16:40:58 +0000 (12:40 -0400)
committerIain Patterson <me@iain.cx>
Wed, 10 Apr 2013 16:40:58 +0000 (12:40 -0400)
New orders can be assigned a recurrence of up to 3 weekly iterations.

lib/order.php

index 40b645d..74306d9 100644 (file)
     echo "<tr>\n";
     echo "  <td>Delivery</td>\n";
     /* XXX: Find suitable dates from area. */
-    echo "  <td>"; show_date_form("date", $order->getDate()); echo "</td>\n";
+    echo "  <td>";
+    show_date_form("date", $order->getDate());
+    if (! $order->getDate()) {
+      echo " and recur for <select name=\"recurrence\">\n";
+      for ($i = 0; $i < 4; $i++) option("recurrence", $i, $i);
+      echo "</select> weeks";
+    }
+    echo "</td>\n";
     echo "</tr>\n";
 
     /* Referrer. */
     $state = $_POST['state'];
     if (! $state) $state = $GLOBALS['STATE_PLACED'];
 
-    if (! $date) $date = time();
+    if ($date) {
+      list($y, $m, $d) = explode('-', $date);
+      $then = mktime(0, 0, 0, $m, $d, $y);
+    }
+    else $then = time();
     /* XXX: check date */
 
     $requester = get_contact_by_id($requester_id);
       return false;
     }
 
-    $order->setDate($date);
-    $order->setRequesterId($requester_id);
-    $order->setBeneficiaryId($beneficiary_id);
-    $order->setHubId($hub_id);
-    $order->setQuantity($quantity);
+    if ($new && isset($_POST['recurrence'])) $recurrence = $_POST['recurrence'];
+    if (! $recurrence) $recurrence = 0;
 
-    try {
-      $order->save();
+    $now = time();
+    for ($i = 0; $i <= $recurrence; $i++) {
+      if ($i) {
+        echo "<p>Creating recurrence $i.</p>\n";
+        $order = new Order;
+      }
 
-      $order_state = new OrderState;
-      $order_state->setUpdated(time());
-      $order_state->setOrderId($order->getId());
-      $order_state->setUserId($user_id);
-      $order_state->setDriverId($driver_id);
-      $order_state->setState($state);
+      $order->setDate($then + 7 * 86400 * $i);
+      $order->setRequesterId($requester_id);
+      $order->setBeneficiaryId($beneficiary_id);
+      $order->setHubId($hub_id);
+      $order->setQuantity($quantity);
 
-      $order_state->save();
-    }
-    catch (Exception $e) {
-      if ($new) echo "<p>Error placing order.</p>\n";
-      else echo "<p>Error updating order.</p>\n";
-      return false;
+      /* XXX: begin/commit */
+      try {
+        $order->save();
+
+        $order_state = new OrderState;
+        $order_state->setUpdated($now);
+        $order_state->setOrderId($order->getId());
+        $order_state->setUserId($user_id);
+        $order_state->setDriverId($driver_id);
+        $order_state->setState($state);
+
+        $order_state->save();
+      }
+      catch (Exception $e) {
+        if ($new) echo "<p>Error placing order.</p>\n";
+        else echo "<p>Error updating order.</p>\n";
+        echo "<p>" . $e->getMessage() . "</p>\n";
+        return false;
+      }
     }
 
     return true;