Skip to content

Commit

Permalink
Extending individual discountRate value (OFBIZ-12802)
Browse files Browse the repository at this point in the history
+ Addition of additional context information "productStoreGroupId" and
"partyId" to the call parameters of a "customMethodName"
price-calc-service
+ Obtaining the fields discountRate and listPrice from calling a
"customMethodName" price-calc-service

+ discountRate as Result Field
+ Transport original discount rate from custom price calculation over
cart to order to have the correct rate instead of back-calculating it
from unitPrice and unitListPrice
  • Loading branch information
Adrian-Wolf authored and mbrohl committed Jan 23, 2024
1 parent a41f054 commit a07c44f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions applications/datamodel/entitydef/order-entitymodel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ under the License.
<field name="unitListPrice" type="currency-precise"></field>
<field name="unitAverageCost" type="currency-amount"></field>
<field name="unitRecurringPrice" type="currency-amount"></field>
<field name="discountRate" type="fixed-point"></field>
<field name="isModifiedPrice" type="indicator"></field>
<field name="recurringFreqUomId" type="id"></field>
<field name="itemDescription" type="description"></field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4376,6 +4376,7 @@ public List<GenericValue> makeOrderItems(boolean explodeItems, boolean replaceAg
orderItem.set("selectedAmount", item.getSelectedAmount());
orderItem.set("unitPrice", item.getBasePrice());
orderItem.set("unitListPrice", item.getListPrice());
orderItem.set("discountRate", item.getDiscountRate());
orderItem.set("isModifiedPrice", item.getIsModifiedPrice() ? "Y" : "N");
orderItem.set("isPromo", item.getIsPromo() ? "Y" : "N");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public class ShoppingCartItem implements java.io.Serializable {
*/
private BigDecimal reservNthPPPerc = BigDecimal.ZERO;
private BigDecimal listPrice = BigDecimal.ZERO;
private BigDecimal discountRate = null;
/**
* flag to know if the price have been modified
*/
Expand Down Expand Up @@ -1412,6 +1413,7 @@ public void updatePrice(LocalDispatcher dispatcher, ShoppingCart cart) throws Ca
}

this.setSpecialPromoPrice((BigDecimal) priceResult.get("specialPromoPrice"));
this.discountRate = (BigDecimal) priceResult.get("discountRate");
}

this.orderItemPriceInfos = UtilGenerics.cast(priceResult.get("orderItemPriceInfos"));
Expand Down Expand Up @@ -2502,6 +2504,14 @@ public void setListPrice(BigDecimal listPrice) {
this.listPrice = listPrice;
}

/**
* Returns the DiscountRate
* @return discountRate
*/
public BigDecimal getDiscountRate() {
return discountRate;
}

/**
* Returns isModifiedPrice
*/
Expand Down
1 change: 1 addition & 0 deletions applications/product/servicedef/services_pricepromo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ under the License.
<attribute name="basePrice" type="BigDecimal" mode="OUT" optional="false"><!-- will only be different from price if there is a display price adjustment, for example: checkIncludeVat=Y and a VAT amount was found --></attribute>
<attribute name="price" type="BigDecimal" mode="OUT" optional="false"/>
<attribute name="listPrice" type="BigDecimal" mode="OUT" optional="true"/>
<attribute name="discountRate" type="BigDecimal" mode="OUT" optional="true" />
<attribute name="defaultPrice" type="BigDecimal" mode="OUT" optional="true"/>
<attribute name="competitivePrice" type="BigDecimal" mode="OUT" optional="true"/>
<attribute name="averageCost" type="BigDecimal" mode="OUT" optional="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ public static Map<String, Object> calculateProductPrice(DispatchContext dctx, Ma

boolean validPriceFound = false;
BigDecimal defaultPrice = BigDecimal.ZERO;
BigDecimal listPrice = null;
BigDecimal discountRate = null;
List<GenericValue> orderItemPriceInfos = new LinkedList<>();
if (defaultPriceValue != null) {
// If a price calc formula (service) is specified, then use it to get the unit price
Expand All @@ -366,13 +368,19 @@ public static Map<String, Object> calculateProductPrice(DispatchContext dctx, Ma
if (UtilValidate.isNotEmpty(customAttributes)) {
inMap.put("customAttributes", customAttributes);
}
inMap.put("productStoreGroupId", productStoreGroupId);
inMap.put("partyId", partyId);
try {
Map<String, Object> outMap = dispatcher.runSync(customMethod.getString("customMethodName"), inMap);
if (ServiceUtil.isSuccess(outMap)) {
BigDecimal calculatedDefaultPrice = (BigDecimal) outMap.get("price");
BigDecimal calculatedListPrice = (BigDecimal) outMap.get("listPrice");
BigDecimal calculatedDiscountRate = (BigDecimal) outMap.get("discountRate");
orderItemPriceInfos = UtilGenerics.cast(outMap.get("orderItemPriceInfos"));
if (UtilValidate.isNotEmpty(calculatedDefaultPrice)) {
defaultPrice = calculatedDefaultPrice;
listPrice = calculatedListPrice;
discountRate = calculatedDiscountRate;
validPriceFound = true;
}
}
Expand All @@ -388,9 +396,13 @@ public static Map<String, Object> calculateProductPrice(DispatchContext dctx, Ma
}
}

BigDecimal listPrice = listPriceValue != null ? listPriceValue.getBigDecimal("price") : null;
boolean skipPriceRules = true;
if (listPrice == null && listPriceValue != null) {
listPrice = listPriceValue.getBigDecimal("price");
skipPriceRules = listPrice == null;
}

if (listPrice == null) {
if (skipPriceRules) {
// no list price, use defaultPrice for the final price

// ========= ensure calculated price is not below minSalePrice or above maxSalePrice =========
Expand All @@ -407,6 +419,8 @@ public static Map<String, Object> calculateProductPrice(DispatchContext dctx, Ma
validPriceFound = true;
}

result.put("listPrice", listPrice);
result.put("discountRate", discountRate);
result.put("basePrice", defaultPrice);
result.put("price", defaultPrice);
result.put("defaultPrice", defaultPrice);
Expand Down

0 comments on commit a07c44f

Please sign in to comment.