A couple of small bugs were fixed in average material cost
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 14 Jan 2010 10:06:22 +0000 (10:06 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 14 Jan 2010 10:06:22 +0000 (10:06 +0000)
CHANGELOG.txt
manufacturing/includes/db/work_orders_db.inc
purchasing/includes/db/grn_db.inc
purchasing/includes/db/invoice_db.inc

index aa7e6d8a02adbf7245718906f95f36ab4ddae827..75801b3c9c4ae1e5d10ca374a127fa97871b5f4f 100644 (file)
@@ -19,6 +19,12 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+14-Jan-2010 Joe Hunt
+# A couple of small bugs were fixed in average material cost
+$ /purchasing/includes/db/grn_db.inc
+  /purchasing/includes/db/invoice_db.inc
+  /manufacturing/includes/db/work_orders_db.inc
+  
 13-Jan-2010 Joe Hunt
 ! Changed so also Item Adjustment and Cost Update allow more than 2 dec in price
 $ /inventory/cost_update.php
index f97f87a5e3813e39d97d6d014029a852850a89b4..a88f96d93804a66585a70cf2441eee654198c894 100644 (file)
@@ -20,6 +20,8 @@ function add_material_cost($stock_id, $qty, $date_)
                $standard_cost = get_standard_cost($bom_item['component']);
                $m_cost += ($bom_item['quantity'] * $standard_cost);
        }
+       $dec = user_price_dec();
+       $m_cost = price_decimal_format($m_cost, $dec);
        $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
                .db_escape($stock_id);
        $result = db_query($sql);
@@ -30,6 +32,7 @@ function add_material_cost($stock_id, $qty, $date_)
                $material_cost = 0;
        else
                $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
+       $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");
@@ -37,6 +40,8 @@ function add_material_cost($stock_id, $qty, $date_)
 
 function add_overhead_cost($stock_id, $qty, $date_, $costs)
 {
+       $dec = user_price_dec();
+       $costs = price_decimal_format($costs, $dec); 
        if ($qty != 0)
                $costs /= $qty;
        $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
@@ -49,6 +54,7 @@ function add_overhead_cost($stock_id, $qty, $date_, $costs)
                $overhead_cost = 0;
        else
                $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
+       $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");
@@ -56,6 +62,8 @@ function add_overhead_cost($stock_id, $qty, $date_, $costs)
 
 function add_labour_cost($stock_id, $qty, $date_, $costs)
 {
+       $dec = user_price_dec();
+       $costs = price_decimal_format($costs, $dec); 
        if ($qty != 0)
                $costs /= $qty;
        $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
@@ -68,6 +76,7 @@ function add_labour_cost($stock_id, $qty, $date_, $costs)
                $labour_cost = 0;
        else
                $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
+       $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");
@@ -82,11 +91,14 @@ function add_issue_cost($stock_id, $qty, $date_, $costs)
        $result = db_query($sql);
        $myrow = db_fetch($result);
        $material_cost =  $myrow['material_cost'];
+       $dec = user_price_dec();
+       $material_cost = price_decimal_format($material_cost, $dec); 
        $qoh = get_qoh_on_date($stock_id, null, $date_);
        if ($qoh + $qty  <= 0)
                $material_cost = 0;
        else
                $material_cost = ($qty * $costs) /      ($qoh + $qty);
+       $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 84a2f938ad83de9ecb9eeddf6fbefe2484ffa6e0..b055c1f94eb8b14a955b9f8afa302d9eaf04303a 100644 (file)
@@ -17,20 +17,27 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
                $currency = get_supplier_currency($supplier);
        else
                $currency = null;
+       $dec = user_price_dec();
+       $price = price_decimal_format($price, $dec);
        if ($currency != null)
-               $price_in_home_currency = to_home_currency($price, $currency, $date);
+       {
+               $ex_rate = get_exchange_rate_to_home_currency($currency, $date_);
+               $price_in_home_currency = $price / $ex_rate;
+               //$price_in_home_currency = to_home_currency($price, $currency, $date);
+       }       
        else
                $price_in_home_currency = $price;
        $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
        $result = db_query($sql);
        $myrow = db_fetch($result);
        $material_cost = $myrow['material_cost'];
+       if ($price > -0.0001 && $price < 0.0001)
+               return $material_cost;
        if ($adj_only)
                $exclude = ST_CUSTDELIVERY;
        else
                $exclude = 0;
        $qoh = get_qoh_on_date($stock_id, null, $date, $exclude);
-
        if ($adj_only)
        {
                if ($qoh <= 0)
@@ -41,7 +48,12 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
        elseif ($qoh + $qty <= 0)
                $material_cost = 0;
        else
+       {
+               if ($qoh < 0)
+                       $qoh = 0;
                $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) /     ($qoh + $qty);
+       }       
+       $material_cost = round2($material_cost, $dec);
 
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
                WHERE stock_id=".db_escape($stock_id);
@@ -66,7 +78,6 @@ function add_grn(&$po, $date_, $reference, $location)
                {
 
                        /*Update sales_order_details for the new quantity received and the standard cost used for postings to GL and recorded in the stock movements for FIFO/LIFO stocks valuations*/
-
                        //------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
                        update_average_material_cost($po->supplier_id, $order_line->stock_id, $order_line->price,
                                $order_line->receive_qty, $date_);
index ffdb8c17316ffebd60e78392c1eb71331be69ea0..ef60a9380ac5312197fffc39e4b67e9b8538531f 100644 (file)
@@ -112,10 +112,15 @@ function get_deliveries_between($stock_id, $from, $to)
 
 function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amount2)
 {
+       $dec = user_price_dec();
+       price_decimal_format($amount2, $dec);
        $currency = get_supplier_currency($supplier);
-       $amount1 = to_home_currency($amount1, $currency, $old_date);
-       $amount2 = to_home_currency($amount2, $currency, $date);
-       return $amount2 - $amount1;
+       $ex_rate = get_exchange_rate_to_home_currency($currency, $old_date);
+       $amount1 = $amount1 / $ex_rate;
+       $ex_rate = get_exchange_rate_to_home_currency($currency, $date_);
+       $amount2 = $amount2 / $ex_rate;
+       $diff = $amount2 - $amount1;
+       return round2($diff, $dec);
 }
 //----------------------------------------------------------------------------------------
 
@@ -207,7 +212,6 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        $entered_grn->this_quantity_inv = -$entered_grn->this_quantity_inv;
                        set_grn_item_credited($entered_grn, $supp_trans->supplier_id, $invoice_id, $date_);
        }
-
                $line_taxfree = $entered_grn->taxfree_charge_price($supp_trans->tax_group_id);
                $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);