+// recursion fixed. Max 10 recursion levels.
+function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level=0)
+{
+ global $bom_list, $qoh_stock;
+
+ $demand = 0.0;
+ if ($level > 10) {
+ display_warning("BOM Too many Manufacturing levels deep $level");
+ return $demand;
+ }
+ // Load all stock levels (stock moves) into $qoh_stock
+ if ($qoh_stock == NULL)
+ $qoh_stock = load_stock_levels($location);
+
+ if (empty($qoh_stock[$stock_id]))
+ $stock_qty = 0;
+ else
+ $stock_qty = $qoh_stock[$stock_id];
+
+ if ($qty <= $stock_qty)
+ return $demand;
+ $bom = @$bom_list[$stock_id];
+ if ($bom == NULL) {
+ $sql = "SELECT parent, component, quantity FROM "
+ .TB_PREF."bom WHERE parent = ".db_escape($stock_id);
+ if ($location != "") $sql .= " AND loc_code = ".db_escape($location);
+ $result = db_query($sql, "Could not search bom");
+ $bom = array();
+ // Even if we get no results, remember that fact
+ $bom[] = array($stock_id, '', 0);
+ while ($row = db_fetch_row($result)) {
+ $bom[] = array($row[0], $row[1], $row[2]);
+ }
+ db_free_result($result);
+ $bom_list[$stock_id] = $bom;
+ }
+ $len = count($bom);
+ $i = 0;
+ while ($i < $len) {
+ $row = $bom[$i];
+ $i++;
+ // Ignore the dummy entry
+ if ($row[1] == '') continue;
+ $q = $qty * $row[2];
+ if ($row[1] == $demand_id) $demand += $q;
+ $demand += stock_demand_manufacture($row[1], $q, $demand_id, $location, $level+1);
+ }
+ return $demand;
+}
+
+// recursion fixed by Tom Moulton
+function get_demand_asm_qty($stock_id, $location)
+{
+ $demand_qty = 0.0;
+ $sql = "SELECT line.stk_code, SUM(line.quantity-line.qty_sent) AS Demmand
+ FROM ".TB_PREF."sales_order_details line,
+ ".TB_PREF."sales_orders sorder,
+ ".TB_PREF."stock_master item
+ WHERE sorder.order_no = line.order_no
+ AND sorder.trans_type=".ST_SALESORDER
+ ." AND sorder.trans_type=line.trans_type
+ AND line.quantity-line.qty_sent > 0
+ AND item.stock_id=line.stk_code
+ AND item.mb_flag='M'";
+ if ($location != "")
+ $sql .= " AND sorder.from_stk_loc =".db_escape($location);
+ $sql .= " GROUP BY line.stk_code";
+ $result = db_query($sql, "No transactions were returned");
+ while ($row = db_fetch_row($result)) {
+ $demand_qty += stock_demand_manufacture($row[0], $row[1], $stock_id, $location);
+ }
+ return $demand_qty;
+}
+
+function get_on_porder_qty($stock_id, $location)
+{
+ $sql = "SELECT SUM(line.quantity_ordered - line.quantity_received) AS qoo
+ FROM ".TB_PREF."purch_order_details line
+ INNER JOIN ".TB_PREF."purch_orders po ON line.order_no=po.order_no
+ WHERE line.item_code=".db_escape($stock_id);
+ if ($location != "")
+ $sql .= " AND po.into_stock_location=".db_escape($location);
+ $sql .= " AND line.item_code=".db_escape($stock_id);
+ $qoo_result = db_query($sql,"could not receive quantity on order for item");
+
+ if (db_num_rows($qoo_result) == 1)
+ {
+ $qoo_row = db_fetch_row($qoo_result);
+ $qoo = $qoo_row[0];
+ }
+ else
+ {
+ $qoo = 0;
+ }
+ return $qoo;
+}
+
+function get_on_worder_qty($stock_id, $location)
+{
+ $sql = "SELECT SUM((wo.units_reqd-wo.units_issued) * (req.units_req-req.units_issued)) AS qoo
+ FROM ".TB_PREF."wo_requirements req
+ INNER JOIN ".TB_PREF."workorders wo ON req.workorder_id=wo.id
+ WHERE req.stock_id=".db_escape($stock_id)
+ ." AND wo.released=1";
+ if ($location != "")
+ $sql .= " AND req.loc_code=".db_escape($location);
+
+ $qoo_result = db_query($sql,"could not receive quantity on order for item");
+ if (db_num_rows($qoo_result) == 1)
+ {
+ $qoo_row = db_fetch_row($qoo_result);
+ $qoo = $qoo_row[0];
+ }
+ else
+ $qoo = 0.0;
+ $flag = get_mb_flag($stock_id);
+ if ($flag == 'M')
+ {
+ $sql = "SELECT SUM((units_reqd-units_issued)) AS qoo
+ FROM ".TB_PREF."workorders
+ WHERE stock_id=".db_escape($stock_id)
+ ." AND released=1";
+
+ if ($location != "")
+ $sql .= " AND loc_code=".db_escape($location);
+
+ $qoo_result = db_query($sql,"could not receive quantity on order for item");
+ if (db_num_rows($qoo_result) == 1)
+ {
+ $qoo_row = db_fetch_row($qoo_result);
+ $qoo += $qoo_row[0];
+ }
+ }
+ return $qoo;
+}
+
+//--------------------------------------------------------------------------------------
+
+function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
+{
+ $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
+ VALUES (".db_escape($selected_parent).", ".db_escape($component) . ","
+ .db_escape($workcentre_added) . ", ".db_escape($loc_code) . ", "
+ . $quantity . ")";
+
+ db_query($sql,"check failed");
+}