3 /* Find orders scheduled for delivery today. */
4 function get_orders_for_today() {
8 $orders = $q->filterByDate(time('Y-m-d'))->find();
10 foreach ($orders as $order) $order_ids[] = $order->getId();
12 else echo "<p>No deliveries today.</p>\n";
17 /* Find drivers with deliveries today. */
18 function get_drivers_by_order_id($order_ids) {
19 $driver_ids = array();
21 if (count($order_ids)) {
22 $dbh = Propel::getConnection();
23 $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");
25 $order_states = OrderStatePeer::populateObjects($sth);
26 if (count($order_states)) {
27 foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId();
29 else echo "<p>No drivers assigned for deliveries.</p>\n";
35 /* Find schedule for a driver today. */
36 function get_driver_schedule_by_order_id($driver_id, $all_order_ids) {
39 if (! count($all_order_ids)) {
40 echo "<p>No orders for today.</p>\n";
44 $dbh = Propel::getConnection();
45 $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");
47 $order_states = OrderStatePeer::populateObjects($sth);
48 if (count($order_states)) {
49 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
51 else echo "<p>No deliveries for this driver.</p>\n";
56 function show_driver_forms($driver_ids) {
59 if (! count($driver_ids)) return;
61 $q = new ContactQuery();
62 $contacts = $q->filterById($driver_ids)->find();
63 if (! count($contacts)) {
64 echo "<p>Can't find drivers!</p>\n";
68 echo "<p>Drivers with deliveries scheduled:";
69 foreach ($contacts as $contact) {
70 printf("<br>\n<a href=\"/$module/driver/%s/%d\">%s</a>", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname()));
74 function show_driver_schedule($driver_name = null, $driver_id = null) {
75 if (isset($driver_id)) $contact = get_contact_by_id($driver_id);
76 else if (isset($driver_name)) $contact = get_contact_by_name($driver_name);
78 echo "<p>No such driver!</p>\n";
82 echo "<h3>Delivery schedule for <strong>" . htmlspecialchars($contact->getDisplayname()) . "</strong></h3>\n";
83 $order_ids = get_driver_schedule_by_order_id($contact->getId(), get_orders_for_today());
85 $orders = $q->filterById($order_ids)->find();
86 foreach ($orders as $order) {
87 $contact = get_contact_by_id($order->getBeneficiaryId());
88 if (! $contact) continue;
90 $area = get_contact_area($contact);
91 echo "<p>Order of <em>" . $order->getQuantity() . "kg</em> for <strong>" . htmlspecialchars($contact->getDisplayname()) . "</strong> in " . htmlspecialchars(get_area_displayname($area)) . ".</p>\n";
92 $hub = get_hub_by_id($order->getHubId(), false);
94 echo "<p>Deliver to hub <strong> " . htmlspecialchars($hub->getName()) . "</strong>";
95 $address = get_hub_address($hub);
98 echo "<p>Deliver direct to beneficiary";
99 $address = get_contact_address($contact);
101 $area = get_address_area($address);
103 echo " in " . htmlspecialchars($area->getName()) . " at:<br>";
104 $city = get_area_city($area);
105 echo "\n<br>" . htmlspecialchars($address->getLine());
106 echo "\n<br>" . htmlspecialchars($city->getName());
107 echo "\n<br>" . htmlspecialchars($address->getPostcode());
114 list($ignored, $id, $args) = parse_parameters($parameters);
115 if (count($args)) show_driver_schedule($args[0], $args[1]);
117 $order_ids = get_orders_for_today();
118 if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids);
119 if ($driver_ids) show_driver_forms($driver_ids);