From 19d08aef75bd8960f1f0452303750ed0387fb1b8 Mon Sep 17 00:00:00 2001 From: Joe Hunt Date: Mon, 21 Mar 2011 09:45:14 +0100 Subject: [PATCH] More Improvements in Costing Valuations, Chaitanya. --- includes/db/inventory_db.inc | 156 ++++++++---- inventory/includes/db/items_adjust_db.inc | 41 +-- inventory/manage/item_codes.php | 4 +- inventory/purchasing_data.php | 4 +- .../includes/db/work_order_issues_db.inc | 71 +++++- .../db/work_order_produce_items_db.inc | 63 ++++- manufacturing/includes/db/work_orders_db.inc | 233 +++++++++++++++--- .../includes/db/work_orders_quick_db.inc | 50 +++- manufacturing/work_order_costs.php | 10 +- purchasing/includes/db/grn_db.inc | 32 ++- purchasing/includes/ui/po_ui.inc | 4 +- 11 files changed, 532 insertions(+), 136 deletions(-) diff --git a/includes/db/inventory_db.inc b/includes/db/inventory_db.inc index 6a1a7f52..244cf93c 100644 --- a/includes/db/inventory_db.inc +++ b/includes/db/inventory_db.inc @@ -65,7 +65,8 @@ function get_item_edit_info($stock_id) function get_standard_cost($stock_id) { - $sql = "SELECT IF(s.mb_flag='D', 0, material_cost + labour_cost + overhead_cost) AS std_cost + //Chaitanya : Compatibility with service items + $sql = "SELECT (material_cost + labour_cost + overhead_cost) AS std_cost FROM ".TB_PREF."stock_master s WHERE stock_id=".db_escape($stock_id); $result = db_query($sql, "The standard cost cannot be retrieved"); @@ -140,8 +141,8 @@ function last_negative_stock_trans_id($stock_id, $to) $flag = 1; $negative_trans_id = $myrow['trans_id']; } - if ($qty > 0) - $flag = 0; + if ($qty >= 0) + $flag = 0; } return $negative_trans_id; @@ -177,11 +178,19 @@ function get_deliveries_from_trans($stock_id, $from) // Get Std cost of previsous transaction before the cut-over delivery // This is useful to get inventory valuation - $prev_trans = $from - 1; - display_notification('From - '.$from.' Prev- '.$prev_trans); + //Chaitanya : Corrected + /*$sql = "SELECT max( `trans_id` ) + 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"); + $trans = db_fetch_row($result); + $prev_trans = $trans[0];*/ + + display_notification('From - '.$from); $sql = "SELECT standard_cost FROM ".TB_PREF."stock_moves - WHERE stock_id=".db_escape($stock_id)." AND - trans_id ='$prev_trans'"; + 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); @@ -211,7 +220,8 @@ function get_purchases_from_trans($stock_id, $from) $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"; - $result = db_query($sql, "The deliveries could not be updated"); + display_notification($sql); + $result = db_query($sql, "Could not get get_purchases_from_trans"); $row = db_fetch_row($result); display_notification('Purchase Qty - '.$row[0].' Cost- '.$row[1]); @@ -272,6 +282,10 @@ function adjust_deliveries($stock_id, $material_cost, $to) $new_sales_cost = $row[0] * $material_cost; $sales_diff = $new_sales_cost - $old_sales_cost; + display_notification(_("new_sales_cost").' - '.$row[0] .' * '. $material_cost); + display_notification(_("new_sales_cost").' - '.$new_sales_cost.' old_sales_cost- '.$old_sales_cost); + display_notification(_("Sales Diff").' - '.$sales_diff); + $row = get_purchases_from_trans($stock_id, $from); $purchase_diff = 0; $old_purchase_cost = 0; @@ -282,8 +296,13 @@ function adjust_deliveries($stock_id, $material_cost, $to) $purchase_diff = $new_purchase_cost - $old_purchase_cost; } + display_notification(_("new_purchase_cost").' - '.$new_purchase_cost.' old_purchase_cost- '.$old_purchase_cost); + display_notification(_("purchase_diff").' - '.$purchase_diff); + $diff = $sales_diff - $purchase_diff; + display_notification(_("Final diff").' - '.$diff); + if ($diff != 0) { $update_no = get_next_trans_no(ST_COSTUPDATE); @@ -309,8 +328,8 @@ function adjust_deliveries($stock_id, $material_cost, $to) function get_stock_gl_code($stock_id) { /*Gets the GL Codes relevant to the item account */ - - $sql = "SELECT inventory_account, cogs_account, + //Chaitanya : Updated to also provide mb_flag + $sql = "SELECT mb_flag, inventory_account, cogs_account, adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id); @@ -318,6 +337,45 @@ function get_stock_gl_code($stock_id) return db_fetch($get); } +//----------------------------------------------------------------------------------------- +//Chaitanya : New Function handle negative stock effect +//Called in add_stock_move +function handle_negative_inventory($stock_id, $quantity, $standard_cost, $date_) +{ + //Chaitanya : If negative adjustment result in negative or zero inventory + //then difference should be adjusted + $qoh = get_qoh_on_date($stock_id); + display_notification($qoh." : ".$quantity); + if ($qoh + $quantity <= 0 && $qoh > 0) //Positive inventory turning zero/negative + { + global $Refs; + + $id = get_next_trans_no(ST_JOURNAL); + $ref = $Refs->get_next(ST_JOURNAL); + $diff = get_standard_cost($stock_id) - $standard_cost; + + if ($diff !=0) + { + $stock_gl_code = get_stock_gl_code($stock_id); + $memo = _("Zero/negative inventory handling"); + //Reverse the inventory effect if $qoh <=0 + 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); + //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); + + add_audit_trail(ST_JOURNAL, $id, $date_); + add_comments(ST_JOURNAL, $id, $date_, $memo); + $Refs->save(ST_JOURNAL, $id, $ref); + } + } +} + //-------------------------------------------------------------------------------------- // $date_ - display / non-sql date @@ -329,9 +387,12 @@ function add_stock_move($type, $stock_id, $trans_no, $location, $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1, $price=0, $discount_percent=0, $error_msg="") { + // Chaitanya : Removed following restriction considering WO issues + // Voiding issues and productions with Service items can not get the + // Service items compatibility // do not add a stock move if it's a non-inventory item - if (!is_inventory_item($stock_id)) - return null; + //if (!is_inventory_item($stock_id)) + //return null; $date = date2sql($date_); @@ -342,8 +403,8 @@ function add_stock_move($type, $stock_id, $trans_no, $location, .", ".db_escape($location).", '$date', " .db_escape($person_id).", ".db_escape($reference).", " .db_escape($quantity).", ".db_escape($std_cost)."," - .db_escape($show_or_hide).", ".db_escape($price).", " - .db_escape($discount_percent).")"; + .db_escape($show_or_hide).", " + .db_escape($price).", ".db_escape($discount_percent).")"; if ($error_msg == "") $error_msg = "The stock movement record cannot be inserted"; @@ -353,15 +414,23 @@ function add_stock_move($type, $stock_id, $trans_no, $location, return db_insert_id(); } -function update_stock_move_pid($type, $stock_id, $from, $to, $pid, $cost) +//Chaitanya : Added function +function update_stock_move($type, $trans_no, $stock_id, $cost) { - $from = date2sql($from); - $to = date2sql($to); - $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost) + $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost) ." WHERE type=".db_escape($type) - ." AND stock_id=".db_escape($stock_id) - ." AND tran_date>='$from' AND tran_date<='$to' - AND person_id = ".db_escape($pid); + ." AND trans_no=".db_escape($trans_no) + ." AND stock_id=".db_escape($stock_id); + db_query($sql, "The stock movement standard_cost cannot be updated"); +} + +function update_stock_move_pid($type, $type_no, $stock_id, $pid, $cost) +{ + $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost) + ." WHERE type=".db_escape($type) + ." AND trans_no=".db_escape($type_no) + ." AND stock_id=".db_escape($stock_id) + ." AND person_id = ".db_escape($pid); db_query($sql, "The stock movement standard_cost cannot be updated"); } @@ -390,7 +459,7 @@ function void_stock_move($type, $type_no) { //Chaitanya : Reversing stock move rather than voiding as it is hazardous to lose stock movement trail with respect to costing /*$sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0, - standard_cost=0 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no); + standard_cost=0 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no); db_query($sql, "Could not void stock moves"); */ @@ -398,30 +467,27 @@ function void_stock_move($type, $type_no) $result = db_query($sql, "Could not void stock moves"); while ($row = db_fetch($result)) { + //Skip cost averaging of service items + if (is_inventory_item($row["stock_id"])) + { + // The cost has to be adjusted. + //Chaitanya : Transaction rates are stored either as price or standard_cost depending + //on types + $types = array(ST_SUPPCREDIT); + if (in_array($type,$types)) + $trans_rate = $row["price"]; + else + $trans_rate = $row["standard_cost"]; + + update_average_material_cost(0, $row["stock_id"], + $trans_rate, -$row["qty"], sql2date($row["tran_date"])); + } + + //Post stock move for service items also add_stock_move($type, $row["stock_id"], $type_no, $row["loc_code"], - sql2date($row["tran_date"]), $row["reference"], -$row["qty"], $row["standard_cost"], $row["person_id"], $row["visible"], - $row["price"], $row["discount_percent"]); - - // If there is cost difference, then cost has to be adjusted. - // IMP : Adjusting cost is suitable instead of normal averaging as it will not work properly for manufactured items. Reason being Avg cost is suitable only for purchased items - - $curr_std_cost = get_standard_cost($row["stock_id"]); - - //Chaitanya : Transaction rates are stored either as price or standard_cost depending - //on types - $types = array(ST_SUPPCREDIT); - if (in_array($type,$types)) - $trans_rate = $row["price"]; - else - $trans_rate = $row["standard_cost"]; - - if ($curr_std_cost != $trans_rate) - { - $cost_diff = $trans_rate - $curr_std_cost; - update_average_material_cost(0, $row["stock_id"], - $cost_diff, -$row["qty"], sql2date($row["tran_date"]), true); - } - + sql2date($row["tran_date"]), $row["reference"], -$row["qty"] + , $row["standard_cost"], $row["person_id"], $row["visible"], + $row["price"], $row["discount_percent"]); } } diff --git a/inventory/includes/db/items_adjust_db.inc b/inventory/includes/db/items_adjust_db.inc index d0b87fc6..77d5b02e 100644 --- a/inventory/includes/db/items_adjust_db.inc +++ b/inventory/includes/db/items_adjust_db.inc @@ -51,8 +51,7 @@ function add_stock_adjustment($items, $location, $date_, $type, $increase, $refe function void_stock_adjustment($type_no) { hook_db_prevoid(ST_INVADJUST, $type_no); - - void_gl_trans(ST_INVADJUST, $type_no); + void_gl_trans(ST_INVADJUST, $type_no); void_stock_move(ST_INVADJUST, $type_no); } @@ -82,6 +81,7 @@ function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $type, display_db_error("Cannot do inventory adjustment for Service item : $stock_id", ""); } + /* Logic shifted to function update_average_material_cost //Chaitanya : If negative adjustment result in negative or zero inventory //then difference should be adjusted $qoh = get_qoh_on_date($stock_id); @@ -93,23 +93,26 @@ function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $type, $ref = $Refs->get_next(ST_JOURNAL); $diff = get_standard_cost($stock_id) - $standard_cost; - $stock_gl_code = get_stock_gl_code($stock_id); - $memo = _("For zero inventory of ").$stock_id." INVADJ REF: ".$reference; - //Reverse the inventory effect if $qoh <=0 - 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); - //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); - - add_audit_trail(ST_JOURNAL, $id, $date_); - add_comments(ST_JOURNAL, $id, $date_, $memo); - $Refs->save(ST_JOURNAL, $id, $ref); - } + if ($diff !=0) + { + $stock_gl_code = get_stock_gl_code($stock_id); + $memo = _("For zero inventory of ").$stock_id." INVADJ REF: ".$reference; + //Reverse the inventory effect if $qoh <=0 + 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); + //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); + + add_audit_trail(ST_JOURNAL, $id, $date_); + add_comments(ST_JOURNAL, $id, $date_, $memo); + $Refs->save(ST_JOURNAL, $id, $ref); + } + }*/ update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_); diff --git a/inventory/manage/item_codes.php b/inventory/manage/item_codes.php index d423b921..1b37162f 100644 --- a/inventory/manage/item_codes.php +++ b/inventory/manage/item_codes.php @@ -103,7 +103,9 @@ if (!isset($_POST['stock_id'])) $_POST['stock_id'] = get_global_stock_item(); echo "
" . _("Item:"). " "; -echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true); +//Chaitanya : Manufcatured item visible +echo stock_items_list('stock_id', $_POST['stock_id'], false, true); +//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true); echo "
"; diff --git a/inventory/purchasing_data.php b/inventory/purchasing_data.php index 1969cf93..f85bc089 100644 --- a/inventory/purchasing_data.php +++ b/inventory/purchasing_data.php @@ -99,7 +99,9 @@ if (!isset($_POST['stock_id'])) $_POST['stock_id'] = get_global_stock_item(); echo "
" . _("Item:"). " "; -echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true); +//Chaitanya : All items can be purchased +echo stock_items_list('stock_id', $_POST['stock_id'], false, true); +//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true); echo "
"; diff --git a/manufacturing/includes/db/work_order_issues_db.inc b/manufacturing/includes/db/work_order_issues_db.inc index 28167da7..7cc79f00 100644 --- a/manufacturing/includes/db/work_order_issues_db.inc +++ b/manufacturing/includes/db/work_order_issues_db.inc @@ -55,9 +55,11 @@ function add_work_order_issue($woid, $ref, $to_work_order, $items, $location, $w if ($to_work_order) $item->quantity = -$item->quantity; + //Chaitanya: Stamp the standard_cost + $standard_cost = get_standard_cost($item->stock_id); // insert a -ve stock move for each item add_stock_move(ST_MANUISSUE, $item->stock_id, $number, - $location, $date_, $memo_, -$item->quantity, 0); + $location, $date_, $memo_, -$item->quantity, $standard_cost); $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued) VALUES (".db_escape($number).", ".db_escape($item->stock_id).", " @@ -68,12 +70,20 @@ function add_work_order_issue($woid, $ref, $to_work_order, $items, $location, $w $issue_cost = $standard_cost * $item->quantity; $issue = get_stock_gl_code($item->stock_id); $stockitem = get_item($item->stock_id); - $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0, + + //Chaitanya : Compatibility for Service Items + if (!is_service($issue["mb_flag"])) + $ivaccount = $issue["inventory_account"]; + else + $ivaccount = $issue["cogs_account"]; + + $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0, $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost); $issue_total += $issue_cost; } if ($issue_total != 0) - add_issue_cost($details['stock_id'], $details['units_reqd'], $date_, $issue_total); + //Chaitanya : Apply cost to QOH as adjustment only + add_issue_cost($details['stock_id'], $details['units_reqd'], $date_, $issue_total, true); $issue = get_stock_gl_code($details['stock_id']); $stockitem = get_item($details['stock_id']); add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], @@ -156,18 +166,63 @@ function void_work_order_issue($type_no) { begin_transaction(); hook_db_prevoid(ST_MANUISSUE, $type_no); + + //Chaitanya : Skip processing already voided entry i.e. explicitly voided + $void_entry = get_voided_entry(ST_MANUISSUE, $type_no); + if ($void_entry) + return; // void the actual issue items and their quantities $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id=" .db_escape($type_no); db_query($sql,"A work order issue item could not be voided"); - // void all related stock moves + // void any related gl trans + //Chaitanya : Nothing happens due to next statement as all gl postings are done against WO + //void_gl_trans(ST_MANUISSUE, $type_no, true); + + //Chaitanya : Reverse the gl posting + $issue = get_work_order_issue($type_no); + $manf_stock_id = $issue["stock_id"]; + $date_ = sql2date($issue["issue_date"]); + $woid = $issue["workorder_id"]; + + $result = get_stock_moves(ST_MANUISSUE, $type_no); + $total_cost = 0; + $issue_total = 0; + while ($myrow = db_fetch($result)) + { + $issue_cost = $myrow["qty"]*$myrow["standard_cost"]; + $issue = get_stock_gl_code($myrow["stock_id"]); + $stockitem = get_item($myrow["stock_id"]); + + //Chaitanya : Compatibility for Service Items + if (!is_service($issue["mb_flag"])) + $ivaccount = $issue["inventory_account"]; + else + $ivaccount = $issue["cogs_account"]; + + if ($issue_cost != 0) + { + $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0, + $date_.": "._("Reversed the issue of")." ".$stockitem["description"], + -$issue_cost); + $issue_total += $issue_cost; + } + } + if ($issue_total != 0) + //Chaitanya : Revese cost effect on manfactured stock item as adjustment only + add_issue_cost($manf_stock_id, 0, $date_, $issue_total, true); + $issue = get_stock_gl_code($manf_stock_id); + $stockitem = get_item($manf_stock_id); + if ($total_cost != 0) + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], + 0, 0, $date_.": "._("Reversed the issue to")." ".$stockitem["description"], + -$total_cost); + + //Chaitanya : Shifted below void all related stock moves void_stock_move(ST_MANUISSUE, $type_no); - - // void any related gl trans - void_gl_trans(ST_MANUISSUE, $type_no, true); - + commit_transaction(); } diff --git a/manufacturing/includes/db/work_order_produce_items_db.inc b/manufacturing/includes/db/work_order_produce_items_db.inc index 9b2095c0..1c72f94f 100644 --- a/manufacturing/includes/db/work_order_produce_items_db.inc +++ b/manufacturing/includes/db/work_order_produce_items_db.inc @@ -44,20 +44,29 @@ function work_order_produce($woid, $ref, $quantity, $date_, $memo_, $close_wo) db_query($sql,"A work order manufacture could not be added"); $id = db_insert_id(); + + // ------------------------------------------------------------------------- + work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id); + // ------------------------------------------------------------------------- - work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id); + // Chaitanya: stamp BOM cost to finished item + $m_cost = 0; + $result = get_bom($details["stock_id"]); + while ($bom_item = db_fetch($result)) + { + $standard_cost = get_standard_cost($bom_item['component']); + $m_cost += ($bom_item['quantity'] * $standard_cost); + } - // ------------------------------------------------------------------------- // insert a +ve stock move for the item being manufactured // negative means "unproduce" or unassemble add_stock_move(ST_MANURECEIVE, $details["stock_id"], $id, - $details["loc_code"], $date_, $ref, $quantity, 0); + $details["loc_code"], $date_, $ref, $quantity, $m_cost); // update wo quantity and close wo if requested work_order_update_finished_quantity($woid, $quantity, $close_wo); - if ($memo_) add_comments(ST_MANURECEIVE, $id, $date_, $memo_); @@ -109,24 +118,56 @@ function void_work_order_produce($type_no) { begin_transaction(); hook_db_prevoid(ST_MANURECEIVE, $type_no); + + //Chaitanya : Skip processing already voided entry i.e. explicitly voided + $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no); + if ($void_entry) + return; $row = get_work_order_produce($type_no); // deduct the quantity of this production from the parent work order work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]); - work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), $type_no); - + //Chaitanya : skipped this step as BOM may have got changed + //work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), $type_no); + + // void any related gl trans + //Chaitanya : Nothing happens due to next statement as all gl postings are done against WO + //void_gl_trans(ST_MANURECEIVE, $type_no, true); + + $woid = $row["workorder_id"]; + $date_ = sql2date($row["date_"]); + + $result = get_stock_moves(ST_MANURECEIVE, $type_no); + while ($myrow = db_fetch($result)) + { + $issue_cost = $myrow["qty"]*$myrow["standard_cost"]; + $issue = get_stock_gl_code($myrow["stock_id"]); + $stockitem = get_item($myrow["stock_id"]); + + //Chaitanya : Compatibility for Service Items + if (!is_service($issue["mb_flag"])) + $ivaccount = $issue["inventory_account"]; + else + $ivaccount = $issue["cogs_account"]; + + if ($issue_cost != 0) + { + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0, + $date_.": "._("Reversed the production ")." ".$stockitem["description"], + -$issue_cost); + } + } + // clear the production record $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no); - db_query($sql, "Cannot void a wo production"); - + db_query($sql, "Cannot void a wo production"); + + //Chaitanya : Shifted below // void all related stock moves void_stock_move(ST_MANURECEIVE, $type_no); - // void any related gl trans - void_gl_trans(ST_MANURECEIVE, $type_no, true); - commit_transaction(); } diff --git a/manufacturing/includes/db/work_orders_db.inc b/manufacturing/includes/db/work_orders_db.inc index e39d334c..8cee8bd9 100644 --- a/manufacturing/includes/db/work_orders_db.inc +++ b/manufacturing/includes/db/work_orders_db.inc @@ -11,7 +11,8 @@ ***********************************************************************/ //-------------------------------------------------------------------------------------- -function add_material_cost($stock_id, $qty, $date_) +//Chaitanya : Added $advanced Parameter for Advanced Manufacturing +function add_material_cost($stock_id, $qty, $date_, $advanced=false) { $m_cost = 0; $result = get_bom($stock_id); @@ -20,26 +21,46 @@ function add_material_cost($stock_id, $qty, $date_) $standard_cost = get_standard_cost($bom_item['component']); $m_cost += ($bom_item['quantity'] * $standard_cost); } + $bom_cost = $m_cost; + //$dec = user_price_dec(); //price_decimal_format($m_cost, $dec); - $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = " + $sql = "SELECT material_cost, labour_cost, overhead_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']; + + //Chaitanya : Upating material cost without considering labour, overhead costs results in invalid costing + if ($advanced) + { + //reduce overhead_cost and labour_cost from price as those will remain as is + $m_cost = $m_cost - $myrow['labour_cost'] - $myrow['overhead_cost']; + } + //$qoh = get_qoh_on_date($stock_id, null, $date_); $qoh = get_qoh_on_date($stock_id); + $cost_adjust = false; if ($qoh < 0) + { + if ($qoh + $qty >= 0) + $cost_adjust = true; $qoh = 0; + } if ($qoh + $qty != 0) $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); + //$material_cost = round2($material_cost, $dec); + + if ($advanced && $cost_adjust) // new 2010-02-10 + adjust_deliveries($stock_id, $bom_cost, $date_); + + $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)." + WHERE stock_id=".db_escape($stock_id); db_query($sql,"The cost details for the inventory item could not be updated"); } -function add_overhead_cost($stock_id, $qty, $date_, $costs) +//Chaitanya : Added Adjustement Only Parameter +function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false) { //$dec = user_price_dec(); //price_decimal_format($costs, $dec); @@ -54,15 +75,50 @@ function add_overhead_cost($stock_id, $qty, $date_, $costs) $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); + if ($adj_only) + { + if ($qty != 0) + $costs = $qty * $costs; + if ($qoh>0) + $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh; + else //Chaitanya : Pass JV if qoh is 0/negative + { + global $Refs; + + $id = get_next_trans_no(ST_JOURNAL); + $ref = $Refs->get_next(ST_JOURNAL); + + $stock_gl_code = get_stock_gl_code($stock_id); + $memo = "WO Overhead cost settlement JV for zero/negative respository of ".$stock_id; + //Reverse the inventory effect if $qoh <=0 + 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, + -$costs); + //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, + $costs); + + add_audit_trail(ST_JOURNAL, $id, $date_); + add_comments(ST_JOURNAL, $id, $date_, $memo); + $Refs->save(ST_JOURNAL, $id, $ref); + } + } + else + { + if ($qoh + $qty != 0) + $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"); } -function add_labour_cost($stock_id, $qty, $date_, $costs) +//Chaitanya : Added Adjustement Only Parameter +function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false) { //$dec = user_price_dec(); //price_decimal_format($costs, $dec); @@ -77,15 +133,50 @@ function add_labour_cost($stock_id, $qty, $date_, $costs) $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); + if ($adj_only) + { + if ($qty != 0) + $costs = $qty * $costs; + if ($qoh>0) + $labour_cost = ($qoh * $labour_cost + $costs) / $qoh; + else //Chaitanya : Pass JV if qoh is 0/negative + { + global $Refs; + + $id = get_next_trans_no(ST_JOURNAL); + $ref = $Refs->get_next(ST_JOURNAL); + + $stock_gl_code = get_stock_gl_code($stock_id); + $memo = "WO labour cost settlement JV for zero/negative respository of ".$stock_id; + //Reverse the inventory effect if $qoh <=0 + 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, + -$costs); + //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, + $costs); + + add_audit_trail(ST_JOURNAL, $id, $date_); + add_comments(ST_JOURNAL, $id, $date_, $memo); + $Refs->save(ST_JOURNAL, $id, $ref); + } + } + else + { + if ($qoh + $qty != 0) + $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"); } -function add_issue_cost($stock_id, $qty, $date_, $costs) +//Chaitanya : Added Adjustement Only Parameter +function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false) { if ($qty != 0) $costs /= $qty; @@ -100,8 +191,42 @@ function add_issue_cost($stock_id, $qty, $date_, $costs) $qoh = get_qoh_on_date($stock_id); if ($qoh < 0) $qoh = 0; - if ($qoh + $qty != 0) - $material_cost = ($qty * $costs) / ($qoh + $qty); + if ($adj_only) + { + if ($qty != 0) + $costs = $qty * $costs; + if ($qoh>0) + $material_cost = $costs / $qoh; + else //Chaitanya : Pass JV if qoh is 0/negative + { + global $Refs; + + $id = get_next_trans_no(ST_JOURNAL); + $ref = $Refs->get_next(ST_JOURNAL); + + $stock_gl_code = get_stock_gl_code($stock_id); + $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id; + //Reverse the inventory effect if $qoh <=0 + 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, + -$costs); + //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, + $costs); + + add_audit_trail(ST_JOURNAL, $id, $date_); + add_comments(ST_JOURNAL, $id, $date_, $memo); + $Refs->save(ST_JOURNAL, $id, $ref); + } + } + else + { + if ($qoh + $qty != 0) + $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) @@ -124,7 +249,8 @@ function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id, $args->woid = 0; hook_db_prewrite($args, ST_WORKORDER); - add_material_cost($stock_id, $units_reqd, $date_); + //Chaitanya : Material cost should be added at time of production as per BOM at the time. + //add_material_cost($stock_id, $units_reqd, $date_); $date = date2sql($date_); $required = date2sql($required_by); @@ -161,8 +287,9 @@ function update_work_order($woid, $loc_code, $units_reqd, $stock_id, 'date_', 'required_by', 'memo_'), $args); hook_db_prewrite($args, ST_WORKORDER); - add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_); - add_material_cost($stock_id, $units_reqd, $date_); + //Chaitanya: Material Cost to be calculated at production + //add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_); + //add_material_cost($stock_id, $units_reqd, $date_); $date = date2sql($date_); $required = date2sql($required_by); @@ -187,7 +314,8 @@ function delete_work_order($woid) begin_transaction(); hook_db_prevoid(ST_WORKORDER, $woid); - add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']); + //Chaitanya : Cost calculation are affected only at time of production + //add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']); // delete the work order requirements delete_wo_requirements($woid); @@ -317,7 +445,9 @@ function void_work_order($woid) $work_order = get_work_order($woid); if (!($work_order["type"] == WO_ADVANCED)) { - $date = sql2date($work_order['date_']); + //Chaitanya : Removed WO costing from here. Handled in void_stock_move + //Reason - if BOM is changed since WO was executed then add_material_cost will result in incorrect costing as it is based on current BOM for material cost. + /*$date = sql2date($work_order['date_']); $qty = $work_order['units_reqd']; add_material_cost($work_order['stock_id'], -$qty, $date); // remove avg. cost for qty $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost @@ -325,7 +455,7 @@ function void_work_order($woid) add_labour_cost($work_order['stock_id'], -$qty, $date, $cost); $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost if ($cost != 0) - add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost); + add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);*/ $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = " .db_escape($woid); @@ -344,24 +474,52 @@ function void_work_order($woid) { // void everything inside the work order : issues, productions, payments $date = sql2date($work_order['date_']); - add_material_cost($work_order['stock_id'], -$work_order['units_reqd'], $date); // remove avg. cost for qty + + //Chaitanya : Removed WO costing from here. Handled in void_stock_move + //Reason - if BOM is changed since WO was executed then add_material_cost will result in incorrect costing as it is based on current BOM for material cost. + //add_material_cost($work_order['stock_id'], -$work_order['units_reqd'], $date); // remove avg. cost for qty + $result = get_work_order_productions($woid); // check the produced quantity $qty = 0; while ($row = db_fetch($result)) { - $qty += $row['quantity']; + //Chaitanya : Use native function for voiding + void_work_order_produce($row['id']); + + //Post voided entry if not prevoided explicitly + $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']); + if ($void_entry) + continue; + $memo_ = _("Voiding Work Order Trans # ").$woid; + add_audit_trail(ST_MANURECEIVE, $row['id'], today(), _("Voided.")."\n".$memo_); + add_voided_entry(ST_MANURECEIVE, $row['id'], today(), $memo_); + + /*$qty += $row['quantity']; // clear the production record - $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$$row['id']; + $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$row['id']; db_query($sql, "Cannot void a wo production"); - void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves; + void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves; */ } - $result = get_additional_issues($woid); // check the issued quantities + //Chaitanya : Get all work order issues + //$result = get_additional_issues($woid); // check the issued quantities + $result = get_work_order_issues($woid); $cost = 0; $issue_no = 0; while ($row = db_fetch($result)) { - $std_cost = get_standard_cost($row['stock_id']); + //Chaitanya : Use native function for voiding + void_work_order_issue($row['issue_no']); + + //Post voided entry if not prevoided explicitly + $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']); + if ($void_entry) + continue; + $memo_ = _("Voiding Work Order Trans # ").$woid; + add_audit_trail(ST_MANUISSUE, $row['issue_no'], today(), _("Voided.")."\n".$memo_); + add_voided_entry(ST_MANUISSUE, $row['issue_no'], today(), $memo_); + + /*$std_cost = get_standard_cost($row['stock_id']); $icost = $std_cost * $row['qty_issued']; $cost += $icost; if ($issue_no == 0) @@ -369,19 +527,24 @@ function void_work_order($woid) // void the actual issue items and their quantities $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0 WHERE issue_id=" .db_escape($row['id']); - db_query($sql,"A work order issue item could not be voided"); - } - if ($issue_no != 0) - void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves - if ($cost != 0) - add_issue_cost($work_order['stock_id'], -$qty, $date, $cost); - + db_query($sql,"A work order issue item could not be voided");*/ + } + + //Chaitaya : Voiding each issue handles the + //if ($issue_no != 0) + //void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves + //if ($cost != 0) + //add_issue_cost($work_order['stock_id'], -$qty, $date, $cost); + + //Chaitanya : Reverse the cost effects on $work_order['stock_id'] as adjustement $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost if ($cost != 0) - add_labour_cost($work_order['stock_id'], -$qty, $date, $cost); + //add_labour_cost($work_order['stock_id'], -$qty, $date, $cost); + add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true); $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost if ($cost != 0) - add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost); + //add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost); + add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true); $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = " .db_escape($woid); diff --git a/manufacturing/includes/db/work_orders_quick_db.inc b/manufacturing/includes/db/work_orders_quick_db.inc index 1534e277..31fbadb8 100644 --- a/manufacturing/includes/db/work_orders_quick_db.inc +++ b/manufacturing/includes/db/work_orders_quick_db.inc @@ -66,10 +66,21 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, db_query($sql, "The work order requirements could not be added"); // insert a -ve stock move for each item + // Chaitanya: Updated to record price and cost as std_cost for work order issued item + $UnitCost = get_standard_cost($bom_item["component"]); add_stock_move(ST_WORKORDER, $bom_item["component"], $woid, - $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0); + $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost); } - + + // ------------------------------------------------------------------------- + //Negative Stock Handling + $qoh = get_qoh_on_date($stock_id); + $cost_adjust = false; + if ($qoh < 0) + { + if ($qoh + $units_reqd > 0) + $cost_adjust = true; + } // ------------------------------------------------------------------------- @@ -82,6 +93,12 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc); // ------------------------------------------------------------------------- + + //Negative Stock Handling + if ($cost_adjust) + adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_); + + //-------------------------------------------------------------------------- add_comments(ST_WORKORDER, $woid, $date_, $memo_); @@ -103,9 +120,10 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced // credit all the components $total_cost = 0; + //Chaitanya : capture the exact i.e. non-rounded cost to avoid rounding error amplification + $total_material_cost = 0; while ($bom_item = db_fetch($result)) { - $bom_accounts = get_stock_gl_code($bom_item["component"]); $bom_cost = $bom_item["ComponentCost"] * $units_reqd; @@ -116,15 +134,26 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced $memo = $date_.": ".$memo; update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd); // insert a -ve stock move for each item + // Chaitanya: Updated to record price and cost as std_cost for work order issued item + $UnitCost = get_standard_cost($bom_item["component"]); add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced, - $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0); + $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost); } - $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0, + + //Chaitanya : Compatibility for Service Items + if (!is_service($bom_accounts["mb_flag"])) + $ivaccount = $bom_accounts["inventory_account"]; + else + $ivaccount = $bom_accounts["cogs_account"]; + $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0, $memo, -$bom_cost); - + $total_material_cost += $bom_cost; } if ($advanced) { + //Chaitanya: Material cost added at time of production as per BOM at the time and in adjustment mode. + add_material_cost($stock_id, $units_reqd, $date_, true); + $wo = get_work_order($woid); // also take the additional issues // moved to work_order_issues_db.inc @@ -144,10 +173,13 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced if ($issue_total != 0) add_issue_cost($stock_id, $units_reqd, $date_, $issue_total); */ + + // Logic moved to work_order_costs.php + /* $lcost = get_gl_wo_cost($woid, WO_LABOUR); add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']); $ocost = get_gl_wo_cost($woid, WO_OVERHEAD); - add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); + add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); */ } // credit additional costs $item_accounts = get_stock_gl_code($stock_id); @@ -190,6 +222,10 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced $memo = $date_.": ".$memo; add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"], 0, 0, $memo, -$total_cost); + + //Chaitanya : Update cost of the manufactured item in stock moves table + $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd; + update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); } //-------------------------------------------------------------------------------------- diff --git a/manufacturing/work_order_costs.php b/manufacturing/work_order_costs.php index 50e7f7fe..590b80c3 100644 --- a/manufacturing/work_order_costs.php +++ b/manufacturing/work_order_costs.php @@ -76,7 +76,7 @@ function can_process() display_error(_("The amount entered is not a valid number or less then zero.")); set_focus('costs'); return false; - } + } if (!is_date($_POST['date_'])) { @@ -119,6 +119,14 @@ if (isset($_POST['process']) && can_process() == true) add_gl_trans_std_cost(ST_WORKORDER, $_POST['selected_id'], $_POST['date_'], $_POST['db_acc'], $_POST['dim1'], $_POST['dim2'], $date.": ".$wo_cost_types[$_POST['PaymentType']], input_num('costs'), PT_WORKORDER, $_POST['PaymentType']); + + //Chaitanya : Apply the costs to manfuctured stock item as adjustement + $wo = get_work_order($_POST['selected_id']); + if ($_POST['PaymentType'] == 0) + add_labour_cost($wo['stock_id'], 0, $_POST['date_'], input_num('costs'), true); + else + add_overhead_cost($wo['stock_id'], 0, $_POST['date_'], input_num('costs'), true); + commit_transaction(); meta_forward($_SERVER['PHP_SELF'], "AddedID=".$_POST['selected_id']); diff --git a/purchasing/includes/db/grn_db.inc b/purchasing/includes/db/grn_db.inc index f76f6c75..1e9f71be 100644 --- a/purchasing/includes/db/grn_db.inc +++ b/purchasing/includes/db/grn_db.inc @@ -12,6 +12,10 @@ //------------------- 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 + if (is_inventory_item($stock_id)) + 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() @@ -36,10 +40,23 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, } else $price_in_home_currency = $price; - $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id); + + $price_in_home_currency_ = $price_in_home_currency; + + $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); $myrow = db_fetch($result); $material_cost = $myrow['material_cost']; + + //Chaitanya : For Manufactured Items + //IF inventory adjustment is made for manufactured item, the material cost averages and labour, overhead remains as is, but in inventory valuation total standard_cost matters resulting in invalid valuation. + if (!$adj_only && $myrow['mb_flag'] == 'M') //For manufactured items price is adjusted + { + $standard_cost = get_standard_cost($stock_id); + //reduce overhead_cost and labour_cost from price as those will remain as is + $price_in_home_currency = $price_in_home_currency - $myrow['labour_cost'] - $myrow['overhead_cost']; + } + //if ($price > -0.0001 && $price < 0.0001) commented out by Chaitanya // return $material_cost; if ($adj_only) @@ -49,7 +66,7 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, $cost_adjust = false; //$qoh = get_qoh_on_date($stock_id, null, $date, $exclude); $qoh = get_qoh_on_date($stock_id); - + if ($adj_only) { if ($qoh > 0) @@ -59,17 +76,18 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, { if ($qoh < 0) { - if ($qoh + $qty > 0) + if ($qoh + $qty >= 0) $cost_adjust = true; $qoh = 0; } - if ($qoh + $qty != 0) - $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) / ($qoh + $qty); + if ($qoh + $qty > 0) + $material_cost += ($qoh * $material_cost + $qty * $price_in_home_currency) / ($qoh + $qty); } //$material_cost = round2($material_cost, $dec); - if ($cost_adjust) // new 2010-02-10 - adjust_deliveries($stock_id, $material_cost, $date); + if ($cost_adjust) // new 2010-02-10 //Chaitanya : Material_cost replaced with price + adjust_deliveries($stock_id, $price_in_home_currency_, $date); $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)." WHERE stock_id=".db_escape($stock_id); db_query($sql,"The cost details for the inventory item could not be updated"); diff --git a/purchasing/includes/ui/po_ui.inc b/purchasing/includes/ui/po_ui.inc index d0aa40ac..ec5ad432 100644 --- a/purchasing/includes/ui/po_ui.inc +++ b/purchasing/includes/ui/po_ui.inc @@ -397,7 +397,9 @@ function po_item_controls(&$order, &$rowcounter, $line_no=-1) { // hidden('line_no', ($_SESSION['PO']->lines_on_order + 1)); - stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true); + //Chaitanya : Manufcatured item can be purchased + stock_items_list_cells(null, 'stock_id', null, false, true, true); + //stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true); if (list_updated('stock_id')) { $Ajax->activate('price'); $Ajax->activate('units'); -- 2.30.2