Small changes in costing by Chaitanya.
[fa-stable.git] / includes / db / inventory_db.inc
index 1f1c3c3046cc01da497fab28e66bd08e01cad6fe..5a5dda4b24485f1b58043b445306bad8eef856f4 100644 (file)
@@ -166,17 +166,20 @@ function get_deliveries_between($stock_id, $from, $to)
 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";
+    //Price for GRN and SUPPCREDIT and std_cost for other trans_types\r
+    $sql = "SELECT SUM(-qty), SUM(-qty*IF(type=".ST_SUPPRECEIVE." OR type=".ST_SUPPCREDIT.", price, standard_cost))\r
+        FROM ".TB_PREF."stock_moves\r
+        WHERE stock_id=".db_escape($stock_id)." AND qty < 0 AND\r
+            trans_id>='$from' GROUP BY stock_id";
        $result = db_query($sql, "The deliveries could not be updated");
        $row = db_fetch_row($result);
        
-       $sql = "SELECT 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);  
+    $sql = "SELECT IF(type=".ST_SUPPRECEIVE." OR type=".ST_SUPPCREDIT.", price, standard_cost)\r
+        FROM ".TB_PREF."stock_moves\r
+        WHERE stock_id=".db_escape($stock_id)\r
+            ." AND trans_id ='$from'";\r
+    $result = db_query($sql, "The deliveries could not be updated");\r
+    $cost = db_fetch_row($result);
        
        // Adjusting QOH valuation 
        $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves