Typo.
[readifood.git] / lib / order.php
index cb40f63..f1911c5 100644 (file)
     echo "  <td>Beneficiary</td>\n";
     echo "  <td><select name=\"beneficiary_id\">\n";
     option("beneficiary_id", null, "");
-    $contacts = get_area_beneficiaries($area_id);
-    foreach ($contacts as $contact) {
-      option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
+    if (! $order->getId() && $order->getBeneficiaryId()) {
+      $contact = get_contact_by_id($order->getBeneficiaryId());
+      if ($contact) option("beneficiary_id", $order->getBeneficiaryId(), $contact->getDisplayname(), $order->getBeneficiaryId());
+    }
+    else {
+      $contacts = get_area_beneficiaries($area_id);
+      foreach ($contacts as $contact) {
+        option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
+      }
     }
     echo "</select></td>\n";
     echo "</tr>\n";
     echo "</select></td>\n";
     echo "</tr>\n";
 
-    /* Quantity. */
-    echo "<tr>\n";
-    echo "  <td>Quantity (kg)</td>\n";
-    echo "  <td>"; input("quantity", sprintf("%0.2f", $order->getQuantity() / 1000)); echo "</td>\n";
-    echo "</tr>\n";
-
     /* Parcel type. */
     echo "<tr>\n";
     echo "  <td>Parcel size</td>\n";
     echo "  <td><select name=\"parcel_size\">\n";
-    $mask = 1 << (count($parcel_sizes) + 1);
+    $mask = 1 << count($parcel_sizes);
     for ($i = 0; $i < count($parcel_sizes); $i++) {
-      option("parcel_size", $i << 1, $parcel_sizes[$i], $order->getParcel() % $mask);
+      option("parcel_size", 1 << $i, $parcel_sizes[$i], $order->getParcel() % $mask);
     }
     echo "</select></td>\n";
     echo "</tr>\n";
     echo "</form>\n";
   }
 
+  function show_contact_order_form($contact) {
+    if (! check_admin(1)) return;
+
+    $area = get_contact_area($contact);
+    if (! $area) {
+      echo "<p>No valid <a href=\"/area\">area</a> for contact!</p>\n";
+      return;
+    }
+
+    $order = new Order;
+    $order->setBeneficiaryId($contact->getId());
+
+    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    echo "<p>Placing order for " . $contact->getStrongLink() . ".";
+    $parcel = $contact->getParcel();
+    if ($parcel) {
+      echo "  Suggested parcel type is <span class=\"strong\">" .  get_contact_parcel_string($contact) . "</span>";
+      $order->setParcel($parcel);
+    }
+    echo "</p>\n";
+
+    echo "<table>\n";
+    show_order_form($order, $area_id);
+
+    echo "<tr>\n";
+    echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
+    echo "</tr>\n";
+    echo "</table>\n";
+    echo "</form>\n";
+  }
+
   function show_add_new_order_form() {
     if (! check_admin(1)) return;
 
       $order->setRequesterId($requester_id);
       $order->setBeneficiaryId($beneficiary_id);
       $order->setHubId($hub_id);
-      $order->setQuantity($quantity * 1000);
       $order->setParcel($parcel);
 
       /* XXX: begin/commit */
         break;
       }
     }
+    else if ($parameters[0] == "place") {
+      if ($parameters[1] == "for") {
+        if ($parameters[2] == "beneficiary") {
+          if ($parameters[4]) $contact = get_contact_by_id($parameters[4]);
+          if (! $contact) $contact = get_contact_by_name(urldecode($parameters[3]));
+          if ($contact) show_contact_order_form($contact);
+        }
+      }
+    }
   }
   list($ignored, $id, $args) = parse_parameters($parameters);
   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";