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