43800caa4edd29d5bfb13058f2425f8651662d7a
[readifood.git] / lib / delivery.php
1 <?php
2
3   if (isset($_POST['show_date'])) {
4     header(sprintf("Location: http%s://%s/%s/date/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['date']));
5     exit;
6   }
7
8   /* Find orders scheduled for delivery on a certain date. */
9   function get_orders_for_date($date) {
10     $order_ids = array();
11
12     /* $date is in Y-m-d format. */
13     list($y, $m, $d) = explode('-', $date);
14     $then = mktime(0, 0, 0, $m, $d, $y);
15
16     $q = new OrderQuery();
17     $orders = $q->filterByDate($then)->find();
18     if (count($orders)) {
19       foreach ($orders as $order) $order_ids[] = $order->getId();
20     }
21     else echo "<p>No deliveries for $date.</p>\n";
22
23     return $order_ids;
24   }
25
26   /* Find orders scheduled for delivery today. */
27   function get_orders_for_today() {
28     return get_orders_for_date(date('Y-m-d'), time());
29   }
30
31   /* Find drivers with deliveries today. */
32   function get_drivers_by_order_id($order_ids) {
33     $driver_ids = array();
34
35     if (count($order_ids)) {
36       $dbh = Propel::getConnection();
37       $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");
38       $sth->execute();
39       $order_states = OrderStatePeer::populateObjects($sth);
40       if (count($order_states)) {
41         foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId();
42       }
43       else echo "<p>No drivers assigned for deliveries.</p>\n";
44     }
45
46     return $driver_ids;
47   }
48
49   /* Find schedule for a driver today. */
50   function get_driver_schedule_by_order_id($driver_id, $all_order_ids) {
51     $order_ids = array();
52
53     if (! count($all_order_ids)) {
54       echo "<p>No orders for today.</p>\n";
55       return null;
56     }
57
58     $dbh = Propel::getConnection();
59     $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");
60     $sth->execute();
61     $order_states = OrderStatePeer::populateObjects($sth);
62     if (count($order_states)) {
63       foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
64     }
65     else echo "<p>No deliveries for this driver.</p>\n";
66
67     return $order_ids;
68   }
69
70   function show_driver_forms($driver_ids, $date = null) {
71     global $module;
72
73     if (! count($driver_ids)) return;
74
75     $q = new ContactQuery();
76     $contacts = $q->filterById($driver_ids)->find();
77     if (! count($contacts)) {
78       echo "<p>Can't find drivers!</p>\n";
79       return;
80     }
81
82     echo "<p>Drivers with deliveries scheduled on ";
83     if ($date) echo $date;
84     else echo date('Y-m-d', time());
85     echo ":";
86     foreach ($contacts as $contact) {
87       printf("<br>\n<a href=\"/$module/%sdriver/%s/%d\">%s</a>", ($date) ? "date/$date/" : "", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname()));
88     }
89   }
90
91   function show_driver_schedule($driver_name = null, $driver_id = null, $date = null) {
92     if (isset($driver_id)) $contact = get_contact_by_id($driver_id);
93     else if (isset($driver_name)) $contact = get_contact_by_name($driver_name);
94     if (! $contact) {
95       echo "<p>No such driver!</p>\n";
96       return;
97     }
98
99     echo "<h3>Delivery schedule for <strong>" . htmlspecialchars($contact->getDisplayname()) . "</strong> on ";
100     if ($date) {
101       $order_ids = get_orders_for_date($date);
102       echo $date;
103     }
104     else {
105       $order_ids = get_orders_for_today();
106       echo date('Y-m-d', time());
107     }
108     echo "</h3>\n";
109
110     $q = new OrderQuery;
111     $orders = $q->filterById(get_driver_schedule_by_order_id($contact->getId(), $order_ids))->find();
112     foreach ($orders as $order) {
113       $contact = get_contact_by_id($order->getBeneficiaryId());
114       if (! $contact) continue;
115
116       $area = get_contact_area($contact);
117       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";
118       $hub = get_hub_by_id($order->getHubId(), false);
119       if ($hub) {
120         echo "<p>Deliver to hub <strong> " . htmlspecialchars($hub->getName()) . "</strong>";
121         $address = get_hub_address($hub);
122       }
123       else {
124         echo "<p>Deliver direct to beneficiary";
125         $address = get_contact_address($contact);
126       }
127       $area = get_address_area($address);
128
129       echo " in " . htmlspecialchars($area->getName()) . " at:<br>";
130       $city = get_area_city($area);
131       echo "\n<br>" . htmlspecialchars($address->getLine());
132       echo "\n<br>" . htmlspecialchars($city->getName());
133       echo "\n<br>" . htmlspecialchars($address->getPostcode());
134       echo "</p>\n";
135
136       $notes = $order->getNotes();
137       if ($notes) {
138         echo "<p><strong>Notes:</strong>\n";
139         echo htmlspecialchars($notes);
140         echo "</p>\n";
141       }
142
143       echo "<hr>\n\n";
144     }
145   }
146
147   function show_delivery_date_form($date = null) {
148     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
149     echo "<p>Show deliveries for\n";
150     show_date_form("date", $date);
151     submit("show_date", "Show");
152     echo "</form>\n";
153   }
154
155   $date = null;
156   list($ignored, $id, $args) = parse_parameters($parameters);
157   if ($parameters[0] == "date") {
158     if ($args[0]) {
159       if (preg_match('/^[1-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/', $args[0])) $date = $args[0];
160       array_shift($args);
161     }
162   }
163
164   if ($args[0] == "driver") array_shift($args);
165   if (count($args)) show_driver_schedule($args[0], $args[1], $date);
166   else {
167     if ($date) $order_ids = get_orders_for_date($date);
168     else $order_ids = get_orders_for_today();
169
170     if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids);
171     if ($driver_ids) show_driver_forms($driver_ids, $date);
172     show_delivery_date_form($date);
173   }
174
175 ?>