From be1b322fa59f83bb8073ddd815b57ec697c47a45 Mon Sep 17 00:00:00 2001 From: Joe Hunt Date: Thu, 14 Jan 2010 10:06:22 +0000 Subject: [PATCH] A couple of small bugs were fixed in average material cost --- CHANGELOG.txt | 6 ++++++ manufacturing/includes/db/work_orders_db.inc | 12 ++++++++++++ purchasing/includes/db/grn_db.inc | 17 ++++++++++++++--- purchasing/includes/db/invoice_db.inc | 12 ++++++++---- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index aa7e6d8a..75801b3c 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 diff --git a/manufacturing/includes/db/work_orders_db.inc b/manufacturing/includes/db/work_orders_db.inc index f97f87a5..a88f96d9 100644 --- a/manufacturing/includes/db/work_orders_db.inc +++ b/manufacturing/includes/db/work_orders_db.inc @@ -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); diff --git a/purchasing/includes/db/grn_db.inc b/purchasing/includes/db/grn_db.inc index 84a2f938..b055c1f9 100644 --- a/purchasing/includes/db/grn_db.inc +++ b/purchasing/includes/db/grn_db.inc @@ -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_); diff --git a/purchasing/includes/db/invoice_db.inc b/purchasing/includes/db/invoice_db.inc index ffdb8c17..ef60a938 100644 --- a/purchasing/includes/db/invoice_db.inc +++ b/purchasing/includes/db/invoice_db.inc @@ -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); -- 2.30.2