Moving bugfixes from 2.3 to 2.4 and updated install languages for Denmark and Sweden.
[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_, $advanced=false, $woid=0)
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         $bom_cost = $m_cost;
24         // new 2015.10.15       
25         // additilnal costs.
26         $i_cost = 0;
27         if ($woid != 0 && work_order_has_issues($woid))
28         {
29                 $res = get_additional_issues($woid);
30                 while ($issue = db_fetch($res))
31                 {
32                         $standard_cost = get_standard_cost($issue['stock_id']);
33                         $i_cost += ($issue['qty_issued'] * $standard_cost) / $qty;
34                 }
35         }
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
41         $m_cost += $i_cost;
42         /* no, why will we do that?? Joe Hunt 2015.10.17
43         if ($advanced)
44         {
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'];
47         }
48         */
49         $qoh = get_qoh_on_date($stock_id);
50         $cost_adjust = false;
51         if ($qoh < 0)
52         {
53                 if ($qoh + $qty >= 0)
54                         $cost_adjust = true;
55                 $qoh = 0;
56         }               
57         if ($qoh + $qty != 0)
58         {
59                 if ($qoh == 0) // 27.10.2014 apmuthu and dz.
60                         $material_cost = $m_cost;
61                 else    
62                         $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
63         }
64         if ($advanced && $cost_adjust) // new 2010-02-10
65                 adjust_deliveries($stock_id, $bom_cost, $date_);        
66         
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");
70 }
71
72 function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
73 {
74         if ($qty != 0)
75                 $costs /= $qty;
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);
82         if ($qoh < 0)
83                 $qoh = 0;
84         if ($adj_only)
85         {
86                 if ($qoh>0)
87                 {
88                         if ($qoh + $qty != 0)   
89                                 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
90                         elseif ($qty == 0)
91                                 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh;
92                 }                       
93                 else // Journal Entry if QOH is 0/negative 
94                 {
95                         global $Refs;
96
97                         $id = get_next_trans_no(ST_JOURNAL);
98                         $ref = $Refs->get_next(ST_JOURNAL);
99                         
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, 
106                                 -$costs);
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,
111                                 $costs);
112                                 
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;
118                 }
119         }
120         else
121         {               
122                 if ($qoh + $qty != 0)   
123                         $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
124         }
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");
128 }
129
130 function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
131 {
132         if ($qty != 0)
133                 $costs /= $qty;
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);
140         if ($qoh < 0)
141                 $qoh = 0;
142         if ($adj_only)
143         {
144                 if ($qoh>0)
145                 {
146                         if ($qoh + $qty != 0)   
147                                 $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
148                         elseif ($qty == 0)
149                                 $labour_cost = ($qoh * $labour_cost + $costs) / $qoh;
150                 }               
151                 else // Journal Entry if QOH is 0/negative 
152                 {
153                         global $Refs;
154
155                         $id = get_next_trans_no(ST_JOURNAL);
156                         $ref = $Refs->get_next(ST_JOURNAL);
157                         
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, 
164                                 -$costs);
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,
169                                 $costs);
170                                 
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;
176                 }
177         }
178         else
179         {               
180                 if ($qoh + $qty != 0)   
181                         $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
182         }       
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");
186 }
187
188 function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
189 {
190         if ($qty != 0)
191                 $costs /= $qty;
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);
198         if ($qoh < 0)
199                 $qoh = 0;
200         if ($adj_only)
201         {
202                 if ($qoh>0)
203                         $material_cost = ($qoh * $material_cost + $costs) / $qoh;
204                 else // Journal Entry if QOH is 0/negative
205                 {
206                         global $Refs;
207
208                         $id = get_next_trans_no(ST_JOURNAL);
209                         $ref = $Refs->get_next(ST_JOURNAL);
210                         
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, 
217                                 -$costs);
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,
222                                 $costs);
223                                 
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; 
229                 }
230         }
231         else
232         {
233                 if ($qoh + $qty != 0)   
234                         $material_cost = ($qoh * $material_cost + $qty * $costs) /      ($qoh + $qty);
235         }       
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");
240 }
241
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)
244 {
245         global $Refs;
246
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);
249
250         begin_transaction();
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);
254         $args->woid = 0;
255         hook_db_prewrite($args, ST_WORKORDER);
256
257         add_material_cost($stock_id, $units_reqd, $date_);
258
259         $date = date2sql($date_);
260         $required = date2sql($required_by);
261
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");
268
269         $woid = db_insert_id();
270
271         add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
272
273         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
274         add_audit_trail(ST_WORKORDER, $woid, $date_);
275
276         $args->woid = $woid;
277         hook_db_postwrite($args, ST_WORKORDER);
278         commit_transaction();
279
280         return $woid;
281 }
282
283 //--------------------------------------------------------------------------------------
284
285 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
286                                         $date_, $required_by, $memo_, $old_stock_id, $old_qty)
287 {
288         begin_transaction();
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);
293
294         add_material_cost($old_stock_id, -$old_qty, $date_);
295         add_material_cost($stock_id, $units_reqd, $date_);
296
297         $date = date2sql($date_);
298         $required = date2sql($required_by);
299
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).",
303                 date_='$date'
304                 WHERE id = ".db_escape($woid);
305
306         db_query($sql, "could not update work order");
307
308         update_comments(ST_WORKORDER, $woid, null, $memo_);
309         add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
310
311         hook_db_postwrite($args, ST_WORKORDER);
312         commit_transaction();
313 }
314
315 function delete_work_order($woid, $stock_id, $qty, $date)
316 {
317         begin_transaction();
318         hook_db_prevoid(ST_WORKORDER, $woid);
319
320         add_material_cost($stock_id, -$qty, $date);
321
322         // delete the work order requirements
323         delete_wo_requirements($woid);
324
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");
328
329         delete_comments(ST_WORKORDER, $woid);
330         add_audit_trail(ST_WORKORDER, $woid, $date, _("Canceled."));
331
332         commit_transaction();
333 }
334
335 //--------------------------------------------------------------------------------------
336
337 function get_work_order($woid, $allow_null=false)
338 {
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)."
345                 GROUP BY wo.id";
346
347         $result = db_query($sql, "The work order issues could not be retrieved");
348
349         if (!$allow_null && db_num_rows($result) == 0)
350                 display_db_error("Could not find work order $woid", $sql);
351
352         return db_fetch($result);
353 }
354
355 //--------------------------------------------------------------------------------------
356
357 function work_order_has_productions($woid)
358 {
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");
361
362         $myrow = db_fetch_row($result);
363         return ($myrow[0] > 0);
364 }
365
366
367 //--------------------------------------------------------------------------------------
368
369 function work_order_has_issues($woid)
370 {
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");
373
374         $myrow = db_fetch_row($result);
375         return ($myrow[0] > 0);
376 }
377
378 //--------------------------------------------------------------------------------------
379
380 function work_order_has_payments($woid)
381 {
382         $result = get_gl_wo_cost_trans($woid);
383
384     return (db_num_rows($result) != 0);
385 }
386
387 //--------------------------------------------------------------------------------------
388
389 function release_work_order($woid, $releaseDate, $memo_)
390 {
391         begin_transaction();
392
393         $myrow = get_work_order($woid);
394         $stock_id = $myrow["stock_id"];
395
396         $date = date2sql($releaseDate);
397
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");
401
402         // create Work Order Requirements based on the bom
403         create_wo_requirements($woid, $stock_id);
404
405         add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
406         add_audit_trail(ST_WORKORDER, $woid, sql2date($myrow['date_']), _("Released."));
407
408         commit_transaction();
409 }
410
411 //--------------------------------------------------------------------------------------
412
413 function close_work_order($woid)
414 {
415         $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
416         db_query($sql, "could not close work order");
417 }
418
419 //--------------------------------------------------------------------------------------
420
421 function work_order_is_closed($woid)
422 {
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);
427 }
428
429 //--------------------------------------------------------------------------------------
430
431 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
432 {
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);
436
437         db_query($sql, "The work order issued quantity couldn't be updated");
438 }
439
440 //--------------------------------------------------------------------------------------
441
442 function void_work_order($woid)
443 {
444         begin_transaction();
445         hook_db_prevoid(ST_WORKORDER, $woid);
446
447         $work_order = get_work_order($woid);
448         if (!($work_order["type"] == WO_ADVANCED))
449         {
450                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
451                         .db_escape($woid);
452                 db_query($sql, "The work order couldn't be voided");
453
454                 // void all related stock moves
455                 void_stock_move(ST_WORKORDER, $woid);
456
457                 // void any related bank/gl trans
458                 void_bank_trans(ST_WORKORDER, $woid, true);
459
460                 // clear the requirements units received
461                 void_wo_requirements($woid);
462         }
463         else
464         {
465                 // void everything inside the work order : issues, productions, payments
466                 $date = sql2date($work_order['date_']);
467                 
468                 $result = get_work_order_productions($woid); // check the produced quantity
469                 while ($row = db_fetch($result))
470                 {
471                         void_work_order_produce($row['id']);
472                         
473                         //Post voided entry if not prevoided explicitly
474                         $void_entry = get_voided_entry(ST_MANURECEIVE, $row['id']);
475                         if ($void_entry)
476                                 continue;
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_);
480                 }
481
482                 $result = get_work_order_issues($woid);
483                 $cost = 0;
484                 while ($row = db_fetch($result))
485                 {
486                         void_work_order_issue($row['issue_no']);
487                         
488                         //Post voided entry if not prevoided explicitly
489                         $void_entry = get_voided_entry(ST_MANUISSUE, $row['issue_no']);
490                         if ($void_entry)
491                                 continue;
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_);                              
495                 }
496
497                 //Adust avg labour cost
498                 $cost = get_gl_wo_cost($woid, WO_LABOUR); 
499                 if ($cost != 0)
500                         add_labour_cost($work_order['stock_id'], 1, $date, -$cost, true);
501                         
502                 //Adust avg overhead cost
503                 $cost = get_gl_wo_cost($woid, WO_OVERHEAD); 
504                 if ($cost != 0)
505                         add_overhead_cost($work_order['stock_id'], 1, $date, -$cost, true);
506                 
507                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
508                         .db_escape($woid);
509                 db_query($sql, "The work order couldn't be voided");
510
511                 // void all related stock moves
512                 void_stock_move(ST_WORKORDER, $woid);
513
514                 // void any related bank/gl trans
515                 void_bank_trans(ST_WORKORDER, $woid, true);
516
517                 // clear the requirements units received
518                 void_wo_requirements($woid);
519         }
520         commit_transaction();
521 }
522
523 function get_sql_for_work_orders($outstanding_only, $stock_id, $location = ALL_TEXT, $order_no='', $order_ref = '', $overdue = false)
524 {
525         $sql = "SELECT
526                 workorder.id,
527                 workorder.wo_ref,
528                 workorder.type,
529                 location.location_name,
530                 item.description,
531                 workorder.units_reqd,
532                 workorder.units_issued,
533                 workorder.date_,
534                 workorder.required_by,
535                 workorder.released_date,
536                 workorder.closed,
537                 workorder.released,
538                 workorder.stock_id,
539                 unit.decimals
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";
547
548         if (check_value('OpenOnly') || $outstanding_only != 0)
549         {
550                 $sql .= " AND workorder.closed=0";
551         }
552
553         if ($location != ALL_TEXT)
554         {
555                 $sql .= " AND workorder.loc_code=".db_escape($location);
556         }
557
558         if ($order_no != '')
559         {
560                 $sql .= " AND workorder.id LIKE ".db_escape('%'.$order_no.'%');
561         }
562
563         if ($order_ref != '')
564         {
565                 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$order_ref.'%');
566         }
567
568         if ($stock_id != ALL_TEXT)
569         {
570                 $sql .= " AND workorder.stock_id=".db_escape($stock_id);
571         }
572
573         if ($overdue)
574         {
575                 $Today = date2sql(Today());
576
577                 $sql .= " AND workorder.required_by < '$Today' ";
578         }
579         return $sql;
580 }
581
582 function get_sql_for_where_used($stock_id)
583 {
584         $sql = "SELECT 
585                         bom.parent,
586                         workcentre.name As WorkCentreName,
587                         location.location_name,
588                         bom.quantity,
589                         parent.description
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);
598         return $sql;
599 }
600 //--------------------------------------------------------------------------------------
601 function get_gl_wo_cost($woid, $cost_type)
602 {
603         $cost = 0;
604         $result = get_gl_wo_cost_trans($woid, $cost_type);
605         while ($row = db_fetch($result))
606                 $cost += -$row['amount'];
607         return $cost;   
608 }
609