Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / includes / db / manufacturing_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 function get_demand_qty($stock_id, $location)
14 {
15         $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - "
16                 .TB_PREF."sales_order_details.qty_sent) AS QtyDemand
17                         FROM ".TB_PREF."sales_order_details,
18                                         ".TB_PREF."sales_orders
19                                 WHERE ".TB_PREF."sales_order_details.order_no="
20                                 .TB_PREF."sales_orders.order_no AND ";
21         if ($location != "")
22                 $sql .= TB_PREF."sales_orders.from_stk_loc =".db_escape($location)." AND ";
23         $sql .= TB_PREF."sales_order_details.stk_code = ".db_escape($stock_id);
24
25     $result = db_query($sql,"No transactions were returned");
26         $row = db_fetch($result);
27         if ($row === false)
28                 return 0;
29         return $row['QtyDemand'];
30 }
31
32 $bom_list = array(); 
33 $qoh_stock = NULL;
34
35 function load_stock_levels($location)
36 {
37         global $qoh_stock;
38         $date = date2sql(Today());
39
40         $sql = "SELECT stock_id, SUM(qty) FROM ".TB_PREF."stock_moves WHERE tran_date <= '$date'";
41         if ($location != '') $sql .= " AND loc_code = ".db_escape($location);
42         $sql .= " GROUP BY stock_id";
43         $result = db_query($sql, "QOH calulcation failed");
44         while ($row = db_fetch($result)) {
45                 $qoh_stock[$row[0]] = $row[1];
46         }
47 }
48
49 // recursion fixed by Tom Moulton. Max 10 recursion levels.
50 function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level=0) 
51 {
52         global $bom_list, $qoh_stock;
53         $demand = 0.0;
54         if ($level > 10) {
55                 display_warning("BOM Too many Manufacturing levels deep $level");
56                 return $demand;
57         }
58         // Load all stock levels (stock moves) into $qoh_stock
59         if ($qoh_stock == NULL) {
60                 $qoh_stock = array();
61                 load_stock_levels($location);
62         }
63         $stock_qty = $qoh_stock[$stock_id];
64         if ($stock_qty == NULL) $stock_qty = 0;
65         if ($qty <= $stock_qty) return $demand;
66         $bom = $bom_list[$stock_id];
67         if ($bom == NULL) {
68                 $sql = "SELECT parent, component, quantity FROM "
69                         .TB_PREF."bom WHERE parent = ".db_escape($stock_id);
70                 if ($location != "") $sql .= " AND loc_code = ".db_escape($location);
71                 $result = db_query($sql, "Could not search bom");
72                 $bom = array();
73                 // Even if we get no results, remember that fact 
74                 $bom[] = array($stock_id, '', 0); 
75                 while ($row = db_fetch_row($result)) {
76                         $bom[] = array($row[0], $row[1], $row[2]);
77                 }
78                 db_free_result($result);
79                 $bom_list[$stock_id] = $bom;
80         }       
81         $len = count($bom);
82         $i = 0;
83         while ($i < $len) {
84                 $row = $bom[$i];
85                 $i++; 
86                 // Ignore the dummy entry
87                 if ($row[1] == '') continue;
88                 $q = $qty * $row[2];
89                 if ($row[1] == $demand_id) $demand += $q;
90                 $demand += stock_demand_manufacture($row[1], $q, $demand_id, $location, $level+1);
91         }
92         return $demand;
93 }
94
95 // recursion fixed by Tom Moulton
96 function get_demand_asm_qty($stock_id, $location) 
97 {
98         $demand_qty = 0.0;
99         $sql = "SELECT ".TB_PREF."sales_order_details.stk_code, SUM(".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)
100                                    AS Demmand
101                                    FROM ".TB_PREF."sales_order_details,
102                                                 ".TB_PREF."sales_orders,
103                                                 ".TB_PREF."stock_master
104                                    WHERE ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
105         if ($location != "")
106                 $sql .= TB_PREF."sales_orders.from_stk_loc =".db_escape($location)." AND ";
107         $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
108                                    ".TB_PREF."stock_master.stock_id=".TB_PREF."sales_order_details.stk_code AND
109                                    (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')
110                                    GROUP BY ".TB_PREF."sales_order_details.stk_code";
111     $result = db_query($sql, "No transactions were returned");
112         while ($row = db_fetch_row($result)) {
113                 $demand_qty += stock_demand_manufacture($row[0], $row[1], $stock_id, $location);
114         }
115         return $demand_qty;
116 }
117
118 function get_on_porder_qty($stock_id, $location)
119 {
120         $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - "
121                 .TB_PREF."purch_order_details.quantity_received) AS qoo
122                 FROM ".TB_PREF."purch_order_details INNER JOIN "
123                         .TB_PREF."purch_orders ON ".TB_PREF."purch_order_details.order_no=".TB_PREF."purch_orders.order_no
124                 WHERE ".TB_PREF."purch_order_details.item_code=".db_escape($stock_id)." ";
125         if ($location != "")
126                 $sql .= "AND ".TB_PREF."purch_orders.into_stock_location=".db_escape($location)." ";
127         $sql .= "AND ".TB_PREF."purch_order_details.item_code=".db_escape($stock_id);
128         $qoo_result = db_query($sql,"could not receive quantity on order for item");
129
130         if (db_num_rows($qoo_result) == 1)
131         {
132                 $qoo_row = db_fetch_row($qoo_result);
133                 $qoo =  $qoo_row[0];
134         }
135         else
136         {
137                 $qoo = 0;
138         }
139         return $qoo;
140 }
141
142 function get_on_worder_qty($stock_id, $location)
143 {
144         $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued) * 
145                 (".TB_PREF."wo_requirements.units_req-".TB_PREF."wo_requirements.units_issued)) AS qoo
146                 FROM ".TB_PREF."wo_requirements INNER JOIN ".TB_PREF."workorders 
147                         ON ".TB_PREF."wo_requirements.workorder_id=".TB_PREF."workorders.id
148                 WHERE ".TB_PREF."wo_requirements.stock_id=".db_escape($stock_id)." ";
149         if ($location != "")
150                 $sql .= "AND ".TB_PREF."wo_requirements.loc_code=".db_escape($location)." ";
151         $sql .= "AND ".TB_PREF."workorders.released=1";
152         $qoo_result = db_query($sql,"could not receive quantity on order for item");
153         if (db_num_rows($qoo_result) == 1)
154         {
155                 $qoo_row = db_fetch_row($qoo_result);
156                 $qoo =  $qoo_row[0];
157         }
158         else
159                 $qoo = 0.0;
160         $flag = get_mb_flag($stock_id);
161         if ($flag == 'A' || $flag == 'M')
162         {
163                 $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued)) AS qoo
164                         FROM ".TB_PREF."workorders 
165                         WHERE ".TB_PREF."workorders.stock_id=".db_escape($stock_id)." ";
166                 if ($location != "")    
167                         $sql .= "AND ".TB_PREF."workorders.loc_code=".db_escape($location)." ";
168                 $sql .= "AND ".TB_PREF."workorders.released=1";
169                 $qoo_result = db_query($sql,"could not receive quantity on order for item");
170                 if (db_num_rows($qoo_result) == 1)
171                 {
172                         $qoo_row = db_fetch_row($qoo_result);
173                         $qoo +=  $qoo_row[0];
174                 }
175         }
176         return $qoo;
177 }
178
179 function get_mb_flag($stock_id)
180 {
181         $sql = "SELECT mb_flag FROM ".TB_PREF."stock_master WHERE stock_id = "
182                 .db_escape($stock_id);
183         $result = db_query($sql, "retreive mb_flag from item");
184         
185         if (db_num_rows($result) == 0)
186                 return -1;
187
188         $myrow = db_fetch_row($result);
189         return $myrow[0];
190 }
191
192 //--------------------------------------------------------------------------------------
193
194 function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
195 {
196         $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
197                 VALUES (".db_escape($selected_parent).", ".db_escape($component) . ","
198                 .db_escape($workcentre_added) . ", ".db_escape($loc_code) . ", "
199                 . $quantity . ")";
200
201         db_query($sql,"check failed");
202 }
203 //--------------------------------------------------------------------------------------
204
205 function update_bom($selected_parent, $selected_component, $work_centre_added, $loc_code, $quantity)
206 {
207         $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($workcentre_added)
208          . ",loc_code=".db_escape($loc_code) . ",
209                 quantity= " . $quantity . "
210                 WHERE parent=".db_escape($selected_parent) . "
211                 AND id=".db_escape($selected_component);
212         check_db_error("Could not update this bom component", $sql);
213
214         db_query($sql,"could not update bom");
215 }
216         
217 function delete_bom($selected_id)
218 {
219         $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
220         db_query($sql,"Could not delete this bom components");
221 }
222
223 function get_bom($item)
224 {
225         $sql = "SELECT ".TB_PREF."bom.*, ".TB_PREF."locations.location_name, ".TB_PREF."workcentres.name AS WorkCentreDescription, 
226         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.mb_flag AS ResourceType, 
227         ".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+".TB_PREF."stock_master.overhead_cost AS standard_cost, units, 
228         ".TB_PREF."bom.quantity * (".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+ ".TB_PREF."stock_master.overhead_cost) AS ComponentCost 
229         FROM (".TB_PREF."workcentres, ".TB_PREF."locations, ".TB_PREF."bom) INNER JOIN ".TB_PREF."stock_master ON ".TB_PREF."bom.component = ".TB_PREF."stock_master.stock_id 
230         WHERE ".TB_PREF."bom.parent = ".db_escape($item)."
231                 AND ".TB_PREF."workcentres.id=".TB_PREF."bom.workcentre_added
232                 AND ".TB_PREF."bom.loc_code = ".TB_PREF."locations.loc_code ORDER BY ".TB_PREF."bom.id";
233         
234         return db_query($sql, "The bill of material could not be retrieved");
235 }
236
237 //--------------------------------------------------------------------------------------
238
239 function get_component_from_bom($selected_id)
240 {
241         $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM "
242                 .TB_PREF."bom,".TB_PREF."stock_master
243                 WHERE id=".db_escape($selected_id)."
244                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
245
246         $result = db_query($sql, "could not get bom");
247         return db_fetch($result);
248 }
249 //--------------------------------------------------------------------------------------
250
251 function has_bom($item)
252 {
253     $result = get_bom($item);
254     
255     return (db_num_rows($result) != 0);
256 }
257
258 //--------------------------------------------------------------------------------------
259
260 function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
261 {
262         $sql = "SELECT component FROM ".TB_PREF."bom
263                 WHERE parent=".db_escape($selected_parent)."
264                 AND component=".db_escape($component) . "
265                 AND workcentre_added=".db_escape($workcentre_added) . "
266                 AND loc_code=".db_escape($loc_code);
267         $result = db_query($sql,"check failed");
268
269         return (db_num_rows($result) > 0);
270 }
271
272 //--------------------------------------------------------------------------------------
273
274 function check_for_recursive_bom($ultimate_parent, $component_to_check)
275 {
276
277         /* returns true ie 1 if the bom contains the parent part as a component
278         ie the bom is recursive otherwise false ie 0 */
279
280         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
281         $result = db_query($sql,"could not check recursive bom");
282
283         if ($result != 0)
284         {
285                 while ($myrow = db_fetch_row($result))
286                 {
287                         if ($myrow[0] == $ultimate_parent)
288                         {
289                                 return 1;
290                         }
291
292                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
293                         {
294                                 return 1;
295                         }
296                 } //(while loop)
297         } //end if $result is true
298
299         return 0;
300 } //end of function check_for_recursive_bom
301
302 ?>