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();
112 foreach ($orders as $order) {
113 $contact = get_contact_by_id($order->getBeneficiaryId());
114 if (! $contact) continue;
117 $area = get_contact_area($contact);
118 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";
119 $hub = get_hub_by_id($order->getHubId(), false);
121 echo "<p>Deliver to hub <strong> " . htmlspecialchars($hub->getName()) . "</strong>";
122 $address = get_hub_address($hub);
123 $phone = $hub->getTelephone1();
124 if ($phone) $phones[] = $phone;
125 $phone = $hub->getTelephone2();
126 if ($phone) $phones[] = $phone;
129 echo "<p>Deliver direct to beneficiary";
130 $address = get_contact_address($contact);
131 $phone = $contact->getTelephone1();
132 if ($phone) $phones[] = $phone;
133 $phone = $contact->getTelephone2();
134 if ($phone) $phones[] = $phone;
136 $area = get_address_area($address);
138 echo " in " . htmlspecialchars($area->getName()) . " at:<br>";
139 $city = get_area_city($area);
140 echo "\n<br>" . htmlspecialchars($address->getLine());
141 echo "\n<br>" . htmlspecialchars($city->getName());
142 echo "\n<br>" . htmlspecialchars($address->getPostcode());
143 if (count($phones)) echo "\n<br><br>Telephone <strong>" . implode(" or ", $phones) . "</strong>";
146 $notes = $order->getNotes();
148 echo "<p><strong>Notes:</strong>\n";
149 echo htmlspecialchars($notes);
157 function show_delivery_date_form($date = null) {
158 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
159 echo "<p>Show deliveries for\n";
160 show_date_form("date", $date);
161 submit("show_date", "Show");
166 list($ignored, $id, $args) = parse_parameters($parameters);
167 if ($parameters[0] == "date") {
169 if (preg_match('/^[1-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/', $args[0])) $date = $args[0];
174 if ($args[0] == "driver") array_shift($args);
175 if (count($args)) show_driver_schedule($args[0], $args[1], $date);
177 if ($date) $order_ids = get_orders_for_date($date);
178 else $order_ids = get_orders_for_today();
180 if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids);
181 if ($driver_ids) show_driver_forms($driver_ids, $date);
182 show_delivery_date_form($date);