From: Iain Patterson Date: Wed, 10 Apr 2013 16:40:58 +0000 (-0400) Subject: Allow placing recurring orders. X-Git-Tag: 2013-04-10~5 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=cf4cf65ff0e729c6ba52cede471b7c5ae2b837ba;p=readifood.git Allow placing recurring orders. New orders can be assigned a recurrence of up to 3 weekly iterations. --- diff --git a/lib/order.php b/lib/order.php index 40b645d..74306d9 100644 --- a/lib/order.php +++ b/lib/order.php @@ -244,7 +244,14 @@ echo "\n"; echo " Delivery\n"; /* XXX: Find suitable dates from area. */ - echo " "; show_date_form("date", $order->getDate()); echo "\n"; + echo " "; + show_date_form("date", $order->getDate()); + if (! $order->getDate()) { + echo " and recur for weeks"; + } + echo "\n"; echo "\n"; /* Referrer. */ @@ -373,7 +380,11 @@ $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); @@ -401,28 +412,41 @@ 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 "

Creating recurrence $i.

\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 "

Error placing order.

\n"; - else echo "

Error updating order.

\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 "

Error placing order.

\n"; + else echo "

Error updating order.

\n"; + echo "

" . $e->getMessage() . "

\n"; + return false; + } } return true;