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']));
8 /* Find orders scheduled for delivery on a certain date. */
9 function get_orders_for_date($date) {
12 /* $date is in Y-m-d format. */
13 list($y, $m, $d) = explode('-', $date);
14 $then = mktime(0, 0, 0, $m, $d, $y);
16 $q = new OrderQuery();
17 $orders = $q->filterByDate($then)->find();
19 foreach ($orders as $order) $order_ids[] = $order->getId();
21 else echo "<p>No deliveries for $date.</p>\n";
26 /* Find orders scheduled for delivery today. */
27 function get_orders_for_today() {
28 return get_orders_for_date(date('Y-m-d'), time());
31 /* Find drivers with deliveries today. */
32 function get_drivers_by_order_id($order_ids) {
33 $driver_ids = array();
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");
39 $order_states = OrderStatePeer::populateObjects($sth);
40 if (count($order_states)) {
41 foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId();
43 else echo "<p>No drivers assigned for deliveries.</p>\n";
49 /* Find schedule for a driver today. */
50 function get_driver_schedule_by_order_id($driver_id, $all_order_ids) {
53 if (! count($all_order_ids)) {
54 echo "<p>No orders for today.</p>\n";
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");
61 $order_states = OrderStatePeer::populateObjects($sth);
62 if (count($order_states)) {
63 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
65 else echo "<p>No deliveries for this driver.</p>\n";
70 function show_driver_forms($driver_ids, $date = null) {
73 if (! count($driver_ids)) return;
75 $q = new ContactQuery();
76 $contacts = $q->filterById($driver_ids)->find();
77 if (! count($contacts)) {
78 echo "<p>Can't find drivers!</p>\n";
82 echo "<p>Drivers with deliveries scheduled on ";
83 if ($date) echo $date;
84 else echo date('Y-m-d', time());
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()));
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);
95 echo "<p>No such driver!</p>\n";
99 echo "<h3>Delivery schedule for <strong>" . htmlspecialchars($contact->getDisplayname()) . "</strong> on ";
101 $order_ids = get_orders_for_date($date);
105 $order_ids = get_orders_for_today();
106 echo date('Y-m-d', time());
111 $orders = $q->filterById(get_driver_schedule_by_order_id($contact->getId(), $order_ids))->find();
114 Only allow drag and drop if there's more than one order otherwise IE will
117 $count = count($orders);
119 foreach ($orders as $order) {
120 if (! get_contact_by_id($order->getBeneficiaryId())) $count--;
125 echo "<div class=\"sortable\">\n";
126 echo "<p class=\"noprint\">Drag delivery details to reorder the schedule.</p>\n";
129 foreach ($orders as $order) {
130 $contact = get_contact_by_id($order->getBeneficiaryId());
131 if (! $contact) continue;
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);
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;
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;
155 $area = get_address_area($address);
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>";
165 $notes = $order->getNotes();
167 echo "<p><strong>Notes:</strong>\n";
168 echo htmlspecialchars($notes);
176 if ($count > 1) echo "</div>\n";
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");
188 list($ignored, $id, $args) = parse_parameters($parameters);
189 if ($parameters[0] == "date") {
191 if (preg_match('/^[1-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/', $args[0])) $date = $args[0];
196 if ($args[0] == "driver") array_shift($args);
197 if (count($args)) show_driver_schedule($args[0], $args[1], $date);
199 if ($date) $order_ids = get_orders_for_date($date);
200 else $order_ids = get_orders_for_today();
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);