Small changes to get_demand_asm_qty
[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 - ".TB_PREF."sales_order_details.qty_sent) AS QtyDemand
16                                 FROM ".TB_PREF."sales_order_details,
17                                         ".TB_PREF."sales_orders
18                                 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND ";
19         if ($location != "")
20                 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
21         $sql .= TB_PREF."sales_order_details.stk_code = '$stock_id'";
22
23     $result = db_query($sql,"No transactions were returned");
24         $row = db_fetch($result);
25         return $row['QtyDemand'];
26 }
27
28 $bom_list = array(); 
29 $qoh_stock = NULL;
30
31 function load_stock_levels($location)
32 {
33         global $qoh_stock;
34         $date = date2sql(Today());
35
36         $sql = "SELECT stock_id, SUM(qty) FROM ".TB_PREF."stock_moves WHERE tran_date <= '$date'";
37         if ($location != null) $sql .= " AND loc_code = '$location'";
38         $sql .= " GROUP BY stock_id";
39         $result = db_query($sql, "QOH calulcation failed");
40         while ($row = db_fetch($result)) {
41                 $qoh_stock[$row[0]] = $row[1];
42         }
43 }
44
45 // recursion fixed by Tom Moulton. Max 10 recursion levels.
46 function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level=0) 
47 {
48         global $bom_list, $qoh_stock;
49         $demand = 0.0;
50         if ($level > 10) {
51                 display_warning("BOM Too many Manufacturing levels deep $level");
52                 return $demand;
53         }
54         // Load all stock levels (stock moves) into $qoh_stock
55         if ($qoh_stock == NULL) {
56                 $qoh_stock = array();
57                 load_stock_levels($location);
58         }
59         $stock_qty = $qoh_stock[$stock_id];
60         if ($stock_qty == NULL) $stock_qty = 0;
61         if ($qty < $stock_qty) return $demand;
62         $bom = $bom_list[$stock_id];
63         if ($bom == NULL) {
64                 $sql = "SELECT parent, component, quantity FROM ".TB_PREF."bom WHERE parent = '$stock_id'";
65                 if ($location != '') $sql .= " AND loc_code = '$location'";
66                 $result = db_query($sql, "Could not search bom");
67                 $bom = array();
68                 // Even if we get no results, remember that fact 
69                 $bom[] = array($stock_id, '', 0); 
70                 while ($row = db_fetch_row($result)) {
71                         $bom[] = array($row[0], $row[1], $row[2]);
72                 }
73                 db_free_result($result);
74                 $bom_list[$stock_id] = $bom;
75         }       
76         $len = count($bom);
77         $i = 0;
78         while ($i < $len) {
79                 $row = $bom[$i];
80                 $i++; 
81                 // Ignore the dummy entry
82                 if ($row[1] == '') continue;
83                 $q = $qty * $row[2];
84                 if ($row[1] == $demand_id) $demand += $q;
85                 $demand += stock_demand_manufacture($row[1], $q, $demand_id, $location, $level+1);
86         }
87         return $demand;
88 }
89
90 // recursion fixed by Tom Moulton
91 function get_demand_asm_qty($stock_id, $location) 
92 {
93         $demand_qty = 0.0;
94         $sql = "SELECT ".TB_PREF."sales_order_details.stk_code, SUM(".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)
95                                    AS Demmand
96                                    FROM ".TB_PREF."sales_order_details,
97                                                 ".TB_PREF."sales_orders,
98                                                 ".TB_PREF."stock_master
99                                    WHERE ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
100         if ($location != "")
101                 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
102         $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
103                                    ".TB_PREF."stock_master.stock_id=".TB_PREF."sales_order_details.stk_code AND
104                                    (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')
105                                    GROUP BY ".TB_PREF."sales_order_details.stk_code";
106     $result = db_query($sql, "No transactions were returned");
107         while ($row = db_fetch_row($result)) {
108                 $demand_qty += stock_demand_manufacture($row[0], $row[1], $stock_id, $location);
109         }
110         return $demand_qty;
111 }
112
113 function get_on_porder_qty($stock_id, $location)
114 {
115         $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS qoo
116                 FROM ".TB_PREF."purch_order_details INNER JOIN ".TB_PREF."purch_orders ON ".TB_PREF."purch_order_details.order_no=".TB_PREF."purch_orders.order_no
117                 WHERE ".TB_PREF."purch_order_details.item_code='$stock_id' ";
118         if ($location != "")
119                 $sql .= "AND ".TB_PREF."purch_orders.into_stock_location='$location' ";
120         $sql .= "AND ".TB_PREF."purch_order_details.item_code='$stock_id'";
121         $qoo_result = db_query($sql,"could not receive quantity on order for item");
122
123         if (db_num_rows($qoo_result) == 1)
124         {
125                 $qoo_row = db_fetch_row($qoo_result);
126                 $qoo =  $qoo_row[0];
127         }
128         else
129         {
130                 $qoo = 0;
131         }
132         return $qoo;
133 }
134
135 function get_on_worder_qty($stock_id, $location)
136 {
137         $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued) * 
138                 (".TB_PREF."wo_requirements.units_req-".TB_PREF."wo_requirements.units_issued)) AS qoo
139                 FROM ".TB_PREF."wo_requirements INNER JOIN ".TB_PREF."workorders 
140                         ON ".TB_PREF."wo_requirements.workorder_id=".TB_PREF."workorders.id
141                 WHERE ".TB_PREF."wo_requirements.stock_id='$stock_id' ";
142         if ($location != "")
143                 $sql .= "AND ".TB_PREF."wo_requirements.loc_code='$location' ";
144         $sql .= "AND ".TB_PREF."workorders.released=1";
145         $qoo_result = db_query($sql,"could not receive quantity on order for item");
146         if (db_num_rows($qoo_result) == 1)
147         {
148                 $qoo_row = db_fetch_row($qoo_result);
149                 $qoo =  $qoo_row[0];
150         }
151         else
152                 $qoo = 0.0;
153         $flag = get_mb_flag($stock_id);
154         if ($flag == 'A' || $flag == 'M')
155         {
156                 $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued)) AS qoo
157                         FROM ".TB_PREF."workorders 
158                         WHERE ".TB_PREF."workorders.stock_id='$stock_id' ";
159                 if ($location != "")    
160                         $sql .= "AND ".TB_PREF."workorders.loc_code='$location' ";
161                 $sql .= "AND ".TB_PREF."workorders.released=1";
162                 $qoo_result = db_query($sql,"could not receive quantity on order for item");
163                 if (db_num_rows($qoo_result) == 1)
164                 {
165                         $qoo_row = db_fetch_row($qoo_result);
166                         $qoo +=  $qoo_row[0];
167                 }
168         }
169         return $qoo;
170 }
171
172 function get_mb_flag($stock_id)
173 {
174         $sql = "SELECT mb_flag FROM ".TB_PREF."stock_master WHERE stock_id = '" . $stock_id . "'";
175         $result = db_query($sql, "retreive mb_flag from item");
176         
177         if (db_num_rows($result) == 0)
178                 return -1;
179
180         $myrow = db_fetch_row($result);
181         return $myrow[0];
182 }
183
184 //--------------------------------------------------------------------------------------
185
186 function get_bom($item)
187 {
188         $sql = "SELECT ".TB_PREF."bom.*, ".TB_PREF."locations.location_name, ".TB_PREF."workcentres.name AS WorkCentreDescription, 
189         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.mb_flag AS ResourceType, 
190         ".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+".TB_PREF."stock_master.overhead_cost AS standard_cost, units, 
191         ".TB_PREF."bom.quantity * (".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+ ".TB_PREF."stock_master.overhead_cost) AS ComponentCost 
192         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 
193         WHERE ".TB_PREF."bom.parent = '" . $item . "'
194                 AND ".TB_PREF."workcentres.id=".TB_PREF."bom.workcentre_added
195                 AND ".TB_PREF."bom.loc_code = ".TB_PREF."locations.loc_code ORDER BY ".TB_PREF."bom.id";
196         
197         return db_query($sql, "The bill of material could not be retrieved");
198 }
199
200 //--------------------------------------------------------------------------------------
201
202 function has_bom($item)
203 {
204     $result = get_bom($item);
205     
206     return (db_num_rows($result) != 0);
207 }
208
209 //--------------------------------------------------------------------------------------
210
211 ?>