Stock Status display, Manufacturing Reports: fixed products on WO count.
[fa-stable.git] / includes / db / manufacturing_db.inc
index c2f34d9be21aac04c17bd4fcde878753313fe158..93027b764b9f32e65beda3e463c4c6ba280ecfb8 100644 (file)
 //----------------------------------------------------------------------------------------
 function get_demand_qty($stock_id, $location)
 {
-       $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS QtyDemand
-                               FROM ".TB_PREF."sales_order_details,
-                                       ".TB_PREF."sales_orders
-                               WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND ";
+       $sql = "SELECT SUM(line.quantity - line.qty_sent) AS QtyDemand
+                       FROM ".TB_PREF."sales_order_details line,
+                               ".TB_PREF."sales_orders sorder
+                               WHERE line.order_no=sorder.order_no AND sorder.trans_type=".ST_SALESORDER
+                               ." AND sorder.trans_type=line.trans_type"
+                               ." AND line.stk_code = ".db_escape($stock_id);
        if ($location != "")
-               $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
-       $sql .= TB_PREF."sales_order_details.stk_code = '$stock_id'";
+               $sql .= " AND sorder.from_stk_loc =".db_escape($location);
 
     $result = db_query($sql,"No transactions were returned");
        $row = db_fetch($result);
@@ -32,39 +33,49 @@ $qoh_stock = NULL;
 
 function load_stock_levels($location)
 {
-       global $qoh_stock;
        $date = date2sql(Today());
 
-       $sql = "SELECT stock_id, SUM(qty) FROM ".TB_PREF."stock_moves WHERE tran_date <= '$date'";
-       if ($location != '') $sql .= " AND loc_code = '$location'";
+       $qoh_stock = array();
+       $sql = "SELECT stock_id, SUM(qty)
+               FROM ".TB_PREF."stock_moves
+               WHERE tran_date <= '$date'";
+       if ($location != '')
+               $sql .= " AND loc_code = ".db_escape($location);
        $sql .= " GROUP BY stock_id";
-       $result = db_query($sql, "QOH calulcation failed");
-       while ($row = db_fetch($result)) {
+
+       $result = db_query($sql, "QOH calculation failed");
+       while ($row = db_fetch($result))
                $qoh_stock[$row[0]] = $row[1];
-       }
+
+       return $qoh_stock;
 }
 
-// recursion fixed by Tom Moulton. Max 10 recursion levels.
+// 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 = array();
-               load_stock_levels($location);
-       }
-       $stock_qty = $qoh_stock[$stock_id];
-       if ($stock_qty == NULL) $stock_qty = 0;
-       if ($qty < $stock_qty) return $demand;
-       $bom = $bom_list[$stock_id];
+       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 = '$stock_id'";
-               if ($location != "") $sql .= " AND loc_code = '$location'";
+               $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 
@@ -74,7 +85,7 @@ function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level
                }
                db_free_result($result);
                $bom_list[$stock_id] = $bom;
-       }       
+       }
        $len = count($bom);
        $i = 0;
        while ($i < $len) {
@@ -93,18 +104,19 @@ function stock_demand_manufacture($stock_id, $qty, $demand_id, $location, $level
 function get_demand_asm_qty($stock_id, $location) 
 {
        $demand_qty = 0.0;
-       $sql = "SELECT ".TB_PREF."sales_order_details.stk_code, SUM(".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)
-                                  AS Demmand
-                                  FROM ".TB_PREF."sales_order_details,
-                                               ".TB_PREF."sales_orders,
-                                               ".TB_PREF."stock_master
-                                  WHERE ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
+       $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 .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
-       $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
-                                  ".TB_PREF."stock_master.stock_id=".TB_PREF."sales_order_details.stk_code AND
-                                  (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')
-                                  GROUP BY ".TB_PREF."sales_order_details.stk_code";
+               $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);
@@ -114,12 +126,13 @@ function get_demand_asm_qty($stock_id, $location)
 
 function get_on_porder_qty($stock_id, $location)
 {
-       $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS qoo
-               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
-               WHERE ".TB_PREF."purch_order_details.item_code='$stock_id' ";
+       $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 ".TB_PREF."purch_orders.into_stock_location='$location' ";
-       $sql .= "AND ".TB_PREF."purch_order_details.item_code='$stock_id'";
+               $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)
@@ -136,71 +149,92 @@ function get_on_porder_qty($stock_id, $location)
 
 function get_on_worder_qty($stock_id, $location)
 {
-       $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued) * 
-               (".TB_PREF."wo_requirements.units_req-".TB_PREF."wo_requirements.units_issued)) AS qoo
-               FROM ".TB_PREF."wo_requirements INNER JOIN ".TB_PREF."workorders 
-                       ON ".TB_PREF."wo_requirements.workorder_id=".TB_PREF."workorders.id
-               WHERE ".TB_PREF."wo_requirements.stock_id='$stock_id' ";
-       if ($location != "")
-               $sql .= "AND ".TB_PREF."wo_requirements.loc_code='$location' ";
-       $sql .= "AND ".TB_PREF."workorders.released=1";
-       $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;
+       $qoo = 0.0;
        $flag = get_mb_flag($stock_id);
-       if ($flag == 'A' || $flag == 'M')
+       if ($flag == 'M')
        {
-               $sql = "SELECT SUM((".TB_PREF."workorders.units_reqd-".TB_PREF."workorders.units_issued)) AS qoo
-                       FROM ".TB_PREF."workorders 
-                       WHERE ".TB_PREF."workorders.stock_id='$stock_id' ";
-               if ($location != "")    
-                       $sql .= "AND ".TB_PREF."workorders.loc_code='$location' ";
-               $sql .= "AND ".TB_PREF."workorders.released=1";
+               $sql = "SELECT SUM((units_reqd-units_issued)) AS qoo
+                       FROM ".TB_PREF."workorders
+                       WHERE stock_id=".db_escape($stock_id)
+                               ." AND released AND NOT closed";
+
+               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];
+                       $qoo = $qoo_row[0];
                }
        }
        return $qoo;
 }
 
-function get_mb_flag($stock_id)
+//--------------------------------------------------------------------------------------
+
+function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
 {
-       $sql = "SELECT mb_flag FROM ".TB_PREF."stock_master WHERE stock_id = '" . $stock_id . "'";
-       $result = db_query($sql, "retreive mb_flag from item");
-       
-       if (db_num_rows($result) == 0)
-               return -1;
+       $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 . ")";
 
-       $myrow = db_fetch_row($result);
-       return $myrow[0];
+       db_query($sql,"check failed");
 }
-
 //--------------------------------------------------------------------------------------
 
+function update_bom($selected_parent, $selected_component, $workcentre_added, $loc_code, $quantity)
+{
+       $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($workcentre_added)
+        . ",loc_code=".db_escape($loc_code) . ",
+               quantity= " . $quantity . "
+               WHERE parent=".db_escape($selected_parent) . "
+               AND id=".db_escape($selected_component);
+       check_db_error("Could not update this bom component", $sql);
+
+       db_query($sql,"could not update bom");
+}
+       
+function delete_bom($selected_id)
+{
+       $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
+       db_query($sql,"Could not delete this bom components");
+}
+
 function get_bom($item)
 {
-       $sql = "SELECT ".TB_PREF."bom.*, ".TB_PREF."locations.location_name, ".TB_PREF."workcentres.name AS WorkCentreDescription, 
-       ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.mb_flag AS ResourceType, 
-       ".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+".TB_PREF."stock_master.overhead_cost AS standard_cost, units, 
-       ".TB_PREF."bom.quantity * (".TB_PREF."stock_master.material_cost+ ".TB_PREF."stock_master.labour_cost+ ".TB_PREF."stock_master.overhead_cost) AS ComponentCost 
-       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 
-       WHERE ".TB_PREF."bom.parent = '" . $item . "'
-               AND ".TB_PREF."workcentres.id=".TB_PREF."bom.workcentre_added
-               AND ".TB_PREF."bom.loc_code = ".TB_PREF."locations.loc_code ORDER BY ".TB_PREF."bom.id";
+       $sql = "SELECT bom.*, loc.location_name,
+               centre.name AS WorkCentreDescription,
+       item.description, item.mb_flag AS ResourceType, 
+       item.material_cost AS ProductCost, units,
+       bom.quantity * item.material_cost AS ComponentCost 
+       FROM ".TB_PREF."workcentres centre,
+               ".TB_PREF."locations loc,
+               ".TB_PREF."bom bom
+                       INNER JOIN ".TB_PREF."stock_master item ON bom.component = item.stock_id 
+       WHERE bom.parent = ".db_escape($item)."
+               AND centre.id=bom.workcentre_added
+               AND bom.loc_code = loc.loc_code ORDER BY bom.id";
        
        return db_query($sql, "The bill of material could not be retrieved");
 }
 
 //--------------------------------------------------------------------------------------
 
+function get_component_from_bom($selected_id)
+{
+       $sql = "SELECT bom.*, item.description
+               FROM ".TB_PREF."bom bom,"
+                       .TB_PREF."stock_master item
+               WHERE id=".db_escape($selected_id)."
+               AND item.stock_id=bom.component";
+
+       $result = db_query($sql, "could not get bom");
+       return db_fetch($result);
+}
+//--------------------------------------------------------------------------------------
+
 function has_bom($item)
 {
     $result = get_bom($item);
@@ -210,4 +244,46 @@ function has_bom($item)
 
 //--------------------------------------------------------------------------------------
 
-?>
+function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
+{
+       $sql = "SELECT component
+               FROM ".TB_PREF."bom
+               WHERE parent=".db_escape($selected_parent)."
+               AND component=".db_escape($component) . "
+               AND workcentre_added=".db_escape($workcentre_added) . "
+               AND loc_code=".db_escape($loc_code);
+       $result = db_query($sql,"check failed");
+
+       return (db_num_rows($result) > 0);
+}
+
+//--------------------------------------------------------------------------------------
+
+function check_for_recursive_bom($ultimate_parent, $component_to_check)
+{
+
+       /* returns true ie 1 if the bom contains the parent part as a component
+       ie the bom is recursive otherwise false ie 0 */
+
+       $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
+       $result = db_query($sql,"could not check recursive bom");
+
+       if ($result)
+       {
+               while ($myrow = db_fetch_row($result))
+               {
+                       if ($myrow[0] == $ultimate_parent)
+                       {
+                               return 1;
+                       }
+
+                       if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
+                       {
+                               return 1;
+                       }
+               }
+       }
+
+       return 0;
+}
+