filterByDate($then)->find(); if (count($orders)) { foreach ($orders as $order) $order_ids[] = $order->getId(); } 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(); if (count($order_ids)) { $dbh = Propel::getConnection(); $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and order_id in (" . implode(",", $order_ids) . ") and driver_id is not null"); $sth->execute(); $order_states = OrderStatePeer::populateObjects($sth); if (count($order_states)) { foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId(); } else echo "

No drivers assigned for deliveries.

\n"; } return $driver_ids; } /* Find schedule for a driver today. */ function get_driver_schedule_by_order_id($driver_id, $all_order_ids) { $order_ids = array(); if (! count($all_order_ids)) { echo "

No orders for today.

\n"; return null; } $dbh = Propel::getConnection(); $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and order_id in (" . implode(",", $all_order_ids) . ") and driver_id=$driver_id"); $sth->execute(); $order_states = OrderStatePeer::populateObjects($sth); if (count($order_states)) { foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId(); } else echo "

No deliveries for this driver.

\n"; return $order_ids; } function show_driver_forms($driver_ids, $date = null) { global $module; if (! count($driver_ids)) return; $q = new ContactQuery(); $contacts = $q->filterById($driver_ids)->find(); if (! count($contacts)) { echo "

Can't find drivers!

\n"; return; } 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", ($date) ? "date/$date/" : "", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname())); } } 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) { echo "

No such driver!

\n"; return; } 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(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; $area = get_contact_area($contact); echo "

Order of " . get_order_parcel_string($order) . " for " . htmlspecialchars($contact->getDisplayname()) . " in " . htmlspecialchars(get_area_displayname($area)) . ".

\n"; $hub = get_hub_by_id($order->getHubId(), false); if ($hub) { echo "

Deliver to hub " . htmlspecialchars($hub->getName()) . ""; $address = get_hub_address($hub); } else { echo "

Deliver direct to beneficiary"; $address = get_contact_address($contact); } $area = get_address_area($address); echo " in " . htmlspecialchars($area->getName()) . " at:
"; $city = get_area_city($area); echo "\n
" . htmlspecialchars($address->getLine()); echo "\n
" . htmlspecialchars($city->getName()); echo "\n
" . htmlspecialchars($address->getPostcode()); echo "

\n"; echo "
\n\n"; } } 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 ($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 { 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, $date); show_delivery_date_form($date); } ?>