Allow reordering the delivery list.
[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
113     /*
114       Only allow drag and drop if there's more than one order otherwise IE will
115       mess up the CSS.
116     */
117     $count = count($orders);
118     if ($count) {
119       foreach ($orders as $order) {
120         if (! get_contact_by_id($order->getBeneficiaryId())) $count--;
121       }
122     }
123
124     if ($count > 1) {
125       echo "<div class=\"sortable\">\n";
126       echo "<p class=\"noprint\">Drag delivery details to reorder the schedule.</p>\n";
127     }
128
129     foreach ($orders as $order) {
130       $contact = get_contact_by_id($order->getBeneficiaryId());
131       if (! $contact) continue;
132
133       echo "<span>\n";
134
135       $phones = array();
136       $area = get_contact_area($contact);
137       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";
138       $hub = get_hub_by_id($order->getHubId(), false);
139       if ($hub) {
140         echo "<p>Deliver to hub <strong> " . htmlspecialchars($hub->getName()) . "</strong>";
141         $address = get_hub_address($hub);
142         $phone = $hub->getTelephone1();
143         if ($phone) $phones[] = $phone;
144         $phone = $hub->getTelephone2();
145         if ($phone) $phones[] = $phone;
146       }
147       else {
148         echo "<p>Deliver direct to beneficiary";
149         $address = get_contact_address($contact);
150         $phone = $contact->getTelephone1();
151         if ($phone) $phones[] = $phone;
152         $phone = $contact->getTelephone2();
153         if ($phone) $phones[] = $phone;
154       }
155       $area = get_address_area($address);
156
157       echo " in " . htmlspecialchars($area->getName()) . " at:<br>";
158       $city = get_area_city($area);
159       echo "\n<br>" . htmlspecialchars($address->getLine());
160       echo "\n<br>" . htmlspecialchars($city->getName());
161       echo "\n<br>" . htmlspecialchars($address->getPostcode());
162       if (count($phones)) echo "\n<br><br>Telephone <strong>" . implode(" or ", $phones) . "</strong>";
163       echo "</p>\n";
164
165       $notes = $order->getNotes();
166       if ($notes) {
167         echo "<p><strong>Notes:</strong>\n";
168         echo htmlspecialchars($notes);
169         echo "</p>\n";
170       }
171
172       echo "<hr>\n";
173       echo "</span>\n\n";
174     }
175
176     if ($count > 1) echo "</div>\n";
177   }
178
179   function show_delivery_date_form($date = null) {
180     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
181     echo "<p>Show deliveries for\n";
182     show_date_form("date", $date);
183     submit("show_date", "Show");
184     echo "</form>\n";
185   }
186
187   $date = null;
188   list($ignored, $id, $args) = parse_parameters($parameters);
189   if ($parameters[0] == "date") {
190     if ($args[0]) {
191       if (preg_match('/^[1-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/', $args[0])) $date = $args[0];
192       array_shift($args);
193     }
194   }
195
196   if ($args[0] == "driver") array_shift($args);
197   if (count($args)) show_driver_schedule($args[0], $args[1], $date);
198   else {
199     if ($date) $order_ids = get_orders_for_date($date);
200     else $order_ids = get_orders_for_today();
201
202     if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids);
203     if ($driver_ids) show_driver_forms($driver_ids, $date);
204     show_delivery_date_form($date);
205   }
206
207 ?>