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_material_cost($stock_id, $qty, $date_)
17 $result = get_bom($stock_id);
18 while ($bom_item = db_fetch($result))
20 $standard_cost = get_standard_cost($bom_item['component']);
21 $m_cost += ($bom_item['quantity'] * $standard_cost);
23 $dec = user_price_dec();
24 price_decimal_format($m_cost, $dec);
25 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
26 .db_escape($stock_id);
27 $result = db_query($sql);
28 $myrow = db_fetch($result);
29 $material_cost = $myrow['material_cost'];
30 //$qoh = get_qoh_on_date($stock_id, null, $date_);
31 $qoh = get_qoh_on_date($stock_id);
35 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
36 //$material_cost = round2($material_cost, $dec);
37 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
38 WHERE stock_id=".db_escape($stock_id);
39 db_query($sql,"The cost details for the inventory item could not be updated");
42 function add_overhead_cost($stock_id, $qty, $date_, $costs)
44 $dec = user_price_dec();
45 price_decimal_format($costs, $dec);
48 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
49 .db_escape($stock_id);
50 $result = db_query($sql);
51 $myrow = db_fetch($result);
52 $overhead_cost = $myrow['overhead_cost'];
53 //$qoh = get_qoh_on_date($stock_id, null, $date_);
54 $qoh = get_qoh_on_date($stock_id);
58 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
59 //$overhead_cost = round2($overhead_cost, $dec);
60 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
61 WHERE stock_id=".db_escape($stock_id);
62 db_query($sql,"The cost details for the inventory item could not be updated");
65 function add_labour_cost($stock_id, $qty, $date_, $costs)
67 $dec = user_price_dec();
68 price_decimal_format($costs, $dec);
71 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
72 .db_escape($stock_id);
73 $result = db_query($sql);
74 $myrow = db_fetch($result);
75 $labour_cost = $myrow['labour_cost'];
76 //$qoh = get_qoh_on_date($stock_id, null, $date_);
77 $qoh = get_qoh_on_date($stock_id);
81 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
82 //$labour_cost = round2($labour_cost, $dec);
83 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
84 WHERE stock_id=".db_escape($stock_id);
85 db_query($sql,"The cost details for the inventory item could not be updated");
88 function add_issue_cost($stock_id, $qty, $date_, $costs)
92 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
93 .db_escape($stock_id);
94 $result = db_query($sql);
95 $myrow = db_fetch($result);
96 $material_cost = $myrow['material_cost'];
97 $dec = user_price_dec();
98 price_decimal_format($material_cost, $dec);
99 //$qoh = get_qoh_on_date($stock_id, null, $date_);
100 $qoh = get_qoh_on_date($stock_id);
103 if ($qoh + $qty != 0)
104 $material_cost = ($qty * $costs) / ($qoh + $qty);
105 //$material_cost = round2($material_cost, $dec);
106 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
107 .db_escape($material_cost)
108 ." WHERE stock_id=".db_escape($stock_id);
109 db_query($sql,"The cost details for the inventory item could not be updated");
112 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
113 $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
117 if (!($type == WO_ADVANCED))
118 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
122 add_material_cost($stock_id, $units_reqd, $date_);
124 $date = date2sql($date_);
125 $required = date2sql($required_by);
127 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
128 type, date_, required_by)
129 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
130 .db_escape($units_reqd).", ".db_escape($stock_id).",
131 ".db_escape($type).", '$date', ".db_escape($required).")";
132 db_query($sql, "could not add work order");
134 $woid = db_insert_id();
136 add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
138 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
139 add_audit_trail(ST_WORKORDER, $woid, $date_);
141 commit_transaction();
146 //--------------------------------------------------------------------------------------
148 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
149 $date_, $required_by, $memo_)
153 add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
154 add_material_cost($stock_id, $units_reqd, $date_);
156 $date = date2sql($date_);
157 $required = date2sql($required_by);
159 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
160 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
161 required_by=".db_escape($required).",
163 WHERE id = ".db_escape($woid);
165 db_query($sql, "could not update work order");
167 update_comments(ST_WORKORDER, $woid, null, $memo_);
168 add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
170 commit_transaction();
173 function delete_work_order($woid)
177 add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
179 // delete the work order requirements
180 delete_wo_requirements($woid);
182 // delete the actual work order
183 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
184 db_query($sql,"The work order could not be deleted");
186 delete_comments(ST_WORKORDER, $woid);
187 add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
189 commit_transaction();
192 //--------------------------------------------------------------------------------------
194 function get_work_order($woid, $allow_null=false)
196 $sql = "SELECT wo.*,st.description As StockItemName,l.location_name,
197 l.delivery_address,l.email, l.contact
198 FROM ".TB_PREF."workorders wo, ".TB_PREF."stock_master st, ".TB_PREF."locations l
199 WHERE st.stock_id=wo.stock_id
200 AND l.loc_code=wo.loc_code
201 AND wo.id=".db_escape($woid)."
204 $result = db_query($sql, "The work order issues could not be retrieved");
206 if (!$allow_null && db_num_rows($result) == 0)
207 display_db_error("Could not find work order $woid", $sql);
209 return db_fetch($result);
212 //--------------------------------------------------------------------------------------
214 function work_order_has_productions($woid)
216 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
217 $result = db_query($sql, "query work order for productions");
219 $myrow = db_fetch_row($result);
220 return ($myrow[0] > 0);
224 //--------------------------------------------------------------------------------------
226 function work_order_has_issues($woid)
228 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
229 $result = db_query($sql, "query work order for issues");
231 $myrow = db_fetch_row($result);
232 return ($myrow[0] > 0);
235 //--------------------------------------------------------------------------------------
237 function work_order_has_payments($woid)
239 $result = get_gl_wo_cost_trans($woid);
241 return (db_num_rows($result) != 0);
244 //--------------------------------------------------------------------------------------
246 function release_work_order($woid, $releaseDate, $memo_)
250 $myrow = get_work_order($woid);
251 $stock_id = $myrow["stock_id"];
253 $date = date2sql($releaseDate);
255 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
256 released=1 WHERE id = ".db_escape($woid);
257 db_query($sql, "could not release work order");
259 // create Work Order Requirements based on the bom
260 create_wo_requirements($woid, $stock_id);
262 add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
263 add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
265 commit_transaction();
268 //--------------------------------------------------------------------------------------
270 function close_work_order($woid)
272 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
273 db_query($sql, "could not close work order");
276 //--------------------------------------------------------------------------------------
278 function work_order_is_closed($woid)
280 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
281 $result = db_query($sql, "could not query work order");
282 $row = db_fetch_row($result);
283 return ($row[0] > 0);
286 //--------------------------------------------------------------------------------------
288 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
290 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
291 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
292 WHERE id = ".db_escape($woid);
294 db_query($sql, "The work order issued quantity couldn't be updated");
297 //--------------------------------------------------------------------------------------
299 function void_work_order($woid)
303 $work_order = get_work_order($woid);
304 if (!($work_order["type"] == WO_ADVANCED))
306 $date = sql2date($work_order['date_']);
307 $qty = $work_order['units_reqd'];
308 add_material_cost($work_order['stock_id'], -$qty, $date); // remove avg. cost for qty
309 $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
311 add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
312 $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
314 add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
316 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
318 db_query($sql, "The work order couldn't be voided");
320 // void all related stock moves
321 void_stock_move(ST_WORKORDER, $woid);
323 // void any related gl trans
324 void_gl_trans(ST_WORKORDER, $woid, true);
326 // clear the requirements units received
327 void_wo_requirements($woid);
331 // void everything inside the work order : issues, productions, payments
332 $date = sql2date($work_order['date_']);
333 add_material_cost($work_order['stock_id'], -$work_order['units_reqd'], $date); // remove avg. cost for qty
334 $result = get_work_order_productions($woid); // check the produced quantity
336 while ($row = db_fetch($result))
338 $qty += $row['quantity'];
339 // clear the production record
340 $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$$row['id'];
341 db_query($sql, "Cannot void a wo production");
343 void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves;
345 $result = get_additional_issues($woid); // check the issued quantities
348 while ($row = db_fetch($result))
350 $std_cost = get_standard_cost($row['stock_id']);
351 $icost = $std_cost * $row['qty_issued'];
354 $issue_no = $row['issue_no'];
355 // void the actual issue items and their quantities
356 $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0 WHERE issue_id="
357 .db_escape($row['id']);
358 db_query($sql,"A work order issue item could not be voided");
361 void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves
363 add_issue_cost($work_order['stock_id'], -$qty, $date, $cost);
365 $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
367 add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
368 $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
370 add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
372 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
374 db_query($sql, "The work order couldn't be voided");
376 // void all related stock moves
377 void_stock_move(ST_WORKORDER, $woid);
379 // void any related gl trans
380 void_gl_trans(ST_WORKORDER, $woid, true);
382 // clear the requirements units received
383 void_wo_requirements($woid);
385 commit_transaction();
388 function get_sql_for_work_orders($outstanding_only, $all_items)
394 location.location_name,
396 workorder.units_reqd,
397 workorder.units_issued,
399 workorder.required_by,
400 workorder.released_date,
405 FROM ".TB_PREF."workorders as workorder,"
406 .TB_PREF."stock_master as item,"
407 .TB_PREF."item_units as unit,"
408 .TB_PREF."locations as location
409 WHERE workorder.stock_id=item.stock_id
410 AND workorder.loc_code=location.loc_code
411 AND item.units=unit.abbr";
413 if (check_value('OpenOnly') || $outstanding_only != 0)
415 $sql .= " AND workorder.closed=0";
418 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
420 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
423 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
425 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
428 if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
430 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
433 if (check_value('OverdueOnly'))
435 $Today = date2sql(Today());
437 $sql .= " AND workorder.required_by < '$Today' ";
442 function get_sql_for_where_used()
446 workcentre.name As WorkCentreName,
447 location.location_name,
450 FROM ".TB_PREF."bom as bom, "
451 .TB_PREF."stock_master as parent, "
452 .TB_PREF."workcentres as workcentre, "
453 .TB_PREF."locations as location
454 WHERE bom.parent = parent.stock_id
455 AND bom.workcentre_added = workcentre.id
456 AND bom.loc_code = location.loc_code
457 AND bom.component=".db_escape($_POST['stock_id']);
460 //--------------------------------------------------------------------------------------
461 function get_gl_wo_cost($woid, $cost_type)
464 $result = get_gl_wo_cost_trans($woid, $cost_type);
465 while ($row = db_fetch($result))
466 $cost += -$row['amount'];