[OFBiz] SVN: r6764 - trunk/applications/product/src/org/ofbiz/shipment/packing

jaz@svn.ofbiz.org jaz at svn.ofbiz.org
Fri Feb 17 12:20:56 CST 2006


Author: jaz
Date: 2006-02-17 12:20:54 -0600 (Fri, 17 Feb 2006)
New Revision: 6764

Modified:
   trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java
   trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java
Log:
fixed packing code to allow packing items with multiple inventory reservations


Modified: trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java
===================================================================
--- trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java	2006-02-17 17:49:07 UTC (rev 6763)
+++ trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java	2006-02-17 18:20:54 UTC (rev 6764)
@@ -59,6 +59,8 @@
             quantity = new Double(1);
         }
 
+        Debug.log("Pack input [" + productId + "] @ [" + quantity + "]", module);
+        
         try {
             session.addOrIncreaseLine(orderId, null, shipGroupSeqId, productId, quantity.doubleValue(), packageSeq.intValue());
         } catch (GeneralException e) {

Modified: trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java
===================================================================
--- trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java	2006-02-17 17:49:07 UTC (rev 6763)
+++ trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java	2006-02-17 18:20:54 UTC (rev 6764)
@@ -99,7 +99,7 @@
     public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId) throws GeneralException {
         // reset the session if we just completed
         if (status == 0) {
-            throw new GeneralException("Packing session has been completed; be sure to CLEAR before packing a new order!");
+            throw new GeneralException("Packing session has been completed; be sure to CLEAR before packing a new order! [000]");
         }
 
         // find the actual product ID
@@ -125,48 +125,54 @@
 
         // no reservations we cannot add this item
         if (UtilValidate.isEmpty(reservations)) {
-            throw new GeneralException("No inventory reservations available; cannot pack this item!");
+            throw new GeneralException("No inventory reservations available; cannot pack this item! [101]");
         }
 
         // find the inventoryItemId to use
-        GenericValue res = null;
-        int checkCode = 0;
         if (reservations.size() == 1) {
-            res = EntityUtil.getFirst(reservations);
-            checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId);
+            GenericValue res = EntityUtil.getFirst(reservations);
+            int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId);
+            this.createPackLineItem(checkCode, res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, packageSeqId);
         } else {
             // more than one reservation found
+            Map toCreateMap = FastMap.newInstance();
             Iterator i = reservations.iterator();
-            while (i.hasNext()) {
-                res = (GenericValue) i.next();
-                int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId);
-                if (thisCheck > 0) {
-                    checkCode = thisCheck;
-                    break;
+            double qtyRemain = quantity;
+
+            while (i.hasNext() && qtyRemain > 0) {
+                GenericValue res = (GenericValue) i.next();
+                double resQty = res.getDouble("quantity").doubleValue();
+                double thisQty = resQty > qtyRemain ? qtyRemain : resQty;
+
+                int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, thisQty, packageSeqId);
+                switch (thisCheck) {
+                    case 2:
+                        Debug.log("Packing check returned '2' - new pack line will be created!", module);
+                        toCreateMap.put(res, new Double(resQty));
+                        qtyRemain -= resQty;
+                        break;
+                    case 1:
+                        Debug.log("Packing check returned '1' - existing pack line has been updated!", module);
+                        qtyRemain -= resQty;
+                        break;
+                    case 0:
+                        Debug.log("Packing check returned '0' - doing nothing.", module);
+                        break;
                 }
             }
-        }
 
-        // process the result; add new item if necessary
-        switch(checkCode) {
-            case 0:
-                // not enough reserved
-                throw new GeneralException("Not enough inventory reservation available; cannot pack the item!");
-            case 1:
-                // we're all good to go; quantity already updated
-                break;
-            case 2:
-                // need to create a new item
-                String invItemId = res.getString("inventoryItemId");
-                packLines.add(new PackingSessionLine(orderId, orderItemSeqId, shipGroupSeqId, productId, invItemId, quantity, packageSeqId));
-                break;
+            if (qtyRemain == 0) {
+                Iterator x = toCreateMap.keySet().iterator();
+                while (x.hasNext()) {
+                    GenericValue res = (GenericValue) x.next();
+                    Double qty = (Double) toCreateMap.get(res);
+                    this.createPackLineItem(2, res, orderId, orderItemSeqId, shipGroupSeqId, productId, qty.doubleValue(), packageSeqId);
+                }
+            } else {
+                throw new GeneralException("Not enough inventory reservation available; cannot pack the item! [103]");
+            }
         }
 
-        // update the package sequence
-        if (packageSeqId > packageSeq) {
-            this.packageSeq = packageSeqId;
-        }
-
         // run the add events
         this.runEvents(PackingEvent.EVENT_CODE_ADD);
     }
@@ -184,14 +190,39 @@
         Iterator i = lines.iterator();
         while (i.hasNext()) {
             PackingSessionLine line = (PackingSessionLine) i.next();
-            if (orderId.equals(line.getOrderId()) && orderItemSeqId.equals(line.getOrderItemSeqId()) &&
-                    shipGroupSeqId.equals(line.getShipGroupSeqId()) && packageSeq == line.getPackageSeq()) {
+            if (orderId.equals(line.getOrderId()) &&
+                    orderItemSeqId.equals(line.getOrderItemSeqId()) &&
+                    shipGroupSeqId.equals(line.getShipGroupSeqId()) &&
+                    inventoryItemId.equals(line.getInventoryItemId()) && 
+                    packageSeq == line.getPackageSeq()) {
                 return line;
             }
         }
         return null;
     }
 
+    protected void createPackLineItem(int checkCode, GenericValue res, String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId) throws GeneralException {
+        // process the result; add new item if necessary
+        switch(checkCode) {
+            case 0:
+                // not enough reserved
+                throw new GeneralException("Not enough inventory reservation available; cannot pack the item! [201]");
+            case 1:
+                // we're all good to go; quantity already updated
+                break;
+            case 2:
+                // need to create a new item
+                String invItemId = res.getString("inventoryItemId");
+                packLines.add(new PackingSessionLine(orderId, orderItemSeqId, shipGroupSeqId, productId, invItemId, quantity, packageSeqId));
+                break;
+        }
+
+        // update the package sequence
+        if (packageSeqId > packageSeq) {
+            this.packageSeq = packageSeqId;
+        }
+    }
+
     protected String findOrderItemSeqId(String productId, String orderId, String shipGroupSeqId, double quantity) throws GeneralException {
         Map lookupMap = FastMap.newInstance();
         lookupMap.put("orderId", orderId);
@@ -228,7 +259,7 @@
 
         PackingSessionLine line = this.findLine(orderId, orderItemSeqId, shipGroupSeqId, invItemId, packageSeqId);
         if (line == null) {
-            Debug.log("No current line found testing R: " + resQty + " / Q: " + quantity, module);
+            Debug.log("No current line found testing [" + invItemId + "] R: " + resQty + " / Q: " + quantity, module);
             if (resQty < quantity) {
                 return 0;
             } else {
@@ -236,7 +267,7 @@
             }
         } else {
             double newQty = line.getQuantity() + quantity;
-            Debug.log("Existing line found testing R: " + resQty + " / Q: " + newQty, module);
+            Debug.log("Existing line found testing [" + invItemId + "] R: " + resQty + " / Q: " + newQty, module);
             if (resQty < newQty) {
                 return 0;
             } else {



More information about the Svn mailing list