Stable merged into unstable again (due to failure on binary file during previous...
[fa-stable.git] / sales / customer_delivery.php
index 2208a455c6b093539576f0c47c31560799c08288..df8bad06d9ebd4016d13e8f24174a1cf90420c50 100644 (file)
@@ -89,24 +89,15 @@ if (isset($_GET['OrderNumber']) && $_GET['OrderNumber'] > 0) {
 
        $ord = new Cart(ST_SALESORDER, $_GET['OrderNumber'], true);
 
-       /*read in all the selected order into the Items cart  */
-
        if ($ord->count_items() == 0) {
                hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
                        _("Select a different sales order to delivery"), "OutstandingOnly=1");
                die ("<br><b>" . _("This order has no items. There is nothing to delivery.") . "</b>");
        }
 
-       $ord->trans_type = ST_CUSTDELIVERY;
-       $ord->src_docs = $ord->trans_no;
-       $ord->order_no = key($ord->trans_no);
-       $ord->trans_no = 0;
-       $ord->reference = $Refs->get_next(ST_CUSTDELIVERY);
-       $ord->document_date = new_doc_date();
-       $cust = get_customer($ord->customer_id);
-       // 2010-09-03 Joe Hunt
-       $ord->dimension_id = $cust['dimension_id'];
-       $ord->dimension2_id = $cust['dimension2_id'];
+       // Adjust Shipping Charge based upon previous deliveries TAM
+       adjust_shipping_charge($ord, $_GET['OrderNumber']);
        $_SESSION['Items'] = $ord;
        copy_from_cart();
 
@@ -279,24 +270,35 @@ function check_quantities()
 
 function check_qoh()
 {
-       global $SysPrefs;
-
-       if (!$SysPrefs->allow_negative_stock()) {
-               foreach ($_SESSION['Items']->line_items as $itm) {
-
-                       if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
-                               $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
-
-                               if ($itm->qty_dispatched > $qoh) {
-                                       display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
-                                               " " . $itm->stock_id . " - " .  $itm->item_description);
-                                       return false;
-                               }
-                       }
-               }
-       }
-       return true;
+    global $SysPrefs;
+    $dn = &$_SESSION['Items'];
+    $newdelivery = ($dn->trans_no==0);
+    if (!$SysPrefs->allow_negative_stock()) {
+        foreach ($_SESSION['Items']->line_items as $itm) {
+
+            if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
+                $qoh_by_date = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
+                $qoh_abs = get_qoh_on_date($itm->stock_id, $_POST['Location'], null);
+                //If editing current delivery delivered qty should be added 
+                if (!$newdelivery)
+                {
+                    $delivered = get_already_delivered($itm->stock_id, $_POST['Location'], key($dn->trans_no));
+
+                    $qoh_abs = $qoh_abs - $delivered;
+                    $qoh_by_date = $qoh_by_date - $delivered;
+                }
+                $qoh = ($qoh_by_date < $qoh_abs ? $qoh_by_date : $qoh_abs); 
+                if ($itm->qty_dispatched > $qoh) {
+                    display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
+                        " " . $itm->stock_id . " - " . $itm->item_description);
+                    return false;
+                }
+            }
+        }
+    }
+    return true;
 }
+
 //------------------------------------------------------------------------------
 
 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {