svn commit: r614970 - in /ofbiz/trunk/applications/product: servicedef/secas.xml servicedef/services_facility.xml src/org/ofbiz/product/inventory/InventoryServices.java

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

svn commit: r614970 - in /ofbiz/trunk/applications/product: servicedef/secas.xml servicedef/services_facility.xml src/org/ofbiz/product/inventory/InventoryServices.java

sichen
Author: sichen
Date: Thu Jan 24 12:13:48 2008
New Revision: 614970

URL: http://svn.apache.org/viewvc?rev=614970&view=rev
Log:
New SECA service to check for over reservations before attempting to reserve for an order item.

Modified:
    ofbiz/trunk/applications/product/servicedef/secas.xml
    ofbiz/trunk/applications/product/servicedef/services_facility.xml
    ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryServices.java

Modified: ofbiz/trunk/applications/product/servicedef/secas.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/secas.xml?rev=614970&r1=614969&r2=614970&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/servicedef/secas.xml (original)
+++ ofbiz/trunk/applications/product/servicedef/secas.xml Thu Jan 24 12:13:48 2008
@@ -70,6 +70,22 @@
         <action service="updateProductIfAvailableFromShipment" mode="sync"/>    
     </eca>
 
+    <!-- Before reserving inventory, ensure that we're not over reserving the item -->
+    <eca service="reserveProductInventory" event="invoke">
+        <action service="checkInventoryAlreadyReserved" mode="sync"/>
+    </eca>
+    <eca service="reserveProductInventoryByFacility" event="invoke">
+        <action service="checkInventoryAlreadyReserved" mode="sync"/>
+    </eca>
+    <eca service="reserveProductInventoryByContainer" event="invoke">
+        <action service="checkInventoryAlreadyReserved" mode="sync"/>
+    </eca>
+    <eca service="reserveStoreInventory" event="invoke">
+        <condition field-name="orderId" operator="is-not-empty"/>
+        <condition field-name="orderItemSeqId" operator="is-not-empty"/>
+        <action service="checkInventoryAlreadyReserved" mode="sync"/>
+    </eca>
+
     <!-- Product Price Change ECAs -->
     <eca service="createProductPrice" event="commit">
         <action service="saveProductPriceChange" mode="sync"/>

Modified: ofbiz/trunk/applications/product/servicedef/services_facility.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_facility.xml?rev=614970&r1=614969&r2=614970&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/servicedef/services_facility.xml (original)
+++ ofbiz/trunk/applications/product/servicedef/services_facility.xml Thu Jan 24 12:13:48 2008
@@ -312,6 +312,14 @@
         <attribute name="sequenceId" type="Long" mode="IN" optional="true"/>
         <attribute name="quantityNotReserved" type="Double" mode="OUT" optional="false"/>
     </service>
+    <service name="checkInventoryAlreadyReserved" engine="java"
+            location="org.ofbiz.product.inventory.InventoryServices" invoke="checkInventoryAlreadyReserved">
+        <description>Ensures that a request to reserve inventory for an order item does not over reserve it.  
+            This should be run as an invoke SECA on all reserveProductInventory services.</description>
+        <attribute name="orderId" type="String" mode="IN" optional="false"/>
+        <attribute name="orderItemSeqId" type="String" mode="IN" optional="false"/>
+        <attribute name="quantity" type="Double" mode="IN" optional="false"/>
+    </service>
     <service name="reserveOrderItemInventory" engine="simple"
                 location="org/ofbiz/product/inventory/InventoryReserveServices.xml" invoke="reserveOrderItemInventory" auth="true">
         <description>Create OrderItemShipGrpInvRes or increment existing reserved quantity.</description>

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryServices.java?rev=614970&r1=614969&r2=614970&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryServices.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/inventory/InventoryServices.java Thu Jan 24 12:13:48 2008
@@ -1004,4 +1004,37 @@
         return result;
     }
 
+    public static Map checkInventoryAlreadyReserved(DispatchContext dctx, Map context) {
+        GenericDelegator delegator = dctx.getDelegator();
+
+        String orderId = (String) context.get("orderId");
+        String orderItemSeqId = (String) context.get("orderItemSeqId");
+        double reserving = (Double) context.get("quantity");
+        try {
+            // count quantity ordered
+            double ordered = 0.0;
+            GenericValue item = delegator.findByPrimaryKey("OrderItem", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId));
+            if (item == null) return ServiceUtil.returnError("Order Item for order ["+orderId+"] with sequence Id ["+orderItemSeqId+"] not found.");
+            ordered = item.getDouble("quantity");
+            ordered -= (item.get("cancelQuantity") == null ? 0 : item.getDouble("cancelQuantity"));
+
+            // count up the quantity already reserved for this item  (note that canceling a reservation deletes it, thus this data represents what's actually reserved)
+            double reserved = 0.0;
+            List<GenericValue> reservations = delegator.findByAnd("OrderItemShipGrpInvRes", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId));
+            for (GenericValue reservation : reservations) {
+                if (reservation.get("quantity") == null) continue; // paranoia
+                reserved += reservation.getDouble("quantity");
+            }
+
+            // make sure we're not over reserving the item
+            if (reserving > (ordered - reserved)) {
+                return ServiceUtil.returnError("Cannot reserve " + reserving + " of Product ["+item.get("productId")+"].  There are already " +
+                        reserved + " reserved out of " + ordered + " ordered for order ["+orderId+"] line item ["+orderItemSeqId+"].");
+            }
+
+            return ServiceUtil.returnSuccess();
+        } catch (GenericEntityException e) {
+            return ServiceUtil.returnError("Inventory Item/Transfer lookup problem [" + e.getMessage() + "]");
+        }
+    }
 }