svn commit: r450171 - in /incubator/ofbiz/trunk/applications: order/entitydef/ order/src/org/ofbiz/order/order/ order/webapp/ordermgr/WEB-INF/actions/order/ product/src/org/ofbiz/product/inventory/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r450171 - in /incubator/ofbiz/trunk/applications: order/entitydef/ order/src/org/ofbiz/order/order/ order/webapp/ordermgr/WEB-INF/actions/order/ product/src/org/ofbiz/product/inventory/

sichen
Author: sichen
Date: Tue Sep 26 13:08:10 2006
New Revision: 450171

URL: http://svn.apache.org/viewvc?view=rev&rev=450171
Log:
Fix bug where outstanding product quantities does not take issuances into account.  This required creating a new view entity to sum up ItemIssuances.  Also refactored a bunch of code out of orderview.bsh and created a handy method to get a distinct set of productIds in OrderReadHelper. (Useful for doing where productId IN queries.)

Modified:
    incubator/ofbiz/trunk/applications/order/entitydef/entitygroup.xml
    incubator/ofbiz/trunk/applications/order/entitydef/entitymodel_view.xml
    incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
    incubator/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/order/orderview.bsh
    incubator/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryWorker.java

Modified: incubator/ofbiz/trunk/applications/order/entitydef/entitygroup.xml
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/entitydef/entitygroup.xml?view=diff&rev=450171&r1=450170&r2=450171
==============================================================================
--- incubator/ofbiz/trunk/applications/order/entitydef/entitygroup.xml (original)
+++ incubator/ofbiz/trunk/applications/order/entitydef/entitygroup.xml Tue Sep 26 13:08:10 2006
@@ -62,6 +62,7 @@
     <entity-group group="org.ofbiz" entity="OrderItemShipGrpInvRes" />
     <entity-group group="org.ofbiz" entity="OrderItemShipGrpInvResAndItem" />
     <entity-group group="org.ofbiz" entity="OrderItemShipGrpInvResAndItemLocation" />
+    <entity-group group="org.ofbiz" entity="OrderItemIssuanceGroupByProduct" />
     <entity-group group="org.ofbiz" entity="OrderItemPriceInfo" />
     <entity-group group="org.ofbiz" entity="OrderItemRole" />
     <entity-group group="org.ofbiz" entity="OrderItemShipGroup" />

Modified: incubator/ofbiz/trunk/applications/order/entitydef/entitymodel_view.xml
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/entitydef/entitymodel_view.xml?view=diff&rev=450171&r1=450170&r2=450171
==============================================================================
--- incubator/ofbiz/trunk/applications/order/entitydef/entitymodel_view.xml (original)
+++ incubator/ofbiz/trunk/applications/order/entitydef/entitymodel_view.xml Tue Sep 26 13:08:10 2006
@@ -734,6 +734,26 @@
         <key-map field-name="inventoryItemId"/>
       </relation>
     </view-entity>
+    <view-entity entity-name="OrderItemIssuanceGroupByProduct"
+            package-name="org.ofbiz.order.order"
+            never-cache="true"
+            title="Reports quantity issued per product across OrderItems">
+      <member-entity entity-alias="OH" entity-name="OrderHeader"/>
+      <member-entity entity-alias="OI" entity-name="OrderItem"/>
+      <member-entity entity-alias="II" entity-name="ItemIssuance"/>
+      <alias entity-alias="OH" name="orderTypeId" group-by="true"/>
+      <alias entity-alias="OH" name="orderStatusId" field="statusId" group-by="false"/>
+      <alias entity-alias="OI" name="orderItemStatusId" field="statusId" group-by="false"/>
+      <alias entity-alias="OI" name="productId" group-by="true"/>
+      <alias entity-alias="II" name="quantity" function="sum"/>
+      <view-link entity-alias="OI" rel-entity-alias="OH">
+        <key-map field-name="orderId"/>
+      </view-link>
+      <view-link entity-alias="OI" rel-entity-alias="II">
+        <key-map field-name="orderId"/>
+        <key-map field-name="orderItemSeqId"/>
+      </view-link>
+    </view-entity>
     <view-entity entity-name="OrderPurchasePaymentSummary"
             package-name="org.ofbiz.order.order"
             never-cache="true"

Modified: incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java?view=diff&rev=450171&r1=450170&r2=450171
==============================================================================
--- incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java (original)
+++ incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java Tue Sep 26 13:08:10 2006
@@ -20,7 +20,9 @@
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -1555,6 +1557,15 @@
             filter.put("shipmentId", shipmentId);
         }
         return EntityUtil.filterByAnd(orderItemIssuances, filter);
+    }
+
+    /** Get a set of productIds in the order. */
+    public Collection getOrderProductIds() {
+        Set productIds = new HashSet();
+        for (Iterator iter = getOrderItems().iterator(); iter.hasNext(); ) {
+            productIds.add(((GenericValue) iter.next()).getString("productId"));
+        }
+        return productIds;
     }
 
     public List getOrderReturnItems() {

Modified: incubator/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/order/orderview.bsh
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/order/orderview.bsh?view=diff&rev=450171&r1=450170&r2=450171
==============================================================================
--- incubator/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/order/orderview.bsh (original)
+++ incubator/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/order/orderview.bsh Tue Sep 26 13:08:10 2006
@@ -24,6 +24,7 @@
 import org.ofbiz.base.util.collections.*;
 import org.ofbiz.order.order.*;
 import org.ofbiz.party.contact.*;
+import org.ofbiz.product.inventory.InventoryWorker;
 
 orderId = parameters.get("orderId");
 context.put("orderId", orderId);
@@ -277,10 +278,7 @@
     context.put("returnQuantityMap", orderReadHelper.getOrderItemReturnedQuantities());
 
     // INVENTORY: construct a Set of productIds in the order for use in querying for inventory, otherwise these queries can get expensive
-    productIds = new HashSet();
-    for (iter = orderItems.iterator(); iter.hasNext(); ) {
-        productIds.add(iter.next().getString("productId"));
-    }
+    productIds = orderReadHelper.getOrderProductIds();
 
     // INVENTORY: get the production quantity for each product and store the results in a map of productId -> quantity
     productionMap = new HashMap();
@@ -296,53 +294,11 @@
     context.put("productionProductQuantityMap", productionMap);
 
     // INVENTORY: find the number of products in outstanding sales orders for the same product store
-    fieldsToSelect = UtilMisc.toList("productId", "quantity");
-    condList = UtilMisc.toList(
-            new EntityExpr("orderTypeId", EntityOperator.EQUALS, "SALES_ORDER"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_COMPLETED"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_REJECTED"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_CANCELLED")
-            );
-    if (productIds.size() > 0) {
-        condList.add(new EntityExpr("productId", EntityOperator.IN, productIds));
-    }
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_COMPLETED"));
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_REJECTED"));
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"));
-    conditions = new EntityConditionList(condList, EntityOperator.AND);
-    List requiredProducts = delegator.findByCondition("OrderReportGroupByProduct", conditions, fieldsToSelect, null);
-
-    // store the results in a map of productId -> quantity
-    requiredMap = new HashMap();
-    for (iter = requiredProducts.iterator(); iter.hasNext(); ) {
-        value = iter.next();
-        requiredMap.put(value.getString("productId"), value.getDouble("quantity"));
-    }
+    requiredMap = InventoryWorker.getOutstandingProductQuantitiesForSalesOrders(productIds, delegator);
     context.put("requiredProductQuantityMap", requiredMap);
 
     // INVENTORY: find the quantity of each product in outstanding purchase orders
-    fieldsToSelect = UtilMisc.toList("productId", "quantity");
-    condList = UtilMisc.toList(
-            new EntityExpr("orderTypeId", EntityOperator.EQUALS, "PURCHASE_ORDER"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_COMPLETED"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_REJECTED"),
-            new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_CANCELLED")
-            );
-    if (productIds.size() > 0) {
-        condList.add(new EntityExpr("productId", EntityOperator.IN, productIds));
-    }
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_COMPLETED"));
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_REJECTED"));
-    condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"));
-    conditions = new EntityConditionList(condList, EntityOperator.AND);
-    List onOrderProducts = delegator.findByCondition("OrderReportGroupByProduct", conditions, fieldsToSelect, null);
-
-    // store the results in a map of productId -> quantity
-    onOrderMap = new HashMap();
-    for (iter = onOrderProducts.iterator(); iter.hasNext(); ) {
-        value = iter.next();
-        onOrderMap.put(value.getString("productId"), value.getDouble("quantity"));
-    }
+    onOrderMap = InventoryWorker.getOutstandingProductQuantitiesForPurchaseOrders(productIds, delegator);
     context.put("onOrderProductQuantityMap", onOrderMap);
 }
 

Modified: incubator/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryWorker.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryWorker.java?view=diff&rev=450171&r1=450170&r2=450171
==============================================================================
--- incubator/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryWorker.java (original)
+++ incubator/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryWorker.java Tue Sep 26 13:08:10 2006
@@ -16,8 +16,12 @@
 
 package org.ofbiz.product.inventory;
 
-import java.util.List;
+import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javolution.util.FastMap;
 
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilMisc;
@@ -89,4 +93,75 @@
         return qty;
     }
 
-}    
\ No newline at end of file
+    /**
+     * Gets the quanitty of each product in the order that is outstanding across all orders of the given input type.
+     * First it gets the ordered quantities outstanding, then subtracts the issued quantities outstanding.
+     * This method relies on the sum view entities OrderReportGroupByProduct and OrderItemIssuanceGroupByProduct.
+     *
+     * @param   productIds  Collection of disticnt productIds in an order. Use OrderReadHelper.getOrderProductIds()
+     * @param   orderTypeId Either "SALES_ORDER" or "PURCHASE_ORDER"
+     * @param   delegator   The delegator to use
+     * @return  Map of productIds to quantities outstanding.
+     */
+    public static Map getOutstandingProductQuantities(Collection productIds, String orderTypeId, GenericDelegator delegator) {
+
+        // both queries use the same condition
+        List fieldsToSelect = UtilMisc.toList("productId", "quantity");    
+        List condList = UtilMisc.toList(
+                new EntityExpr("orderTypeId", EntityOperator.EQUALS, orderTypeId),
+                new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_COMPLETED"),
+                new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_REJECTED"),
+                new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_CANCELLED")
+                );    
+        if (productIds.size() > 0) {
+            condList.add(new EntityExpr("productId", EntityOperator.IN, productIds));
+        }
+        condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_COMPLETED"));
+        condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_REJECTED"));
+        condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"));
+        EntityConditionList conditions = new EntityConditionList(condList, EntityOperator.AND);
+
+        Map results = FastMap.newInstance();
+        try {
+            // find the ordered products outstading and build a map of productId to quantity
+            List orderedProducts = delegator.findByCondition("OrderReportGroupByProduct", conditions, fieldsToSelect, null);
+            Map orderedProductsMap = FastMap.newInstance();
+            for (Iterator iter = orderedProducts.iterator(); iter.hasNext(); ) {
+                GenericValue value = (GenericValue) iter.next();
+                orderedProductsMap.put(value.getString("productId"), value.getDouble("quantity"));
+            }
+
+            // find the issued quantities outstanding and build a map of productId to quantity
+            List issuedProducts = delegator.findByCondition("OrderItemIssuanceGroupByProduct", conditions, fieldsToSelect, null);
+            Map issuedProductsMap = FastMap.newInstance();
+            for (Iterator iter = issuedProducts.iterator(); iter.hasNext(); ) {
+                GenericValue value = (GenericValue) iter.next();
+                issuedProductsMap.put(value.getString("productId"), value.getDouble("quantity"));
+            }
+
+            // now go through the ordered map and subtract corresponding issued quantities
+            for (Iterator iter = orderedProductsMap.keySet().iterator(); iter.hasNext(); ) {
+                String productId = (String) iter.next();
+                Double quantityOrdered = (Double) orderedProductsMap.get(productId);
+                Double quantityIssued = (Double) issuedProductsMap.get(productId);
+                double quantity = 0;
+                if (quantityOrdered != null) quantity += quantityOrdered.doubleValue();
+                if (quantityIssued != null) quantity -= quantityIssued.doubleValue();
+                results.put(productId, new Double(quantity));
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+        return results;
+    }
+
+    /** As above, but for sales orders */
+    public static Map getOutstandingProductQuantitiesForSalesOrders(Collection productIds, GenericDelegator delegator) {
+        return getOutstandingProductQuantities(productIds, "SALES_ORDER", delegator);
+    }
+
+    /** As above, but for purhcase orders */
+    public static Map getOutstandingProductQuantitiesForPurchaseOrders(Collection productIds, GenericDelegator delegator) {
+        return getOutstandingProductQuantities(productIds, "PURCHASE_ORDER", delegator);
+    }
+}