Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- * Description : Custom discounts Price Engine extension
- * Created by : Jakub Wanowski (jakub.wanowski@enxoo.com)
- * Date created : 2019-06-20
- ************************************************************************/
- global class PRI02_PriceCalculatorClsImpl extends enxCPQ.CPQ_PRI02_PriceCalculatorCls {
- // TL2-97
- List<ExpandedBundleElementWrapper> bundleElementWrappersList = new List<ExpandedBundleElementWrapper>();
- final String AMOUNT_DISCOUNT = 'AMOUNT_DISCOUNT';
- final String PERCENT_DISCOUNT = 'PERCENT_DISCOUNT';
- /************************************************************************
- Description: Method extended with custom attribute dependencies check
- Require: JSON file with dependencies saved as Static Resource
- ************************************************************************/
- global override List<enxCPQ.T_QuoteItem> calculatePrices(List<enxCPQ.T_QuoteItem> items) {
- return super.calculatePrices(items);
- }
- /************************************************************************
- Description: Tele2 Custom Pricing Logic
- param 1: T_PriceItem object
- ************************************************************************/
- global override void applyCustomLogic(enxCPQ.T_PriceItem item) {
- // TL2-97 - TEAMER PRODUCTS PRICING
- if (item.childItems != null && !item.childItems.isEmpty()) {
- Map<String, enxCPQ.T_PriceItemCharge> charges = new Map<String, enxCPQ.T_PriceItemCharge>();
- // Get parent item pricing fields
- enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrcField = item.fields.get('enxcpq__mrc__c');
- enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrcListField = item.fields.get('enxcpq__mrc_list__c');
- Decimal simCardsQuantity = 1;
- Decimal sumOfChildMrcList = 0;
- for (enxCPQ.T_PriceItem child : item.childItems) {
- // Get child item pricing fields
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcField = child.fields.get('enxcpq__mrc__c');
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcListField = child.fields.get('enxcpq__mrc_list__c');
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcTotalField = child.fields.get('enxcpq__mrc_total__c');
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcListTotalField = child.fields.get('enxcpq__mrc_list_total__c');
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childQuantity = child.fields.get('quantity');
- Decimal childMrcList = Decimal.valueOf(childMrcListField.value);
- // Multiply MRC by SIM Count attribute value
- if (item.fields.containsKey('sim_cards_quantity__c') && item.fields.get('sim_cards_quantity__c').value != null) {
- simCardsQuantity = Decimal.valueOf(item.fields.get('sim_cards_quantity__c').value);
- }
- for (ExpandedBundleElementWrapper bundleElementWrapper : bundleElementWrappersList) {
- if (child.masterProductId == bundleElementWrapper.masterProductId) {
- childMrcList += bundleElementWrapper.mrcToAdd;
- // Create Charge
- enxCPQ.T_PriceItemCharge charge = new enxCPQ.T_PriceItemCharge();
- charge.chargeId = bundleElementWrapper.masterProductId;
- charge.name = bundleElementWrapper.productName;
- charge.chargeName = bundleElementWrapper.productName;
- charge.sortingOrder = -1;
- charge.chargeListPrice = childMrcList;
- charge.chargeListPriceTotal = childMrcList * simCardsQuantity;
- charge.chargeUnitPrice = childMrcList;
- charge.chargeUnitPriceTotal = childMrcList * simCardsQuantity;
- charge.chargeModel = 'Flat Fee';
- charge.chargeType = 'Recurring';
- charge.syncPrice = true;
- charge.unitOfMeasure = 'SIM';
- charge.billingFrequency = 'Monthly';
- charge.multiplierValue = simCardsQuantity;
- charges.put(charge.chargeId, charge);
- break;
- }
- }
- if (areFieldsAvailable(child)) {
- childMrcField.value = String.valueOf(childMrcList);
- childMrcListField.value = String.valueOf(childMrcList);
- childMrcTotalField.value = String.valueOf(childMrcList * simCardsQuantity);
- childMrcListTotalField.value = String.valueOf(childMrcList * simCardsQuantity);
- childQuantity.value = String.valueOf(simCardsQuantity);
- child.fields.put('enxcpq__mrc__c', childMrcField);
- child.fields.put('enxcpq__mrc_list__c', childMrcListField);
- child.fields.put('enxcpq__mrc_total__c', childMrcTotalField);
- child.fields.put('enxcpq__mrc_list_total__c', childMrcListTotalField);
- child.fields.put('quantity', childQuantity);
- }
- }
- item.charges = charges;
- }
- // DISCOUNTS START
- // -----------------------------------------------------------------
- Boolean percentDiscountIsUsed = true;
- Boolean amountDiscountIsUsed = (item.fields.containsKey('tech_percent_discount__c') && item.fields.get('tech_percent_discount__c').value == null) &&
- (item.fields.containsKey('tech_amount_discount__c') && item.fields.get('tech_amount_discount__c').value != null);
- // Apply discount
- if (percentDiscountIsUsed) {
- String discountAsString = '50';
- applyDiscount(item, Decimal.valueOf(discountAsString), PERCENT_DISCOUNT);
- } else if (amountDiscountIsUsed) {
- String discountAsString = '50';
- applyDiscount(item, Decimal.valueOf(discountAsString), AMOUNT_DISCOUNT);
- }
- // DISCOUNTS END
- // -----------------------------------------------------------------
- // SET SELF PRICE
- setSelfPriceFieldOnQuoteItem(item);
- }
- /************************************************************************
- Description: Method sets discounts according to discount type
- param 1: enxCPQ.T_PriceItem item - Quote Line Item as transfer object
- param 2: Decimal discount - Discount from item.fields on front side
- param 3: String discountType - Type set by attribute on front side
- ************************************************************************/
- private void applyDiscount(enxCPQ.T_PriceItem item, Decimal discount, String discountType) {
- if (discount != 0) {
- // Update enxCPQ__Otc__c field
- if (item.fields.containsKey('enxcpq__otc__c') && item.fields.get('enxcpq__otc__c').value != null) {
- enxCPQ.T_PriceItem.ItemFieldValueWrapper itemOtc = item.fields.get('enxcpq__otc__c');
- Decimal itemOtcList = Decimal.valueOf(item.fields.get('enxcpq__otc_list__c').value);
- if (discountType == AMOUNT_DISCOUNT) {
- itemOtc.value = String.valueOf(itemOtcList - discount);
- } else if (discountType == PERCENT_DISCOUNT) {
- itemOtc.value = String.valueOf(itemOtcList - (itemOtcList * discount/100));
- }
- item.fields.put('enxcpq__otc__c', itemOtc);
- }
- // Update enxCPQ__Mrc__c field
- if (item.fields.containsKey('enxcpq__mrc__c') && item.fields.get('enxcpq__mrc__c').value != null) {
- enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrc = item.fields.get('enxcpq__mrc__c');
- Decimal itemMrcList = Decimal.valueOf(item.fields.get('enxcpq__mrc_list__c').value);
- if (discountType == AMOUNT_DISCOUNT) {
- itemMrc.value = String.valueOf(itemMrcList - discount);
- } else if (discountType == PERCENT_DISCOUNT) {
- itemMrc.value = String.valueOf(itemMrcList - (itemMrcList * discount/100));
- }
- item.fields.put('enxcpq__mrc__c', itemMrc);
- }
- // Update Reccuring Charges Fields
- Map<String, enxCPQ.T_PriceItemCharge> chargesMap = item.charges;
- for (String chargeId : chargesMap.keySet()) {
- enxCPQ.T_PriceItemCharge charge = chargesMap.get(chargeId);
- if (charge.chargeType != 'Self Price') {
- if (discountType == AMOUNT_DISCOUNT) {
- charge.chargeUnitPrice = charge.chargeListPrice - discount;
- if (charge.chargeUnitPriceTotal != null) { charge.chargeUnitPriceTotal = charge.chargeUnitPriceTotal - discount; }
- } else if (discountType == PERCENT_DISCOUNT) {
- charge.chargeUnitPrice = charge.chargeListPrice - (charge.chargeListPrice * discount/100);
- if (charge.chargeUnitPriceTotal != null) { charge.chargeUnitPriceTotal = charge.chargeUnitPriceTotal - (charge.chargeUnitPriceTotal * discount/100); }
- }
- }
- item.charges.put(chargeId, charge);
- }
- // TL2-97
- if (item.childItems != null && !item.childItems.isEmpty()) {
- for (enxCPQ.T_PriceItem child : item.childItems) {
- if (child.fields.containsKey('enxcpq__mrc__c') && child.fields.get('enxcpq__mrc__c').value != null) {
- enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrc = child.fields.get('enxcpq__mrc__c');
- Decimal childMrcList = Decimal.valueOf(child.fields.get('enxcpq__mrc_list__c').value);
- if (discountType == AMOUNT_DISCOUNT) {
- childMrc.value = String.valueOf(childMrcList - discount);
- } else if (discountType == PERCENT_DISCOUNT) {
- childMrc.value = String.valueOf(childMrcList - (childMrcList * discount/100));
- }
- // Decimal childMrcAfterDiscount = childMrcList - (childMrcList * discount/100);
- // childMrc.value = String.valueOf(childMrcAfterDiscount);
- // child.mrc = childMrcAfterDiscount;
- child.fields.put('enxcpq__mrc__c', childMrc);
- }
- }
- }
- }
- }
- /************************************************************************
- Description: Method sets TECH_Self_Price_Base__c field on QLI with value from Charge type Self Price
- param 1: enxCPQ.T_PriceItem item - Quote Line Item as transfer object
- ************************************************************************/
- private void setSelfPriceFieldOnQuoteItem(enxCPQ.T_PriceItem item) {
- Map<String, enxCPQ.T_PriceItemCharge> charges = item.charges;
- List<enxCPQ.T_PriceItemCharge> selfPriceCharges = new List<enxCPQ.T_PriceItemCharge> ();
- if (!charges.isEmpty()) {
- for (enxCPQ.T_PriceItemCharge c : charges.values()) {
- if (c.chargeType == 'Self Price') {
- if (item.fields.containsKey('tech_self_price_base__c')) {
- item.fields.get('tech_self_price_base__c').value = String.valueOf(c.chargeListPrice);
- }
- }
- }
- }
- }
- /************************************************************************
- Description: Check availibity of fields on T_PriceItem
- param 1: enxCPQ.T_PriceItem child - single child item of main calculated item
- ************************************************************************/
- private Boolean areFieldsAvailable(enxCPQ.T_PriceItem child) {
- Boolean result = true;
- if (child.fields.get('enxcpq__mrc__c') == null) result = false;
- if (child.fields.get('enxcpq__mrc_list__c') == null) result = false;
- if (child.fields.get('enxcpq__mrc_total__c') == null) result = false;
- if (child.fields.get('enxcpq__mrc_list_total__c') == null) result = false;
- if (child.fields.get('quantity') == null) result = false;
- return result;
- }
- global class ExpandedBundleElementWrapper {
- String productName {get; set;}
- String masterProductId {get;set;}
- Decimal mrcToAdd {get;set;}
- List<PricebookEntry> connectedPricebookEntryList {get;set;}
- global ExpandedBundleElementWrapper(enxCPQ.T_PriceItem item) {
- this.masterProductId = item.masterProductId;
- this.mrcToAdd = 0;
- connectedPricebookEntryList = new List<PricebookEntry>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement