From bfff2c5a7f96e5d63cbcaefb7ca2ce97cf63cd38 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Tue, 30 Apr 2013 12:44:39 -0400 Subject: [PATCH] Show deliveries on arbitrary dates. Allow showing drivers and deliveries scheduled on dates other than today. --- lib/delivery.php | 74 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/lib/delivery.php b/lib/delivery.php index dddf2a2..4c7c4f0 100644 --- a/lib/delivery.php +++ b/lib/delivery.php @@ -1,19 +1,33 @@ filterByDate(time('Y-m-d'))->find(); + $orders = $q->filterByDate($then)->find(); if (count($orders)) { foreach ($orders as $order) $order_ids[] = $order->getId(); } - else echo "

No deliveries today.

\n"; + else echo "

No deliveries for $date.

\n"; return $order_ids; } + /* Find orders scheduled for delivery today. */ + function get_orders_for_today() { + return get_orders_for_date(date('Y-m-d'), time()); + } + /* Find drivers with deliveries today. */ function get_drivers_by_order_id($order_ids) { $driver_ids = array(); @@ -53,7 +67,7 @@ return $order_ids; } - function show_driver_forms($driver_ids) { + function show_driver_forms($driver_ids, $date = null) { global $module; if (! count($driver_ids)) return; @@ -65,13 +79,16 @@ return; } - echo "

Drivers with deliveries scheduled:"; + echo "

Drivers with deliveries scheduled on "; + if ($date) echo $date; + else echo date('Y-m-d', time()); + echo ":"; foreach ($contacts as $contact) { - printf("
\n%s", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname())); + printf("
\n%s", ($date) ? "date/$date/" : "", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname())); } } - function show_driver_schedule($driver_name = null, $driver_id = null) { + function show_driver_schedule($driver_name = null, $driver_id = null, $date = null) { if (isset($driver_id)) $contact = get_contact_by_id($driver_id); else if (isset($driver_name)) $contact = get_contact_by_name($driver_name); if (! $contact) { @@ -79,10 +96,19 @@ return; } - echo "

Delivery schedule for " . htmlspecialchars($contact->getDisplayname()) . "

\n"; - $order_ids = get_driver_schedule_by_order_id($contact->getId(), get_orders_for_today()); + echo "

Delivery schedule for " . htmlspecialchars($contact->getDisplayname()) . " on "; + if ($date) { + $order_ids = get_orders_for_date($date); + echo $date; + } + else { + $order_ids = get_orders_for_today(); + echo date('Y-m-d', time()); + } + echo "

\n"; + $q = new OrderQuery; - $orders = $q->filterById($order_ids)->find(); + $orders = $q->filterById(get_driver_schedule_by_order_id($contact->getId(), $order_ids))->find(); foreach ($orders as $order) { $contact = get_contact_by_id($order->getBeneficiaryId()); if (! $contact) continue; @@ -111,12 +137,32 @@ } } + function show_delivery_date_form($date = null) { + echo "
\n"; + echo "

Show deliveries for\n"; + show_date_form("date", $date); + submit("show_date", "Show"); + echo "

\n"; + } + + $date = null; list($ignored, $id, $args) = parse_parameters($parameters); - if (count($args)) show_driver_schedule($args[0], $args[1]); + if ($parameters[0] == "date") { + if ($args[0]) { + if (preg_match('/^[1-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/', $args[0])) $date = $args[0]; + array_shift($args); + } + } + + if ($args[0] == "driver") array_shift($args); + if (count($args)) show_driver_schedule($args[0], $args[1], $date); else { - $order_ids = get_orders_for_today(); + if ($date) $order_ids = get_orders_for_date($date); + else $order_ids = get_orders_for_today(); + if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids); - if ($driver_ids) show_driver_forms($driver_ids); + if ($driver_ids) show_driver_forms($driver_ids, $date); + show_delivery_date_form($date); } ?> -- 2.7.4