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)
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";
158 $sql .= " AND req.loc_code=".db_escape($location);
160 $qoo_result = db_query($sql,"could not receive quantity on order for item");
161 if (db_num_rows($qoo_result) == 1)
163 $qoo_row = db_fetch_row($qoo_result);
168 $flag = get_mb_flag($stock_id);
171 $sql = "SELECT SUM((units_reqd-units_issued)) AS qoo
172 FROM ".TB_PREF."workorders
173 WHERE stock_id=".db_escape($stock_id)
177 $sql .= " AND loc_code=".db_escape($location);
179 $qoo_result = db_query($sql,"could not receive quantity on order for item");
180 if (db_num_rows($qoo_result) == 1)
182 $qoo_row = db_fetch_row($qoo_result);
189 //--------------------------------------------------------------------------------------
191 function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
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) . ", "
198 db_query($sql,"check failed");
200 //--------------------------------------------------------------------------------------
202 function update_bom($selected_parent, $selected_component, $workcentre_added, $loc_code, $quantity)
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);
211 db_query($sql,"could not update bom");
214 function delete_bom($selected_id)
216 $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
217 db_query($sql,"Could not delete this bom components");
220 function get_bom($item)
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,
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";
235 return db_query($sql, "The bill of material could not be retrieved");
238 //--------------------------------------------------------------------------------------
240 function get_component_from_bom($selected_id)
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";
248 $result = db_query($sql, "could not get bom");
249 return db_fetch($result);
251 //--------------------------------------------------------------------------------------
253 function has_bom($item)
255 $result = get_bom($item);
257 return (db_num_rows($result) != 0);
260 //--------------------------------------------------------------------------------------
262 function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
264 $sql = "SELECT component
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");
272 return (db_num_rows($result) > 0);
275 //--------------------------------------------------------------------------------------
277 function check_for_recursive_bom($ultimate_parent, $component_to_check)
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 */
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");
288 while ($myrow = db_fetch_row($result))
290 if ($myrow[0] == $ultimate_parent)
295 if (check_for_recursive_bom($ultimate_parent, $myrow[0]))