***********************************************************************/
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];
}
//--------------------------------------------------------------------------------------
//-------------------------------------------------------------------
// 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
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()) {