X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Forder.php;h=2a126d27b065fcac08aee415d75a93eab7469344;hb=6a5bca8bea9c16060c3c90bbbb1cbbfe86562666;hp=95d96bb859e0db9a908779aafd3b5fe9aae8a6a8;hpb=5707529d8056238ba42e16f55b23b15a5e8c4d9b;p=readifood.git diff --git a/lib/order.php b/lib/order.php index 95d96bb..2a126d2 100644 --- a/lib/order.php +++ b/lib/order.php @@ -1,10 +1,12 @@ Order placed.

\n"; @@ -26,19 +28,22 @@ } } else if ($_POST['show_in_area']) { + set_last_selected("area_id", $_POST['area_id']); $q = new AreaQuery; $area = $q->findOneById($_POST['area_id']); - header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_uri(get_order_state_mask()))); + header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask(), get_order_date_query_uri($_POST['from'], $_POST['to'])))); exit; } else if ($_POST['show_in_city']) { + set_last_selected("city_id", $_POST['city_id']); $q = new CityQuery; $city = $q->findOneById($_POST['city_id']); - header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_uri(get_order_state_mask()))); + header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask()), get_order_date_query_uri($_POST['from'], $_POST['to']))); exit; } - function show_orders($offset, $per_page, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) { + function show_orders($city_id = null, $area_id = null, $requester_ids = null, $beneficiary_ids = null, $state_mask = null, $from = null, $to = null) { + list($first_page, $per_page) = pagination(); /* XXX: Use Propel methods. */ if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask); $q = new OrderQuery; @@ -46,60 +51,64 @@ if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids); # XXX: Doesn't work. #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse(); + if (isset($city_id) || isset($area_id)) { + $q->joinWith('Order.Beneficiary'); + $q->joinWith('Beneficiary.Address'); + if (isset($area_id)) $q->where('Address.AreaId=?', $area_id); + if (isset($city_id)) { + $q->joinWith('Address.Area'); + $q->where('Area.CityId=?', $city_id); + } + } if (isset($state_mask)) $q->filterById($order_ids); - $orders = $q->orderByDate()->find(); + if (isset($from)) $q->where('Order.Date >= ?', $from); + if (isset($to)) $q->where('Order.Date <= ?', $to); + $orders = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page); if (count($orders)) { foreach ($orders as $order) { echo "
\n" . get_order_summary($order) . "
\n"; } + show_pagination($orders); } else echo " none"; } - function show_city_orders($offset, $per_page, $city_name, $city_id = null, $state_mask = null) { + function show_city_orders($city_name, $city_id = null, $state_mask = null, $from = null, $to = null) { if (isset($city_id)) $city = get_city_by_id($city_id); else if ($city_name) $city = get_city_by_name($city_name); if ($city) { - $contacts = get_city_contacts($city->getId(), $GLOBALS['ROLE_BENEFICIARY']); - $beneficiary_ids = array(); - foreach ($contacts as $contact) $beneficiary_ids[] = $contact->getId(); - echo "

Orders in city " . $city->getLink(get_city_displayname($city)) . ":"; - return show_orders($offset, $per_page, null, $beneficiary_ids, $state_mask); + return show_orders($city->getId(), null, null, null, $state_mask, $from, $to); } else echo "

No such city!

\n"; } - function show_requester_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) { + function show_requester_orders($contact_name, $contact_id = null, $state_mask = null, $from = null, $to = null) { if (isset($contact_id)) $contact = get_contact_by_id($contact_id); else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders from referrer " . $contact->getLink() . ":"; - return show_orders($offset, $per_page, $contact->getId(), null, $state_mask); + return show_orders(null, null, $contact->getId(), null, $state_mask, $from, $to); } else echo "

No such contact!

\n"; } - function show_beneficiary_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) { + function show_beneficiary_orders($contact_name, $contact_id = null, $state_mask = null, $from = null, $to = null) { if (isset($contact_id)) $contact = get_contact_by_id($contact_id); else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders to beneficiary " . $contact->getLink() . ":"; - return show_orders($offset, $per_page, null, $contact->getId(), $state_mask); + return show_orders(null, null, null, $contact->getId(), $state_mask, $from, $to); } else echo "

No such contact!

\n"; } - function show_area_orders($offset, $per_page, $area_name, $area_id = null, $state_mask = null) { + function show_area_orders($area_name, $area_id = null, $state_mask = null, $from = null, $to = null) { if (isset($area_id)) $area = get_area_by_id($area_id); else if ($area_name) $area = get_area_by_name($area_name); if ($area) { - $contacts = get_area_contacts($area->getId(), $GLOBALS['ROLE_BENEFICIARY']); - $contact_ids = array(); - foreach ($contacts as $contact) $contact_ids[] = $contact->getId(); - echo "

Orders in area " . $area->getLink() . ":"; - return show_orders($offset, $per_page, null, $contact_ids, $state_mask); + return show_orders(null, $area->getId(), null, null, $state_mask, $from, $to); } else echo "

No such area!

\n"; } @@ -111,9 +120,9 @@ echo "

Restrict to order states:\n"; for ($i = 0; $i < count($states); $i++) { - echo " $states[$i]\n"; + echo ">\n"; } echo "

\n"; } @@ -139,7 +148,7 @@ return $mask; } - function get_order_state_string($mask) { + function get_order_state_query_string($mask) { global $states; $selected = array(); @@ -151,13 +160,23 @@ return implode("+", $selected); } - function get_order_state_uri($mask) { + function get_order_state_query_uri($mask) { global $all_states; if (is_null($mask)) return ""; if ($mask == $all_states) return ""; - return "/state/" . get_order_state_string($mask); + return "/state/" . get_order_state_query_string($mask); + } + + function get_order_date_query_uri($from, $to) { + $uri = ""; + if ($from) $uri .= "/from/$from"; + if ($to) $uri .= "/to/$to"; + + if (! $uri) return ""; + + return "/dated$uri"; } function show_order_areas_form($city_id = null) { @@ -167,9 +186,16 @@ return; } + $candidates = array(); + foreach ($areas as $area) { + if (! count(get_area_contacts($area->getId()))) continue; + $candidates[] = $area; + } + if (! count($candidates)) return; + echo "

Show orders in area\n"; echo "\n"; @@ -185,18 +211,29 @@ return; } + $candidates = array(); + foreach ($cities as $city) { + if (! count(get_city_contacts($city->getId()))) continue; + $candidates[] = $city; + } + if (! count($candidates)) return; + echo "

Show orders in city\n"; echo "\n"; echo "\n"; } - function show_order_forms($city_id, $state_mask) { + function show_order_forms($city_id, $state_mask, $from = null, $to = null) { form("noprint standout"); show_order_state_form($state_mask); + echo "

Restrict to orders for delivery between "; + datepicker("from", $from, true, null, false, "to"); + echo " and "; + datepicker("to", $to, true, "from", false); show_order_areas_form($city_id); show_order_cities_form($city_id); end_form(); @@ -206,8 +243,7 @@ global $states, $parcel_sizes, $parcel_contents; if ($order) { - $q = new OrderStateQuery; - $order_state = $q->filterByOrderId($order->getId())->orderByUpdated('desc')->limit(1)->findOne(); + $order_state = get_order_state($order); if ($order_state) { $state = $order_state->getState(); $driver_id = $order_state->getDriverId(); @@ -215,7 +251,6 @@ } else $order = new Order; - /* Date. */ echo "\n"; echo " Delivery\n"; @@ -297,13 +332,19 @@ echo " Parcel contents\n"; echo " "; for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) { - echo " getParcel() & (1 << $i)) echo " checked"; - echo ">$parcel_contents[$i]\n"; + echo ">\n"; } echo "\n"; echo "\n"; + /* Notes. */ + echo "\n"; + echo " Notes\n"; + echo " "; textarea("notes", $order->getNotes()); echo "\n"; + echo "\n"; + /* Driver. */ echo "\n"; echo " Driver\n"; @@ -328,7 +369,7 @@ echo " State\n"; echo " \n"; echo "\n"; @@ -425,6 +466,7 @@ for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) { if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i); } + $notes = $_POST['notes']; if ($date) { list($y, $m, $d) = explode('-', $date); @@ -469,6 +511,7 @@ $order->setBeneficiaryId($beneficiary_id); $order->setHubId($hub_id); $order->setParcel($parcel); + $order->setNotes($notes); /* XXX: begin/commit */ try { @@ -502,6 +545,16 @@ return $order->getId(); } + function confirm_delete_order($id = null) { + if (! check_admin(1, "delete an order")) return; + + if (isset($id)) $order = get_order_by_id($id); + if (! $order) return false; + + echo "

Confirm deletion

\n"; + echo "

You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "

\n"; + } + function delete_order($id = null) { if (! check_admin(1, "delete an order")) return; @@ -544,16 +597,7 @@ if ($driver_id) $driver = get_contact_by_id($driver_id); else $driver = null; - /* XXX */ - $state = $order_state->getState(); - for ($i = 0; $i < count($states); $i++) { - if ((1 << $i) == $state) { - $state = $states[$i]; - break; - } - } - #$state = $states[$order_state->getState()]; - echo "$username changed order to state $state"; + echo "$username changed order to state " . get_order_state_string($order_state) . ""; if ($driver) echo " for driver " . $driver->getDisplayname(); echo " on $date.
\n"; } @@ -598,6 +642,18 @@ } } + for ($i = 1; $i < count($parameters); $i++) { + if ($parameters[$i] == "dated") { + for ($j = $i + 1; $j < count($parameters) - 1; $j++) { + if ($parameters[$j] == "from") $from = $parameters[$j + 1]; + if ($parameters[$j] == "to") $to = $parameters[$j + 1]; + } + + if ($from || $to) check_dates('order', $from, $to, false, false); + break; + } + } + if ($parameters[0] == "in") { /* /order/in/area/Romsey+Town/1 */ switch ($parameters[1]) { @@ -609,7 +665,7 @@ $area = $q->findOneById($area_id); $city = get_area_city($area); if ($city) $city_id = $city->getId(); - show_area_orders(0, 10, $parameters[2], $area_id, $state_mask); + show_area_orders($parameters[2], $area_id, $state_mask, $from, $to); break; case "city": @@ -617,7 +673,7 @@ $_POST['city_id'] = $city_id; $q = new CityQuery; $city = $q->findOneById($city_id); - show_city_orders(0, 10, $parameters[2], $city_id, $state_mask); + show_city_orders($parameters[2], $city_id, $state_mask, $from, $to); break; } } @@ -628,7 +684,7 @@ $contact_id = $parameters[3]; $q = new ContactQuery; $contact = $q->findOneById($contact_id); - show_requester_orders(0, 10, $parameters[2], $contact_id, $state_mask); + show_requester_orders($parameters[2], $contact_id, $state_mask, $from, $to); break; } } @@ -639,7 +695,7 @@ $contact_id = $parameters[3]; $q = new ContactQuery; $hub = $q->findOneById($contact_id); - show_beneficiary_orders(0, 10, $parameters[2], $contact_id, $state_mask); + show_beneficiary_orders($parameters[2], $contact_id, $state_mask, $from, $to); break; } } @@ -658,14 +714,18 @@ if (count($args)) { switch ($args[0]) { case "delete": + confirm_delete_order($id); + break; + + case "confirmdelete": delete_order($id); break; } } else if (isset($id)) show_order($id); - else if ($state_mask) show_orders(0, 10, null, null, $state_mask); + else if ($state_mask) show_orders(null, null, null, null, $state_mask, $from, $to); - show_order_forms($city_id, $state_mask); + show_order_forms($city_id, $state_mask, $from, $to); show_add_new_order_form($city_id);