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