Fixed inventory valuation bug after voiding foreign supplier GRN leading to negative...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 19 Nov 2013 14:09:02 +0000 (15:09 +0100)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 19 Nov 2013 14:09:02 +0000 (15:09 +0100)
includes/db/inventory_db.inc
purchasing/includes/db/grn_db.inc

index 288434d50a133217e8896db499201448f0d12390..e8f8e3c28888c4cc07b0c3d8d91e9eb4dd6742ca 100644 (file)
@@ -286,9 +286,9 @@ function handle_negative_inventory($stock_id, $quantity, $standard_cost, $date_)
 
                $id = get_next_trans_no(ST_JOURNAL);
                $ref = $Refs->get_next(ST_JOURNAL);
-               $diff = get_standard_cost($stock_id) - $standard_cost;
-               
-               if ($diff !=0)
+               $diff = round($qoh*get_standard_cost($stock_id) + $quantity*$standard_cost, user_price_dec());
+
+               if ($diff != 0)
                {
                        $stock_gl_code = get_stock_gl_code($stock_id);
                        $memo = _("Zero/negative inventory handling");
@@ -296,13 +296,13 @@ function handle_negative_inventory($stock_id, $quantity, $standard_cost, $date_)
                        add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
                                $stock_gl_code["inventory_account"],
                                $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo, 
-                               -$qoh * $diff);
+                               -$diff);
                        //GL Posting to inventory adjustment account
                        add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
                                $stock_gl_code["adjustment_account"],
                                $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
-                               $qoh * $diff);
-                               
+                               $diff);
+
                        add_audit_trail(ST_JOURNAL, $id, $date_);
                        add_comments(ST_JOURNAL, $id, $date_, $memo);
                        $Refs->save(ST_JOURNAL, $id, $ref);     
index f5bbf4ac8a0e993004cc085621eb97fafaefcd6c..4d85cc9fc8f9c8cd710c7e58d64f9884aabe02b5 100644 (file)
 //------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
 function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, $adj_only=false)
 {
-       //Handle if inventory will become negative
-    //8-OCT-2011 : Skip negative inventory adjustment for case of adjust_only
-    if (is_inventory_item($stock_id) && !$adj_only) 
-        handle_negative_inventory($stock_id, $qty, $price, $date);     
-
        // probably this function should be optimized
        // passing transaction cart as argument. This would
        // save a couple of db calls like get_supplier()
@@ -40,6 +35,11 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
                $price_in_home_currency = $price;
        
        $price_in_home_currency_ = $price_in_home_currency;
+
+       //Handle if inventory will become negative
+    //8-OCT-2011 : Skip negative inventory adjustment for case of adjust_only
+    if (is_inventory_item($stock_id) && !$adj_only) 
+        handle_negative_inventory($stock_id, $qty, $price_in_home_currency, $date);
        
        $sql = "SELECT mb_flag, material_cost, labour_cost, overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
        $result = db_query($sql);