svn commit: r496780 - in /ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing: PackingEvent.java PackingServices.java PackingSession.java PackingSessionLine.java

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

svn commit: r496780 - in /ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing: PackingEvent.java PackingServices.java PackingSession.java PackingSessionLine.java

jaz-3
Author: jaz
Date: Tue Jan 16 09:21:14 2007
New Revision: 496780

URL: http://svn.apache.org/viewvc?view=rev&rev=496780
Log:
updated packing code used in production by several clients; just now getting back into the project

Modified:
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingEvent.java
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSessionLine.java

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingEvent.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingEvent.java?view=diff&rev=496780&r1=496779&r2=496780
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingEvent.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingEvent.java Tue Jan 16 09:21:14 2007
@@ -1,13 +1,12 @@
 /*
+ * Copyright 2001-2007 The Apache Software Foundation
  *
- * Copyright 2001-2006 The Apache Software Foundation
- *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- *
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java?view=diff&rev=496780&r1=496779&r2=496780
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingServices.java Tue Jan 16 09:21:14 2007
@@ -1,13 +1,12 @@
 /*
+ * Copyright 2001-2007 The Apache Software Foundation
  *
- * Copyright 2001-2006 The Apache Software Foundation
- *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- *
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -41,6 +40,10 @@
         String instructions = (String) context.get("handlingInstructions");
         session.setHandlingInstructions(instructions);
 
+        // set the picker party id -- will clear out previous if now null
+        String pickerPartyId = (String) context.get("pickerPartyId");
+        session.setPickerPartyId(pickerPartyId);
+
         if (quantity == null) {
             quantity = new Double(1);
         }
@@ -48,7 +51,7 @@
         Debug.log("Pack input [" + productId + "] @ [" + quantity + "]", module);
         
         try {
-            session.addOrIncreaseLine(orderId, null, shipGroupSeqId, productId, quantity.doubleValue(), packageSeq.intValue());
+            session.addOrIncreaseLine(orderId, null, shipGroupSeqId, productId, quantity.doubleValue(), packageSeq.intValue(), false);
         } catch (GeneralException e) {
             Debug.logError(e, module);
             return ServiceUtil.returnError(e.getMessage());
@@ -61,42 +64,86 @@
         PackingSession session = (PackingSession) context.get("packingSession");
         String orderId = (String) context.get("orderId");
         String shipGroupSeqId = (String) context.get("shipGroupSeqId");
+        Boolean updateQuantity = (Boolean) context.get("updateQuantity");
+        if (updateQuantity == null) {
+            updateQuantity = Boolean.FALSE;
+        }
 
         // set the instructions -- will clear out previous if now null
         String instructions = (String) context.get("handlingInstructions");
         session.setHandlingInstructions(instructions);
 
+        // set the picker party id -- will clear out previous if now null
+        String pickerPartyId = (String) context.get("pickerPartyId");
+        session.setPickerPartyId(pickerPartyId);
+
+        Map selInfo = (Map) context.get("selInfo");
         Map prdInfo = (Map) context.get("prdInfo");
         Map qtyInfo = (Map) context.get("qtyInfo");
         Map pkgInfo = (Map) context.get("pkgInfo");
-        Map selInfo = (Map) context.get("selInfo");
+
         if (selInfo != null) {
             Iterator i = selInfo.keySet().iterator();
             while (i.hasNext()) {
                 String orderItemSeqId = (String) i.next();
-                String qtyStr = (String) qtyInfo.get(orderItemSeqId);
-                String pkgStr = (String) pkgInfo.get(orderItemSeqId);
                 String prdStr = (String) prdInfo.get(orderItemSeqId);
                 if (UtilValidate.isEmpty(prdStr)) {
                     // set the productId to null if empty
                     prdStr = null;
                 }
+
+                // base package/quantity strings
+                String pkgStr = (String) pkgInfo.get(orderItemSeqId);
+                String qtyStr = (String) qtyInfo.get(orderItemSeqId);
+
                 Debug.log("Item: " + orderItemSeqId + " / Product: " + prdStr + " / Quantity: " + qtyStr + " /  Package: " + pkgStr, module);
 
-                double quantity = 0;
-                int packageSeq = 0;
-                try {
-                    quantity = Double.parseDouble(qtyStr);
-                    packageSeq = Integer.parseInt(pkgStr);
-                } catch (Exception e) {
-                    return ServiceUtil.returnError(e.getMessage());
+                // array place holders
+                String[] quantities;
+                String[] packages;
+
+                // process the package array
+                if (pkgStr.indexOf(",") != -1) {
+                    // this is a multi-box update
+                    packages = pkgStr.split(",");
+                } else {
+                    packages = new String[] { pkgStr };
+                }
+
+                // check to make sure there is at least one package
+                if (packages == null || packages.length == 0) {
+                    return ServiceUtil.returnError("No packages defined for processing.");
+                }
+
+                // process the quantity array
+                if (qtyStr == null) {
+                    quantities = new String[packages.length];
+                    for (int p = 0; p < packages.length; p++) {
+                        quantities[p] = (String) qtyInfo.get(orderItemSeqId + ":" + packages[p]);
+                    }
+                    if (quantities.length != packages.length) {
+                        return ServiceUtil.returnError("Packages and quantities do not match.");
+                    }
+                } else {
+                    quantities = new String[] { qtyStr };
                 }
 
-                try {
-                    session.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, prdStr, quantity, packageSeq);
-                } catch (GeneralException e) {
-                    Debug.logError(e, module);
-                    return ServiceUtil.returnError(e.getMessage());
+                for (int p = 0; p < packages.length; p++) {
+                    double quantity;
+                    int packageSeq;
+                    try {
+                        quantity = Double.parseDouble(quantities[p]);
+                        packageSeq = Integer.parseInt(packages[p]);
+                    } catch (Exception e) {
+                        return ServiceUtil.returnError(e.getMessage());
+                    }
+
+                    try {
+                        session.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, prdStr, quantity, packageSeq, updateQuantity.booleanValue());
+                    } catch (GeneralException e) {
+                        Debug.logError(e, module);
+                        return ServiceUtil.returnError(e.getMessage());
+                    }
                 }
             }
         }
@@ -112,6 +159,14 @@
         return result;
     }
 
+    public static Map clearLastPackage(DispatchContext dctx, Map context) {
+        PackingSession session = (PackingSession) context.get("packingSession");
+        int nextSeq = session.clearLastPackage();
+        Map result = ServiceUtil.returnSuccess();
+        result.put("nextPackageSeq", new Integer(nextSeq));
+        return result;
+    }
+
     public static Map clearPackLine(DispatchContext dctx, Map context) {
         PackingSession session = (PackingSession) context.get("packingSession");
         String orderId = (String) context.get("orderId");
@@ -135,7 +190,7 @@
 
     public static Map clearPackAll(DispatchContext dctx, Map context) {
         PackingSession session = (PackingSession) context.get("packingSession");
-        session.clear();
+        session.clearAllLines();
 
         return ServiceUtil.returnSuccess();
     }
@@ -146,7 +201,9 @@
 
         // set the instructions -- will clear out previous if now null
         String instructions = (String) context.get("handlingInstructions");
+        String pickerPartyId = (String) context.get("pickerPartyId");
         session.setHandlingInstructions(instructions);
+        session.setPickerPartyId(pickerPartyId);
 
         Boolean force = (Boolean) context.get("forceComplete");
         if (force == null) {

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java?view=diff&rev=496780&r1=496779&r2=496780
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSession.java Tue Jan 16 09:21:14 2007
@@ -1,13 +1,12 @@
 /*
+ * Copyright 2001-2007 The Apache Software Foundation
  *
- * Copyright 2001-2006 The Apache Software Foundation
- *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- *
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -20,9 +19,11 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javolution.util.FastMap;
 import javolution.util.FastList;
+import javolution.util.FastSet;
 
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
@@ -31,6 +32,7 @@
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.entity.GenericDelegator;
 import org.ofbiz.entity.GenericValue;
+import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.util.EntityUtil;
 import org.ofbiz.service.GenericDispatcher;
 import org.ofbiz.service.GenericServiceException;
@@ -43,10 +45,12 @@
     public static final String module = PackingSession.class.getName();
 
     protected GenericValue userLogin = null;
+    protected String pickerPartyId = null;
     protected String primaryOrderId = null;
     protected String primaryShipGrp = null;
     protected String dispatcherName = null;
     protected String delegatorName = null;
+    protected String picklistBinId = null;
     protected String facilityId = null;
     protected String shipmentId = null;
     protected String instructions = null;
@@ -58,7 +62,7 @@
     private transient GenericDelegator _delegator = null;
     private transient LocalDispatcher _dispatcher = null;
 
-    public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin, String facilityId, String orderId, String shipGrp) {
+    public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin, String facilityId, String binId, String orderId, String shipGrp) {
         this._dispatcher = dispatcher;
         this.dispatcherName = dispatcher.getName();
 
@@ -67,6 +71,7 @@
 
         this.primaryOrderId = orderId;
         this.primaryShipGrp = shipGrp;
+        this.picklistBinId = binId;
         this.userLogin = userLogin;
         this.facilityId = facilityId;
         this.packLines = new ArrayList();
@@ -75,19 +80,24 @@
     }
 
     public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin, String facilityId) {
-        this(dispatcher, userLogin, facilityId, null, null);
+        this(dispatcher, userLogin, facilityId, null, null, null);
     }
 
     public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin) {
-        this(dispatcher, userLogin, null, null, null);
+        this(dispatcher, userLogin, null, null, null, null);
     }
 
-    public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId) throws GeneralException {
+    public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId, boolean update) 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! [000]");
         }
 
+        // do nothing if we are trying to add a quantity of 0
+        if (!update && quantity == 0) {
+            return;
+        }
+
         // find the actual product ID
         productId = ProductWorker.findProductId(this.getDelegator(), productId);
 
@@ -117,7 +127,7 @@
         // find the inventoryItemId to use
         if (reservations.size() == 1) {
             GenericValue res = EntityUtil.getFirst(reservations);
-            int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId);
+            int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId, update);
             this.createPackLineItem(checkCode, res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, packageSeqId);
         } else {
             // more than one reservation found
@@ -130,7 +140,7 @@
                 double resQty = res.getDouble("quantity").doubleValue();
                 double thisQty = resQty > qtyRemain ? qtyRemain : resQty;
 
-                int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, thisQty, packageSeqId);
+                int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, thisQty, packageSeqId, update);
                 switch (thisCheck) {
                     case 2:
                         Debug.log("Packing check returned '2' - new pack line will be created!", module);
@@ -164,11 +174,11 @@
     }
 
     public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, double quantity, int packageSeqId) throws GeneralException {
-        this.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, null, quantity, packageSeqId);
+        this.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, null, quantity, packageSeqId, false);
     }
 
     public void addOrIncreaseLine(String productId, double quantity, int packageSeqId) throws GeneralException {
-        this.addOrIncreaseLine(null, null, null, productId, quantity, packageSeqId);
+        this.addOrIncreaseLine(null, null, null, productId, quantity, packageSeqId, false);
     }
 
     public PackingSessionLine findLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String inventoryItemId, int packageSeq) {
@@ -213,6 +223,7 @@
         Map lookupMap = FastMap.newInstance();
         lookupMap.put("orderId", orderId);
         lookupMap.put("productId", productId);
+        lookupMap.put("statusId", "ITEM_APPROVED");
         lookupMap.put("shipGroupSeqId", shipGroupSeqId);
 
         List sort = UtilMisc.toList("-quantity");
@@ -223,10 +234,21 @@
             Iterator i = orderItems.iterator();
             while (i.hasNext()) {
                 GenericValue item = (GenericValue) i.next();
-                Double qty = item.getDouble("quantity");
-                if (quantity <= qty.doubleValue()) {
-                    orderItemSeqId = item.getString("orderItemSeqId");
-                    break;
+
+                // get the reservations for the item
+                Map invLookup = FastMap.newInstance();
+                invLookup.put("orderId", orderId);
+                invLookup.put("orderItemSeqId", item.getString("orderItemSeqId"));
+                invLookup.put("shipGroupSeqId", shipGroupSeqId);
+                List reservations = this.getDelegator().findByAnd("OrderItemShipGrpInvRes", invLookup);
+                Iterator resIter = reservations.iterator();
+                while (resIter.hasNext()) {
+                    GenericValue res = (GenericValue) resIter.next();
+                    Double qty = res.getDouble("quantity");
+                    if (quantity <= qty.doubleValue()) {
+                        orderItemSeqId = item.getString("orderItemSeqId");
+                        break;
+                    }
                 }
             }
         }
@@ -238,23 +260,29 @@
         }
     }
 
-    protected int checkLineForAdd(GenericValue res, String orderId, String orderItemSeqId, String shipGroupSeqId, double quantity, int packageSeqId) {
+    protected int checkLineForAdd(GenericValue res, String orderId, String orderItemSeqId, String shipGroupSeqId, double quantity, int packageSeqId, boolean update) {
         // check to see if the reservation can hold the requested quantity amount
         String invItemId = res.getString("inventoryItemId");
         double resQty = res.getDouble("quantity").doubleValue();
 
         PackingSessionLine line = this.findLine(orderId, orderItemSeqId, shipGroupSeqId, invItemId, packageSeqId);
+        double packedQty = this.getPackedQuantity(orderId, orderItemSeqId, shipGroupSeqId);
+
+        Debug.log("Packed quantity [" + packedQty + "] + [" + quantity + "]", module);
+
         if (line == null) {
-            Debug.log("No current line found testing [" + invItemId + "] R: " + resQty + " / Q: " + quantity, module);
-            if (resQty < quantity) {
+            double checkQty = packedQty + quantity;
+            Debug.log("No current line found testing [" + invItemId + "] R: " + resQty + " / Q: " + checkQty, module);
+            if (resQty < checkQty) {
                 return 0;
             } else {
                 return 2;
             }
         } else {
-            double newQty = line.getQuantity() + quantity;
+            double checkQty = update ? ((packedQty - line.getQuantity()) + quantity) : packedQty + quantity;
+            double newQty = update ? quantity : (line.getQuantity() + quantity);            
             Debug.log("Existing line found testing [" + invItemId + "] R: " + resQty + " / Q: " + newQty, module);
-            if (resQty < newQty) {
+            if (resQty < checkQty) {
                 return 0;
             } else {
                 line.setQuantity(newQty);
@@ -280,6 +308,14 @@
     }
 
     public double getPackedQuantity(String orderId, String orderItemSeqId, String shipGroupSeqId) {
+        return getPackedQuantity(orderId, orderItemSeqId, shipGroupSeqId, null, -1);
+    }
+
+    public double getPackedQuantity(String orderId, String orderItemSeqId, String shipGroupSeqId, int packageSeq) {
+        return getPackedQuantity(orderId, orderItemSeqId, shipGroupSeqId, null, packageSeq);
+    }
+
+    public double getPackedQuantity(String orderId, String orderItemSeqId, String shipGroupSeqId, String inventoryItemId, int packageSeq) {
         double total = 0.0;
         List lines = this.getLines();
         Iterator i = lines.iterator();
@@ -287,12 +323,119 @@
             PackingSessionLine line = (PackingSessionLine) i.next();
             if (orderId.equals(line.getOrderId()) && orderItemSeqId.equals(line.getOrderItemSeqId()) &&
                     shipGroupSeqId.equals(line.getShipGroupSeqId())) {
+                if (inventoryItemId == null || inventoryItemId.equals(line.getInventoryItemId())) {
+                    if (packageSeq == -1 || packageSeq == line.getPackageSeq()) {
+                        total += line.getQuantity();
+                    }
+                }
+            }
+        }
+        return total;
+    }
+
+    public double getPackedQuantity(String productId, int packageSeq) {
+        if (productId != null) {
+            try {
+                productId = ProductWorker.findProductId(this.getDelegator(), productId);
+            } catch (GenericEntityException e) {
+                Debug.logError(e, module);
+            }
+        }
+
+        double total = 0.0;
+        if (productId != null ) {
+            List lines = this.getLines();
+            Iterator i = lines.iterator();
+            while (i.hasNext()) {
+                PackingSessionLine line = (PackingSessionLine) i.next();
+                if (productId.equals(line.getProductId())) {
+                    if (packageSeq == -1 || packageSeq == line.getPackageSeq()) {
+                        total += line.getQuantity();
+                    }
+                }
+            }
+        }
+        return total;
+    }
+
+    public double getPackedQuantity(int packageSeq) {
+        double total = 0.0;
+        List lines = this.getLines();
+        Iterator i = lines.iterator();
+        while (i.hasNext()) {
+            PackingSessionLine line = (PackingSessionLine) i.next();
+            if (packageSeq == -1 || packageSeq == line.getPackageSeq()) {
                 total += line.getQuantity();
             }
         }
         return total;
     }
 
+    public double getPackedQuantity(String productId) {
+        return getPackedQuantity(productId, -1);
+    }
+
+    public double getCurrentReservedQuantity(String orderId, String orderItemSeqId, String shipGroupSeqId) {
+        double reserved = -1;
+        List res = null;
+        try {
+            res = this.getDelegator().findByAnd("OrderItemShipGrpInvRes", UtilMisc.toMap("orderId", orderId,
+                    "orderItemSeqId", orderItemSeqId, "shipGroupSeqId", shipGroupSeqId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+
+        if (res != null) {
+            reserved = 0.0;
+            Iterator i = res.iterator();
+            while (i.hasNext()) {
+                GenericValue v = (GenericValue) i.next();
+                Double qty = v.getDouble("quantity");
+                if (qty == null) qty = new Double(0);
+                reserved += qty.doubleValue();
+            }
+        }
+
+        return reserved;
+    }
+
+    public double getCurrentShippedQuantity(String orderId, String orderItemSeqId, String shipGroupSeqId) {
+        double shipped = 0.0;
+        List issues = this.getItemIssuances(orderId, orderItemSeqId, shipGroupSeqId);
+        if (issues != null) {
+            Iterator i = issues.iterator();
+            while (i.hasNext()) {
+                GenericValue v = (GenericValue) i.next();
+                Double qty = v.getDouble("quantity");
+                if (qty == null) qty = new Double(0);
+                shipped += qty.doubleValue();
+            }
+        }
+
+        return shipped;
+    }
+
+    public List getCurrentShipmentIds(String orderId, String orderItemSeqId, String shipGroupSeqId) {
+        Set shipmentIds = FastSet.newInstance();
+        List issues = this.getItemIssuances(orderId, orderItemSeqId, shipGroupSeqId);
+
+        if (issues != null) {
+            Iterator i = issues.iterator();
+            while (i.hasNext()) {
+                GenericValue v = (GenericValue) i.next();
+                shipmentIds.add(v.getString("shipmentId"));
+            }
+        }
+
+        List retList = FastList.newInstance();
+        retList.addAll(shipmentIds);
+        return retList;
+    }
+
+    public List getCurrentShipmentIds(String orderId, String shipGroupSeqId) {
+        return this.getCurrentShipmentIds(orderId, null, shipGroupSeqId);
+    }
+
     public void registerEvent(PackingEvent event) {
         this.packEvents.add(event);
         this.runEvents(PackingEvent.EVENT_CODE_EREG);
@@ -348,6 +491,14 @@
         this.primaryShipGrp = shipGroupSeqId;
     }
 
+    public void setPicklistBinId(String binId) {
+        this.picklistBinId = binId;
+    }
+
+    public String getPicklistBinId() {
+        return this.picklistBinId;
+    }
+
     public String getHandlingInstructions() {
         return this.instructions;
     }
@@ -356,13 +507,44 @@
         this.instructions = instructions;
     }
 
+    public void setPickerPartyId(String partyId) {
+        this.pickerPartyId = partyId;
+    }
+
+    public String getPickerPartyId() {
+        return this.pickerPartyId;
+    }
+
+    public int clearLastPackage() {
+        if (packageSeq == 1) {
+            this.clear();
+            return packageSeq;
+        }
+        
+        List currentLines = new ArrayList(this.packLines);
+        Iterator i = currentLines.iterator();
+        while (i.hasNext()) {
+            PackingSessionLine line = (PackingSessionLine) i.next();
+            if (line.getPackageSeq() == packageSeq) {
+                this.clearLine(line);
+            }
+        }
+        return --packageSeq;
+    }
+
     public void clearLine(PackingSessionLine line) {
         this.packLines.remove(line);
     }
 
+    public void clearAllLines() {
+        this.packLines.clear();
+    }
+
     public void clear() {
         this.packLines.clear();
         this.instructions = null;
+        this.pickerPartyId = null;
+        this.picklistBinId = null;
         this.primaryOrderId = null;
         this.primaryShipGrp = null;
         this.packageSeq = 1;
@@ -371,6 +553,10 @@
     }
 
     public String complete(boolean force) throws GeneralException {
+        // clear out empty lines
+        // this.checkEmptyLines(); // removing, this seems to be causeing issues -  mja
+
+        // check to see if there is anything to process
         if (this.getLines().size() == 0) {
             return "EMPTY";
         }
@@ -389,6 +575,8 @@
         this.applyItemsToPackages();
         // set the shipment to packed
         this.setShipmentToPacked();
+        // set role on picklist
+        this.setPickerOnPicklist();
         // run the complete events
         this.runEvents(PackingEvent.EVENT_CODE_COMPLETE);
 
@@ -400,32 +588,35 @@
         Iterator i = this.getLines().iterator();
         while (i.hasNext()) {
             PackingSessionLine line = (PackingSessionLine) i.next();
-            Map invLookup = FastMap.newInstance();
-            invLookup.put("orderId", line.getOrderId());
-            invLookup.put("orderItemSeqId", line.getOrderItemSeqId());
-            invLookup.put("shipGroupSeqId", line.getShipGroupSeqId());
-            invLookup.put("inventoryItemId", line.getInventoryItemId());
-            GenericValue res = this.getDelegator().findByPrimaryKey("OrderItemShipGrpInvRes", invLookup);
-            Double qty = res.getDouble("quantity");
-            if (qty == null) qty = new Double(0);
-
-            double resQty = qty.doubleValue();
-            double lineQty = line.getQuantity();
+            double reservedQty =  this.getCurrentReservedQuantity(line.getOrderId(), line.getOrderItemSeqId(), line.getShipGroupSeqId());
+            double packedQty = this.getPackedQuantity(line.getOrderId(), line.getOrderItemSeqId(), line.getShipGroupSeqId());
 
-            if (lineQty != resQty) {
-                errors.add("Packed amount does not match reserved amount for item (" + line.getProductId() + ") [" + lineQty + " / " + resQty + "]");
+            if (packedQty != reservedQty) {
+                errors.add("Packed amount does not match reserved amount for item (" + line.getProductId() + ") [" + packedQty + " / " + reservedQty + "]");
             }
         }
 
         if (errors.size() > 0) {
             if (!ignore) {
-                throw new GeneralException("Attempt to pack order failed. Click COMPLETE again to force.", errors);
+                throw new GeneralException("Attempt to pack order failed.", errors);
             } else {
                 Debug.logWarning("Packing warnings: " + errors, module);
             }
         }
     }
 
+    protected void checkEmptyLines() throws GeneralException {
+        List lines = FastList.newInstance();
+        lines.addAll(this.getLines());
+        Iterator i = lines.iterator();
+        while (i.hasNext()) {
+            PackingSessionLine l = (PackingSessionLine) i.next();
+            if (l.getQuantity() == 0) {
+                this.packLines.remove(l);
+            }
+        }
+    }
+
     protected void runEvents(int eventCode) {
         if (this.packEvents.size() > 0) {
             Iterator i = this.packEvents.iterator();
@@ -436,6 +627,29 @@
         }
     }
 
+    protected List getItemIssuances(String orderId, String orderItemSeqId, String shipGroupSeqId) {
+        List issues = null;
+        if (orderId == null) {
+            throw new IllegalArgumentException("Value for orderId is  null");
+        }
+
+        Map lookupMap = FastMap.newInstance();
+        lookupMap.put("orderId", orderId);
+        if (UtilValidate.isNotEmpty(orderItemSeqId)) {
+            lookupMap.put("orderItemSeqId", orderItemSeqId);
+        }
+        if (UtilValidate.isNotEmpty(shipGroupSeqId)) {
+            lookupMap.put("shipGroupSeqId", shipGroupSeqId);
+        }
+        try {
+            issues = this.getDelegator().findByAnd("ItemIssuance",  lookupMap);
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+
+        return issues;
+    }
+
     protected void createShipment() throws GeneralException {
         // first create the shipment
         Map newShipment = FastMap.newInstance();
@@ -445,6 +659,7 @@
         newShipment.put("shipmentTypeId", "OUTGOING_SHIPMENT");
         newShipment.put("statusId", "SHIPMENT_INPUT");
         newShipment.put("handlingInstructions", instructions);
+        newShipment.put("picklistBinId", picklistBinId);
         newShipment.put("userLogin", userLogin);
         Debug.log("Creating new shipment with context: " + newShipment, module);
         Map newShipResp = this.getDispatcher().runSync("createShipment", newShipment);
@@ -456,14 +671,34 @@
     }
 
     protected void issueItemsToShipment() throws GeneralException {
+        List processedLines = FastList.newInstance();
         List lines = this.getLines();
         Iterator i = lines.iterator();
         while (i.hasNext()) {
             PackingSessionLine line = (PackingSessionLine) i.next();
-            line.issueItemToShipment(shipmentId, userLogin, getDispatcher());
+            if (this.checkLine(processedLines, line)) {
+                double totalPacked = this.getPackedQuantity(line.getOrderId(),  line.getOrderItemSeqId(),
+                        line.getShipGroupSeqId(), line.getInventoryItemId(), -1);
+
+                line.issueItemToShipment(shipmentId, picklistBinId, userLogin, new Double(totalPacked), getDispatcher());
+                processedLines.add(line);
+            }
         }
     }
 
+    protected boolean checkLine(List processedLines, PackingSessionLine line) {
+        Iterator i = processedLines.iterator();
+        while (i.hasNext()) {
+            PackingSessionLine l = (PackingSessionLine) i.next();
+            if (line.isSameItem(l)) {
+                line.setShipmentItemSeqId(l.getShipmentItemSeqId());
+                return false;
+            }
+        }
+
+        return true;
+    }
+    
     protected void createPackages() throws GeneralException {
         for (int i = 0; i < packageSeq; i++) {
             String shipmentPackageSeqId = UtilFormatOut.formatPaddedNumber(i+1, 5);
@@ -495,6 +730,32 @@
         Map packedResp = this.getDispatcher().runSync("updateShipment", packedCtx);
         if (packedResp != null && ServiceUtil.isError(packedResp)) {
             throw new GeneralException(ServiceUtil.getErrorMessage(packedResp));
+        }
+    }
+
+    protected void setPickerOnPicklist() throws GeneralException {
+        if (picklistBinId != null) {
+            // first find the picklist id
+            GenericValue bin = this.getDelegator().findByPrimaryKey("PicklistBin", UtilMisc.toMap("picklistBinId", picklistBinId));
+            if (bin != null) {
+                Map ctx = FastMap.newInstance();
+                ctx.put("picklistId", bin.getString("picklistId"));
+                ctx.put("partyId", pickerPartyId);
+                ctx.put("roleTypeId", "PICKER");
+
+                // check if the role already exists and is valid
+                List currentRoles = this.getDelegator().findByAnd("PicklistRole", ctx);
+                currentRoles = EntityUtil.filterByDate(currentRoles);
+
+                // if not; create the role
+                if (currentRoles != null && currentRoles.size() > 0) {
+                    ctx.put("userLogin", userLogin);
+                    Map addRole = this.getDispatcher().runSync("createPicklistRole", ctx);
+                    if (ServiceUtil.isError(addRole)) {
+                        throw new GeneralException(ServiceUtil.getErrorMessage(addRole));
+                    }
+                }
+            }
         }
     }
 }

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSessionLine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSessionLine.java?view=diff&rev=496780&r1=496779&r2=496780
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSessionLine.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/packing/PackingSessionLine.java Tue Jan 16 09:21:14 2007
@@ -1,30 +1,31 @@
 /*
+ * Copyright 2001-2007 The Apache Software Foundation
  *
- * Copyright 2001-2006 The Apache Software Foundation
- *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- *
+ *
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
-
 package org.ofbiz.shipment.packing;
 
 import java.util.Map;
 
-import org.ofbiz.service.LocalDispatcher;
-import org.ofbiz.service.ServiceUtil;
-import org.ofbiz.entity.GenericValue;
+import javolution.util.FastMap;
+
 import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.UtilFormatOut;
-import javolution.util.FastMap;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.entity.GenericValue;
+import org.ofbiz.entity.GenericDelegator;
+import org.ofbiz.service.LocalDispatcher;
+import org.ofbiz.service.ServiceUtil;
 
 public class PackingSessionLine implements java.io.Serializable {
 
@@ -93,14 +94,31 @@
         return this.packageSeq;
     }
 
-    protected void issueItemToShipment(String shipmentId, GenericValue userLogin, LocalDispatcher dispatcher) throws GeneralException {
+    public boolean isSameItem(PackingSessionLine line) {
+        if (this.getInventoryItemId().equals(line.getInventoryItemId())) {
+            if (this.getOrderItemSeqId().equals(line.getOrderItemSeqId())) {
+                if (this.getOrderId().equals(line.getOrderId())) {
+                    if (this.getShipGroupSeqId().equals(line.getShipGroupSeqId())) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
+    protected void issueItemToShipment(String shipmentId, String picklistBinId, GenericValue userLogin, Double quantity, LocalDispatcher dispatcher) throws GeneralException {
+        if (quantity == null) {
+            quantity = new Double(this.getQuantity());
+        }
+
         Map issueMap = FastMap.newInstance();
         issueMap.put("shipmentId", shipmentId);
         issueMap.put("orderId", this.getOrderId());
         issueMap.put("orderItemSeqId", this.getOrderItemSeqId());
         issueMap.put("shipGroupSeqId", this.getShipGroupSeqId());
         issueMap.put("inventoryItemId", this.getInventoryItemId());
-        issueMap.put("quantity", new Double(this.getQuantity()));
+        issueMap.put("quantity", quantity);
         issueMap.put("userLogin", userLogin);
 
         Map issueResp = dispatcher.runSync("issueOrderItemShipGrpInvResToShipment", issueMap);
@@ -113,6 +131,39 @@
             throw new GeneralException("Issue item did not return a valid shipmentItemSeqId!");
         } else {
             this.setShipmentItemSeqId(shipmentItemSeqId);
+        }
+
+        if (picklistBinId != null) {
+            // find the pick list item
+            Debug.log("Looking up picklist item for bin ID #" + picklistBinId, module);
+            GenericDelegator delegator = dispatcher.getDelegator();
+            Map itemLookup = FastMap.newInstance();
+            itemLookup.put("picklistBinId", picklistBinId);
+            itemLookup.put("orderId", this.getOrderId());
+            itemLookup.put("orderItemSeqId", this.getOrderItemSeqId());
+            itemLookup.put("shipGroupSeqId", this.getShipGroupSeqId());
+            itemLookup.put("inventoryItemId", this.getInventoryItemId());
+            GenericValue plItem = delegator.findByPrimaryKey("PicklistItem", itemLookup);
+            if (plItem != null) {
+                Debug.log("Found picklist bin: " + plItem, module);
+                Double itemQty = plItem.getDouble("quantity");
+                if (itemQty.doubleValue() == quantity.doubleValue()) {
+                    // set to complete
+                    itemLookup.put("itemStatusId", "PICKITEM_COMPLETED");
+                } else {
+                    itemLookup.put("itemStatusId", "PICKITEM_CANCELLED");
+                }
+                itemLookup.put("userLogin", userLogin);
+
+                Map itemUpdateResp = dispatcher.runSync("updatePicklistItem", itemLookup);
+                if (ServiceUtil.isError(itemUpdateResp)) {
+                    throw new GeneralException(ServiceUtil.getErrorMessage(issueResp));
+                }
+            } else {
+                Debug.log("No item was found for lookup: " + itemLookup, module);
+            }
+        } else {
+            Debug.logWarning("*** NO Picklist Bin ID set; cannot update picklist status!", module);
         }
     }