Changed calculating of cost update (average material price) for better sync.
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 15 Jul 2010 14:16:12 +0000 (14:16 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 15 Jul 2010 14:16:12 +0000 (14:16 +0000)
CHANGELOG.txt
manufacturing/includes/db/work_orders_db.inc
purchasing/includes/db/grn_db.inc
purchasing/includes/db/invoice_db.inc

index 5756b79f16e99ac65f35e1dceaa19f46176eb735..a25d4097741a730bf2a31b33697e25f8cfa9f7d3 100644 (file)
@@ -19,6 +19,12 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+15-Jul-2010 Chaitanya/Joe Hunt
+! Changed calculating of cost update (average material price) for better sync.
+$ /purchasing/includes/db/grn_db.inc
+  /purchasing/includes/db/invoice_db.inc
+  /manufacturing/includes/db/work_orders_db.inc
+  
 ------------------------------- Release 2.3 Beta --------------------------------
 28-Jun-2010 Joe Hunt
 ! Release 2.3 Beta
index 8766ffd1ca62442c16553531ba746b59ba72e219..4a442a8b6a3ecff0dc31ceddae0c5193bbae79eb 100644 (file)
@@ -27,12 +27,13 @@ function add_material_cost($stock_id, $qty, $date_)
        $result = db_query($sql);
        $myrow = db_fetch($result);
        $material_cost =  $myrow['material_cost'];
-       $qoh = get_qoh_on_date($stock_id, null, $date_);
+       //$qoh = get_qoh_on_date($stock_id, null, $date_);
+       $qoh = get_qoh_on_date($stock_id);
        if ($qoh < 0)
                $qoh = 0;
        if ($qoh + $qty != 0)   
                $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
-       $material_cost = round2($material_cost, $dec);  
+       //$material_cost = round2($material_cost, $dec);        
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
                WHERE stock_id=".db_escape($stock_id);
        db_query($sql,"The cost details for the inventory item could not be updated");
@@ -49,12 +50,13 @@ function add_overhead_cost($stock_id, $qty, $date_, $costs)
        $result = db_query($sql);
        $myrow = db_fetch($result);
        $overhead_cost =  $myrow['overhead_cost'];
-       $qoh = get_qoh_on_date($stock_id, null, $date_);
+       //$qoh = get_qoh_on_date($stock_id, null, $date_);
+       $qoh = get_qoh_on_date($stock_id);
        if ($qoh < 0)
                $qoh = 0;
        if ($qoh + $qty != 0)   
                $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
-       $overhead_cost = round2($overhead_cost, $dec);  
+       //$overhead_cost = round2($overhead_cost, $dec);        
        $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
                WHERE stock_id=".db_escape($stock_id);
        db_query($sql,"The cost details for the inventory item could not be updated");
@@ -71,12 +73,13 @@ function add_labour_cost($stock_id, $qty, $date_, $costs)
        $result = db_query($sql);
        $myrow = db_fetch($result);
        $labour_cost =  $myrow['labour_cost'];
-       $qoh = get_qoh_on_date($stock_id, null, $date_);
+       //$qoh = get_qoh_on_date($stock_id, null, $date_);
+       $qoh = get_qoh_on_date($stock_id);
        if ($qoh < 0)
                $qoh = 0;
        if ($qoh + $qty != 0)   
                $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
-       $labour_cost = round2($labour_cost, $dec);      
+       //$labour_cost = round2($labour_cost, $dec);    
        $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
                WHERE stock_id=".db_escape($stock_id);
        db_query($sql,"The cost details for the inventory item could not be updated");
@@ -93,12 +96,13 @@ function add_issue_cost($stock_id, $qty, $date_, $costs)
        $material_cost =  $myrow['material_cost'];
        $dec = user_price_dec();
        price_decimal_format($material_cost, $dec); 
-       $qoh = get_qoh_on_date($stock_id, null, $date_);
+       //$qoh = get_qoh_on_date($stock_id, null, $date_);
+       $qoh = get_qoh_on_date($stock_id);
        if ($qoh < 0)
                $qoh = 0;
        if ($qoh + $qty != 0)   
                $material_cost = ($qty * $costs) /      ($qoh + $qty);
-       $material_cost = round2($material_cost, $dec);  
+       //$material_cost = round2($material_cost, $dec);        
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
                .db_escape($material_cost)
                ." WHERE stock_id=".db_escape($stock_id);
index 2e7053574d2f594691cf5d614efe3121a15d92f3..e0284cec532cc69ee6c8c10afe08e205035d3dfd 100644 (file)
@@ -38,7 +38,9 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
        else
                $exclude = 0;
        $cost_adjust = false;
-       $qoh = get_qoh_on_date($stock_id, null, $date, $exclude);
+       //$qoh = get_qoh_on_date($stock_id, null, $date, $exclude);
+       $qoh = get_qoh_on_date($stock_id);
+       
        if ($adj_only)
        {
                if ($qoh > 0)
@@ -55,7 +57,8 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
                if ($qoh + $qty != 0)
                        $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) /     ($qoh + $qty);
        }       
-       $material_cost = round2($material_cost, $dec);
+       //$material_cost = round2($material_cost, $dec);
+       
        if ($cost_adjust) // new 2010-02-10
                adjust_deliveries($stock_id, $material_cost, $date);
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
index c584115c858330bc3bd016d50c0939cbb91a674e..de5e8f0a5a6da22f523d84dd505dcd5141186f2c 100644 (file)
@@ -108,7 +108,8 @@ function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amoun
        $ex_rate = get_exchange_rate_to_home_currency($currency, $date);
        $amount2 = $amount2 / $ex_rate;
        $diff = $amount2 - $amount1;
-       return round2($diff, $dec);
+       //return round2($diff, $dec);
+       return $diff;
 }
 //----------------------------------------------------------------------------------------
 
@@ -203,9 +204,12 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                $line_tax = $entered_grn->full_charge_price($supp_trans->tax_group_id) - $line_taxfree;
                $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
                $iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] : $stock_gl_code["cogs_account"]);
-               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier_accounts["purchase_account"],
                        $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
-                       $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
+                       $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);             
+               /*$total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+                       $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
+                       $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);*/
        // -------------- if price changed since po received. 16 Aug 2008 Joe Hunt
        if ($trans_type == ST_SUPPINVOICE)
        {
@@ -227,13 +231,31 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        //$diff = $entered_grn->chg_price - $old_price;
                        $old_date = sql2date($old[1]);
                        $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_price, 
-                               $entered_grn->chg_price);
+                               $entered_grn->chg_price);       
+                       
                        // always return due to change in currency.
+                       /*$mat_cost = update_average_material_cost(null, $entered_grn->item_code,
+                               $diff, $entered_grn->this_quantity_inv, $old_date, true); */
                        $mat_cost = update_average_material_cost(null, $entered_grn->item_code,
-                               $diff, $entered_grn->this_quantity_inv, $old_date, true);
+                               $diff, $entered_grn->this_quantity_inv, null, true);
+                               
+                       //Add GL transaction for GRN Provision in case of difference
+                       if (($diff * $entered_grn->this_quantity_inv) != 0 )
+                       {
+                               $diff_amt = $diff * $entered_grn->this_quantity_inv;
+                               add_gl_trans($trans_type, $invoice_id, $date_,  $iv_act,
+                                       $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], 'GRN Provision',
+                                       $diff_amt, null, null, null,
+                                       "The general ledger transaction could not be added for the GRN of the inventory item"); 
+                                       
+                               add_gl_trans($trans_type, $invoice_id, $date_,  $stock_gl_code["cogs_account"],
+                                       0, 0, 'GRN Provision', -$diff_amt, null, null, null,
+                                       "The general ledger transaction could not be added for the GRN of the inventory item");                         
+                       }
+                       
                        // added 2008-12-08 Joe Hunt. Update the purchase data table
                        add_or_update_purchase_data($supp_trans->supplier_id, $entered_grn->item_code, $entered_grn->chg_price); 
-                       $deliveries = get_deliveries_between($entered_grn->item_code, $old_date, Today()); // extend the period, if invoice is before any deliveries.
+                       /*$deliveries = get_deliveries_between($entered_grn->item_code, $old_date, Today()); // extend the period, if invoice is before any deliveries.
                        if ($deliveries[0] != 0) // have deliveries been done during the period?
                        {
                                $deliveries[1] /= $deliveries[0];
@@ -254,7 +276,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                                                "The general ledger transaction could not be added for the price variance of the inventory item");
                                }               
                                update_stock_move_pid(ST_CUSTDELIVERY, $entered_grn->item_code, $old_date, $date_, 0, $mat_cost);
-                       }
+                       } */
                        update_stock_move_pid(ST_SUPPRECEIVE, $entered_grn->item_code, $old_date, $old_date, $supp_trans->supplier_id, $mat_cost);
                //}
                }