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_, $advanced=false, $woid=0)
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);
24 // new Joe Hunt 2015.10.15
26 if ($woid != 0 && work_order_has_issues($woid))
28 $res = get_additional_issues($woid);
29 while ($issue = db_fetch($res))
31 $standard_cost = get_standard_cost($issue['stock_id']);
32 $m_cost += ($issue['qty_issued'] * $standard_cost);
35 $sql = "SELECT material_cost, labour_cost, overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
36 .db_escape($stock_id);
37 $result = db_query($sql);
38 $myrow = db_fetch($result);
39 $material_cost = $myrow['material_cost'];
40 /* no, why will we do that?? Joe Hunt 2015.10.17
43 //reduce overhead_cost and labour_cost from price as those will remain as is
44 $m_cost = $m_cost - $myrow['labour_cost'] - $myrow['overhead_cost'];
47 $qoh = get_qoh_on_date($stock_id);
57 if ($qoh == 0) // 27.10.2014 apmuthu and dz.
58 $material_cost = $m_cost;
60 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
62 if ($advanced && $cost_adjust) // new 2010-02-10
63 adjust_deliveries($stock_id, $bom_cost, $date_);
65 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
66 WHERE stock_id=".db_escape($stock_id);
67 db_query($sql,"The cost details for the inventory item could not be updated");
70 function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
74 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
75 .db_escape($stock_id);
76 $result = db_query($sql);
77 $myrow = db_fetch($result);
78 $overhead_cost = $myrow['overhead_cost'];
79 $qoh = get_qoh_on_date($stock_id);
85 $costs = $qty * $costs;
87 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh;
88 else // Journal Entry if QOH is 0/negative
92 $id = get_next_trans_no(ST_JOURNAL);
93 $ref = $Refs->get_next(ST_JOURNAL);
95 $stock_gl_code = get_stock_gl_code($stock_id);
96 $memo = "WO Overhead cost settlement JV for zero/negative respository of ".$stock_id;
97 //Reverse the inventory effect if $qoh <=0
98 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
99 $stock_gl_code["inventory_account"],
100 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
102 //GL Posting to inventory adjustment account
103 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
104 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
105 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
108 add_audit_trail(ST_JOURNAL, $id, $date_);
109 add_comments(ST_JOURNAL, $id, $date_, $memo);
110 $Refs->save(ST_JOURNAL, $id, $ref);
111 if ($qty != 0) // 27.10.2014 dz
112 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qty;
117 if ($qoh + $qty != 0)
118 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
120 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=overhead_cost+".db_escape($overhead_cost)."
121 WHERE stock_id=".db_escape($stock_id);
122 db_query($sql,"The cost details for the inventory item could not be updated");
125 function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
129 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
130 .db_escape($stock_id);
131 $result = db_query($sql);
132 $myrow = db_fetch($result);
133 $labour_cost = $myrow['labour_cost'];
134 $qoh = get_qoh_on_date($stock_id);
140 $costs = $qty * $costs;
142 $labour_cost = ($qoh * $labour_cost + $costs) / $qoh;
143 else // Journal Entry if QOH is 0/negative
147 $id = get_next_trans_no(ST_JOURNAL);
148 $ref = $Refs->get_next(ST_JOURNAL);
150 $stock_gl_code = get_stock_gl_code($stock_id);
151 $memo = "WO labour cost settlement JV for zero/negative respository of ".$stock_id;
152 //Reverse the inventory effect if $qoh <=0
153 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
154 $stock_gl_code["inventory_account"],
155 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
157 //GL Posting to inventory adjustment account
158 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
159 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
160 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
163 add_audit_trail(ST_JOURNAL, $id, $date_);
164 add_comments(ST_JOURNAL, $id, $date_, $memo);
165 $Refs->save(ST_JOURNAL, $id, $ref);
166 if ($qty != 0) // 27.10.2014 dz
167 $labour_cost = ($qoh * $labour_cost + $costs) / $qty;
172 if ($qoh + $qty != 0)
173 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
175 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=labour_cost+".db_escape($labour_cost)."
176 WHERE stock_id=".db_escape($stock_id);
177 db_query($sql,"The cost details for the inventory item could not be updated");
180 function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
184 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
185 .db_escape($stock_id);
186 $result = db_query($sql);
187 $myrow = db_fetch($result);
188 $material_cost = $myrow['material_cost'];
189 $qoh = get_qoh_on_date($stock_id);
195 $costs = $qty * $costs;
197 $material_cost = $costs / $qoh;
198 else // Journal Entry if QOH is 0/negative
202 $id = get_next_trans_no(ST_JOURNAL);
203 $ref = $Refs->get_next(ST_JOURNAL);
205 $stock_gl_code = get_stock_gl_code($stock_id);
206 $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
207 //Reverse the inventory effect if $qoh <=0
208 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
209 $stock_gl_code["inventory_account"],
210 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
212 //GL Posting to inventory adjustment account
213 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
214 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
215 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
218 add_audit_trail(ST_JOURNAL, $id, $date_);
219 add_comments(ST_JOURNAL, $id, $date_, $memo);
220 $Refs->save(ST_JOURNAL, $id, $ref);
221 if ($qty != 0) // 27.10.2014 dz
222 $material_cost = $costs / $qty;
227 if ($qoh + $qty != 0)
228 $material_cost = ($qty * $costs) / ($qoh + $qty);
230 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
231 .db_escape($material_cost)
232 ." WHERE stock_id=".db_escape($stock_id);
233 db_query($sql,"The cost details for the inventory item could not be updated");
236 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
237 $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
241 if (!($type == WO_ADVANCED))
242 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
245 $args = func_get_args();
246 $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
247 'type', 'date_', 'required_by', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
249 hook_db_prewrite($args, ST_WORKORDER);
251 $date = date2sql($date_);
252 $required = date2sql($required_by);
254 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
255 type, date_, required_by)
256 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
257 .db_escape($units_reqd).", ".db_escape($stock_id).",
258 ".db_escape($type).", '$date', ".db_escape($required).")";
259 db_query($sql, "could not add work order");
261 $woid = db_insert_id();
263 add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
265 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
266 add_audit_trail(ST_WORKORDER, $woid, $date_);
269 hook_db_postwrite($args, ST_WORKORDER);
270 commit_transaction();
275 //--------------------------------------------------------------------------------------
277 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
278 $date_, $required_by, $memo_)
281 $args = func_get_args();
282 $args = (object)array_combine(array('woid', 'loc_code', 'units_reqd', 'stock_id',
283 'date_', 'required_by', 'memo_'), $args);
284 hook_db_prewrite($args, ST_WORKORDER);
286 $date = date2sql($date_);
287 $required = date2sql($required_by);
289 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
290 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
291 required_by=".db_escape($required).",
293 WHERE id = ".db_escape($woid);
295 db_query($sql, "could not update work order");
297 update_comments(ST_WORKORDER, $woid, null, $memo_);
298 add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
300 hook_db_postwrite($args, ST_WORKORDER);
301 commit_transaction();
304 function delete_work_order($woid)
307 hook_db_prevoid(ST_WORKORDER, $woid);
309 // delete the work order requirements
310 delete_wo_requirements($woid);
312 // delete the actual work order
313 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
314 db_query($sql,"The work order could not be deleted");
316 delete_comments(ST_WORKORDER, $woid);
317 add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
319 commit_transaction();
322 //--------------------------------------------------------------------------------------
324 function get_work_order($woid, $allow_null=false)
326 $sql = "SELECT wo.*,st.description As StockItemName,l.location_name,
327 l.delivery_address,l.email, l.contact
328 FROM ".TB_PREF."workorders wo, ".TB_PREF."stock_master st, ".TB_PREF."locations l
329 WHERE st.stock_id=wo.stock_id
330 AND l.loc_code=wo.loc_code
331 AND wo.id=".db_escape($woid)."
334 $result = db_query($sql, "The work order issues could not be retrieved");
336 if (!$allow_null && db_num_rows($result) == 0)
337 display_db_error("Could not find work order $woid", $sql);
339 return db_fetch($result);
342 //--------------------------------------------------------------------------------------
344 function work_order_has_productions($woid)
346 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
347 $result = db_query($sql, "query work order for productions");
349 $myrow = db_fetch_row($result);
350 return ($myrow[0] > 0);
354 //--------------------------------------------------------------------------------------
356 function work_order_has_issues($woid)
358 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
359 $result = db_query($sql, "query work order for issues");
361 $myrow = db_fetch_row($result);
362 return ($myrow[0] > 0);
365 //--------------------------------------------------------------------------------------
367 function work_order_has_payments($woid)
369 $result = get_gl_wo_cost_trans($woid);
371 return (db_num_rows($result) != 0);
374 //--------------------------------------------------------------------------------------
376 function release_work_order($woid, $releaseDate, $memo_)
380 $myrow = get_work_order($woid);
381 $stock_id = $myrow["stock_id"];
383 $date = date2sql($releaseDate);
385 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
386 released=1 WHERE id = ".db_escape($woid);
387 db_query($sql, "could not release work order");
389 // create Work Order Requirements based on the bom
390 create_wo_requirements($woid, $stock_id);
392 add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
393 add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
395 commit_transaction();
398 //--------------------------------------------------------------------------------------
400 function close_work_order($woid)
402 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
403 db_query($sql, "could not close work order");
406 //--------------------------------------------------------------------------------------
408 function work_order_is_closed($woid)
410 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
411 $result = db_query($sql, "could not query work order");
412 $row = db_fetch_row($result);
413 return ($row[0] > 0);
416 //--------------------------------------------------------------------------------------
418 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
420 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
421 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
422 WHERE id = ".db_escape($woid);
424 db_query($sql, "The work order issued quantity couldn't be updated");
427 //--------------------------------------------------------------------------------------
429 function void_work_order($woid)
432 hook_db_prevoid(ST_WORKORDER, $woid);
434 $work_order = get_work_order($woid);
435 if (!($work_order["type"] == WO_ADVANCED))
437 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
439 db_query($sql, "The work order couldn't be voided");
441 // void all related stock moves
442 void_stock_move(ST_WORKORDER, $woid);
444 // void any related bank/gl trans
445 void_bank_trans(ST_WORKORDER, $woid, true);
447 // clear the requirements units received
448 void_wo_requirements($woid);
452 // void everything inside the work order : issues, productions, payments
453 $date = sql2date($work_order['date_']);
455 $result = get_work_order_productions($woid); // check the produced quantity
456 while ($row = db_fetch($result))
458 void_work_order_produce($row['id']);
460 //Post voided entry if not prevoided explicitly
461 $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']);
464 $memo_ = _("Voiding Work Order Trans # ").$woid;
465 add_audit_trail(ST_MANURECEIVE, $row['id'], today(), _("Voided.")."\n".$memo_);
466 add_voided_entry(ST_MANURECEIVE, $row['id'], today(), $memo_);
469 $result = get_work_order_issues($woid);
471 while ($row = db_fetch($result))
473 void_work_order_issue($row['issue_no']);
475 //Post voided entry if not prevoided explicitly
476 $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']);
479 $memo_ = _("Voiding Work Order Trans # ").$woid;
480 add_audit_trail(ST_MANUISSUE, $row['issue_no'], today(), _("Voided.")."\n".$memo_);
481 add_voided_entry(ST_MANUISSUE, $row['issue_no'], today(), $memo_);
484 //Adust avg labour cost
485 $cost = get_gl_wo_cost($woid, WO_LABOUR);
487 add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true);
489 //Adust avg overhead cost
490 $cost = get_gl_wo_cost($woid, WO_OVERHEAD);
492 add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true);
494 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
496 db_query($sql, "The work order couldn't be voided");
498 // void all related stock moves
499 void_stock_move(ST_WORKORDER, $woid);
501 // void any related bank/gl trans
502 void_bank_trans(ST_WORKORDER, $woid, true);
504 // clear the requirements units received
505 void_wo_requirements($woid);
507 commit_transaction();
510 function get_sql_for_work_orders($outstanding_only, $all_items)
516 location.location_name,
518 workorder.units_reqd,
519 workorder.units_issued,
521 workorder.required_by,
522 workorder.released_date,
527 FROM ".TB_PREF."workorders as workorder,"
528 .TB_PREF."stock_master as item,"
529 .TB_PREF."item_units as unit,"
530 .TB_PREF."locations as location
531 WHERE workorder.stock_id=item.stock_id
532 AND workorder.loc_code=location.loc_code
533 AND item.units=unit.abbr";
535 if (check_value('OpenOnly') || $outstanding_only != 0)
537 $sql .= " AND workorder.closed=0";
540 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
542 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
545 if (isset($_POST['OrderId']) && $_POST['OrderId'] != "")
547 $sql .= " AND workorder.id LIKE ".db_escape('%'.$_POST['OrderId'].'%');
550 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
552 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
555 if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
557 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
560 if (check_value('OverdueOnly'))
562 $Today = date2sql(Today());
564 $sql .= " AND workorder.required_by < '$Today' ";
566 $sql .= " ORDER BY workorder.id DESC";
570 function get_sql_for_where_used()
574 workcentre.name As WorkCentreName,
575 location.location_name,
578 FROM ".TB_PREF."bom as bom, "
579 .TB_PREF."stock_master as parent, "
580 .TB_PREF."workcentres as workcentre, "
581 .TB_PREF."locations as location
582 WHERE bom.parent = parent.stock_id
583 AND bom.workcentre_added = workcentre.id
584 AND bom.loc_code = location.loc_code
585 AND bom.component=".db_escape($_POST['stock_id']);
588 //--------------------------------------------------------------------------------------
589 function get_gl_wo_cost($woid, $cost_type)
592 $result = get_gl_wo_cost_trans($woid, $cost_type);
593 while ($row = db_fetch($result))
594 $cost += -$row['amount'];