Allow reordering the delivery list.
[readifood.git] / lib / order.php
1 <?php
2
3   if (isset($_POST['show_add_order'])) {
4     $area_id = $_POST['area_id'];
5     show_new_order_form($area_id);
6   }
7   else if (isset($_POST['add_order'])) {
8     $id = add_order();
9     if ($id !== false) {
10       echo "<p>Order placed.</p>\n";
11       $parameters = array("id", $id);
12     }
13   }
14   else if (isset($_POST['update_order'])) {
15     list($ignored, $id, $args) = parse_parameters($parameters);
16     $q = new OrderQuery;
17     $order = $q->findOneById($id);
18     if ($order) {
19       if (update_order($order) !== false) {
20         echo "<p>Updated order.</p>\n";
21         $parameters = array("id", $order->getId());
22       }
23     }
24     else {
25       echo "<p>No such contact!</p>\n";
26     }
27   }
28   else if ($_POST['show_in_area']) {
29     $q = new AreaQuery;
30     $area = $q->findOneById($_POST['area_id']);
31     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_query_uri(get_order_state_mask())));
32     exit;
33   }
34   else if ($_POST['show_in_city']) {
35     $q = new CityQuery;
36     $city = $q->findOneById($_POST['city_id']);
37     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_query_uri(get_order_state_mask())));
38     exit;
39   }
40
41   function show_orders($offset, $per_page, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) {
42     /* XXX: Use Propel methods. */
43     if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask);
44     $q = new OrderQuery;
45     if (isset($requester_ids)) $q->filterByRequesterId($requester_ids);
46     if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids);
47     # XXX: Doesn't work.
48     #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse();
49     if (isset($state_mask)) $q->filterById($order_ids);
50     $orders = $q->orderByDate()->find();
51     if (count($orders)) {
52       foreach ($orders as $order) {
53         echo "<br>\n" . get_order_summary($order) . "<br>\n";
54       }
55     }
56     else echo " none";
57   }
58
59   function show_city_orders($offset, $per_page, $city_name, $city_id = null, $state_mask = null) {
60     if (isset($city_id)) $city = get_city_by_id($city_id);
61     else if ($city_name) $city = get_city_by_name($city_name);
62     if ($city) {
63       $contacts = get_city_contacts($city->getId(), $GLOBALS['ROLE_BENEFICIARY']);
64       $beneficiary_ids = array();
65       foreach ($contacts as $contact) $beneficiary_ids[] = $contact->getId();
66
67       echo "<p>Orders in city " . $city->getLink(get_city_displayname($city)) . ":";
68       return show_orders($offset, $per_page, null, $beneficiary_ids, $state_mask);
69     }
70     else echo "<p>No such city!</p>\n";
71   }
72
73   function show_requester_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) {
74     if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
75     else if ($contact_name) $contact = get_contact_by_name($contact_name);
76     if ($contact) {
77       echo "<p>Orders from referrer " . $contact->getLink() . ":";
78       return show_orders($offset, $per_page, $contact->getId(), null, $state_mask);
79     }
80     else echo "<p>No such contact!</p>\n";
81   }
82
83   function show_beneficiary_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) {
84     if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
85     else if ($contact_name) $contact = get_contact_by_name($contact_name);
86     if ($contact) {
87       echo "<p>Orders to beneficiary " . $contact->getLink() . ":";
88       return show_orders($offset, $per_page, null, $contact->getId(), $state_mask);
89     }
90     else echo "<p>No such contact!</p>\n";
91   }
92
93   function show_area_orders($offset, $per_page, $area_name, $area_id = null, $state_mask = null) {
94     if (isset($area_id)) $area = get_area_by_id($area_id);
95     else if ($area_name) $area = get_area_by_name($area_name);
96     if ($area) {
97       $contacts = get_area_contacts($area->getId(), $GLOBALS['ROLE_BENEFICIARY']);
98       $contact_ids = array();
99       foreach ($contacts as $contact) $contact_ids[] = $contact->getId();
100
101       echo "<p>Orders in area " . $area->getLink() . ":";
102       return show_orders($offset, $per_page, null, $contact_ids, $state_mask);
103     }
104     else echo "<p>No such area!</p>\n";
105   }
106
107   function show_order_state_form($state_mask = null) {
108     global $states, $all_states;
109
110     if (is_null($state_mask)) $state_mask = $all_states;
111
112     echo "<p>Restrict to order states:\n";
113     for ($i = 0; $i < count($states); $i++) {
114       echo " <input type=\"checkbox\" id=\"state_$i\" name=\"state_$i\"";
115       if ($state_mask & (1 << $i)) echo " checked";
116       echo "><label for=\"state_$i\">$states[$i]</label>\n";
117     }
118     echo "</p>\n";
119   }
120
121   function get_order_state_mask($string = null) {
122     global $states, $all_states;
123
124     $mask = 0;
125
126     if (isset($string)) {
127       $selected = explode("+", $string);
128       for ($i = 0; $i < count($states); $i++) {
129         if (in_array($states[$i], $selected)) $mask |= (1 << $i);
130       }
131     }
132     else {
133       for ($i = 0; $i < count($states); $i++) {
134         if ($_POST['state_' . $i] == "on") $mask |= (1 << $i);
135       }
136     }
137
138     if (! $mask) $mask = $all_states;
139     return $mask;
140   }
141
142   function get_order_state_query_string($mask) {
143     global $states;
144
145     $selected = array();
146
147     for ($i = 0; $i < count($states); $i++) {
148       if ($mask & (1 << $i)) $selected[] = $states[$i];
149     }
150
151     return implode("+", $selected);
152   }
153
154   function get_order_state_query_uri($mask) {
155     global $all_states;
156
157     if (is_null($mask)) return "";
158     if ($mask == $all_states) return "";
159
160     return "/state/" . get_order_state_query_string($mask);
161   }
162
163   function show_order_areas_form($city_id = null) {
164     $areas = get_city_areas($city_id);
165     if (! count($areas)) {
166       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
167       return;
168     }
169
170     $candidates = array();
171     foreach ($areas as $area) {
172       if (! count(get_area_contacts($area->getId()))) continue;
173       $candidates[] = $area;
174     }
175     if (! count($candidates)) return;
176
177     echo "<p>Show orders in area\n";
178     echo "<select name=\"area_id\">\n";
179     foreach ($candidates as $area) {
180       option("area_id", $area->getId(), get_area_displayname($area));
181     }
182     echo "</select>\n";
183     echo "<input type=\"submit\" name=\"show_in_area\" value=\"Show\">\n";
184   }
185
186   function show_order_cities_form($city_id = null) {
187     $q = new CityQuery;
188     $cities = $q->orderByName()->find();
189
190     if (! count($cities)) {
191       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
192       return;
193     }
194
195     $candidates = array();
196     foreach ($cities as $city) {
197       if (! count(get_city_contacts($city->getId()))) continue;
198       $candidates[] = $city;
199     }
200     if (! count($candidates)) return;
201
202     echo "<p>Show orders in city\n";
203     echo "<select name=\"city_id\">\n";
204     foreach ($candidates as $city) {
205       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
206     }
207     echo "</select>\n";
208     echo "<input type=\"submit\" name=\"show_in_city\" value=\"Show\">\n";
209   }
210
211   function show_order_forms($city_id, $state_mask) {
212     form("noprint standout");
213     show_order_state_form($state_mask);
214     show_order_areas_form($city_id);
215     show_order_cities_form($city_id);
216     end_form();
217   }
218
219   function show_order_form($order = null, $area_id = null) {
220     global $states, $parcel_sizes, $parcel_contents;
221
222     if ($order) {
223       $order_state = get_order_state($order);
224       if ($order_state) {
225         $state = $order_state->getState();
226         $driver_id = $order_state->getDriverId();
227       }
228     }
229     else $order = new Order;
230
231     /* Date. */
232     echo "<tr>\n";
233     echo "  <td>Delivery</td>\n";
234     /* XXX: Find suitable dates from area. */
235     echo "  <td>";
236     show_date_form("date", $order->getDate());
237     if (! $order->getDate()) {
238       echo " and recur for <select name=\"recurrence\">\n";
239       for ($i = 0; $i < 4; $i++) option("recurrence", $i, $i);
240       echo "</select> weeks";
241     }
242     echo "</td>\n";
243     echo "</tr>\n";
244
245     /* Referrer. */
246     echo "<tr>\n";
247     echo "  <td>Referrer</td>\n";
248     echo "  <td><select name=\"requester_id\">\n";
249     option("requester_id", null, "");
250     $contacts = get_area_requesters();
251     foreach ($contacts as $contact) {
252       option("requester_id", $contact->getId(), $contact->getDisplayname(), $order->getRequesterId());
253     }
254     echo "</select>";
255     $contact = get_contact_by_id($order->getRequesterId(), false);
256     if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
257     echo "</td>\n";
258     echo "</tr>\n";
259
260     /* Beneficiary. */
261     echo "<tr>\n";
262     echo "  <td>Beneficiary</td>\n";
263     echo "  <td><select name=\"beneficiary_id\">\n";
264     option("beneficiary_id", null, "");
265     if (! $order->getId() && $order->getBeneficiaryId()) {
266       $contact = get_contact_by_id($order->getBeneficiaryId());
267       if ($contact) option("beneficiary_id", $order->getBeneficiaryId(), $contact->getDisplayname(), $order->getBeneficiaryId());
268     }
269     else {
270       $contacts = get_area_beneficiaries($area_id);
271       foreach ($contacts as $contact) {
272         option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
273       }
274     }
275     echo "</select>";
276     $contact = get_contact_by_id($order->getBeneficiaryId(), false);
277     if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
278     echo "</td>\n";
279     echo "</tr>\n";
280
281     /* Hub. */
282     echo "<tr>\n";
283     echo "  <td>Hub</td>\n";
284     echo "  <td><select name=\"hub_id\">\n";
285     option("hub_id", null, "");
286     $hubs = get_area_hubs();
287     foreach ($hubs as $hub) {
288       option("hub_id", $hub->getId(), $hub->getDisplayname(), $order->getHubId());
289     }
290     echo "</select>";
291     $hub = get_hub_by_id($order->getHubId(), false);
292     if ($hub) echo " " . get_small_link($hub->getDisplayname(), $hub->getURL());
293     echo "</td>\n";
294     echo "</tr>\n";
295
296     /* Parcel type. */
297     echo "<tr>\n";
298     echo "  <td>Parcel size</td>\n";
299     echo "  <td><select name=\"parcel_size\">\n";
300     $mask = 1 << count($parcel_sizes);
301     for ($i = 0; $i < count($parcel_sizes); $i++) {
302       option("parcel_size", 1 << $i, $parcel_sizes[$i], $order->getParcel() % $mask);
303     }
304     echo "</select></td>\n";
305     echo "</tr>\n";
306
307     /* Parcel contents. */
308     echo "<tr>\n";
309     echo "  <td>Parcel contents</td>\n";
310     echo "  <td>";
311     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
312       echo "  <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
313       if ($order->getParcel() & (1 << $i)) echo " checked";
314       echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
315     }
316     echo "</td>\n";
317     echo "</tr>\n";
318
319     /* Notes. */
320     echo "<tr>\n";
321     echo "  <td>Notes</td>\n";
322     echo "  <td>"; textarea("notes", $order->getNotes()); echo "</td>\n";
323     echo "</tr>\n";
324
325     /* Driver. */
326     echo "<tr>\n";
327     echo "  <td>Driver</td>\n";
328     $contacts = get_city_drivers();
329     if (count($contacts)) {
330       echo "  <td><select name=\"driver_id\">\n";
331       option("driver_id", null, "");
332       foreach ($contacts as $contact) {
333         option("driver_id", $contact->getId(), $contact->getDisplayname(), $driver_id);
334       }
335       echo "</select>";
336       $contact = get_contact_by_id($driver_id, false);
337       if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
338       echo "</td>\n";
339     }
340     else echo "  <td>No drivers!</td>\n";
341     echo "</tr>\n";
342
343     /* State. */
344     if ($order->getId()) {
345       echo "<tr>\n";
346       echo "  <td>State</td>\n";
347       echo "  <td><select name=\"state\">\n";
348       for ($i = 0; $i < count($states); $i++) {
349         option("state", 1 << $i, ucfirst($states[$i]), $state);
350       }
351       echo "</select></td>\n";
352       echo "</tr>\n";
353     }
354   }
355
356   function show_new_order_form($area_id = null) {
357     if (! check_admin(1)) return;
358
359     $area = get_area_by_id($area_id);
360     if (! count($area)) {
361       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
362       return;
363     }
364
365     form("noprint");
366     echo "<p>Place an order:</p>\n";
367
368     echo "<table>\n";
369     show_order_form(null, $area_id);
370
371     echo "<tr>\n";
372     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
373     echo "</tr>\n";
374     echo "</table>\n";
375     end_form();
376   }
377
378   function show_contact_order_form($contact) {
379     if (! check_admin(1)) return;
380
381     $area = get_contact_area($contact);
382     if (! $area) {
383       echo "<p>No valid <a href=\"/area\">area</a> for contact!</p>\n";
384       return;
385     }
386
387     $order = new Order;
388     $order->setBeneficiaryId($contact->getId());
389
390     form("standout");
391     echo "<p>Placing order for " . $contact->getStrongLink() . ".";
392     $parcel = $contact->getParcel();
393     if ($parcel) {
394       echo "  Suggested parcel type is <span class=\"strong\">" .  get_contact_parcel_string($contact) . "</span>";
395       $order->setParcel($parcel);
396     }
397     echo "</p>\n";
398
399     echo "<table>\n";
400     show_order_form($order, $area_id);
401
402     echo "<tr>\n";
403     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
404     echo "</tr>\n";
405     echo "</table>\n";
406     end_form();
407   }
408
409   function show_add_new_order_form() {
410     if (! check_admin(1)) return;
411
412     /* We intentionally hide areas with no contacts. */
413     $areas = get_city_areas_with_contacts(null, $GLOBALS['ROLE_BENEFICIARY']);
414     if (! count($areas)) {
415       echo "<p>Can't place any orders until at least one <a href=\"/area\">area</a> has a <a href=\"/contact\">contact</a>!</p>\n";
416       return;
417     }
418
419     form("noprint standout");
420     echo "<p>Place an order in <select name=\"area_id\">\n";
421     foreach ($areas as $area) {
422       option("area_id", $area->getId(), get_area_displayname($area));
423     }
424     echo "</select>";
425     submit("show_add_order", "Proceed");
426     echo "</p>\n";
427     end_form();
428   }
429
430   function update_order(&$order, $new = false) {
431     global $user_id, $parcel_sizes, $parcel_contents;
432
433     #$date = ymd_to_iso8601("date");
434     $date = $_POST['date'];
435     $requester_id = $_POST['requester_id'];
436     $beneficiary_id = $_POST['beneficiary_id'];
437     $hub_id = $_POST['hub_id'];
438     $driver_id = $_POST['driver_id'];
439     if (! $driver_id) $driver_id = null;
440     $state = $_POST['state'];
441     if (! $state) $state = $GLOBALS['STATE_PLACED'];
442     $parcel = $_POST['parcel_size'];
443     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
444       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
445     }
446     $notes = $_POST['notes'];
447
448     if ($date) {
449       list($y, $m, $d) = explode('-', $date);
450       $then = mktime(0, 0, 0, $m, $d, $y);
451     }
452     else $then = time();
453     /* XXX: check date */
454
455     $requester = get_contact_by_id($requester_id);
456     if (! $requester) {
457       echo "<p>Invalid referrer!</p>\n";
458       return false;
459     }
460
461     $beneficiary = get_contact_by_id($beneficiary_id);
462     if (! $beneficiary) {
463       echo "<p>Invalid beneficiary!</p>\n";
464       return false;
465     }
466
467     if ($hub_id) {
468       $hub = get_hub_by_id($hub_id);
469       if (! $hub) {
470         echo "<p>Invalid hub!</p>\n";
471         return false;
472       }
473     }
474     else $hub_id = null;
475
476     if ($new && isset($_POST['recurrence'])) $recurrence = $_POST['recurrence'];
477     if (! $recurrence) $recurrence = 0;
478
479     $now = time();
480     for ($i = 0; $i <= $recurrence; $i++) {
481       if ($i) {
482         echo "<p>Creating recurrence $i.</p>\n";
483         $order = new Order;
484       }
485
486       $order->setDate($then + 7 * 86400 * $i);
487       $order->setRequesterId($requester_id);
488       $order->setBeneficiaryId($beneficiary_id);
489       $order->setHubId($hub_id);
490       $order->setParcel($parcel);
491       $order->setNotes($notes);
492
493       /* XXX: begin/commit */
494       try {
495         $order->save();
496
497         $order_state = new OrderState;
498         $order_state->setUpdated($now);
499         $order_state->setOrderId($order->getId());
500         $order_state->setUserId($user_id);
501         $order_state->setDriverId($driver_id);
502         $order_state->setState($state);
503
504         $order_state->save();
505       }
506       catch (Exception $e) {
507         if ($new) echo "<p>Error placing order.</p>\n";
508         else echo "<p>Error updating order.</p>\n";
509         echo "<p>" . $e->getMessage() . "</p>\n";
510         return false;
511       }
512     }
513
514     return true;
515   }
516
517   function add_order() {
518     if (! check_admin(1, "place an order")) return;
519
520     $order = new Order;
521     if (! update_order($order, true)) return false;
522     return $order->getId();
523   }
524
525   function confirm_delete_order($id = null) {
526     if (! check_admin(1, "delete an order")) return;
527
528     if (isset($id)) $order = get_order_by_id($id);
529     if (! $order) return false;
530
531     echo "<h3>Confirm deletion</h3>\n";
532     echo "<p>You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "</p>\n";
533   }
534
535   function delete_order($id = null) {
536     if (! check_admin(1, "delete an order")) return;
537
538     if (isset($id)) $order = get_order_by_id($id);
539     if (! $order) return false;
540
541     try {
542       $q = new OrderStateQuery;
543       $order_states = $q->filterByOrderId($id)->find();
544       foreach ($order_states as $order_state) $order_state->delete();
545       $order->delete();
546       echo "<p>Deleted order.</p>\n";
547     }
548     catch (Exception $e) {
549       echo "<p>Error deleting order $id!</p>\n";
550       return false;
551     }
552
553     return true;
554   }
555
556   function show_order_history($id) {
557     global $states;
558
559     $q = new OrderStateQuery();
560     $order_states = $q->filterByOrderId($id)->orderById()->find();
561
562     if (! count($order_states)) return;
563
564     echo "<h3>Order history</h3>\n";
565     echo "<p class=\"history\">\n";
566     foreach ($order_states as $order_state) {
567       $date = $order_state->getUpdated();
568
569       $user = get_contact_by_id($order_state->getUserId());
570       if ($user) $username = $user->getDisplayname();
571       else $username = "unknown user";
572
573       $driver_id = $order_state->getDriverId();
574       if ($driver_id) $driver = get_contact_by_id($driver_id);
575       else $driver = null;
576
577       echo "<strong>$username</strong> changed order to state <strong>" . get_order_state_string($order_state) . "</strong>";
578       if ($driver) echo " for driver " . $driver->getDisplayname();
579       echo " on $date.<br>\n";
580     }
581     echo "</p>\n";
582   }
583
584   function show_order(&$id = null) {
585     if (isset($id)) $order = get_order_by_id($id);
586     if (! $order) return;
587
588     form();
589     echo "<p>Order: <span class=\"strong\">" . $order->getId() . "</span>";
590     if (check_admin(1)) {
591       echo " " . $order->getDeleteLink();
592     }
593     echo ": ";
594     echo "\n</p>";
595
596     echo "<table>\n";
597     show_order_form($order);
598
599     if (check_admin(1)) {
600       echo "<tr>\n";
601       echo "  <td colspan=2>";
602       submit("update_order", "Update");
603       echo "</td>\n";
604       echo "</tr>\n";
605     }
606
607     echo "</table>\n";
608     end_form();
609
610     show_order_history($order->getId());
611   }
612
613   $state_mask = null;
614   if (count($parameters)) {
615     for ($i = 1; $i < count($parameters); $i++) {
616       if ($parameters[$i] == "state") {
617         /* /order/state/placed+picked */
618         $state_mask = get_order_state_mask($parameters[$i + 1]);
619       }
620     }
621
622     if ($parameters[0] == "in") {
623       /* /order/in/area/Romsey+Town/1 */
624       switch ($parameters[1]) {
625       case "area":
626         case "area":
627           $area_id = $parameters[3];
628           $_POST['area_id'] = $area_id;
629           $q = new AreaQuery;
630           $area = $q->findOneById($area_id);
631           $city = get_area_city($area);
632           if ($city) $city_id = $city->getId();
633           show_area_orders(0, 10, $parameters[2], $area_id, $state_mask);
634         break;
635
636         case "city":
637           $city_id = $parameters[3];
638           $_POST['city_id'] = $city_id;
639           $q = new CityQuery;
640           $city = $q->findOneById($city_id);
641           show_city_orders(0, 10, $parameters[2], $city_id, $state_mask);
642         break;
643       }
644     }
645     else if ($parameters[0] == "from") {
646       /* /order/from/referrer/Iain+Patterson/4 */
647       switch ($parameters[1]) {
648         case "referrer":
649           $contact_id = $parameters[3];
650           $q = new ContactQuery;
651           $contact = $q->findOneById($contact_id);
652           show_requester_orders(0, 10, $parameters[2], $contact_id, $state_mask);
653         break;
654       }
655     }
656     else if ($parameters[0] == "to") {
657       /* /order/to/beneficiary/Cambridge+Community+Church/1 */
658       switch ($parameters[1]) {
659         case "beneficiary":
660           $contact_id = $parameters[3];
661           $q = new ContactQuery;
662           $hub = $q->findOneById($contact_id);
663           show_beneficiary_orders(0, 10, $parameters[2], $contact_id, $state_mask);
664         break;
665       }
666     }
667     else if ($parameters[0] == "place") {
668       if ($parameters[1] == "for") {
669         if ($parameters[2] == "beneficiary") {
670           if ($parameters[4]) $contact = get_contact_by_id($parameters[4]);
671           if (! $contact) $contact = get_contact_by_name(urldecode($parameters[3]));
672           if ($contact) show_contact_order_form($contact);
673         }
674       }
675     }
676   }
677   list($ignored, $id, $args) = parse_parameters($parameters);
678   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
679   if (count($args)) {
680     switch ($args[0]) {
681       case "delete":
682         confirm_delete_order($id);
683       break;
684
685       case "confirmdelete":
686         delete_order($id);
687       break;
688     }
689   }
690   else if (isset($id)) show_order($id);
691   else if ($state_mask) show_orders(0, 10, null, null, $state_mask);
692
693   show_order_forms($city_id, $state_mask);
694   show_add_new_order_form($city_id);
695
696
697 ?>