[0000608] Need better Negative stock prevention checking
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 10 Mar 2011 10:45:45 +0000 (11:45 +0100)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 10 Mar 2011 10:45:45 +0000 (11:45 +0100)
includes/db/inventory_db.inc
sales/customer_delivery.php

index 533657659aafc3e2a23500b1a1286fcf8a54bcb8..56e9419679d035adf4e148db8052ed1c581a4dd9 100644 (file)
 ***********************************************************************/
 function get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
 {
-       if ($date_ == null)
-               $date_ = Today();
-
-       $date = date2sql($date_);
-
-       $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
-               WHERE stock_id=".db_escape($stock_id)."
-               AND tran_date <= '$date'";
-
-       if ($location != null)
-               $sql .= " AND loc_code = ".db_escape($location);
+    if ($date_ == null)
+    {
+        $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
+            WHERE stock_id=".db_escape($stock_id);
+        $date_ = Today();
+        $date = date2sql($date_);
+    }
+    else
+    {
+        $date = date2sql($date_);
+        $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
+            WHERE stock_id=".db_escape($stock_id)."
+            AND tran_date <= '$date'"; 
+    }
+        
+    if ($location != null)
+        $sql .= " AND loc_code = ".db_escape($location);
 
-       $result = db_query($sql, "QOH calulcation failed");
+    $result = db_query($sql, "QOH calulcation failed");
 
-       $myrow = db_fetch_row($result);
-       if ($exclude > 0)
-       {
-               $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
-                       WHERE stock_id=".db_escape($stock_id)
-                       ." AND type=".db_escape($exclude)
-                       ." AND tran_date = '$date'";
-
-               $result = db_query($sql, "QOH calulcation failed");
-               $myrow2 = db_fetch_row($result);
-               if ($myrow2 !== false)
-                       $myrow[0] -= $myrow2[0];
-       }
+    $myrow = db_fetch_row($result);
+    if ($exclude > 0)
+    {
+        $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
+            WHERE stock_id=".db_escape($stock_id)
+            ." AND type=".db_escape($exclude)
+            ." AND tran_date = '$date'";
+
+        $result = db_query($sql, "QOH calulcation failed");
+        $myrow2 = db_fetch_row($result);
+        if ($myrow2 !== false)
+            $myrow[0] -= $myrow2[0];
+    }
 
-       return $myrow[0];
+    return $myrow[0];
 }
 
 //--------------------------------------------------------------------------------------
@@ -101,6 +107,19 @@ function last_negative_stock_begin_date($stock_id, $to)
 
 //-------------------------------------------------------------------
 // Newly written
+
+function get_already_delivered($stock_id, $location, $trans_no)
+{
+       $sql = "SELECT ".TB_PREF."stock_moves.qty
+               FROM ".TB_PREF."stock_moves
+               WHERE ".TB_PREF."stock_moves.stock_id = ".db_escape($stock_id)."
+               AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location)."
+               AND type=".ST_CUSTDELIVERY." AND trans_no=".db_escape($trans_no);
+       $result = db_query($sql, "Could not get stock moves");
+       $row = db_fetch_row($result);
+       return $row[0];
+}
+
 function last_negative_stock_trans_id($stock_id, $to)
 {
        $sql = "SELECT * from ".TB_PREF."stock_moves
index d8545523a328740e4447db2dea9ed5aec61415fa..c7e051e1bab3c43962a3c1ae33e8df3dc1e0979a 100644 (file)
@@ -266,24 +266,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()) {