Fixed and optimized On Order in Inventory Items Status and reports
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 19 May 2009 23:23:45 +0000 (23:23 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 19 May 2009 23:23:45 +0000 (23:23 +0000)
CHANGELOG.txt
includes/db/manufacturing_db.inc
inventory/inquiry/stock_status.php
reporting/rep302.php
reporting/rep303.php
sales/includes/db/sales_order_db.inc

index 2f2ddb38bb5e8bef7eaefd2f78c0221f2366ee3b..24088faaba23137eabe135dc1f189a9ae81195cf 100644 (file)
@@ -19,6 +19,14 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+20-May-2009 Joe Hunt
+# Fixed and optimized On Order in Inventory Items Status and reports
+$ /includes/db/manufacturing_db.inc
+  /inventory/inquiry/stock_status.php
+  /reporting/rep302.php
+  /reporting/rep303.php
+  /sales/includes/db/sales_order_db.inc
+  
 18-May-2009 Joe Hunt
 # html header shown in backup downloads.
 $ /admin/backups.php
index 622b6822740ef35c8b5ac03c804da5889cd05b15..6778cff7d17604166abfff7a4f11f305af01022e 100644 (file)
@@ -9,6 +9,109 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+//----------------------------------------------------------------------------------------
+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 ";
+       if ($location != "")
+               $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
+       $sql .= TB_PREF."sales_order_details.stk_code = '$stock_id'";
+
+    $result = db_query($sql,"No transactions were returned");
+       $row = db_fetch($result);
+       return $row['QtyDemand'];
+}
+
+function get_demand_asm_qty($stock_id, $location)
+{
+       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
+                                  AS Dem
+                                  FROM ".TB_PREF."sales_order_details,
+                                               ".TB_PREF."sales_orders,
+                                               ".TB_PREF."bom,
+                                               ".TB_PREF."stock_master
+                                  WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
+                                  ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
+       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."bom.component='$stock_id' AND
+                                  ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
+                                  (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
+
+    $result = db_query($sql,"No transactions were returned");
+       if (db_num_rows($result)==1)
+       {
+               $row = db_fetch_row($result);
+               $demand_qty = $row[0];
+       }
+       else
+               $demand_qty = 0.0;
+       return $demand_qty;
+}
+
+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' ";
+       if ($location != "")
+               $sql .= "AND ".TB_PREF."purch_orders.into_stock_location='$location' ";
+       $sql .= "AND ".TB_PREF."purch_order_details.item_code='$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((".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;
+       $flag = get_mb_flag($stock_id);
+       if ($flag == 'A' || $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";
+               $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 get_mb_flag($stock_id)
 {
        $sql = "SELECT mb_flag FROM ".TB_PREF."stock_master WHERE stock_id = '" . $stock_id . "'";
index c683fa950868af4e20df9e77684a40998a109055..331ea38321fc7290e7f22f166d3ebbafaaa20533 100644 (file)
@@ -79,77 +79,15 @@ while ($myrow = db_fetch($loc_details))
 
        alt_table_row_color($k);
 
-       $sql = "SELECT Sum(".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent) AS DEM
-               FROM ".TB_PREF."sales_order_details, ".TB_PREF."sales_orders
-               WHERE ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no
-               AND ".TB_PREF."sales_orders.from_stk_loc='" . $myrow["loc_code"] . "'
-               AND ".TB_PREF."sales_order_details.qty_sent < ".TB_PREF."sales_order_details.quantity
-               AND ".TB_PREF."sales_order_details.stk_code='" . $_POST['stock_id'] . "'";
-
-       $demand_result = db_query($sql,"Could not retreive demand for item");
-
-       if (db_num_rows($demand_result) == 1)
-       {
-         $demand_row = db_fetch_row($demand_result);
-         $demand_qty =  $demand_row[0];
-       }
-       else
-       {
-         $demand_qty =0;
-       }
-
-       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
-                                  AS DemAsm
-                                  FROM ".TB_PREF."sales_order_details,
-                                               ".TB_PREF."sales_orders,
-                                               ".TB_PREF."bom,
-                                               ".TB_PREF."stock_master
-                                  WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent 
-                                               AND ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no 
-                                               AND ".TB_PREF."sales_orders.from_stk_loc ='" . $myrow["loc_code"] . "' 
-                                               AND ".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 
-                                               AND ".TB_PREF."bom.component='" . $_POST['stock_id'] . "' 
-                                               AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent 
-                                               AND (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
-
-    $result = db_query($sql,"No transactions were returned");
-       if (db_num_rows($result)==1)
-       {
-               $demand_row = db_fetch_row($result);
-               $demand_qty += $demand_row[0];
-       }
+       $demand_qty = get_demand_qty($_POST['stock_id'], $myrow["loc_code"]);
+       $demand_qty += get_demand_asm_qty($_POST['stock_id'], $myrow["loc_code"]);
 
        $qoh = get_qoh_on_date($_POST['stock_id'], $myrow["loc_code"]);
 
        if ($kitset_or_service == false)
        {
-               $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_orders.into_stock_location='" . $myrow["loc_code"] . "'
-                       AND ".TB_PREF."purch_order_details.item_code='" . $_POST['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;
-               }
-               $sql = "SELECT Sum(".TB_PREF."workorders.units_reqd * ".TB_PREF."wo_requirements.units_req) 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.loc_code='" . $myrow["loc_code"] . "'
-                               AND ".TB_PREF."wo_requirements.stock_id='" . $_POST['stock_id'] . "'
-                               AND ".TB_PREF."workorders.closed=0";
-               $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 = get_on_porder_qty($_POST['stock_id'], $myrow["loc_code"]);
+               $qoo += get_on_worder_qty($_POST['stock_id'], $myrow["loc_code"]);
                label_cell($myrow["location_name"]);
                qty_cell($qoh, false, $dec);
         qty_cell($myrow["reorder_level"], false, $dec);
index f2297d8915b0333a111f3d50e0f6711d304e765f..6806e4c3931fabca11c49f040092b7d4a7e9b56f 100644 (file)
@@ -23,6 +23,7 @@ include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
+include_once($path_to_root . "/includes/db/manufacturing_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
@@ -57,65 +58,6 @@ function getTransactions($category, $location)
 
 }
 
-function getCustQty($stockid, $location)
-{
-       $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS qty_demand
-                               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 ";
-       if ($location != "")
-               $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
-       $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
-
-    $TransResult = db_query($sql,"No transactions were returned");
-       $DemandRow = db_fetch($TransResult);
-       return $DemandRow['qty_demand'];
-}
-
-function getCustAsmQty($stockid, $location)
-{
-       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
-                                  AS Dem
-                                  FROM ".TB_PREF."sales_order_details,
-                                               ".TB_PREF."sales_orders,
-                                               ".TB_PREF."bom,
-                                               ".TB_PREF."stock_master
-                                  WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
-                                  ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
-       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."bom.component='$stockid' AND
-                                  ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
-                                  (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
-
-    $TransResult = db_query($sql,"No transactions were returned");
-       if (db_num_rows($TransResult) == 1)
-       {
-               $DemandRow = db_fetch_row($TransResult);
-               $DemandQty = $DemandRow[0];
-       }
-       else
-               $DemandQty = 0.0;
-
-    return $DemandQty;
-}
-
-function getSuppQty($stockid, $location)
-{
-       $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS QtyOnOrder
-                               FROM ".TB_PREF."purch_order_details,
-                                       ".TB_PREF."purch_orders
-                               WHERE ".TB_PREF."purch_order_details.order_no = ".TB_PREF."purch_orders.order_no
-                               AND ".TB_PREF."purch_order_details.item_code = '$stockid'";
-       if ($location != "")                    
-               $sql .= " AND ".TB_PREF."purch_orders.into_stock_location= '$location'";
-
-    $TransResult = db_query($sql,"No transactions were returned");
-       $DemandRow = db_fetch($TransResult);
-       return $DemandRow['QtyOnOrder'];
-}
-
 function getPeriods($stockid, $location)
 {
        $date5 = date('Y-m-d');
@@ -213,9 +155,10 @@ function print_inventory_planning()
                        $loc_code = "";
                else
                        $loc_code = $trans['loc_code'];
-               $custqty = getCustQty($trans['stock_id'], $loc_code);
-               $custqty += getCustAsmQty($trans['stock_id'], $loc_code);
-               $suppqty = getSuppQty($trans['stock_id'], $loc_code);
+               $custqty = get_demand_qty($trans['stock_id'], $loc_code);
+               $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
+               $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
+               $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
                $period = getPeriods($trans['stock_id'], $trans['loc_code']);
                $rep->NewLine();
                $dec = get_qty_dec($trans['stock_id']);
index ffeb9b8195c091966e85aa3fd7c82ba67f117d74..d697029d99b318ea99ea3d0ae20ef725c59ed2e2 100644 (file)
@@ -23,6 +23,7 @@ include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
+include_once($path_to_root . "/includes/db/manufacturing_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
@@ -56,50 +57,6 @@ function getTransactions($category, $location)
     return db_query($sql,"No transactions were returned");
 }
 
-function getDemandQty($stockid, $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 ";
-       if ($location != "")
-               $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
-       $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
-
-    $TransResult = db_query($sql,"No transactions were returned");
-       $DemandRow = db_fetch($TransResult);
-       return $DemandRow['QtyDemand'];
-}
-
-function getDemandAsmQty($stockid, $location)
-{
-       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
-                                  AS Dem
-                                  FROM ".TB_PREF."sales_order_details,
-                                               ".TB_PREF."sales_orders,
-                                               ".TB_PREF."bom,
-                                               ".TB_PREF."stock_master
-                                  WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
-                                  ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
-       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."bom.component='$stockid' AND
-                                  ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
-                                  (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
-
-    $TransResult = db_query($sql,"No transactions were returned");
-       if (db_num_rows($TransResult)==1)
-       {
-               $DemandRow = db_fetch_row($TransResult);
-               $DemandQty = $DemandRow[0];
-       }
-       else
-               $DemandQty = 0.0;
-
-    return $DemandQty;
-}
-
 //----------------------------------------------------------------------------------------------------
 
 function print_stock_check()
@@ -180,8 +137,8 @@ function print_stock_check()
                        $loc_code = "";
                else
                        $loc_code = $trans['loc_code'];
-               $demandqty = getDemandQty($trans['stock_id'], $loc_code);
-               $demandqty += getDemandAsmQty($trans['stock_id'], $loc_code);
+               $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
+               $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
                $rep->NewLine();
                $dec = get_qty_dec($trans['stock_id']);
                $rep->TextCol(0, 1, $trans['stock_id']);
index bbaf9706c86fe4271a86c2e54f9a229fb34fb06d..f48cbcf140d31a606e06beb4064f0c6aa08df8c6 100644 (file)
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 //----------------------------------------------------------------------------------------
-function get_demand_qty($stockid, $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
-                                       ".TB_PREF."sales_orders.from_stk_loc ='$location' AND
-                                       ".TB_PREF."sales_order_details.stk_code = '$stockid'";
-
-       $TransResult = db_query($sql,"No transactions were returned");
-       $DemandRow = db_fetch($TransResult);
-       return $DemandRow['QtyDemand'];
-}
-
-function get_demand_asm_qty($stockid, $location)
-{
-       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
-                               AS Dem
-                               FROM ".TB_PREF."sales_order_details,
-                                               ".TB_PREF."sales_orders,
-                                               ".TB_PREF."bom,
-                                               ".TB_PREF."stock_master
-                               WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
-                               ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND
-                               ".TB_PREF."sales_orders.from_stk_loc='$location' AND
-                               ".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
-                               ".TB_PREF."bom.component='$stockid' AND
-                               ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
-                               (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
-
-       $TransResult = db_query($sql,"No transactions were returned");
-       if (db_num_rows($TransResult)==1)
-       {
-               $DemandRow = db_fetch_row($TransResult);
-               $DemandQty = $DemandRow[0];
-       }
-       else
-               $DemandQty = 0.0;
-
-       return $DemandQty;
-}
-
 function add_sales_order(&$order)
 {
        global $loc_notification, $path_to_root;