You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2020/06/30 13:19:10 UTC

[ofbiz-framework] branch trunk updated: Fixed: BigDecimal casting in Groovy (OFBIZ-11862)

This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new df17977  Fixed: BigDecimal casting in Groovy (OFBIZ-11862)
df17977 is described below

commit df179777b68f8d9411b84b3e11d38e0bda5c0bb4
Author: Jacques Le Roux <ja...@les7arts.com>
AuthorDate: Tue Jun 30 15:17:47 2020 +0200

    Fixed: BigDecimal casting in Groovy (OFBIZ-11862)
    
    Sometimes use wrong type castings for BigDecimal:
    
    I have observed that for typecasting in groovy, we have used the wrong coding
    pattern like
    
    (BigDecimal) 0
    It should be like
    0 as BigDecimal
    
    Note that this does not apply to decimal because in case of decimal, the default
    type is always BigDecimal in Groovy.
    
    Thanks: Pritam Kute for spotting the issue in OFBIZ-11843
---
 .../groovyScripts/order/OrderReturnServices.groovy | 20 ++++++++++----------
 .../inventory/InventoryIssueServices.groovy        | 10 +++++-----
 .../shipment/ShipmentReceiptServices.groovy        |  8 ++++----
 .../groovyScripts/shipment/ShipmentServices.groovy | 22 +++++++++++-----------
 4 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/applications/order/groovyScripts/order/OrderReturnServices.groovy b/applications/order/groovyScripts/order/OrderReturnServices.groovy
index f390412..4583812 100644
--- a/applications/order/groovyScripts/order/OrderReturnServices.groovy
+++ b/applications/order/groovyScripts/order/OrderReturnServices.groovy
@@ -129,7 +129,7 @@ def updateReturnHeader() {
         List returnItems = from("ReturnItem").where(returnId: returnHeader.returnId).distinct().queryList()
         
         // this is used to make sure we don't return a negative amount
-        BigDecimal returnTotalAmount = (BigDecimal) 0
+        BigDecimal returnTotalAmount = 0 as BigDecimal
         
         // check them all to make sure that the return total does not exceed order total.
         for (GenericValue returnItem : returnItems) {
@@ -144,7 +144,7 @@ def updateReturnHeader() {
                 // no adjustment needed: adjustment is passed in to calculate
                 // the effect of an additional item on return total.
                 Map serviceResult = run service:"getOrderAvailableReturnedTotal", with: [orderId: returnItem.orderId,
-                                                                                         adjustment : (BigDecimal) 0]
+                                                                                         adjustment : 0 as BigDecimal]
                 BigDecimal availableReturnTotal = serviceResult.availableReturnTotal
                 BigDecimal returnTotal = serviceResult.returnTotal
                 BigDecimal orderTotal = serviceResult.returnTotal
@@ -205,13 +205,13 @@ def createReturnItem() {
         && ("RTN_CSREPLACE" == parameters.returnTypeId || "RTN_REPAIR_REPLACE" == parameters.returnTypeId)) {
         return informError("OrderReturnPaymentMethodNeededForThisTypeOfReturn")
     }
-    if (parameters.returnQuantity == (BigDecimal) 0) {
+    if (parameters.returnQuantity == 0 as BigDecimal) {
         return informError("OrderNoReturnQuantityAvailablePreviousReturnsMayExist")
     }
     
     // setup some default values for protection
-    BigDecimal returnableQuantity = (BigDecimal) 0
-    BigDecimal returnablePrice = (BigDecimal) 0
+    BigDecimal returnableQuantity = 0 as BigDecimal
+    BigDecimal returnablePrice = 0 as BigDecimal
     
     // if an orderItemSeqId is provided, then find the corresponding orderItem
     if (parameters.orderItemSeqId) {
@@ -232,7 +232,7 @@ def createReturnItem() {
         returnableQuantity = serviceResult.returnableQuantity ?: returnableQuantity
         returnablePrice = serviceResult.returnablePrice ?: returnablePrice
     }
-    if (returnableQuantity > (BigDecimal) 0) {
+    if (returnableQuantity > 0 as BigDecimal) {
         // the user is only allowed to set a returnPrice if he has ORDERMGR_CREATE privilege,
         // otherwise only the returnablePrice calculated by service is used
         if (!security.hasEntityPermission("ORDERMGR", "_CREATE", parameters.userLogin)) {
@@ -369,7 +369,7 @@ def updateReturnStatusFromReceipt() {
     Map totalsMap = [:]
     for (GenericValue receipt : shipmentReceipts) {
         if (!totalsMap[receipt?.returnItemSeqId]) {
-            totalsMap[receipt.returnItemSeqId] = (BigDecimal) 0
+            totalsMap[receipt.returnItemSeqId] = 0 as BigDecimal
         }
         totalsMap[receipt.returnItemSeqId] += receipt.quantityAccepted + receipt.quantityRejected
     }
@@ -520,7 +520,7 @@ def quickReturnFromOrder() {
             logInfo("Found unexpected orderAdjustment: ${newItemCtx.orderAdjustmentId}")
             newItemCtx.orderAdjustmentId = null
         }
-        if (newItemCtx.returnQuantity > (BigDecimal) 0) {
+        if (newItemCtx.returnQuantity > 0 as BigDecimal) {
             // otherwise, items which have been fully returned would still get passed in and then come back with an error
             run service:"createReturnItem", with: newItemCtx
         } else {
@@ -551,7 +551,7 @@ def quickReturnFromOrder() {
     logInfo("OrderTotal [${orderTotal}] - ReturnTotal [${returnTotal}] = available Return Total [${}]")
     
     // create a manual balance adjustment based on the difference between order total and return total
-    if (availableReturnTotal != (BigDecimal) 0) {
+    if (availableReturnTotal != 0 as BigDecimal) {
         logWarning("Creating a balance adjustment of [" + availableReturnTotal + "] for return [" + returnId + "]")
         
         // create the balance adjustment return item
@@ -958,7 +958,7 @@ def createReturnItemForRental() {
         createReturnCtx.returnTypeId = "RTN_RENTAL"
         createReturnCtx.returnItemTypeId = "RET_FDPROD_ITEM"
         createReturnCtx.expectedItemStatus = "INV_RETURNED"
-        createReturnCtx.returnPrice = (BigDecimal) 0
+        createReturnCtx.returnPrice = 0 as BigDecimal
         
         List orderItems = from("OrderItemAndProduct")
                 .where(orderId: orderHeader.orderId,
diff --git a/applications/product/groovyScripts/product/inventory/InventoryIssueServices.groovy b/applications/product/groovyScripts/product/inventory/InventoryIssueServices.groovy
index a1b6f09..e9faa29 100644
--- a/applications/product/groovyScripts/product/inventory/InventoryIssueServices.groovy
+++ b/applications/product/groovyScripts/product/inventory/InventoryIssueServices.groovy
@@ -116,7 +116,7 @@ def issueImmediatelyFulfilledOrderItem() {
         parameters.quantityNotIssued = orderItem.quantity
         // if quantityNotIssued is not 0, then pull it from the last non-serialized inventory item found,
         // in the quantityNotIssued field
-        if (parameters.quantityNotIssued != (BigDecimal) 0) {
+        if (parameters.quantityNotIssued != 0 as BigDecimal) {
             BigDecimal availableToPromiseDiff = - parameters.quantityNotIssued
             BigDecimal quantityOnHandDiff = - parameters.quantityNotIssued
             if (lastNonSerInventoryItem) {
@@ -175,7 +175,7 @@ def issueImmediatelyFulfilledOrderItem() {
 def issueImmediateForInventoryItemInline(GenericValue inventoryItem) {
     GenericValue lastNonSerInventoryItem
     // only do something with this inventoryItem if there is more inventory to issue
-    if (parameters.quantityNotIssued > (BigDecimal) 0) {
+    if (parameters.quantityNotIssued > 0 as BigDecimal) {
         if ("SERIALIZED_INV_ITEM" == inventoryItem.inventoryItemTypeId) {
             if ("INV_AVAILABLE" == inventoryItem.statusId) {
                 // change status on inventoryItem
@@ -186,9 +186,9 @@ def issueImmediateForInventoryItemInline(GenericValue inventoryItem) {
                 run service: "createItemIssuance", with: [orderId: parameters.orderId,
                                                           orderItemSeqId: parameters.orderItemSeqId,
                                                           inventoryItemId: inventoryItem.inventoryItemId,
-                                                          quantity: (BigDecimal) 1]
+                                                          quantity: 1 as BigDecimal]
 
-                parameters.quantityNotIssued -= (BigDecimal) 1
+                parameters.quantityNotIssued -= 1 as BigDecimal
             }
         }
         if (inventoryItem.inventoryItemTypeId == "NON_SERIAL_INV_ITEM") {
@@ -196,7 +196,7 @@ def issueImmediateForInventoryItemInline(GenericValue inventoryItem) {
             // if not the code at the end of this method will handle it
             if ((!inventoryItem.statusId || inventoryItem.statusId == "INV_AVAILABLE") &&
                     inventoryItem.availableToPromiseTotal &&
-                    inventoryItem.availableToPromiseTotal > (BigDecimal) 0) {
+                    inventoryItem.availableToPromiseTotal > 0 as BigDecimal) {
                 parameters.deductAmount = parameters.quantityNotIssued > inventoryItem.availableToPromiseTotal ?
                         inventoryItem.availableToPromiseTotal :
                         parameters.quantityNotIssued
diff --git a/applications/product/groovyScripts/shipment/ShipmentReceiptServices.groovy b/applications/product/groovyScripts/shipment/ShipmentReceiptServices.groovy
index 2646ed7..4365e1e 100644
--- a/applications/product/groovyScripts/shipment/ShipmentReceiptServices.groovy
+++ b/applications/product/groovyScripts/shipment/ShipmentReceiptServices.groovy
@@ -82,13 +82,13 @@ def receiveInventoryProduct () {
     Double loops = 1.0
     if (parameters.inventoryItemTypeId == "SERIALIZED_INV_ITEM") {
         // if we are serialized and either a serialNumber or inventoyItemId is passed in and the quantityAccepted is greater than 1 then complain
-        if ((parameters.serialNumber || parameters.currentInventoryItemId) && (parameters.quantityAccepted > (BigDecimal) 1)) {
+        if ((parameters.serialNumber || parameters.currentInventoryItemId) && (parameters.quantityAccepted > 1 as BigDecimal)) {
             Map errorLog = [parameters: parameters]
             return error(UtilProperties.getMessage("ProductUiLabels", "FacilityReceiveInventoryProduct", errorLog,  parameters.locale))
             // before getting going, see if there are any validation issues so far
         }
         loops = parameters.quantityAccepted
-        parameters.quantityAccepted = (BigDecimal) 1
+        parameters.quantityAccepted = 1 as BigDecimal
     }
     parameters.quantityOnHandDiff = parameters.quantityAccepted
     parameters.availableToPromiseDiff = parameters.quantityAccepted
@@ -236,7 +236,7 @@ def quickReceiveReturn() {
                     }
                     if (!setNonSerial) {
                         parameters.inventoryItemTypeId = "SERIALIZED_INV_ITEM"
-                        returnItem.returnQuantity = (BigDecimal) 1
+                        returnItem.returnQuantity = 1 as BigDecimal
                     }
                     receiveCtx = [inventoryItemTypeId: parameters.inventoryItemTypeId,
                         statusId: returnItem.expectedItemStatus,
@@ -248,7 +248,7 @@ def quickReceiveReturn() {
                         shipmentId: shipmentId, // important: associate ShipmentReceipt with return shipment created
                         comments: "Returned Item RA# ${returnItem.returnId}",
                         datetimeReceived: nowTimestamp,
-                        quantityRejected: (BigDecimal) 0
+                        quantityRejected: 0 as BigDecimal
                     ]
                     Map serviceResult = run service:"receiveInventoryProduct", with: receiveCtx
                     result.successMessageList = serviceResult.successMessageList
diff --git a/applications/product/groovyScripts/shipment/ShipmentServices.groovy b/applications/product/groovyScripts/shipment/ShipmentServices.groovy
index c51ffe6..b452759 100644
--- a/applications/product/groovyScripts/shipment/ShipmentServices.groovy
+++ b/applications/product/groovyScripts/shipment/ShipmentServices.groovy
@@ -506,7 +506,7 @@ def balanceItemIssuancesForShipment() {
                         orderId: issuance.orderId,
                         orderItemSeqId: issuance.orderItemSeqId)
                 .queryList()
-        BigDecimal issuanceQuantity = (BigDecimal) 0
+        BigDecimal issuanceQuantity = 0 as BigDecimal
         for (GenericValue receipt : receipts) {
             issuanceQuantity = issuanceQuantity + receipt.quantityAccepted + receipt.quantityRejected
         }
@@ -545,7 +545,7 @@ def splitShipmentItemByQuantity() {
             .queryList()
     BigDecimal orderShipmentQuantityLeft = parameters.newItemQuantity
     for (GenericValue itemOrderShipment : itemOrderShipmentList) {
-        if (orderShipmentQuantityLeft > (BigDecimal) 0) {
+        if (orderShipmentQuantityLeft > 0 as BigDecimal) {
             if (itemOrderShipment.quantity > orderShipmentQuantityLeft) {
                 // there is enough in this OrderShipment record, so just adjust it and move on
                 Map updateOrderShipmentMap = itemOrderShipment.getAllFields()
@@ -556,7 +556,7 @@ def splitShipmentItemByQuantity() {
                                                            shipmentId: itemOrderShipment.shipmentId,
                                                            shipmentItemSeqId: newShipmentItemSeqId,
                                                            quantity: orderShipmentQuantityLeft]
-                orderShipmentQuantityLeft = (BigDecimal) 0
+                orderShipmentQuantityLeft = 0 as BigDecimal
             } else {
                 // not enough on this one, create a new one for the new item and delete this one
                 run service: "deleteOrderShipment", with: itemOrderShipment.getAllFields()
@@ -1336,7 +1336,7 @@ def removeOrderShipmentFromShipment() {
     GenericValue shipmentItem = from("ShipmentItem").where(parameters).queryOne()
     run service: "deleteOrderShipment", with: parameters
     shipmentItem.quantity = orderShipment.quantity - shipmentItem.quantity
-    if (shipmentItem.quantity > (BigDecimal) 0) {
+    if (shipmentItem.quantity > 0 as BigDecimal) {
         run service: "updateShipmentItem", with: shipmentItem.getAllFields()
     } else {
         run service: "deleteShipmentItem", with: parameters
@@ -1353,7 +1353,7 @@ def removeOrderShipmentFromShipment() {
 def addOrderShipmentToShipment() {
     Map result = success()
     // if quantity is greater than 0 we add or update the ShipmentPlan
-    if (parameters.quantity > (BigDecimal) 0) {
+    if (parameters.quantity > 0 as BigDecimal) {
         // get orderHeader
         GenericValue orderHeader = from("OrderHeader").where(parameters).queryOne()
         // get orderItem
@@ -1397,25 +1397,25 @@ def addOrderShipmentToShipment() {
  */
 def getQuantityForShipment() {
     Map result = success()
-    BigDecimal plannedQuantity = (BigDecimal) 0
-    BigDecimal issuedQuantity = (BigDecimal) 0
+    BigDecimal plannedQuantity = 0 as BigDecimal
+    BigDecimal issuedQuantity = 0 as BigDecimal
     // get orderItem
     GenericValue orderItem = from("OrderItem").where(parameters).queryOne()
     Map orderShipmentLookup = [orderId: parameters.orderId,
                                orderItemSeqId: parameters.orderItemSeqId]
     List existingOrderShipments = from("OrderShipment").where(orderShipmentLookup).queryList()
     for (GenericValue orderShipment : existingOrderShipments) {
-        plannedQuantity += orderShipment.quantity ?: (BigDecimal) 0
+        plannedQuantity += orderShipment.quantity ?: 0 as BigDecimal
     }
     existingOrderShipments = from("ItemIssuance").where(orderShipmentLookup).queryList()
     for (GenericValue itemIssuance : existingOrderShipments) {
-        BigDecimal quantity = itemIssuance.quantity ?: (BigDecimal) 0
-        BigDecimal cancelQuantity = itemIssuance.cancelQuantity ?: (BigDecimal) 0
+        BigDecimal quantity = itemIssuance.quantity ?: 0 as BigDecimal
+        BigDecimal cancelQuantity = itemIssuance.cancelQuantity ?: 0 as BigDecimal
         issuedQuantity += quantity - cancelQuantity
     }
 
     BigDecimal totPlannedOrIssuedQuantity = issuedQuantity + plannedQuantity
-    BigDecimal orderCancelQuantity = orderItem.cancelQuantity ?: (BigDecimal) 0
+    BigDecimal orderCancelQuantity = orderItem.cancelQuantity ?: 0 as BigDecimal
 
     result.remainingQuantity = orderCancelQuantity + totPlannedOrIssuedQuantity - orderItem.quantity
     return result