Moved all SQL statements from PHP files into relevant *_db.inc files.
[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         $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
24                 .db_escape($stock_id);
25         $result = db_query($sql);
26         $myrow = db_fetch($result);
27         $material_cost =  $myrow['material_cost'];
28         $qoh = get_qoh_on_date($stock_id, null, $date_);
29         if ($qoh + $qty <= 0)
30                 $material_cost = 0;
31         else
32                 $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
33         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
34                 WHERE stock_id=".db_escape($stock_id);
35         db_query($sql,"The cost details for the inventory item could not be updated");
36 }
37
38 function add_overhead_cost($stock_id, $qty, $date_, $costs)
39 {
40         if ($qty != 0)
41                 $costs /= $qty;
42         $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
43                 .db_escape($stock_id);
44         $result = db_query($sql);
45         $myrow = db_fetch($result);
46         $overhead_cost =  $myrow['overhead_cost'];
47         $qoh = get_qoh_on_date($stock_id, null, $date_);
48         if ($qoh + $qty <= 0)
49                 $overhead_cost = 0;
50         else
51                 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
52         $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
53                 WHERE stock_id=".db_escape($stock_id);
54         db_query($sql,"The cost details for the inventory item could not be updated");
55 }
56
57 function add_labour_cost($stock_id, $qty, $date_, $costs)
58 {
59         if ($qty != 0)
60                 $costs /= $qty;
61         $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
62                 .db_escape($stock_id);
63         $result = db_query($sql);
64         $myrow = db_fetch($result);
65         $labour_cost =  $myrow['labour_cost'];
66         $qoh = get_qoh_on_date($stock_id, null, $date_);
67         if ($qoh + $qty <= 0)
68                 $labour_cost = 0;
69         else
70                 $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
71         $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
72                 WHERE stock_id=".db_escape($stock_id);
73         db_query($sql,"The cost details for the inventory item could not be updated");
74 }
75
76 function add_issue_cost($stock_id, $qty, $date_, $costs)
77 {
78         if ($qty != 0)
79                 $costs /= $qty;
80         $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
81                 .db_escape($stock_id);
82         $result = db_query($sql);
83         $myrow = db_fetch($result);
84         $material_cost =  $myrow['material_cost'];
85         $qoh = get_qoh_on_date($stock_id, null, $date_);
86         if ($qoh + $qty  <= 0)
87                 $material_cost = 0;
88         else
89                 $material_cost = ($qty * $costs) /      ($qoh + $qty);
90         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
91                 .db_escape($material_cost)
92                 ." WHERE stock_id=".db_escape($stock_id);
93         db_query($sql,"The cost details for the inventory item could not be updated");
94 }
95
96 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
97         $type, $date_, $required_by, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
98 {
99         global $Refs;
100
101         if (!($type == WO_ADVANCED))
102                 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
103
104         begin_transaction();
105
106         add_material_cost($stock_id, $units_reqd, $date_);
107
108         $date = date2sql($date_);
109         $required = date2sql($required_by);
110
111         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
112                 type, date_, required_by)
113         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
114         .db_escape($units_reqd).", ".db_escape($stock_id).",
115                 ".db_escape($type).", '$date', ".db_escape($required).")";
116         db_query($sql, "could not add work order");
117
118         $woid = db_insert_id();
119
120         add_comments(ST_WORKORDER, $woid, $required_by, $memo_);
121
122         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
123         add_audit_trail(ST_WORKORDER, $woid, $date_);
124
125         commit_transaction();
126
127         return $woid;
128 }
129
130 //--------------------------------------------------------------------------------------
131
132 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
133                                         $date_, $required_by, $memo_)
134 {
135         begin_transaction();
136
137         add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
138         add_material_cost($stock_id, $units_reqd, $date_);
139
140         $date = date2sql($date_);
141         $required = date2sql($required_by);
142
143         $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
144                 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
145                 required_by=".db_escape($required).",
146                 date_='$date'
147                 WHERE id = ".db_escape($woid);
148
149         db_query($sql, "could not update work order");
150
151         update_comments(ST_WORKORDER, $woid, null, $memo_);
152         add_audit_trail(ST_WORKORDER, $woid, $date_, _("Updated."));
153
154         commit_transaction();
155 }
156
157 function delete_work_order($woid)
158 {
159         begin_transaction();
160
161         add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
162
163         // delete the work order requirements
164         delete_wo_requirements($woid);
165
166         // delete the actual work order
167         $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
168         db_query($sql,"The work order could not be deleted");
169
170         delete_comments(ST_WORKORDER, $woid);
171         add_audit_trail(ST_WORKORDER, $woid, $_POST['date_'], _("Canceled."));
172
173         commit_transaction();
174 }
175
176 //--------------------------------------------------------------------------------------
177
178 function get_work_order($woid, $allow_null=false)
179 {
180     $sql = "SELECT ".TB_PREF."workorders.*, ".TB_PREF."stock_master.description As StockItemName,
181                 ".TB_PREF."locations.location_name, ".TB_PREF."locations.delivery_address 
182                 FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations
183                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
184                 AND     ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code
185                 AND ".TB_PREF."workorders.id=".db_escape($woid)."
186                 GROUP BY ".TB_PREF."workorders.id";
187
188         $result = db_query($sql, "The work order issues could not be retrieved");
189
190         if (!$allow_null && db_num_rows($result) == 0)
191                 display_db_error("Could not find work order $woid", $sql);
192
193         return db_fetch($result);
194 }
195
196 //--------------------------------------------------------------------------------------
197
198 function work_order_has_productions($woid)
199 {
200         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
201         $result = db_query($sql, "query work order for productions");
202
203         $myrow = db_fetch_row($result);
204         return ($myrow[0] > 0);
205 }
206
207
208 //--------------------------------------------------------------------------------------
209
210 function work_order_has_issues($woid)
211 {
212         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
213         $result = db_query($sql, "query work order for issues");
214
215         $myrow = db_fetch_row($result);
216         return ($myrow[0] > 0);
217 }
218
219 //--------------------------------------------------------------------------------------
220
221 function work_order_has_payments($woid)
222 {
223         $result = get_gl_wo_cost_trans($woid);
224
225     return (db_num_rows($result) != 0);
226 }
227
228 //--------------------------------------------------------------------------------------
229
230 function release_work_order($woid, $releaseDate, $memo_)
231 {
232         begin_transaction();
233
234         $myrow = get_work_order($woid);
235         $stock_id = $myrow["stock_id"];
236
237         $date = date2sql($releaseDate);
238
239         $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
240                 released=1 WHERE id = ".db_escape($woid);
241         db_query($sql, "could not release work order");
242
243         // create Work Order Requirements based on the bom
244         create_wo_requirements($woid, $stock_id);
245
246         add_comments(ST_WORKORDER, $woid, $releaseDate, $memo_);
247         add_audit_trail(ST_WORKORDER, $woid, $releaseDate,_("Released."));
248
249         commit_transaction();
250 }
251
252 //--------------------------------------------------------------------------------------
253
254 function close_work_order($woid)
255 {
256         $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
257         db_query($sql, "could not close work order");
258 }
259
260 //--------------------------------------------------------------------------------------
261
262 function work_order_is_closed($woid)
263 {
264         $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
265         $result = db_query($sql, "could not query work order");
266         $row = db_fetch_row($result);
267         return ($row[0] > 0);
268 }
269
270 //--------------------------------------------------------------------------------------
271
272 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
273 {
274         $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
275                 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
276                 WHERE id = ".db_escape($woid);
277
278         db_query($sql, "The work order issued quantity couldn't be updated");
279 }
280
281 //--------------------------------------------------------------------------------------
282
283 function void_work_order($woid)
284 {
285         $work_order = get_work_order($woid);
286
287         if (!($work_order["type"] == WO_ADVANCED))
288         {
289                 begin_transaction();
290
291                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = "
292                         .db_escape($woid);
293                 db_query($sql, "The work order couldn't be voided");
294
295                 // void all related stock moves
296                 void_stock_move(ST_WORKORDER, $woid);
297
298                 // void any related gl trans
299                 void_gl_trans(ST_WORKORDER, $woid, true);
300
301                 // clear the requirements units received
302                 void_wo_requirements($woid);
303
304                 commit_transaction();
305         }
306         else
307         {
308                 // void everything inside the work order : issues, productions, payments
309         }
310 }
311
312 function get_sql_for_work_orders($outstanding_only, $all_items)
313 {
314         $sql = "SELECT
315                 workorder.id,
316                 workorder.wo_ref,
317                 workorder.type,
318                 location.location_name,
319                 item.description,
320                 workorder.units_reqd,
321                 workorder.units_issued,
322                 workorder.date_,
323                 workorder.required_by,
324                 workorder.released_date,
325                 workorder.closed,
326                 workorder.released,
327                 workorder.stock_id,
328                 unit.decimals
329                 FROM ".TB_PREF."workorders as workorder,"
330                         .TB_PREF."stock_master as item,"
331                         .TB_PREF."item_units as unit,"
332                         .TB_PREF."locations as location
333                 WHERE workorder.stock_id=item.stock_id 
334                         AND workorder.loc_code=location.loc_code
335                         AND item.units=unit.abbr";
336
337         if (check_value('OpenOnly') || $outstanding_only != 0)
338         {
339                 $sql .= " AND workorder.closed=0";
340         }
341
342         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
343         {
344                 $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
345         }
346
347         if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
348         {
349                 $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
350         }
351
352         if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
353         {
354                 $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
355         }
356
357         if (check_value('OverdueOnly'))
358         {
359                 $Today = date2sql(Today());
360
361                 $sql .= " AND workorder.required_by < '$Today' ";
362         }
363         return $sql;
364 }
365
366 function get_sql_for_where_used()
367 {
368         $sql = "SELECT 
369                         bom.parent,
370                         workcentre.name As WorkCentreName,
371                         location.location_name,
372                         bom.quantity,
373                         parent.description
374                         FROM ".TB_PREF."bom as bom, "
375                                 .TB_PREF."stock_master as parent, "
376                                 .TB_PREF."workcentres as workcentre, "
377                                 .TB_PREF."locations as location
378                         WHERE bom.parent = parent.stock_id 
379                                 AND bom.workcentre_added = workcentre.id
380                                 AND bom.loc_code = location.loc_code
381                                 AND bom.component=".db_escape($_POST['stock_id']);
382         return $sql;                    
383 }
384 //--------------------------------------------------------------------------------------
385
386 ?>