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
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"],
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"],
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=".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"],
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 $date = date2sql($date_);
258 $required = date2sql($required_by);
260 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
261 type, date_, required_by)
262 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
263 .db_escape($units_reqd).", ".db_escape($stock_id).",
264 ".db_escape($type).", '$date', ".db_escape($required).")";
265 db_query($sql, "could not add work order");
267 $woid = db_insert_id();
269 add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
271 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
272 add_audit_trail(ST_WORKORDER, $woid, $date_);
275 hook_db_postwrite($args, ST_WORKORDER);
276 commit_transaction();
281 //--------------------------------------------------------------------------------------
283 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
284 $date_, $required_by, $memo_)
287 $args = func_get_args();
288 $args = (object)array_combine(array('woid', 'loc_code', 'units_reqd', 'stock_id',
289 'date_', 'required_by', 'memo_'), $args);
290 hook_db_prewrite($args, ST_WORKORDER);
292 $date = date2sql($date_);
293 $required = date2sql($required_by);
295 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
296 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
297 required_by=".db_escape($required).",
299 WHERE id = ".db_escape($woid);
301 db_query($sql, "could not update work order");
303 update_comments(ST_WORKORDER, $woid, null, $memo_);
304 add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
306 hook_db_postwrite($args, ST_WORKORDER);
307 commit_transaction();
310 function delete_work_order($woid)
313 hook_db_prevoid(ST_WORKORDER, $woid);
315 // delete the work order requirements
316 delete_wo_requirements($woid);
318 // delete the actual work order
319 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
320 db_query($sql,"The work order could not be deleted");
322 delete_comments(ST_WORKORDER, $woid);
323 add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
325 commit_transaction();
328 //--------------------------------------------------------------------------------------
330 function get_work_order($woid, $allow_null=false)
332 $sql = "SELECT wo.*,st.description As StockItemName,l.location_name,
333 l.delivery_address,l.email, l.contact
334 FROM ".TB_PREF."workorders wo, ".TB_PREF."stock_master st, ".TB_PREF."locations l
335 WHERE st.stock_id=wo.stock_id
336 AND l.loc_code=wo.loc_code
337 AND wo.id=".db_escape($woid)."
340 $result = db_query($sql, "The work order issues could not be retrieved");
342 if (!$allow_null && db_num_rows($result) == 0)
343 display_db_error("Could not find work order $woid", $sql);
345 return db_fetch($result);
348 //--------------------------------------------------------------------------------------
350 function work_order_has_productions($woid)
352 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
353 $result = db_query($sql, "query work order for productions");
355 $myrow = db_fetch_row($result);
356 return ($myrow[0] > 0);
360 //--------------------------------------------------------------------------------------
362 function work_order_has_issues($woid)
364 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
365 $result = db_query($sql, "query work order for issues");
367 $myrow = db_fetch_row($result);
368 return ($myrow[0] > 0);
371 //--------------------------------------------------------------------------------------
373 function work_order_has_payments($woid)
375 $result = get_gl_wo_cost_trans($woid);
377 return (db_num_rows($result) != 0);
380 //--------------------------------------------------------------------------------------
382 function release_work_order($woid, $releaseDate, $memo_)
386 $myrow = get_work_order($woid);
387 $stock_id = $myrow["stock_id"];
389 $date = date2sql($releaseDate);
391 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
392 released=1 WHERE id = ".db_escape($woid);
393 db_query($sql, "could not release work order");
395 // create Work Order Requirements based on the bom
396 create_wo_requirements($woid, $stock_id);
398 add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
399 add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
401 commit_transaction();
404 //--------------------------------------------------------------------------------------
406 function close_work_order($woid)
408 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
409 db_query($sql, "could not close work order");
412 //--------------------------------------------------------------------------------------
414 function work_order_is_closed($woid)
416 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
417 $result = db_query($sql, "could not query work order");
418 $row = db_fetch_row($result);
419 return ($row[0] > 0);
422 //--------------------------------------------------------------------------------------
424 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
426 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
427 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
428 WHERE id = ".db_escape($woid);
430 db_query($sql, "The work order issued quantity couldn't be updated");
433 //--------------------------------------------------------------------------------------
435 function void_work_order($woid)
438 hook_db_prevoid(ST_WORKORDER, $woid);
440 $work_order = get_work_order($woid);
441 if (!($work_order["type"] == WO_ADVANCED))
443 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
445 db_query($sql, "The work order couldn't be voided");
447 // void all related stock moves
448 void_stock_move(ST_WORKORDER, $woid);
450 // void any related bank/gl trans
451 void_bank_trans(ST_WORKORDER, $woid, true);
453 // clear the requirements units received
454 void_wo_requirements($woid);
458 // void everything inside the work order : issues, productions, payments
459 $date = sql2date($work_order['date_']);
461 $result = get_work_order_productions($woid); // check the produced quantity
462 while ($row = db_fetch($result))
464 void_work_order_produce($row['id']);
466 //Post voided entry if not prevoided explicitly
467 $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']);
470 $memo_ = _("Voiding Work Order Trans # ").$woid;
471 add_audit_trail(ST_MANURECEIVE, $row['id'], today(), _("Voided.")."\n".$memo_);
472 add_voided_entry(ST_MANURECEIVE, $row['id'], today(), $memo_);
475 $result = get_work_order_issues($woid);
477 while ($row = db_fetch($result))
479 void_work_order_issue($row['issue_no']);
481 //Post voided entry if not prevoided explicitly
482 $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']);
485 $memo_ = _("Voiding Work Order Trans # ").$woid;
486 add_audit_trail(ST_MANUISSUE, $row['issue_no'], today(), _("Voided.")."\n".$memo_);
487 add_voided_entry(ST_MANUISSUE, $row['issue_no'], today(), $memo_);
490 //Adust avg labour cost
491 $cost = get_gl_wo_cost($woid, WO_LABOUR);
493 add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true);
495 //Adust avg overhead cost
496 $cost = get_gl_wo_cost($woid, WO_OVERHEAD);
498 add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true);
500 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
502 db_query($sql, "The work order couldn't be voided");
504 // void all related stock moves
505 void_stock_move(ST_WORKORDER, $woid);
507 // void any related bank/gl trans
508 void_bank_trans(ST_WORKORDER, $woid, true);
510 // clear the requirements units received
511 void_wo_requirements($woid);
513 commit_transaction();
516 function get_sql_for_work_orders($outstanding_only, $all_items)
522 location.location_name,
524 workorder.units_reqd,
525 workorder.units_issued,
527 workorder.required_by,
528 workorder.released_date,
533 FROM ".TB_PREF."workorders as workorder,"
534 .TB_PREF."stock_master as item,"
535 .TB_PREF."item_units as unit,"
536 .TB_PREF."locations as location
537 WHERE workorder.stock_id=item.stock_id
538 AND workorder.loc_code=location.loc_code
539 AND item.units=unit.abbr";
541 if (check_value('OpenOnly') || $outstanding_only != 0)
543 $sql .= " AND workorder.closed=0";
546 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
548 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
551 if (isset($_POST['OrderId']) && $_POST['OrderId'] != "")
553 $sql .= " AND workorder.id LIKE ".db_escape('%'.$_POST['OrderId'].'%');
556 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
558 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
561 if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
563 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
566 if (check_value('OverdueOnly'))
568 $Today = date2sql(Today());
570 $sql .= " AND workorder.required_by < '$Today' ";
572 $sql .= " ORDER BY workorder.id DESC";
576 function get_sql_for_where_used()
580 workcentre.name As WorkCentreName,
581 location.location_name,
584 FROM ".TB_PREF."bom as bom, "
585 .TB_PREF."stock_master as parent, "
586 .TB_PREF."workcentres as workcentre, "
587 .TB_PREF."locations as location
588 WHERE bom.parent = parent.stock_id
589 AND bom.workcentre_added = workcentre.id
590 AND bom.loc_code = location.loc_code
591 AND bom.component=".db_escape($_POST['stock_id']);
594 //--------------------------------------------------------------------------------------
595 function get_gl_wo_cost($woid, $cost_type)
598 $result = get_gl_wo_cost_trans($woid, $cost_type);
599 while ($row = db_fetch($result))
600 $cost += -$row['amount'];