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)
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);
25 $sql = "SELECT material_cost, labour_cost, overhead_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'];
33 //reduce overhead_cost and labour_cost from price as those will remain as is
34 $m_cost = $m_cost - $myrow['labour_cost'] - $myrow['overhead_cost'];
37 $qoh = get_qoh_on_date($stock_id);
46 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
48 if ($advanced && $cost_adjust) // new 2010-02-10
49 adjust_deliveries($stock_id, $bom_cost, $date_);
51 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
52 WHERE stock_id=".db_escape($stock_id);
53 db_query($sql,"The cost details for the inventory item could not be updated");
56 function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
60 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
61 .db_escape($stock_id);
62 $result = db_query($sql);
63 $myrow = db_fetch($result);
64 $overhead_cost = $myrow['overhead_cost'];
65 $qoh = get_qoh_on_date($stock_id);
71 $costs = $qty * $costs;
73 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh;
74 else // Journal Entry if QOH is 0/negative
78 $id = get_next_trans_no(ST_JOURNAL);
79 $ref = $Refs->get_next(ST_JOURNAL);
81 $stock_gl_code = get_stock_gl_code($stock_id);
82 $memo = "WO Overhead cost settlement JV for zero/negative respository of ".$stock_id;
83 //Reverse the inventory effect if $qoh <=0
84 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
85 $stock_gl_code["inventory_account"],
86 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
88 //GL Posting to inventory adjustment account
89 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
90 $stock_gl_code["adjustment_account"],
91 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
94 add_audit_trail(ST_JOURNAL, $id, $date_);
95 add_comments(ST_JOURNAL, $id, $date_, $memo);
96 $Refs->save(ST_JOURNAL, $id, $ref);
101 if ($qoh + $qty != 0)
102 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
104 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
105 WHERE stock_id=".db_escape($stock_id);
106 db_query($sql,"The cost details for the inventory item could not be updated");
109 function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
113 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
114 .db_escape($stock_id);
115 $result = db_query($sql);
116 $myrow = db_fetch($result);
117 $labour_cost = $myrow['labour_cost'];
118 $qoh = get_qoh_on_date($stock_id);
124 $costs = $qty * $costs;
126 $labour_cost = ($qoh * $labour_cost + $costs) / $qoh;
127 else // Journal Entry if QOH is 0/negative
131 $id = get_next_trans_no(ST_JOURNAL);
132 $ref = $Refs->get_next(ST_JOURNAL);
134 $stock_gl_code = get_stock_gl_code($stock_id);
135 $memo = "WO labour cost settlement JV for zero/negative respository of ".$stock_id;
136 //Reverse the inventory effect if $qoh <=0
137 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
138 $stock_gl_code["inventory_account"],
139 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
141 //GL Posting to inventory adjustment account
142 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
143 $stock_gl_code["adjustment_account"],
144 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
147 add_audit_trail(ST_JOURNAL, $id, $date_);
148 add_comments(ST_JOURNAL, $id, $date_, $memo);
149 $Refs->save(ST_JOURNAL, $id, $ref);
154 if ($qoh + $qty != 0)
155 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
157 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
158 WHERE stock_id=".db_escape($stock_id);
159 db_query($sql,"The cost details for the inventory item could not be updated");
162 function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
166 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
167 .db_escape($stock_id);
168 $result = db_query($sql);
169 $myrow = db_fetch($result);
170 $material_cost = $myrow['material_cost'];
171 $qoh = get_qoh_on_date($stock_id);
177 $costs = $qty * $costs;
179 $material_cost = $costs / $qoh;
180 else // Journal Entry if QOH is 0/negative
184 $id = get_next_trans_no(ST_JOURNAL);
185 $ref = $Refs->get_next(ST_JOURNAL);
187 $stock_gl_code = get_stock_gl_code($stock_id);
188 $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
189 //Reverse the inventory effect if $qoh <=0
190 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
191 $stock_gl_code["inventory_account"],
192 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
194 //GL Posting to inventory adjustment account
195 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
196 $stock_gl_code["adjustment_account"],
197 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
200 add_audit_trail(ST_JOURNAL, $id, $date_);
201 add_comments(ST_JOURNAL, $id, $date_, $memo);
202 $Refs->save(ST_JOURNAL, $id, $ref);
207 if ($qoh + $qty != 0)
208 $material_cost = ($qty * $costs) / ($qoh + $qty);
210 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
211 .db_escape($material_cost)
212 ." WHERE stock_id=".db_escape($stock_id);
213 db_query($sql,"The cost details for the inventory item could not be updated");
216 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
217 $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
221 if (!($type == WO_ADVANCED))
222 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
225 $args = func_get_args();
226 $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
227 'type', 'date_', 'required_by', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
229 hook_db_prewrite($args, ST_WORKORDER);
231 $date = date2sql($date_);
232 $required = date2sql($required_by);
234 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
235 type, date_, required_by)
236 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
237 .db_escape($units_reqd).", ".db_escape($stock_id).",
238 ".db_escape($type).", '$date', ".db_escape($required).")";
239 db_query($sql, "could not add work order");
241 $woid = db_insert_id();
243 add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
245 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
246 add_audit_trail(ST_WORKORDER, $woid, $date_);
249 hook_db_postwrite($args, ST_WORKORDER);
250 commit_transaction();
255 //--------------------------------------------------------------------------------------
257 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
258 $date_, $required_by, $memo_)
261 $args = func_get_args();
262 $args = (object)array_combine(array('woid', 'loc_code', 'units_reqd', 'stock_id',
263 'date_', 'required_by', 'memo_'), $args);
264 hook_db_prewrite($args, ST_WORKORDER);
266 $date = date2sql($date_);
267 $required = date2sql($required_by);
269 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
270 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
271 required_by=".db_escape($required).",
273 WHERE id = ".db_escape($woid);
275 db_query($sql, "could not update work order");
277 update_comments(ST_WORKORDER, $woid, null, $memo_);
278 add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
280 hook_db_postwrite($args, ST_WORKORDER);
281 commit_transaction();
284 function delete_work_order($woid)
287 hook_db_prevoid(ST_WORKORDER, $woid);
289 // delete the work order requirements
290 delete_wo_requirements($woid);
292 // delete the actual work order
293 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
294 db_query($sql,"The work order could not be deleted");
296 delete_comments(ST_WORKORDER, $woid);
297 add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
299 commit_transaction();
302 //--------------------------------------------------------------------------------------
304 function get_work_order($woid, $allow_null=false)
306 $sql = "SELECT wo.*,st.description As StockItemName,l.location_name,
307 l.delivery_address,l.email, l.contact
308 FROM ".TB_PREF."workorders wo, ".TB_PREF."stock_master st, ".TB_PREF."locations l
309 WHERE st.stock_id=wo.stock_id
310 AND l.loc_code=wo.loc_code
311 AND wo.id=".db_escape($woid)."
314 $result = db_query($sql, "The work order issues could not be retrieved");
316 if (!$allow_null && db_num_rows($result) == 0)
317 display_db_error("Could not find work order $woid", $sql);
319 return db_fetch($result);
322 //--------------------------------------------------------------------------------------
324 function work_order_has_productions($woid)
326 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
327 $result = db_query($sql, "query work order for productions");
329 $myrow = db_fetch_row($result);
330 return ($myrow[0] > 0);
334 //--------------------------------------------------------------------------------------
336 function work_order_has_issues($woid)
338 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
339 $result = db_query($sql, "query work order for issues");
341 $myrow = db_fetch_row($result);
342 return ($myrow[0] > 0);
345 //--------------------------------------------------------------------------------------
347 function work_order_has_payments($woid)
349 $result = get_gl_wo_cost_trans($woid);
351 return (db_num_rows($result) != 0);
354 //--------------------------------------------------------------------------------------
356 function release_work_order($woid, $releaseDate, $memo_)
360 $myrow = get_work_order($woid);
361 $stock_id = $myrow["stock_id"];
363 $date = date2sql($releaseDate);
365 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
366 released=1 WHERE id = ".db_escape($woid);
367 db_query($sql, "could not release work order");
369 // create Work Order Requirements based on the bom
370 create_wo_requirements($woid, $stock_id);
372 add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
373 add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
375 commit_transaction();
378 //--------------------------------------------------------------------------------------
380 function close_work_order($woid)
382 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
383 db_query($sql, "could not close work order");
386 //--------------------------------------------------------------------------------------
388 function work_order_is_closed($woid)
390 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
391 $result = db_query($sql, "could not query work order");
392 $row = db_fetch_row($result);
393 return ($row[0] > 0);
396 //--------------------------------------------------------------------------------------
398 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
400 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
401 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
402 WHERE id = ".db_escape($woid);
404 db_query($sql, "The work order issued quantity couldn't be updated");
407 //--------------------------------------------------------------------------------------
409 function void_work_order($woid)
412 hook_db_prevoid(ST_WORKORDER, $woid);
414 $work_order = get_work_order($woid);
415 if (!($work_order["type"] == WO_ADVANCED))
417 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
419 db_query($sql, "The work order couldn't be voided");
421 // void all related stock moves
422 void_stock_move(ST_WORKORDER, $woid);
424 // void any related bank/gl trans
425 void_bank_trans(ST_WORKORDER, $woid, true);
427 // clear the requirements units received
428 void_wo_requirements($woid);
432 // void everything inside the work order : issues, productions, payments
433 $date = sql2date($work_order['date_']);
435 $result = get_work_order_productions($woid); // check the produced quantity
437 while ($row = db_fetch($result))
439 void_work_order_produce($row['id']);
441 //Post voided entry if not prevoided explicitly
442 $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']);
445 $memo_ = _("Voiding Work Order Trans # ").$woid;
446 add_audit_trail(ST_MANURECEIVE, $row['id'], today(), _("Voided.")."\n".$memo_);
447 add_voided_entry(ST_MANURECEIVE, $row['id'], today(), $memo_);
450 $result = get_work_order_issues($woid);
453 while ($row = db_fetch($result))
455 void_work_order_issue($row['issue_no']);
457 //Post voided entry if not prevoided explicitly
458 $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']);
461 $memo_ = _("Voiding Work Order Trans # ").$woid;
462 add_audit_trail(ST_MANUISSUE, $row['issue_no'], today(), _("Voided.")."\n".$memo_);
463 add_voided_entry(ST_MANUISSUE, $row['issue_no'], today(), $memo_);
466 //Adust avg labour cost
467 $cost = get_gl_wo_cost($woid, WO_LABOUR);
469 add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true);
471 //Adust avg overhead cost
472 $cost = get_gl_wo_cost($woid, WO_OVERHEAD);
474 add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true);
476 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
478 db_query($sql, "The work order couldn't be voided");
480 // void all related stock moves
481 void_stock_move(ST_WORKORDER, $woid);
483 // void any related bank/gl trans
484 void_bank_trans(ST_WORKORDER, $woid, true);
486 // clear the requirements units received
487 void_wo_requirements($woid);
489 commit_transaction();
492 function get_sql_for_work_orders($outstanding_only, $all_items)
498 location.location_name,
500 workorder.units_reqd,
501 workorder.units_issued,
503 workorder.required_by,
504 workorder.released_date,
509 FROM ".TB_PREF."workorders as workorder,"
510 .TB_PREF."stock_master as item,"
511 .TB_PREF."item_units as unit,"
512 .TB_PREF."locations as location
513 WHERE workorder.stock_id=item.stock_id
514 AND workorder.loc_code=location.loc_code
515 AND item.units=unit.abbr";
517 if (check_value('OpenOnly') || $outstanding_only != 0)
519 $sql .= " AND workorder.closed=0";
522 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
524 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
527 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
529 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
532 if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
534 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
537 if (check_value('OverdueOnly'))
539 $Today = date2sql(Today());
541 $sql .= " AND workorder.required_by < '$Today' ";
546 function get_sql_for_where_used()
550 workcentre.name As WorkCentreName,
551 location.location_name,
554 FROM ".TB_PREF."bom as bom, "
555 .TB_PREF."stock_master as parent, "
556 .TB_PREF."workcentres as workcentre, "
557 .TB_PREF."locations as location
558 WHERE bom.parent = parent.stock_id
559 AND bom.workcentre_added = workcentre.id
560 AND bom.loc_code = location.loc_code
561 AND bom.component=".db_escape($_POST['stock_id']);
564 //--------------------------------------------------------------------------------------
565 function get_gl_wo_cost($woid, $cost_type)
568 $result = get_gl_wo_cost_trans($woid, $cost_type);
569 while ($row = db_fetch($result))
570 $cost += -$row['amount'];