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