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);
27 if ($woid != 0 && work_order_has_issues($woid))
29 $res = get_additional_issues($woid);
30 while ($issue = db_fetch($res))
32 $standard_cost = get_standard_cost($issue['stock_id']);
33 $i_cost += ($issue['qty_issued'] * $standard_cost) / $qty;
36 $sql = "SELECT material_cost, labour_cost, overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
37 .db_escape($stock_id);
38 $result = db_query($sql);
39 $myrow = db_fetch($result);
40 $material_cost = $myrow['material_cost'] - $i_cost; // $i_cost was already added to material cost
42 /* no, why will we do that?? Joe Hunt 2015.10.17
45 //reduce overhead_cost and labour_cost from price as those will remain as is
46 $m_cost = $m_cost - $myrow['labour_cost'] - $myrow['overhead_cost'];
49 $qoh = get_qoh_on_date($stock_id);
59 if ($qoh == 0) // 27.10.2014 apmuthu and dz.
60 $material_cost = $m_cost;
62 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
64 if ($advanced && $cost_adjust) // new 2010-02-10
65 adjust_deliveries($stock_id, $bom_cost, $date_);
67 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
68 WHERE stock_id=".db_escape($stock_id);
69 db_query($sql,"The cost details for the inventory item could not be updated");
72 function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
76 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
77 .db_escape($stock_id);
78 $result = db_query($sql);
79 $myrow = db_fetch($result);
80 $overhead_cost = $myrow['overhead_cost'];
81 $qoh = get_qoh_on_date($stock_id);
89 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
91 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh;
93 else // Journal Entry if QOH is 0/negative
97 $id = get_next_trans_no(ST_JOURNAL);
98 $ref = $Refs->get_next(ST_JOURNAL);
100 $stock_gl_code = get_stock_gl_code($stock_id);
101 $memo = "WO Overhead cost settlement JV for zero/negative respository of ".$stock_id;
102 //Reverse the inventory effect if $qoh <=0
103 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
104 $stock_gl_code["inventory_account"],
105 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
107 //GL Posting to inventory adjustment account
108 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
109 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
110 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
113 add_audit_trail(ST_JOURNAL, $id, $date_);
114 add_comments(ST_JOURNAL, $id, $date_, $memo);
115 $Refs->save(ST_JOURNAL, $id, $ref);
116 if ($qty != 0) // 27.10.2014 dz
117 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qty;
122 if ($qoh + $qty != 0)
123 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
125 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
126 WHERE stock_id=".db_escape($stock_id);
127 db_query($sql,"The cost details for the inventory item could not be updated");
130 function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
134 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
135 .db_escape($stock_id);
136 $result = db_query($sql);
137 $myrow = db_fetch($result);
138 $labour_cost = $myrow['labour_cost'];
139 $qoh = get_qoh_on_date($stock_id);
146 if ($qoh + $qty != 0)
147 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
149 $labour_cost = ($qoh * $labour_cost + $costs) / $qoh;
151 else // Journal Entry if QOH is 0/negative
155 $id = get_next_trans_no(ST_JOURNAL);
156 $ref = $Refs->get_next(ST_JOURNAL);
158 $stock_gl_code = get_stock_gl_code($stock_id);
159 $memo = "WO labour cost settlement JV for zero/negative respository of ".$stock_id;
160 //Reverse the inventory effect if $qoh <=0
161 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
162 $stock_gl_code["inventory_account"],
163 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
165 //GL Posting to inventory adjustment account
166 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
167 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
168 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
171 add_audit_trail(ST_JOURNAL, $id, $date_);
172 add_comments(ST_JOURNAL, $id, $date_, $memo);
173 $Refs->save(ST_JOURNAL, $id, $ref);
174 if ($qty != 0) // 27.10.2014 dz
175 $labour_cost = ($qoh * $labour_cost + $costs) / $qty;
180 if ($qoh + $qty != 0)
181 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
183 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=labour_cost+".db_escape($labour_cost)."
184 WHERE stock_id=".db_escape($stock_id);
185 db_query($sql,"The cost details for the inventory item could not be updated");
188 function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
192 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
193 .db_escape($stock_id);
194 $result = db_query($sql);
195 $myrow = db_fetch($result);
196 $material_cost = $myrow['material_cost'];
197 $qoh = get_qoh_on_date($stock_id);
203 $material_cost = ($qoh * $material_cost + $costs) / $qoh;
204 else // Journal Entry if QOH is 0/negative
208 $id = get_next_trans_no(ST_JOURNAL);
209 $ref = $Refs->get_next(ST_JOURNAL);
211 $stock_gl_code = get_stock_gl_code($stock_id);
212 $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
213 //Reverse the inventory effect if $qoh <=0
214 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
215 $stock_gl_code["inventory_account"],
216 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
218 //GL Posting to inventory adjustment account
219 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
220 $stock_gl_code["assembly_account"], // changed 2015.10.14 from adjustment to assembly account. Petros.
221 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
224 add_audit_trail(ST_JOURNAL, $id, $date_);
225 add_comments(ST_JOURNAL, $id, $date_, $memo);
226 $Refs->save(ST_JOURNAL, $id, $ref);
227 if ($qty != 0) // 27.10.2014 dz
228 $material_cost = $costs / $qty;
233 if ($qoh + $qty != 0)
234 $material_cost = ($qoh * $material_cost + $qty * $costs) / ($qoh + $qty);
236 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost="
237 .db_escape($material_cost)
238 ." WHERE stock_id=".db_escape($stock_id);
239 db_query($sql,"The cost details for the inventory item could not be updated");
242 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
243 $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
247 if (!($type == WO_ADVANCED))
248 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
251 $args = func_get_args();
252 $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
253 'type', 'date_', 'required_by', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
255 hook_db_prewrite($args, ST_WORKORDER);
257 add_material_cost($stock_id, $units_reqd, $date_);
259 $date = date2sql($date_);
260 $required = date2sql($required_by);
262 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
263 type, date_, required_by)
264 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
265 .db_escape($units_reqd).", ".db_escape($stock_id).",
266 ".db_escape($type).", '$date', ".db_escape($required).")";
267 db_query($sql, "could not add work order");
269 $woid = db_insert_id();
271 add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
273 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
274 add_audit_trail(ST_WORKORDER, $woid, $date_);
277 hook_db_postwrite($args, ST_WORKORDER);
278 commit_transaction();
283 //--------------------------------------------------------------------------------------
285 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
286 $date_, $required_by, $memo_, $old_stock_id, $old_qty)
289 $args = func_get_args();
290 $args = (object)array_combine(array('woid', 'loc_code', 'units_reqd', 'stock_id',
291 'date_', 'required_by', 'memo_'), $args);
292 hook_db_prewrite($args, ST_WORKORDER);
294 add_material_cost($old_stock_id, -$old_qty, $date_);
295 add_material_cost($stock_id, $units_reqd, $date_);
297 $date = date2sql($date_);
298 $required = date2sql($required_by);
300 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
301 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
302 required_by=".db_escape($required).",
304 WHERE id = ".db_escape($woid);
306 db_query($sql, "could not update work order");
308 update_comments(ST_WORKORDER, $woid, null, $memo_);
309 add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
311 hook_db_postwrite($args, ST_WORKORDER);
312 commit_transaction();
315 function delete_work_order($woid, $stock_id, $qty, $date)
318 hook_db_prevoid(ST_WORKORDER, $woid);
320 add_material_cost($stock_id, -$qty, $date);
322 // delete the work order requirements
323 delete_wo_requirements($woid);
325 // delete the actual work order
326 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
327 db_query($sql,"The work order could not be deleted");
329 delete_comments(ST_WORKORDER, $woid);
330 add_audit_trail(ST_WORKORDER, $woid, $date, _("Canceled."));
332 commit_transaction();
335 //--------------------------------------------------------------------------------------
337 function get_work_order($woid, $allow_null=false)
339 $sql = "SELECT wo.*,st.description As StockItemName,l.location_name,
340 l.delivery_address,l.email, l.contact
341 FROM ".TB_PREF."workorders wo, ".TB_PREF."stock_master st, ".TB_PREF."locations l
342 WHERE st.stock_id=wo.stock_id
343 AND l.loc_code=wo.loc_code
344 AND wo.id=".db_escape($woid)."
347 $result = db_query($sql, "The work order issues could not be retrieved");
349 if (!$allow_null && db_num_rows($result) == 0)
350 display_db_error("Could not find work order $woid", $sql);
352 return db_fetch($result);
355 //--------------------------------------------------------------------------------------
357 function work_order_has_productions($woid)
359 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
360 $result = db_query($sql, "query work order for productions");
362 $myrow = db_fetch_row($result);
363 return ($myrow[0] > 0);
367 //--------------------------------------------------------------------------------------
369 function work_order_has_issues($woid)
371 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
372 $result = db_query($sql, "query work order for issues");
374 $myrow = db_fetch_row($result);
375 return ($myrow[0] > 0);
378 //--------------------------------------------------------------------------------------
380 function work_order_has_payments($woid)
382 $result = get_gl_wo_cost_trans($woid);
384 return (db_num_rows($result) != 0);
387 //--------------------------------------------------------------------------------------
389 function release_work_order($woid, $releaseDate, $memo_)
393 $myrow = get_work_order($woid);
394 $stock_id = $myrow["stock_id"];
396 $date = date2sql($releaseDate);
398 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
399 released=1 WHERE id = ".db_escape($woid);
400 db_query($sql, "could not release work order");
402 // create Work Order Requirements based on the bom
403 create_wo_requirements($woid, $stock_id);
405 add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
406 add_audit_trail(ST_WORKORDER, $woid, sql2date($myrow['date_']), _("Released."));
408 commit_transaction();
411 //--------------------------------------------------------------------------------------
413 function close_work_order($woid)
415 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
416 db_query($sql, "could not close work order");
419 //--------------------------------------------------------------------------------------
421 function work_order_is_closed($woid)
423 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
424 $result = db_query($sql, "could not query work order");
425 $row = db_fetch_row($result);
426 return ($row[0] > 0);
429 //--------------------------------------------------------------------------------------
431 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
433 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
434 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
435 WHERE id = ".db_escape($woid);
437 db_query($sql, "The work order issued quantity couldn't be updated");
440 //--------------------------------------------------------------------------------------
442 function void_work_order($woid)
445 hook_db_prevoid(ST_WORKORDER, $woid);
447 $work_order = get_work_order($woid);
448 if (!($work_order["type"] == WO_ADVANCED))
450 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
452 db_query($sql, "The work order couldn't be voided");
454 // void all related stock moves
455 void_stock_move(ST_WORKORDER, $woid);
457 // void any related bank/gl trans
458 void_bank_trans(ST_WORKORDER, $woid, true);
460 // clear the requirements units received
461 void_wo_requirements($woid);
465 // void everything inside the work order : issues, productions, payments
466 $date = sql2date($work_order['date_']);
468 $result = get_work_order_productions($woid); // check the produced quantity
469 while ($row = db_fetch($result))
471 void_work_order_produce($row['id']);
473 //Post voided entry if not prevoided explicitly
474 $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']);
477 $memo_ = _("Voiding Work Order Trans # ").$woid;
478 add_audit_trail(ST_MANURECEIVE, $row['id'], today(), _("Voided.")."\n".$memo_);
479 add_voided_entry(ST_MANURECEIVE, $row['id'], today(), $memo_);
482 $result = get_work_order_issues($woid);
484 while ($row = db_fetch($result))
486 void_work_order_issue($row['issue_no']);
488 //Post voided entry if not prevoided explicitly
489 $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']);
492 $memo_ = _("Voiding Work Order Trans # ").$woid;
493 add_audit_trail(ST_MANUISSUE, $row['issue_no'], today(), _("Voided.")."\n".$memo_);
494 add_voided_entry(ST_MANUISSUE, $row['issue_no'], today(), $memo_);
497 //Adust avg labour cost
498 $cost = get_gl_wo_cost($woid, WO_LABOUR);
500 add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true);
502 //Adust avg overhead cost
503 $cost = get_gl_wo_cost($woid, WO_OVERHEAD);
505 add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true);
507 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
509 db_query($sql, "The work order couldn't be voided");
511 // void all related stock moves
512 void_stock_move(ST_WORKORDER, $woid);
514 // void any related bank/gl trans
515 void_bank_trans(ST_WORKORDER, $woid, true);
517 // clear the requirements units received
518 void_wo_requirements($woid);
520 commit_transaction();
523 function get_sql_for_work_orders($outstanding_only, $stock_id, $location = ALL_TEXT, $order_no='', $order_ref = '', $overdue = false)
529 location.location_name,
531 workorder.units_reqd,
532 workorder.units_issued,
534 workorder.required_by,
535 workorder.released_date,
540 FROM ".TB_PREF."workorders as workorder,"
541 .TB_PREF."stock_master as item,"
542 .TB_PREF."item_units as unit,"
543 .TB_PREF."locations as location
544 WHERE workorder.stock_id=item.stock_id
545 AND workorder.loc_code=location.loc_code
546 AND item.units=unit.abbr";
548 if (check_value('OpenOnly') || $outstanding_only != 0)
550 $sql .= " AND workorder.closed=0";
553 if ($location != ALL_TEXT)
555 $sql .= " AND workorder.loc_code=".db_escape($location);
560 $sql .= " AND workorder.id LIKE ".db_escape('%'.$order_no.'%');
563 if ($order_ref != '')
565 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$order_ref.'%');
568 if ($stock_id != ALL_TEXT)
570 $sql .= " AND workorder.stock_id=".db_escape($stock_id);
575 $Today = date2sql(Today());
577 $sql .= " AND workorder.required_by < '$Today' ";
582 function get_sql_for_where_used($stock_id)
586 workcentre.name As WorkCentreName,
587 location.location_name,
590 FROM ".TB_PREF."bom as bom, "
591 .TB_PREF."stock_master as parent, "
592 .TB_PREF."workcentres as workcentre, "
593 .TB_PREF."locations as location
594 WHERE bom.parent = parent.stock_id
595 AND bom.workcentre_added = workcentre.id
596 AND bom.loc_code = location.loc_code
597 AND bom.component=".db_escape($stock_id);
600 //--------------------------------------------------------------------------------------
601 function get_gl_wo_cost($woid, $cost_type)
604 $result = get_gl_wo_cost_trans($woid, $cost_type);
605 while ($row = db_fetch($result))
606 $cost += -$row['amount'];