Merged changes from main trunk up to 2.2.7
[fa-stable.git] / manufacturing / includes / db / work_orders_db.inc
1 <?php
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 //--------------------------------------------------------------------------------------
13
14 function add_material_cost($stock_id, $qty, $date_)
15 {
16         $m_cost = 0;
17     $result = get_bom($stock_id);
18         while ($bom_item = db_fetch($result))
19         {
20                 $standard_cost = get_standard_cost($bom_item['component']);
21                 $m_cost += ($bom_item['quantity'] * $standard_cost);
22         }
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         if ($qoh < 0)
32                 $qoh = 0;
33         if ($qoh + $qty != 0)   
34                 $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
35         $material_cost = round2($material_cost, $dec);  
36         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
37                 WHERE stock_id=".db_escape($stock_id);
38         db_query($sql,"The cost details for the inventory item could not be updated");
39 }
40
41 function add_overhead_cost($stock_id, $qty, $date_, $costs)
42 {
43         $dec = user_price_dec();
44         price_decimal_format($costs, $dec); 
45         if ($qty != 0)
46                 $costs /= $qty;
47         $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
48                 .db_escape($stock_id);
49         $result = db_query($sql);
50         $myrow = db_fetch($result);
51         $overhead_cost =  $myrow['overhead_cost'];
52         $qoh = get_qoh_on_date($stock_id, null, $date_);
53         if ($qoh < 0)
54                 $qoh = 0;
55         if ($qoh + $qty != 0)   
56                 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
57         $overhead_cost = round2($overhead_cost, $dec);  
58         $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
59                 WHERE stock_id=".db_escape($stock_id);
60         db_query($sql,"The cost details for the inventory item could not be updated");
61 }
62
63 function add_labour_cost($stock_id, $qty, $date_, $costs)
64 {
65         $dec = user_price_dec();
66         price_decimal_format($costs, $dec); 
67         if ($qty != 0)
68                 $costs /= $qty;
69         $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
70                 .db_escape($stock_id);
71         $result = db_query($sql);
72         $myrow = db_fetch($result);
73         $labour_cost =  $myrow['labour_cost'];
74         $qoh = get_qoh_on_date($stock_id, null, $date_);
75         if ($qoh < 0)
76                 $qoh = 0;
77         if ($qoh + $qty != 0)   
78                 $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
79         $labour_cost = round2($labour_cost, $dec);      
80         $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
81                 WHERE stock_id=".db_escape($stock_id);
82         db_query($sql,"The cost details for the inventory item could not be updated");
83 }
84
85 function add_issue_cost($stock_id, $qty, $date_, $costs)
86 {
87         if ($qty != 0)
88                 $costs /= $qty;
89         $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
90                 .db_escape($stock_id);
91         $result = db_query($sql);
92         $myrow = db_fetch($result);
93         $material_cost =  $myrow['material_cost'];
94         $dec = user_price_dec();
95         price_decimal_format($material_cost, $dec); 
96         $qoh = get_qoh_on_date($stock_id, null, $date_);
97         if ($qoh < 0)
98                 $qoh = 0;
99         if ($qoh + $qty != 0)   
100                 $material_cost = ($qty * $costs) /      ($qoh + $qty);
101         $material_cost = round2($material_cost, $dec);  
102         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
103                 .db_escape($material_cost)
104                 ." WHERE stock_id=".db_escape($stock_id);
105         db_query($sql,"The cost details for the inventory item could not be updated");
106 }
107
108 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
109         $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
110 {
111         global $Refs;
112
113         if (!($type == WO_ADVANCED))
114                 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
115
116         begin_transaction();
117
118         add_material_cost($stock_id, $units_reqd, $date_);
119
120         $date = date2sql($date_);
121         $required = date2sql($required_by);
122
123         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
124                 type, date_, required_by)
125         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
126         .db_escape($units_reqd).", ".db_escape($stock_id).",
127                 ".db_escape($type).", '$date', ".db_escape($required).")";
128         db_query($sql, "could not add work order");
129
130         $woid = db_insert_id();
131
132         add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
133
134         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
135         add_audit_trail(ST_WORKORDER, $woid, $date_);
136
137         commit_transaction();
138
139         return $woid;
140 }
141
142 //--------------------------------------------------------------------------------------
143
144 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
145                                         $date_, $required_by, $memo_)
146 {
147         begin_transaction();
148
149         add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
150         add_material_cost($stock_id, $units_reqd, $date_);
151
152         $date = date2sql($date_);
153         $required = date2sql($required_by);
154
155         $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
156                 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
157                 required_by=".db_escape($required).",
158                 date_='$date'
159                 WHERE id = ".db_escape($woid);
160
161         db_query($sql, "could not update work order");
162
163         update_comments(ST_WORKORDER, $woid, null, $memo_);
164         add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
165
166         commit_transaction();
167 }
168
169 function delete_work_order($woid)
170 {
171         begin_transaction();
172
173         add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
174
175         // delete the work order requirements
176         delete_wo_requirements($woid);
177
178         // delete the actual work order
179         $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
180         db_query($sql,"The work order could not be deleted");
181
182         delete_comments(ST_WORKORDER, $woid);
183         add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
184
185         commit_transaction();
186 }
187
188 //--------------------------------------------------------------------------------------
189
190 function get_work_order($woid, $allow_null=false)
191 {
192     $sql = "SELECT ".TB_PREF."workorders.*, ".TB_PREF."stock_master.description As StockItemName,
193                 ".TB_PREF."locations.location_name, ".TB_PREF."locations.delivery_address 
194                 FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations
195                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
196                 AND     ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code
197                 AND ".TB_PREF."workorders.id=".db_escape($woid)."
198                 GROUP BY ".TB_PREF."workorders.id";
199
200         $result = db_query($sql, "The work order issues could not be retrieved");
201
202         if (!$allow_null && db_num_rows($result) == 0)
203                 display_db_error("Could not find work order $woid", $sql);
204
205         return db_fetch($result);
206 }
207
208 //--------------------------------------------------------------------------------------
209
210 function work_order_has_productions($woid)
211 {
212         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
213         $result = db_query($sql, "query work order for productions");
214
215         $myrow = db_fetch_row($result);
216         return ($myrow[0] > 0);
217 }
218
219
220 //--------------------------------------------------------------------------------------
221
222 function work_order_has_issues($woid)
223 {
224         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
225         $result = db_query($sql, "query work order for issues");
226
227         $myrow = db_fetch_row($result);
228         return ($myrow[0] > 0);
229 }
230
231 //--------------------------------------------------------------------------------------
232
233 function work_order_has_payments($woid)
234 {
235         $result = get_gl_wo_cost_trans($woid);
236
237     return (db_num_rows($result) != 0);
238 }
239
240 //--------------------------------------------------------------------------------------
241
242 function release_work_order($woid, $releaseDate, $memo_)
243 {
244         begin_transaction();
245
246         $myrow = get_work_order($woid);
247         $stock_id = $myrow["stock_id"];
248
249         $date = date2sql($releaseDate);
250
251         $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
252                 released=1 WHERE id = ".db_escape($woid);
253         db_query($sql, "could not release work order");
254
255         // create Work Order Requirements based on the bom
256         create_wo_requirements($woid, $stock_id);
257
258         add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
259         add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
260
261         commit_transaction();
262 }
263
264 //--------------------------------------------------------------------------------------
265
266 function close_work_order($woid)
267 {
268         $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
269         db_query($sql, "could not close work order");
270 }
271
272 //--------------------------------------------------------------------------------------
273
274 function work_order_is_closed($woid)
275 {
276         $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
277         $result = db_query($sql, "could not query work order");
278         $row = db_fetch_row($result);
279         return ($row[0] > 0);
280 }
281
282 //--------------------------------------------------------------------------------------
283
284 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
285 {
286         $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
287                 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
288                 WHERE id = ".db_escape($woid);
289
290         db_query($sql, "The work order issued quantity couldn't be updated");
291 }
292
293 //--------------------------------------------------------------------------------------
294
295 function void_work_order($woid)
296 {
297         begin_transaction();
298
299         $work_order = get_work_order($woid);
300         if (!($work_order["type"] == WO_ADVANCED))
301         {
302                 $date = sql2date($work_order['date_']);
303                 $qty = $work_order['units_reqd'];
304                 add_material_cost($work_order['stock_id'], -$qty, $date); // remove avg. cost for qty
305                 $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
306                 if ($cost != 0)
307                         add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
308                 $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
309                 if ($cost != 0)
310                         add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
311
312                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
313                         .db_escape($woid);
314                 db_query($sql, "The work order couldn't be voided");
315
316                 // void all related stock moves
317                 void_stock_move(ST_WORKORDER, $woid);
318
319                 // void any related gl trans
320                 void_gl_trans(ST_WORKORDER, $woid, true);
321
322                 // clear the requirements units received
323                 void_wo_requirements($woid);
324         }
325         else
326         {
327                 // void everything inside the work order : issues, productions, payments
328                 $date = sql2date($work_order['date_']);
329                 add_material_cost($work_order['stock_id'], -$work_order['units_reqd'], $date); // remove avg. cost for qty
330                 $result = get_work_order_productions($woid); // check the produced quantity
331                 $qty = 0;
332                 while ($row = db_fetch($result))
333                 {
334                         $qty += $row['quantity'];
335                         // clear the production record
336                         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$$row['id'];
337                         db_query($sql, "Cannot void a wo production");
338                         
339                         void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves; 
340                 }
341                 $result = get_additional_issues($woid); // check the issued quantities
342                 $cost = 0;
343                 $issue_no = 0;
344                 while ($row = db_fetch($result))
345                 {
346                         $std_cost = get_standard_cost($row['stock_id']);
347                         $icost = $std_cost * $row['qty_issued'];
348                         $cost += $icost;
349                         if ($issue_no == 0)
350                                 $issue_no = $row['issue_no'];
351                         // void the actual issue items and their quantities
352                         $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0 WHERE issue_id="
353                                 .db_escape($row['id']);
354                         db_query($sql,"A work order issue item could not be voided");
355                 }       
356                 if ($issue_no != 0)
357                         void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves 
358                 if ($cost != 0)
359                         add_issue_cost($work_order['stock_id'], -$qty, $date, $cost);
360
361                 $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
362                 if ($cost != 0)
363                         add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
364                 $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
365                 if ($cost != 0)
366                         add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
367                 
368                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
369                         .db_escape($woid);
370                 db_query($sql, "The work order couldn't be voided");
371
372                 // void all related stock moves
373                 void_stock_move(ST_WORKORDER, $woid);
374
375                 // void any related gl trans
376                 void_gl_trans(ST_WORKORDER, $woid, true);
377
378                 // clear the requirements units received
379                 void_wo_requirements($woid);
380         }
381         commit_transaction();
382 }
383
384 function get_sql_for_work_orders($outstanding_only, $all_items)
385 {
386         $sql = "SELECT
387                 workorder.id,
388                 workorder.wo_ref,
389                 workorder.type,
390                 location.location_name,
391                 item.description,
392                 workorder.units_reqd,
393                 workorder.units_issued,
394                 workorder.date_,
395                 workorder.required_by,
396                 workorder.released_date,
397                 workorder.closed,
398                 workorder.released,
399                 workorder.stock_id,
400                 unit.decimals
401                 FROM ".TB_PREF."workorders as workorder,"
402                         .TB_PREF."stock_master as item,"
403                         .TB_PREF."item_units as unit,"
404                         .TB_PREF."locations as location
405                 WHERE workorder.stock_id=item.stock_id 
406                         AND workorder.loc_code=location.loc_code
407                         AND item.units=unit.abbr";
408
409         if (check_value('OpenOnly') || $outstanding_only != 0)
410         {
411                 $sql .= " AND workorder.closed=0";
412         }
413
414         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
415         {
416                 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
417         }
418
419         if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
420         {
421                 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
422         }
423
424         if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
425         {
426                 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
427         }
428
429         if (check_value('OverdueOnly'))
430         {
431                 $Today = date2sql(Today());
432
433                 $sql .= " AND workorder.required_by < '$Today' ";
434         }
435         return $sql;
436 }
437
438 function get_sql_for_where_used()
439 {
440         $sql = "SELECT 
441                         bom.parent,
442                         workcentre.name As WorkCentreName,
443                         location.location_name,
444                         bom.quantity,
445                         parent.description
446                         FROM ".TB_PREF."bom as bom, "
447                                 .TB_PREF."stock_master as parent, "
448                                 .TB_PREF."workcentres as workcentre, "
449                                 .TB_PREF."locations as location
450                         WHERE bom.parent = parent.stock_id 
451                                 AND bom.workcentre_added = workcentre.id
452                                 AND bom.loc_code = location.loc_code
453                                 AND bom.component=".db_escape($_POST['stock_id']);
454         return $sql;                    
455 }
456 //--------------------------------------------------------------------------------------
457 function get_gl_wo_cost($woid, $cost_type)
458 {
459         $cost = 0;
460         $result = get_gl_wo_cost_trans($woid, $cost_type);
461         while ($row = db_fetch($result))
462                 $cost += -$row['amount'];
463         return $cost;   
464 }
465
466 ?>