X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=manufacturing%2Fincludes%2Fdb%2Fwork_orders_quick_db.inc;h=3c6e9a86d3a1ff1b1f418a0e596aa15ea6db69d4;hb=8a7017fb79e97afda2cd20e9ab763f57fde97df2;hp=1eccbdcfc05605b9ce491cf3054460d4dcec7a4f;hpb=f7c8004b367843301c98f49db953907e20f5fe39;p=fa-stable.git diff --git a/manufacturing/includes/db/work_orders_quick_db.inc b/manufacturing/includes/db/work_orders_quick_db.inc index 1eccbdcf..3c6e9a86 100644 --- a/manufacturing/includes/db/work_orders_quick_db.inc +++ b/manufacturing/includes/db/work_orders_quick_db.inc @@ -1,31 +1,62 @@ . +***********************************************************************/ //-------------------------------------------------------------------------------------- -function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_) +function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, + $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc) { + global $loc_notification, $path_to_root, $Refs; + begin_transaction(); + $args = func_get_args(); + $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id', + 'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args); + $args->woid = 0; + hook_db_prewrite($args, ST_WORKORDER); // if unassembling, reverse the stock movements - if ($type == wo_types::unassemble()) + if ($type == WO_UNASSEMBLY) $units_reqd = -$units_reqd; add_material_cost($stock_id, $units_reqd, $date_); $date = date2sql($date_); - if (!isset($additional_costs) || ($additional_costs == "")) - $additional_costs = 0; - + if (!isset($costs) || ($costs == "")) + $costs = 0; + add_overhead_cost($stock_id, $units_reqd, $date_, $costs); + if (!isset($labour) || ($labour == "")) + $labour = 0; + add_labour_cost($stock_id, $units_reqd, $date_, $labour); + $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id, type, additional_costs, date_, released_date, required_by, released, closed) - VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id', - $type, $additional_costs, '$date', '$date', '$date', 1, 1)"; + VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd) + .", ".db_escape($units_reqd).", ".db_escape($stock_id).", + ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)"; db_query($sql, "could not add work order"); $woid = db_insert_id(); //-------------------------------------------------------------------------- + if ($loc_notification == 1) + { + include_once($path_to_root . "/sales/includes/cart_class.inc"); + include_once($path_to_root . "/inventory/includes/inventory_db.inc"); + $st_ids = array(); + $st_names = array(); + $st_num = array(); + $st_reorder = array(); + } // create Work Order Requirements based on the bom $result = get_bom($stock_id); @@ -35,7 +66,12 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $unit_quantity = $bom_item["quantity"]; $item_quantity = $bom_item["quantity"] * $units_reqd; - + if ($loc_notification == 1 && is_inventory_item($bom_item["component"])) + { + + $line = new line_details($bom_item['component'], $item_quantity, 0, 0, 0, 0, $bom_item['description']); + $loc = calculate_reorder_level($bom_item["loc_code"], $line, $st_ids, $st_names, $st_num, $st_reorder); + } $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code) VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ", '". $bom_item["workcentre_added"] . "', @@ -44,80 +80,191 @@ 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 - add_stock_move(systypes::work_order(), $bom_item["component"], $woid, - $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0); + // 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, $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; } - // ------------------------------------------------------------------------- + + //Negative Stock Handling First; Prior to add_stock_move + if ($cost_adjust) + adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_); + + // ------------------------------------------------------------------------- // insert a +ve stock move for the item being manufactured - add_stock_move(systypes::work_order(), $stock_id, $woid, $loc_code, $date_, + add_stock_move(ST_WORKORDER, $stock_id, $woid, $loc_code, $date_, $wo_ref, $units_reqd, 0); // ------------------------------------------------------------------------- - work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs); - - // ------------------------------------------------------------------------- + work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc); + + //-------------------------------------------------------------------------- - add_comments(systypes::work_order(), $woid, $date_, $memo_); + add_comments(ST_WORKORDER, $woid, $date_, $memo_); - references::save_last($wo_ref, systypes::work_order()); + $Refs->save(ST_WORKORDER, $woid, $wo_ref); + add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production.")); + $args->woid = $woid; + hook_db_postwrite($args, ST_WORKORDER); commit_transaction(); + if ($loc_notification == 1 && count($st_ids) > 0) + send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder); + return $woid; } //-------------------------------------------------------------------------------------- -function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs, $advanced=false) +function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="") { + global $loc_notification, $path_to_root, $wo_cost_types; $result = get_bom($stock_id); + if ($loc_notification == 1 && $advanced) + { + include_once($path_to_root . "/sales/includes/cart_class.inc"); + include_once($path_to_root . "/inventory/includes/inventory_db.inc"); + $st_ids = array(); + $st_names = array(); + $st_num = array(); + $st_reorder = array(); + } + // 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["standard_cost"] * $bom_item["quantity"] * $units_reqd; - + $bom_cost = $bom_item["ComponentCost"] * $units_reqd; + + $memo = $bom_item["quantity"] ." * ".$bom_item["description"]; if ($advanced) { + if ($loc_notification == 1 && is_inventory_item($bom_item['component'])) + { + $line = new line_details($bom_item['component'], $bom_item["quantity"] * $units_reqd, 0, 0, 0, 0, $bom_item['description']); + $loc = calculate_reorder_level($bom_item["loc_code"], $line, $st_ids, $st_names, $st_num, $st_reorder); + } + $memo = $date_.": ".$memo; + update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd); // insert a -ve stock move for each item - add_stock_move(systypes::work_order(), $bom_item["component"], $woid, - $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0); + // 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, $UnitCost, 0, 1, $UnitCost); } - add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0, - null, -$bom_cost); - - $total_cost += $bom_cost; + + //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, $woid); + + // $wo = get_work_order($woid); // also take the additional issues + // moved to work_order_issues_db.inc + /* $res = get_additional_issues($woid); + $issue_total = 0; while ($item = db_fetch($res)) { $standard_cost = get_standard_cost($item['stock_id']); - $issue_cost = $standard_cost * $item['qty_issued']; + $issue_cost = $standard_cost * $item['qty_issued'] * $units_reqd / $wo['units_reqd']; $issue = get_stock_gl_code($item['stock_id']); - add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $issue["inventory_account"], 0, 0, - null, -$issue_cost); - $total_cost += $issue_cost; + $stockitem = get_item($item['stock_id']); + $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0, + $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost); + $issue_total += $issue_cost; } + 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']); */ } // credit additional costs $item_accounts = get_stock_gl_code($stock_id); - add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"], - $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs); - - // debit total components + additional - $total_cost += $additional_costs; - add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"], - 0, 0, null, $total_cost); + if ($costs != 0.0) + { + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_acc, + 0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, PT_WORKORDER, WO_OVERHEAD); + $is_bank_to = is_bank_account($cr_acc); + if ($is_bank_to) + { + add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "", + $date_, -$costs, PT_WORKORDER, WO_OVERHEAD, get_company_currency(), + "Cannot insert a destination bank transaction"); + } + + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"], + $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs, + PT_WORKORDER, WO_OVERHEAD); + } + if ($labour != 0.0) + { + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_lab_acc, + 0, 0, $wo_cost_types[WO_LABOUR], -$labour, PT_WORKORDER, WO_LABOUR); + $is_bank_to = is_bank_account($cr_lab_acc); + if ($is_bank_to) + { + add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "", + $date_, -$labour, PT_WORKORDER, WO_LABOUR, get_company_currency(), + "Cannot insert a destination bank transaction"); + } + + add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"], + $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour, + PT_WORKORDER, WO_LABOUR); + } + // debit total components $total_cost + $stockitem = get_item($stock_id); + $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"]; + if ($advanced) + { + if ($loc_notification == 1 && count($st_ids) > 0) + send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder); + $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 + if (!$advanced) + { + $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd; + update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); + } } //--------------------------------------------------------------------------------------