From d6218b293d038284e1283ca91ef69302dd8cb67e Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Sat, 12 Jan 2019 12:57:57 +0000 Subject: [PATCH] "Applied fix from trunk for revision: 1850914" ------------------------------------------------------------------------ r1850914 | paulfoxworthy | 2019-01-10 04:32:33 +0100 (jeu. 10 janv. 2019) | 7 lignes Fixed: In packing, only use reservations with stock on hand (OFBIZ-9677) During packing, OFBiz looks for reservations (OrderItemShipGrpInvRes) for the order item. Some reservations are for back ordered items not on hand. These reservations should not be used during packing, but they are. ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/branches/release17.12@1851164 13f79535-47bb-0310-9956-ffa450edef68 --- .../shipment/packing/PackingSession.java | 75 ++++++++++++------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/applications/product/src/main/java/org/apache/ofbiz/shipment/packing/PackingSession.java b/applications/product/src/main/java/org/apache/ofbiz/shipment/packing/PackingSession.java index 26c0b01228..4fef9df249 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/shipment/packing/PackingSession.java +++ b/applications/product/src/main/java/org/apache/ofbiz/shipment/packing/PackingSession.java @@ -144,8 +144,12 @@ public class PackingSession implements java.io.Serializable { // find the inventoryItemId to use if (reservations.size() == 1) { GenericValue res = EntityUtil.getFirst(reservations); - int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, packageSeqId, update); - this.createPackLineItem(checkCode, res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, weight, packageSeqId); + BigDecimal resQty = numAvailableItems(res); + + if (resQty.compareTo(quantity) > 0) { + int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, packageSeqId, update); + this.createPackLineItem(checkCode, res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, weight, packageSeqId); + } } else { // more than one reservation found Map toCreateMap = new HashMap(); @@ -160,33 +164,36 @@ public class PackingSession implements java.io.Serializable { continue; } - BigDecimal resQty = res.getBigDecimal("quantity"); - BigDecimal resPackedQty = this.getPackedQuantity(orderId, orderItemSeqId, shipGroupSeqId, productId, res.getString("inventoryItemId"), -1); - if (resPackedQty.compareTo(resQty) >= 0) { - continue; - } else if (!update) { - resQty = resQty.subtract(resPackedQty); - } + BigDecimal resQty = numAvailableItems(res); - BigDecimal thisQty = resQty.compareTo(qtyRemain) > 0 ? qtyRemain : resQty; + if (resQty.compareTo(BigDecimal.ZERO) > 0) { + BigDecimal resPackedQty = this.getPackedQuantity(orderId, orderItemSeqId, shipGroupSeqId, productId, res.getString("inventoryItemId"), -1); + if (resPackedQty.compareTo(resQty) >= 0) { + continue; + } else if (!update) { + resQty = resQty.subtract(resPackedQty); + } - int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, productId, thisQty, packageSeqId, update); - switch (thisCheck) { - case 2: - Debug.logInfo("Packing check returned '2' - new pack line will be created!", module); - toCreateMap.put(res, thisQty); - qtyRemain = qtyRemain.subtract(thisQty); - break; - case 1: - Debug.logInfo("Packing check returned '1' - existing pack line has been updated!", module); - qtyRemain = qtyRemain.subtract(thisQty); - break; - case 0: - Debug.logInfo("Packing check returned '0' - doing nothing.", module); - break; - default: - Debug.logInfo("Packing check returned '> 2' or '< 0'", module); - break; + BigDecimal thisQty = resQty.compareTo(qtyRemain) > 0 ? qtyRemain : resQty; + + int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, productId, thisQty, packageSeqId, update); + switch (thisCheck) { + case 2: + Debug.logInfo("Packing check returned '2' - new pack line will be created!", module); + toCreateMap.put(res, thisQty); + qtyRemain = qtyRemain.subtract(thisQty); + break; + case 1: + Debug.logInfo("Packing check returned '1' - existing pack line has been updated!", module); + qtyRemain = qtyRemain.subtract(thisQty); + break; + case 0: + Debug.logInfo("Packing check returned '0' - doing nothing.", module); + break; + default: + Debug.logInfo("Packing check returned '> 2' or '< 0'", module); + break; + } } } @@ -205,6 +212,20 @@ public class PackingSession implements java.io.Serializable { this.runEvents(PackingEvent.EVENT_CODE_ADD); } + private BigDecimal numAvailableItems(GenericValue res) { + // In simple situations, the reserved quantity will match the quantity from the order item. + // If there is a back order, quantity from order may exceed quantity currently reserved and on hand. + // resQty should never exceed the quantity from the order item, because that quantity was the quantity reserved in the first place. + BigDecimal notAvailable = res.getBigDecimal("quantityNotAvailable"); + BigDecimal resQty = res.getBigDecimal("quantity"); + + if (notAvailable != null) { + resQty = resQty.subtract(notAvailable); + } + + return resQty; + } + public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, BigDecimal quantity, int packageSeqId) throws GeneralException { this.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, null, quantity, packageSeqId, BigDecimal.ZERO, false); }