+ $qoh = $myrow[0];
+ return $qoh ? $qoh : 0;
+}
+
+/**
+* Check whether change in stock on date would not cause negative qoh in stock history.
+* Returns null on success or max. available quantity with respective date otherwise.
+* Running balance is checked on daily basis only, as we do not control time of transaction.
+*
+* $delta_qty - tested change in stock qty at $date.
+* $date - check date; when set to null checks all the stock history.
+**/
+
+function check_negative_stock($stock_id, $delta_qty, $location=null, $date=null)
+{
+
+ if ($delta_qty >= 0)
+ return null; // qty increese is always safe
+
+ if (!isset($date))
+ $date = Today();
+
+ $date = date2sql($date);
+
+ // check stock status on date
+ $sql = "SELECT SUM(qty) qty, '$date' tran_date FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)."
+ AND tran_date <= '$date'";
+
+ if ($location)
+ $sql .= " AND loc_code = ".db_escape($location);
+
+ $result = db_query($sql, "QOH calculation failed");
+ $qos = db_fetch_assoc($result);
+
+ // check also all stock changes after the date to avoid negative stock in future
+ $sql = TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) . " AND tran_date > '$date'";
+
+ if ($location)
+ $sql .= " AND loc_code=".db_escape($location);
+
+ $rt = running_total_sql($sql, 'qty', 'tran_date');
+
+ $sql = "SELECT {$qos['qty']}+total qty, tran_date FROM ($rt) stock_status ORDER by total, tran_date";
+ $history = db_query($sql, 'cannot check stock history');
+ $min_qos = db_fetch($history);
+
+ if ($min_qos && ($min_qos['qty'] < $qos['qty']))
+ $qos = $min_qos;
+
+ return -$delta_qty > $qos['qty'] ? $qos : null;