Skip to content

Instantly share code, notes, and snippets.

@noedlm
Last active November 15, 2017 21:09
Show Gist options
  • Save noedlm/084f0bd1357adbec6557f58d04ae4d6f to your computer and use it in GitHub Desktop.
Save noedlm/084f0bd1357adbec6557f58d04ae4d6f to your computer and use it in GitHub Desktop.
public struct function calculate(required string prodL) {
var totalSavings = 0.00;
var priceList = "";
var BigDecimal = createObject("java", "java.math.BigDecimal");
var nerismoProducts = model("nerismoProduct").findAll(where = "id IN (#arguments.prodL#)");
var items = [];
var pArray = listToArray(arguments.prodL);
for (pid in pArray) {
var item = model("nerismoOrderItem").new();
item["quantity"] = 1;
item["nerismopid"] = pid;
var nerismoProductLength = nerismoProducts.recordCount;
for (var i = 1; i <= nerismoProductLength; i++) {
if (nerismoProducts["id"][i] == pid) {
item["title"] = nerismoProducts["title"][i];
var found = False;
if (!arrayLen(this.aceCouponProducts)) {
var discount = nerismoProducts["price"][i] * (this.discountpercent/100.00);
totalSavings += decimalToCurrency(discount, BigDecimal.ROUND_DOWN);
priceList = listAppend(priceList, decimalToCurrency(nerismoProducts["price"][i] - discount, BigDecimal.ROUND_DOWN));
item["price"] = decimalToCurrency(nerismoProducts["price"][i] - discount, BigDecimal.ROUND_DOWN);
} else {
for (couponProduct in this.aceCouponProducts) {
if (couponProduct.nerismopid == pid) {
found = true;
var discount = nerismoProducts["price"][i] * (this.discountpercent/100.00);
totalSavings += decimalToCurrency(discount, BigDecimal.ROUND_DOWN);
priceList = listAppend(priceList, decimalToCurrency(nerismoProducts["price"][i] - discount, BigDecimal.ROUND_DOWN));
item["price"] = decimalToCurrency(nerismoProducts["price"][i] - discount, BigDecimal.ROUND_DOWN);
}
}
if (!found) {
priceList = listAppend(priceList, nerismoProducts["price"][i]);
item["price"] = nerismoProducts["price"][i];
}
}
}
}
var foundIndex = 0;
var itemsLength = arrayLen(items);
for (var i = 1; i<= itemsLength;i++) {
if (items[i].nerismopid == pid) {
foundIndex = i;
}
}
if (foundIndex) {
items[foundIndex].price = item.price;
items[foundIndex].quantity = items[foundIndex].quantity + 1;
items[foundIndex].subtotal += item.price;
} else {
item["subTotal"] = item.price;
arrayAppend(items, item);
}
}
return {
'totalSavings' = totalSavings,
'priceList' = priceList,
'items' = items
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment