Security statements update against sql injection attacks.
[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         if (!($type == wo_types::advanced()))
100                 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc);
101
102         begin_transaction();
103
104         add_material_cost($stock_id, $units_reqd, $date_);
105
106         $date = date2sql($date_);
107         $required = date2sql($required_by);
108
109         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
110                 type, date_, required_by)
111         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", "
112         .db_escape($units_reqd).", ".db_escape($stock_id).",
113                 ".db_escape($type).", '$date', ".db_escape($required).")";
114         db_query($sql, "could not add work order");
115
116         $woid = db_insert_id();
117
118         add_comments(systypes::work_order(), $woid, $required_by, $memo_);
119
120         references::save_last($wo_ref, systypes::work_order());
121
122         commit_transaction();
123
124         return $woid;
125 }
126
127 //--------------------------------------------------------------------------------------
128
129 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
130                                         $date_, $required_by, $memo_)
131 {
132         begin_transaction();
133
134         add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
135         add_material_cost($stock_id, $units_reqd, $date_);
136
137         $date = date2sql($date_);
138         $required = date2sql($required_by);
139
140         $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
141                 units_reqd=".db_escape($units_reqd).", stock_id=".db_escape($stock_id).",
142                 required_by=".db_escape($required).",
143                 date_='$date'
144                 WHERE id = ".db_escape($woid);
145
146         db_query($sql, "could not update work order");
147
148         update_comments(systypes::work_order(), $woid, null, $memo_);
149
150         commit_transaction();
151 }
152
153 function delete_work_order($woid)
154 {
155         begin_transaction();
156
157         add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
158
159         // delete the work order requirements
160         delete_wo_requirements($woid);
161
162         // delete the actual work order
163         $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=".db_escape($woid);
164         db_query($sql,"The work order could not be deleted");
165
166         delete_comments(systypes::work_order(), $woid);
167
168         commit_transaction();
169 }
170
171 //--------------------------------------------------------------------------------------
172
173 function get_work_order($woid, $allow_null=false)
174 {
175     $sql = "SELECT ".TB_PREF."workorders.*, ".TB_PREF."stock_master.description As StockItemName,
176                 ".TB_PREF."locations.location_name
177                 FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations
178                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
179                 AND     ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code
180                 AND ".TB_PREF."workorders.id=".db_escape($woid)."
181                 GROUP BY ".TB_PREF."workorders.id";
182
183         $result = db_query($sql, "The work order issues could not be retrieved");
184
185         if (!$allow_null && db_num_rows($result) == 0)
186                 display_db_error("Could not find work order $woid", $sql);
187
188         return db_fetch($result);
189 }
190
191 //--------------------------------------------------------------------------------------
192
193 function work_order_has_productions($woid)
194 {
195         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=".db_escape($woid);
196         $result = db_query($sql, "query work order for productions");
197
198         $myrow = db_fetch_row($result);
199         return ($myrow[0] > 0);
200 }
201
202
203 //--------------------------------------------------------------------------------------
204
205 function work_order_has_issues($woid)
206 {
207         $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid);
208         $result = db_query($sql, "query work order for issues");
209
210         $myrow = db_fetch_row($result);
211         return ($myrow[0] > 0);
212 }
213
214 //--------------------------------------------------------------------------------------
215
216 function work_order_has_payments($woid)
217 {
218         $result = get_gl_wo_cost_trans($woid);
219
220     return (db_num_rows($result) != 0);
221 }
222
223 //--------------------------------------------------------------------------------------
224
225 function release_work_order($woid, $releaseDate, $memo_)
226 {
227         begin_transaction();
228
229         $myrow = get_work_order($woid);
230         $stock_id = $myrow["stock_id"];
231
232         $date = date2sql($releaseDate);
233
234         $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
235                 released=1 WHERE id = ".db_escape($woid);
236         db_query($sql, "could not release work order");
237
238         // create Work Order Requirements based on the bom
239         create_wo_requirements($woid, $stock_id);
240
241         add_comments(systypes::work_order(), $woid, $releaseDate, $memo_);
242
243         commit_transaction();
244 }
245
246 //--------------------------------------------------------------------------------------
247
248 function close_work_order($woid)
249 {
250         $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = ".db_escape($woid);
251         db_query($sql, "could not close work order");
252 }
253
254 //--------------------------------------------------------------------------------------
255
256 function work_order_is_closed($woid)
257 {
258         $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = ".db_escape($woid);
259         $result = db_query($sql, "could not query work order");
260         $row = db_fetch_row($result);
261         return ($row[0] > 0);
262 }
263
264 //--------------------------------------------------------------------------------------
265
266 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
267 {
268         $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity).",
269                 closed = ((units_issued >= units_reqd) OR ".db_escape($force_close).")
270                 WHERE id = ".db_escape($woid);
271
272         db_query($sql, "The work order issued quantity couldn't be updated");
273 }
274
275 //--------------------------------------------------------------------------------------
276
277 function void_work_order($woid)
278 {
279         $work_order = get_work_order($woid);
280
281         if (!($work_order["type"] == wo_types::advanced()))
282         {
283                 begin_transaction();
284
285                 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = "
286                         .db_escape($woid);
287                 db_query($sql, "The work order couldn't be voided");
288
289                 // void all related stock moves
290                 void_stock_move(systypes::work_order(), $woid);
291
292                 // void any related gl trans
293                 void_gl_trans(systypes::work_order(), $woid, true);
294
295                 // clear the requirements units received
296                 void_wo_requirements($woid);
297
298                 commit_transaction();
299         }
300         else
301         {
302                 // void everything inside the work order : issues, productions, payments
303         }
304 }
305
306 //--------------------------------------------------------------------------------------
307
308 ?>