Author: deepak
Date: Mon Dec 21 05:38:43 2015
New Revision: 1721093

URL: http://svn.apache.org/viewvc?rev=1721093&view=rev
Log:
(OFBIZ-4954) Applied patch from jira issue OFBIZ-4954
============================================================
Order item quantity cancel issue.

If user partially received an order and then cancel order item, system cancel 
all the order item quantity.System does not check for shipped/received order 
item quantity.
Expected behavior: Only remaining order item quantity should be cancelled and 
order item should marked as complete with partial received/shipped quantity.
============================================================

Modified:
    ofbiz/trunk/applications/order/servicedef/secas.xml
    ofbiz/trunk/applications/order/servicedef/services_cart.xml
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartServices.java

Modified: ofbiz/trunk/applications/order/servicedef/secas.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/servicedef/secas.xml?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/servicedef/secas.xml (original)
+++ ofbiz/trunk/applications/order/servicedef/secas.xml Mon Dec 21 05:38:43 2015
@@ -150,7 +150,7 @@ under the License.
     </eca>
 
     <!-- cancel order items -->
-    <eca service="cancelOrderItem" event="commit">
+    <eca service="cancelOrderItem" event="global-commit-post-run">
         <action service="recreateOrderAdjustments" mode="sync"/>
         <action service="resetGrandTotal" mode="sync"/>
         <action service="sendOrderChangeNotification" mode="async" 
persist="true"/>

Modified: ofbiz/trunk/applications/order/servicedef/services_cart.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/servicedef/services_cart.xml?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/servicedef/services_cart.xml (original)
+++ ofbiz/trunk/applications/order/servicedef/services_cart.xml Mon Dec 21 
05:38:43 2015
@@ -112,6 +112,7 @@ under the License.
             location="org.ofbiz.order.shoppingcart.ShoppingCartServices" 
invoke="loadCartFromOrder">
         <description>Create a ShoppingCart Object based on an existing 
order</description>
         <attribute name="orderId" type="String" mode="IN" optional="false"/>
+        <attribute name="createAsNewOrder" type="String" mode="IN" 
default-value="N"/>
         <attribute name="skipInventoryChecks" type="Boolean" mode="IN" 
optional="true"/>
         <attribute name="skipProductChecks" type="Boolean" mode="IN" 
optional="true"/>
         <attribute name="includePromoItems" type="Boolean" mode="IN" 
optional="true"/>

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java 
(original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java 
Mon Dec 21 05:38:43 2015
@@ -3025,4 +3025,27 @@ public class OrderReadHelper {
         }
         return shippableSizes;
     }
+    public BigDecimal getItemReceivedQuantity(GenericValue orderItem) {
+        BigDecimal totalReceived = BigDecimal.ZERO;
+        try {
+            if (UtilValidate.isNotEmpty(orderItem)) {
+                EntityCondition cond = 
EntityCondition.makeCondition(UtilMisc.toList(
+                        EntityCondition.makeCondition("orderId", 
orderItem.getString("orderId")),
+                        EntityCondition.makeCondition("quantityAccepted", 
EntityOperator.GREATER_THAN, BigDecimal.ZERO),
+                        EntityCondition.makeCondition("orderItemSeqId", 
orderItem.getString("orderItemSeqId"))));
+                Delegator delegator = orderItem.getDelegator();
+                List<GenericValue> shipmentReceipts = 
EntityQuery.use(delegator).select("quantityAccepted", 
"quantityRejected").from("ShipmentReceiptAndItem").where(cond).queryList();
+                for (GenericValue shipmentReceipt : shipmentReceipts) {
+                    if (shipmentReceipt.getBigDecimal("quantityAccepted") != 
null)
+                        totalReceived = 
totalReceived.add(shipmentReceipt.getBigDecimal("quantityAccepted"));
+                    if (shipmentReceipt.getBigDecimal("quantityRejected") != 
null)
+                        totalReceived = 
totalReceived.add(shipmentReceipt.getBigDecimal("quantityRejected"));
+                }
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+        return totalReceived;
+    }
+
 }

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java 
(original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java 
Mon Dec 21 05:38:43 2015
@@ -2066,6 +2066,7 @@ public class OrderServices {
             fields.put("shipGroupSeqId", shipGroupSeqId);
         }
 
+        OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
         List<GenericValue> orderItemShipGroupAssocs = null;
         try {
             orderItemShipGroupAssocs = 
EntityQuery.use(delegator).from("OrderItemShipGroupAssoc").where(fields).queryList();
@@ -2078,6 +2079,7 @@ public class OrderServices {
         if (orderItemShipGroupAssocs != null) {
             for (GenericValue orderItemShipGroupAssoc : 
orderItemShipGroupAssocs) {
                 GenericValue orderItem = null;
+                String itemStatus = "ITEM_CANCELLED";
                 try {
                     orderItem = 
orderItemShipGroupAssoc.getRelatedOne("OrderItem", false);
                 } catch (GenericEntityException e) {
@@ -2103,11 +2105,23 @@ public class OrderServices {
                 if (availableQuantity == null) availableQuantity = 
BigDecimal.ZERO;
                 if (itemQuantity == null) itemQuantity = BigDecimal.ZERO;
 
+                if ("PURCHASE_ORDER".equals(orh.getOrderTypeId())) {
+                    BigDecimal receivedQty = 
orh.getItemReceivedQuantity(orderItem);
+                    if (receivedQty.compareTo(BigDecimal.ZERO) > 0)
+                        itemStatus = "ITEM_COMPLETED";
+                    itemQuantity = itemQuantity.subtract(receivedQty);
+                } else {
+                    BigDecimal shippedQty = 
orh.getItemShippedQuantity(orderItem);
+                    if (shippedQty.compareTo(BigDecimal.ZERO) > 0 )
+                        itemStatus = "ITEM_COMPLETED";
+                    itemQuantity = itemQuantity.subtract(shippedQty);
+                }
+
                 BigDecimal thisCancelQty = null;
                 if (cancelQuantity != null) {
                     thisCancelQty = cancelQuantity;
                 } else {
-                    thisCancelQty = availableQuantity;
+                    thisCancelQty = itemQuantity;
                 }
 
                 if (availableQuantity.compareTo(thisCancelQty) >= 0) {
@@ -2179,8 +2193,19 @@ public class OrderServices {
                     }
 
                     if (thisCancelQty.compareTo(itemQuantity) >= 0) {
+                        if ("ITEM_COMPLETED".equals(itemStatus) && 
"SALES_ORDER".equals(orh.getOrderTypeId())) {
+                            //If partial item shipped then release remaining 
inventory of SO item and marked SO item as completed.
+                            Map<String, Object> cancelOrderItemInvResCtx = 
UtilMisc.toMap("orderId", orderId, "orderItemSeqId", 
orderItem.getString("orderItemSeqId"), "shipGroupSeqId",
+                                    shipGroupSeqId, "cancelQuantity", 
thisCancelQty, "userLogin", userLogin);
+                            try {
+                                
dispatcher.runSyncIgnore("cancelOrderItemInvResQty", cancelOrderItemInvResCtx);
+                            } catch (GenericServiceException e) {
+                                Debug.logError(e, module);
+                                return 
ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToUpdateInventoryReservations",
 UtilMisc.toMap("itemMsgInfo",itemMsgInfo), locale));
+                            }
+                        }
                         // all items are cancelled -- mark the item as 
cancelled
-                        Map<String, Object> statusCtx = UtilMisc.<String, 
Object>toMap("orderId", orderId, "orderItemSeqId", 
orderItem.getString("orderItemSeqId"), "statusId", "ITEM_CANCELLED", 
"userLogin", userLogin);
+                        Map<String, Object> statusCtx = UtilMisc.<String, 
Object>toMap("orderId", orderId, "orderItemSeqId", 
orderItem.getString("orderItemSeqId"), "statusId", itemStatus, "userLogin", 
userLogin);
                         try {
                             dispatcher.runSyncIgnore("changeOrderItemStatus", 
statusCtx);
                         } catch (GenericServiceException e) {

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
 (original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
 Mon Dec 21 05:38:43 2015
@@ -1451,11 +1451,12 @@ public class ShoppingCartEvents {
         Delegator delegator = (Delegator) request.getAttribute("delegator");
 
         String orderId = request.getParameter("orderId");
+        String createAsNewOrder = request.getParameter("createAsNewOrder");
 
         ShoppingCart cart = null;
         try {
             Map<String, Object> outMap = 
dispatcher.runSync("loadCartFromOrder",
-                                                UtilMisc.<String, 
Object>toMap("orderId", orderId,
+                                                UtilMisc.<String, 
Object>toMap("orderId", orderId, "createAsNewOrder", createAsNewOrder,
                                                         "skipProductChecks", 
Boolean.TRUE, // the products have already been checked in the order, no need 
to check their validity again
                                                         "userLogin", 
userLogin));
             if (!ServiceUtil.isSuccess(outMap)) {

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartServices.java?rev=1721093&r1=1721092&r2=1721093&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartServices.java
 (original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartServices.java
 Mon Dec 21 05:38:43 2015
@@ -166,6 +166,8 @@ public class ShoppingCartServices {
         Boolean skipProductChecks = (Boolean) context.get("skipProductChecks");
         boolean includePromoItems = 
Boolean.TRUE.equals(context.get("includePromoItems"));
         Locale locale = (Locale) context.get("locale");
+        //FIXME: deepak:Personally I don't like the idea of passing flag but 
for orderItem quantity calculation we need this flag.
+        String createAsNewOrder = (String) context.get("createAsNewOrder");
 
         if (UtilValidate.isEmpty(skipInventoryChecks)) {
             skipInventoryChecks = Boolean.FALSE;
@@ -395,7 +397,13 @@ public class ShoppingCartServices {
                 if (amount == null) {
                     amount = BigDecimal.ZERO;
                 }
-                BigDecimal quantity = item.getBigDecimal("quantity");
+                //BigDecimal quantity = item.getBigDecimal("quantity");
+                BigDecimal quantity = BigDecimal.ZERO;
+                if("ITEM_COMPLETED".equals(item.getString("statusId")) && 
"N".equals(createAsNewOrder)) {
+                    quantity = item.getBigDecimal("quantity");
+                } else {
+                    quantity = OrderReadHelper.getOrderItemQuantity(item);
+                }
                 if (quantity == null) {
                     quantity = BigDecimal.ZERO;
                 }


Reply via email to