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