Introduction
I want to apply a discount on a child product with a consumption schedule.
If the child is a certain product, I want the discount from this product child.
If the parent is a product, I want the parent’s discount.
Also, I want to generate a specific discount formula according to the calculation method of this quote. How to do it?
Field you need for this use case:
Here you will need in quote :
You will also need this in your quote line:
For the based discount and formula discount, you can modify this field to adapt to your business logic. Here, you put it manually. But you can calculate it with price rule, custom script, apex, ….
Custom script
In the custom script, you will be able to apply bundle-specific discount and consumption rates to the provided lines or apply discount based on the consumption rates to the provided lines.
/**
* This function is called before calculating the quote.
* It applies the bundle discount and other discount rates to the quote lines.
*
* @param {Object} quoteModel - The main quote model.
* @param {Array} quoteLineModels - An array of line items associated with the quote.
* @return {Promise} - Resolves when calculations are done.
*/
export function onBeforeCalculate(quoteModel, quoteLineModels) {
ApplyBundleDiscountConsumptionRates(quoteLineModels);
ApplyDiscountOnConsumptionRates(quoteLineModels, quoteModel);
return Promise.resolve();
}
/**
* Apply bundle-specific discount and consumption rates to the provided lines.
*
* @param {Array} lines - An array of line items.
*/
function ApplyBundleDiscountConsumptionRates(lines) {
if (!lines) return;
lines.forEach((line) => {
let discount = 0;
const record = line.record;
const requiredBy = record["SBQQ__RequiredBy__r"];
const productCode = requiredBy ? requiredBy["SBQQ__ProductCode__c"] : null;
if (
(requiredBy &&
((productCode == "Bundle1" && requiredBy["SBQQ__Discount__c"]) || productCode == "Bundle2")) ||
record["SBQQ__ProductCode__c"] == "SAMPLE1"
) {
if (record["SBQQ__ProductCode__c"] == "SAMPLE1") {
discount = record["SBQQ__Discount__c"] / 100;
} else if (productCode == "Bundle1") {
discount = (record["SBQQ__Discount__c"] || requiredBy["SBQQ__Discount__c"]) / 100;
} else if (productCode == "Bundle2") {
discount = record["SBQQ__Discount__c"] / 100;
}
const agencyCommission = record["SBQQ__PartnerDiscount__c"] / 100;
if (line.consumptionSchedules) {
line.consumptionSchedules.forEach((cs) => {
const rates = cs.getRates();
if (rates) {
rates.forEach((rate) => {
const originalPrice = rate.get('SBQQ__OriginalPrice__c');
const priceAfterDiscount = originalPrice * (1 - discount);
const discountedPrice = priceAfterDiscount * (1 - agencyCommission);
rate.set('SBQQ__Price__c', Number(discountedPrice.toFixed(2)));
record['PriceAfterDiscountCurrency__c'] = Number(priceAfterDiscount.toFixed(2));
record['SBQQ__ListPrice__c'] = originalPrice;
record['ConsumptionPriceDiscountedCurrency__c'] = rate.get('SBQQ__Price__c');
});
}
});
}
}
});
}
/**
* Apply discount based on the consumption rates to the provided lines.
*
* @param {Array} lines - An array of line items.
* @param {Object} quoteModel - The main quote model.
*/
function ApplyDiscountOnConsumptionRates(lines, quoteModel) {
if (!lines) return;
lines.forEach((line) => {
let discount = 0;
const record = line.record;
const requiredBy = record["SBQQ__RequiredBy__r"];
const productCode = requiredBy ? requiredBy["SBQQ__ProductCode__c"] : null;
if (requiredBy && (productCode == "Bundle3" || productCode == "Bundle4")) {
if (quoteModel.record["MethodeDeCalcul__c"] == 'Formula' && record["DiscountFormula__c"]) {
discount = record["DiscountFormula__c"];
} else {
discount = record["BasicDiscount__c"] || 0;
}
discount += record["SBQQ__Discount__c"] || 0;
discount = discount / 100;
const agencyCommission = record["SBQQ__PartnerDiscount__c"] / 100;
if (line.consumptionSchedules) {
line.consumptionSchedules.forEach((cs) => {
const rates = cs.getRates();
if (rates) {
rates.forEach((rate) => {
const originalPrice = rate.get('SBQQ__OriginalPrice__c');
const priceAfterDiscount = originalPrice * (1 - discount);
const discountedPrice = priceAfterDiscount * (1 - agencyCommission);
rate.set('SBQQ__Price__c', Number(discountedPrice.toFixed(2)));
record['PriceAfterDiscountCurrency__c'] = Number(priceAfterDiscount.toFixed(2));
record['SBQQ__ListPrice__c'] = originalPrice;
record['ConsumptionPriceDiscountedCurrency__c'] = rate.get('SBQQ__Price__c');
});
}
});
}
}
});
}
Conclusion:
You can now apply discount on it
Result
the link for the github: https://github.com/AourLegacy/AourFactory/tree/Discount-consumption-schedule
Comments