+
+ return db_num_rows($result) > 0;
+}
+
+//-------------------------------------------------------------------
+
+function last_negative_stock_begin_date($stock_id, $to)
+{
+ $to = date2sql($to);
+ $sql ="SET @q = 0";
+ db_query($sql);
+ $sql = "SET @flag = 0";
+ db_query($sql);
+ $sql = "SELECT SUM(qty), @q:= @q + qty, IF(@q < 0 AND @flag=0, @flag:=1,@flag:=0), IF(@q < 0 AND @flag=1, tran_date,'') AS begin_date
+ FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)." AND tran_date<='$to'
+ AND qty <> 0
+ GROUP BY stock_id ORDER BY tran_date";
+
+ $result = db_query($sql, "The dstock moves could not be retrieved");
+ $row = db_fetch_row($result);
+ return $row[3];
+}
+
+//-------------------------------------------------------------------
+// Newly written
+function last_negative_stock_trans_id($stock_id, $to)
+{
+ $sql = "SELECT * from ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)."
+ AND qty <> 0 order by trans_id asc";
+
+ $result = db_query($sql, "The query on stock moves failed.");
+
+ $qty = 0;
+ $flag = 0;
+ $negative_trans_id = 1;
+
+ while ($myrow = db_fetch($result))
+ {
+ $qty += $myrow['qty'];
+ if ($qty < 0 && $flag == 0)
+ {
+ $flag = 1;
+ $negative_trans_id = $myrow['trans_id'];
+ }
+ if ($qty > 0)
+ $flag = 0;
+ }
+
+ return $negative_trans_id;
+}
+
+//-------------------------------------------------------------------
+
+function get_deliveries_between($stock_id, $from, $to)
+{
+ $from = date2sql($from);
+ $to = date2sql($to);
+ $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
+ WHERE type=".ST_CUSTDELIVERY." AND stock_id=".db_escape($stock_id)." AND
+ tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
+
+ $result = db_query($sql, "The deliveries could not be updated");
+ return db_fetch_row($result);
+}
+
+//Newly written
+function get_deliveries_from_trans($stock_id, $from)
+{
+ // -ve qty is delivery either by ST_CUSTDELIVERY or inventory adjustment
+ $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)." AND qty < 0 AND
+ trans_id>='$from' GROUP BY stock_id";
+ $result = db_query($sql, "The deliveries could not be updated");
+ $row = db_fetch_row($result);
+
+ display_notification('Row0 - '.$row[0].' Row1- '.$row[1]);
+
+ //return $row;
+
+ // Get Std cost of previsous transaction before the cut-over delivery
+ // This is useful to get inventory valuation
+ $prev_trans = $from - 1;
+ display_notification('From - '.$from.' Prev- '.$prev_trans);
+ $sql = "SELECT standard_cost FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)." AND
+ trans_id ='$prev_trans'";
+ $result = db_query($sql, "The deliveries could not be updated");
+ $cost = db_fetch_row($result);
+
+ display_notification('Last Delivery Cost - '.$cost[0]);
+
+ // Adjusting QOH valuation
+ $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)." AND
+ trans_id<'$from' GROUP BY stock_id";
+ $result = db_query($sql, "The deliveries could not be updated");
+ $qoh = db_fetch_row($result);
+
+ display_notification('QOH before last delivery - '.$qoh[0]);