Stock Status display, Manufacturing Reports: fixed products on WO count.
[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         $qoo = 0.0;
153         $flag = get_mb_flag($stock_id);
154         if ($flag == 'M')
155         {
156                 $sql = "SELECT SUM((units_reqd-units_issued)) AS qoo
157                         FROM ".TB_PREF."workorders
158                         WHERE stock_id=".db_escape($stock_id)
159                                 ." AND released AND NOT closed";
160
161                 if ($location != "")
162                         $sql .= " AND loc_code=".db_escape($location);
163
164                 $qoo_result = db_query($sql,"could not receive quantity on order for item");
165                 if (db_num_rows($qoo_result) == 1)
166                 {
167                         $qoo_row = db_fetch_row($qoo_result);
168                         $qoo = $qoo_row[0];
169                 }
170         }
171         return $qoo;
172 }
173
174 //--------------------------------------------------------------------------------------
175
176 function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
177 {
178         $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
179                 VALUES (".db_escape($selected_parent).", ".db_escape($component) . ","
180                 .db_escape($workcentre_added) . ", ".db_escape($loc_code) . ", "
181                 . $quantity . ")";
182
183         db_query($sql,"check failed");
184 }
185 //--------------------------------------------------------------------------------------
186
187 function update_bom($selected_parent, $selected_component, $workcentre_added, $loc_code, $quantity)
188 {
189         $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($workcentre_added)
190          . ",loc_code=".db_escape($loc_code) . ",
191                 quantity= " . $quantity . "
192                 WHERE parent=".db_escape($selected_parent) . "
193                 AND id=".db_escape($selected_component);
194         check_db_error("Could not update this bom component", $sql);
195
196         db_query($sql,"could not update bom");
197 }
198         
199 function delete_bom($selected_id)
200 {
201         $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
202         db_query($sql,"Could not delete this bom components");
203 }
204
205 function get_bom($item)
206 {
207         $sql = "SELECT bom.*, loc.location_name,
208                 centre.name AS WorkCentreDescription,
209         item.description, item.mb_flag AS ResourceType, 
210         item.material_cost AS ProductCost, units,
211         bom.quantity * item.material_cost AS ComponentCost 
212         FROM ".TB_PREF."workcentres centre,
213                 ".TB_PREF."locations loc,
214                 ".TB_PREF."bom bom
215                         INNER JOIN ".TB_PREF."stock_master item ON bom.component = item.stock_id 
216         WHERE bom.parent = ".db_escape($item)."
217                 AND centre.id=bom.workcentre_added
218                 AND bom.loc_code = loc.loc_code ORDER BY bom.id";
219         
220         return db_query($sql, "The bill of material could not be retrieved");
221 }
222
223 //--------------------------------------------------------------------------------------
224
225 function get_component_from_bom($selected_id)
226 {
227         $sql = "SELECT bom.*, item.description
228                 FROM ".TB_PREF."bom bom,"
229                         .TB_PREF."stock_master item
230                 WHERE id=".db_escape($selected_id)."
231                 AND item.stock_id=bom.component";
232
233         $result = db_query($sql, "could not get bom");
234         return db_fetch($result);
235 }
236 //--------------------------------------------------------------------------------------
237
238 function has_bom($item)
239 {
240     $result = get_bom($item);
241     
242     return (db_num_rows($result) != 0);
243 }
244
245 //--------------------------------------------------------------------------------------
246
247 function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
248 {
249         $sql = "SELECT component
250                 FROM ".TB_PREF."bom
251                 WHERE parent=".db_escape($selected_parent)."
252                 AND component=".db_escape($component) . "
253                 AND workcentre_added=".db_escape($workcentre_added) . "
254                 AND loc_code=".db_escape($loc_code);
255         $result = db_query($sql,"check failed");
256
257         return (db_num_rows($result) > 0);
258 }
259
260 //--------------------------------------------------------------------------------------
261
262 function check_for_recursive_bom($ultimate_parent, $component_to_check)
263 {
264
265         /* returns true ie 1 if the bom contains the parent part as a component
266         ie the bom is recursive otherwise false ie 0 */
267
268         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
269         $result = db_query($sql,"could not check recursive bom");
270
271         if ($result)
272         {
273                 while ($myrow = db_fetch_row($result))
274                 {
275                         if ($myrow[0] == $ultimate_parent)
276                         {
277                                 return 1;
278                         }
279
280                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
281                         {
282                                 return 1;
283                         }
284                 }
285         }
286
287         return 0;
288 }
289