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