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