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)
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);
22 $sql .= " AND sorder.from_stk_loc =".db_escape($location);
24 $result = db_query($sql,"No transactions were returned");
25 $row = db_fetch($result);
28 return $row['QtyDemand'];
34 function load_stock_levels($location)
36 $date = date2sql(Today());
39 $sql = "SELECT stock_id, SUM(qty)
40 FROM ".TB_PREF."stock_moves
41 WHERE tran_date <= '$date'";
43 $sql .= " AND loc_code = ".db_escape($location);
44 $sql .= " GROUP BY stock_id";
46 $result = db_query($sql, "QOH calculation failed");
47 while ($row = db_fetch($result))
48 $qoh_stock[$row[0]] = $row[1];
53 // recursion fixed. Max 10 recursion levels.
54 function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level=0)
56 global $bom_list, $qoh_stock;
60 display_warning("BOM Too many Manufacturing levels deep $level");
63 // Load all stock levels (stock moves) into $qoh_stock
64 if ($qoh_stock == NULL)
65 $qoh_stock = load_stock_levels($location);
67 if (empty($qoh_stock[$stock_id]))
70 $stock_qty = $qoh_stock[$stock_id];
72 if ($qty <= $stock_qty)
74 $bom = @$bom_list[$stock_id];
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");
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]);
86 db_free_result($result);
87 $bom_list[$stock_id] = $bom;
94 // Ignore the dummy entry
95 if ($row[1] == '') continue;
97 if ($row[1] == $demand_id) $demand += $q;
98 $demand += stock_demand_manufacture($row[1], $q, $demand_id, $location, $level+1);
103 // recursion fixed by Tom Moulton
104 function get_demand_asm_qty($stock_id, $location)
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'";
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);
127 function get_on_porder_qty($stock_id, $location)
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);
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");
138 if (db_num_rows($qoo_result) == 1)
140 $qoo_row = db_fetch_row($qoo_result);
150 function get_on_worder_qty($stock_id, $location)
153 $flag = get_mb_flag($stock_id);
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";
162 $sql .= " AND loc_code=".db_escape($location);
164 $qoo_result = db_query($sql,"could not receive quantity on order for item");
165 if (db_num_rows($qoo_result) == 1)
167 $qoo_row = db_fetch_row($qoo_result);
174 //--------------------------------------------------------------------------------------
176 function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
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) . ", "
183 db_query($sql,"check failed");
185 //--------------------------------------------------------------------------------------
187 function update_bom($selected_parent, $selected_component, $workcentre_added, $loc_code, $quantity)
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);
196 db_query($sql,"could not update bom");
199 function delete_bom($selected_id)
201 $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
202 db_query($sql,"Could not delete this bom components");
205 function get_bom($item)
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,
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";
220 return db_query($sql, "The bill of material could not be retrieved");
223 //--------------------------------------------------------------------------------------
225 function get_component_from_bom($selected_id)
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";
233 $result = db_query($sql, "could not get bom");
234 return db_fetch($result);
236 //--------------------------------------------------------------------------------------
238 function has_bom($item)
240 $result = get_bom($item);
242 return (db_num_rows($result) != 0);
245 //--------------------------------------------------------------------------------------
247 function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
249 $sql = "SELECT component
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");
257 return (db_num_rows($result) > 0);
260 //--------------------------------------------------------------------------------------
262 function check_for_recursive_bom($ultimate_parent, $component_to_check)
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 */
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");
273 while ($myrow = db_fetch_row($result))
275 if ($myrow[0] == $ultimate_parent)
280 if (check_for_recursive_bom($ultimate_parent, $myrow[0]))