Typo.
[readifood.git] / lib / delivery.php
1 <?php
2
3   /* Find orders scheduled for delivery today. */
4   function get_orders_for_today() {
5     $order_ids = array();
6
7     $q = new OrderQuery();
8     $orders = $q->filterByDate(time('Y-m-d'))->find();
9     if (count($orders)) {
10       foreach ($orders as $order) $order_ids[] = $order->getId();
11     }
12     else echo "<p>No deliveries today.</p>\n";
13
14     return $order_ids;
15   }
16
17   /* Find drivers with deliveries today. */
18   function get_drivers_by_order_id($order_ids) {
19     $driver_ids = array();
20
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");
24       $sth->execute();
25       $order_states = OrderStatePeer::populateObjects($sth);
26       if (count($order_states)) {
27         foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId();
28       }
29       else echo "<p>No drivers assigned for deliveries.</p>\n";
30     }
31
32     return $driver_ids;
33   }
34
35   /* Find schedule for a driver today. */
36   function get_driver_schedule_by_order_id($driver_id, $all_order_ids) {
37     $order_ids = array();
38
39     if (! count($all_order_ids)) {
40       echo "<p>No orders for today.</p>\n";
41       return null;
42     }
43
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");
46     $sth->execute();
47     $order_states = OrderStatePeer::populateObjects($sth);
48     if (count($order_states)) {
49       foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
50     }
51     else echo "<p>No deliveries for this driver.</p>\n";
52
53     return $order_ids;
54   }
55
56   function show_driver_forms($driver_ids) {
57     global $module;
58
59     if (! count($driver_ids)) return;
60
61     $q = new ContactQuery();
62     $contacts = $q->filterById($driver_ids)->find();
63     if (! count($contacts)) {
64       echo "<p>Can't find drivers!</p>\n";
65       return;
66     }
67
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()));
71     }
72   }
73
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);
77     if (! $contact) {
78       echo "<p>No such driver!</p>\n";
79       return;
80     }
81
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());
84     $q = new OrderQuery;
85     $orders = $q->filterById($order_ids)->find();
86     foreach ($orders as $order) {
87       $contact = get_contact_by_id($order->getBeneficiaryId());
88       if (! $contact) continue;
89
90       $area = get_contact_area($contact);
91       echo "<p>Order of <em>" . get_order_parcel_string($order) . "</em> for <strong>" . htmlspecialchars($contact->getDisplayname()) . "</strong> in " . htmlspecialchars(get_area_displayname($area)) . ".</p>\n";
92       $hub = get_hub_by_id($order->getHubId(), false);
93       if ($hub) {
94         echo "<p>Deliver to hub <strong> " . htmlspecialchars($hub->getName()) . "</strong>";
95         $address = get_hub_address($hub);
96       }
97       else {
98         echo "<p>Deliver direct to beneficiary";
99         $address = get_contact_address($contact);
100       }
101       $area = get_address_area($address);
102
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());
108       echo "</p>\n";
109
110       echo "<hr>\n\n";
111     }
112   }
113
114   list($ignored, $id, $args) = parse_parameters($parameters);
115   if (count($args)) show_driver_schedule($args[0], $args[1]);
116   else {
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);
120   }
121
122 ?>