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