A couple of small bugs were fixed in average material cost
[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         $m_cost = 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 + $qty <= 0)
32                 $material_cost = 0;
33         else
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         $costs = 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 + $qty <= 0)
54                 $overhead_cost = 0;
55         else
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         $costs = 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 + $qty <= 0)
76                 $labour_cost = 0;
77         else
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         $material_cost = price_decimal_format($material_cost, $dec); 
96         $qoh = get_qoh_on_date($stock_id, null, $date_);
97         if ($qoh + $qty  <= 0)
98                 $material_cost = 0;
99         else
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         $work_order = get_work_order($woid);
298
299         if (!($work_order["type"] == WO_ADVANCED))
300         {
301                 begin_transaction();
302
303                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = "
304                         .db_escape($woid);
305                 db_query($sql, "The work order couldn't be voided");
306
307                 // void all related stock moves
308                 void_stock_move(ST_WORKORDER, $woid);
309
310                 // void any related gl trans
311                 void_gl_trans(ST_WORKORDER, $woid, true);
312
313                 // clear the requirements units received
314                 void_wo_requirements($woid);
315
316                 commit_transaction();
317         }
318         else
319         {
320                 // void everything inside the work order : issues, productions, payments
321         }
322 }
323
324 //--------------------------------------------------------------------------------------
325
326 ?>