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