2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //--------------------------------------------------------------------------------------
14 function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
15 $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
20 $args = func_get_args();
21 $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
22 'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
24 hook_db_prewrite($args, ST_WORKORDER);
26 // if unassembling, reverse the stock movements
27 if ($type == WO_UNASSEMBLY)
28 $units_reqd = -$units_reqd;
30 add_material_cost($stock_id, $units_reqd, $date_);
32 $date = date2sql($date_);
33 if (!isset($costs) || ($costs == ""))
35 add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
36 if (!isset($labour) || ($labour == ""))
38 add_labour_cost($stock_id, $units_reqd, $date_, $labour);
40 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
41 type, additional_costs, date_, released_date, required_by, released, closed)
42 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd)
43 .", ".db_escape($units_reqd).", ".db_escape($stock_id).",
44 ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)";
45 db_query($sql, "could not add work order");
47 $woid = db_insert_id();
49 //--------------------------------------------------------------------------
51 // create Work Order Requirements based on the bom
52 $result = get_bom($stock_id);
54 while ($bom_item = db_fetch($result))
57 $unit_quantity = $bom_item["quantity"];
58 $item_quantity = $bom_item["quantity"] * $units_reqd;
61 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
62 VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
63 '". $bom_item["workcentre_added"] . "',
64 $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
66 db_query($sql, "The work order requirements could not be added");
68 // insert a -ve stock move for each item
69 // Chaitanya: Updated to record price and cost as std_cost for work order issued item
70 $UnitCost = get_standard_cost($bom_item["component"]);
71 add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
72 $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost);
75 // -------------------------------------------------------------------------
76 //Negative Stock Handling
77 $qoh = get_qoh_on_date($stock_id);
81 if ($qoh + $units_reqd >= 0)
85 // -------------------------------------------------------------------------
87 //Negative Stock Handling First; Prior to add_stock_move
89 adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
91 // -------------------------------------------------------------------------
93 // insert a +ve stock move for the item being manufactured
94 add_stock_move(ST_WORKORDER, $stock_id, $woid, $loc_code, $date_,
95 $wo_ref, $units_reqd, 0);
97 // -------------------------------------------------------------------------
99 work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
101 //--------------------------------------------------------------------------
103 add_comments(ST_WORKORDER, $woid, $date_, $memo_);
105 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
106 add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
109 hook_db_postwrite($args, ST_WORKORDER);
110 commit_transaction();
114 //--------------------------------------------------------------------------------------
116 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
118 global $wo_cost_types;
119 $result = get_bom($stock_id);
121 // credit all the components
123 //Chaitanya : capture the exact i.e. non-rounded cost to avoid rounding error amplification
124 $total_material_cost = 0;
125 while ($bom_item = db_fetch($result))
127 $bom_accounts = get_stock_gl_code($bom_item["component"]);
129 $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
131 $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
134 $memo = $date_.": ".$memo;
135 update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd);
136 // insert a -ve stock move for each item
137 // Chaitanya: Updated to record price and cost as std_cost for work order issued item
138 $UnitCost = get_standard_cost($bom_item["component"]);
139 add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
140 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost);
143 //Chaitanya : Compatibility for Service Items
144 if (!is_service($bom_accounts["mb_flag"]))
145 $ivaccount = $bom_accounts["inventory_account"];
147 $ivaccount = $bom_accounts["cogs_account"];
148 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
150 $total_material_cost += $bom_cost;
154 //Chaitanya: Material cost added at time of production as per BOM at the time and in adjustment mode.
155 add_material_cost($stock_id, $units_reqd, $date_, true);
157 $wo = get_work_order($woid);
158 // also take the additional issues
159 // moved to work_order_issues_db.inc
161 $res = get_additional_issues($woid);
163 while ($item = db_fetch($res))
165 $standard_cost = get_standard_cost($item['stock_id']);
166 $issue_cost = $standard_cost * $item['qty_issued'] * $units_reqd / $wo['units_reqd'];
167 $issue = get_stock_gl_code($item['stock_id']);
168 $stockitem = get_item($item['stock_id']);
169 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
170 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
171 $issue_total += $issue_cost;
173 if ($issue_total != 0)
174 add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
177 // Logic moved to work_order_costs.php
179 $lcost = get_gl_wo_cost($woid, WO_LABOUR);
180 add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
181 $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
182 add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); */
184 // credit additional costs
185 $item_accounts = get_stock_gl_code($stock_id);
188 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_acc,
189 0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, PT_WORKORDER, WO_OVERHEAD);
190 $is_bank_to = is_bank_account($cr_acc);
193 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
194 $date_, -$costs, PT_WORKORDER, WO_OVERHEAD, get_company_currency(),
195 "Cannot insert a destination bank transaction");
198 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
199 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs,
200 PT_WORKORDER, WO_OVERHEAD);
204 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_lab_acc,
205 0, 0, $wo_cost_types[WO_LABOUR], -$labour, PT_WORKORDER, WO_LABOUR);
206 $is_bank_to = is_bank_account($cr_lab_acc);
209 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
210 $date_, -$labour, PT_WORKORDER, WO_LABOUR, get_company_currency(),
211 "Cannot insert a destination bank transaction");
214 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
215 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour,
216 PT_WORKORDER, WO_LABOUR);
218 // debit total components $total_cost
219 $stockitem = get_item($stock_id);
220 $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
222 $memo = $date_.": ".$memo;
223 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
224 0, 0, $memo, -$total_cost);
226 //Chaitanya : Update cost of the manufactured item in stock moves table
227 $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd;
228 update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost);
231 //--------------------------------------------------------------------------------------