+function get_deliveries_from_trans($stock_id, $from)
+{
+ // -ve qty is delivery either by ST_CUSTDELIVERY or inventory adjustment
+ //Price for GRN and SUPPCREDIT and std_cost for other trans_types
+ $sql = "SELECT SUM(-qty), SUM(-qty*IF(type=".ST_SUPPRECEIVE." OR type=".ST_SUPPCREDIT.", price, 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);
+
+ $sql = "SELECT IF(type=".ST_SUPPRECEIVE." OR type=".ST_SUPPCREDIT.", price, standard_cost)
+ FROM ".TB_PREF."stock_moves
+ WHERE stock_id=".db_escape($stock_id)
+ ." AND trans_id ='$from'";
+ $result = db_query($sql, "The deliveries could not be updated");
+ $cost = db_fetch_row($result);
+
+ // 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);
+
+ $qty = $row[0] - $qoh[0]; //QOH prior to -ve stock is subtracted
+ $final_cost = $row[1] - $qoh[0]*$cost[0];
+
+ return array($qty,$final_cost);
+}
+
+function get_purchases_from_trans($stock_id, $from)
+{
+ // Calculate All inward stock moves i.e. qty > 0
+ $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, "Could not get get_purchases_from_trans");
+ $row = db_fetch_row($result);
+
+ return $row;
+}
+