Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.22 KB | None | 0 0
  1. /************************************************************************
  2. * Description : Custom discounts Price Engine extension
  3. * Created by : Jakub Wanowski (jakub.wanowski@enxoo.com)
  4. * Date created : 2019-06-20
  5. ************************************************************************/
  6. global class PRI02_PriceCalculatorClsImpl extends enxCPQ.CPQ_PRI02_PriceCalculatorCls {
  7.  
  8. // TL2-97
  9. List<ExpandedBundleElementWrapper> bundleElementWrappersList = new List<ExpandedBundleElementWrapper>();
  10.  
  11. final String AMOUNT_DISCOUNT = 'AMOUNT_DISCOUNT';
  12. final String PERCENT_DISCOUNT = 'PERCENT_DISCOUNT';
  13.  
  14.  
  15. /************************************************************************
  16. Description: Method extended with custom attribute dependencies check
  17. Require: JSON file with dependencies saved as Static Resource
  18. ************************************************************************/
  19. global override List<enxCPQ.T_QuoteItem> calculatePrices(List<enxCPQ.T_QuoteItem> items) {
  20.  
  21.  
  22.  
  23. return super.calculatePrices(items);
  24. }
  25.  
  26.  
  27. /************************************************************************
  28. Description: Tele2 Custom Pricing Logic
  29. param 1: T_PriceItem object
  30. ************************************************************************/
  31. global override void applyCustomLogic(enxCPQ.T_PriceItem item) {
  32.  
  33.  
  34. // TL2-97 - TEAMER PRODUCTS PRICING
  35. if (item.childItems != null && !item.childItems.isEmpty()) {
  36. Map<String, enxCPQ.T_PriceItemCharge> charges = new Map<String, enxCPQ.T_PriceItemCharge>();
  37.  
  38. // Get parent item pricing fields
  39. enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrcField = item.fields.get('enxcpq__mrc__c');
  40. enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrcListField = item.fields.get('enxcpq__mrc_list__c');
  41.  
  42. Decimal simCardsQuantity = 1;
  43. Decimal sumOfChildMrcList = 0;
  44.  
  45. for (enxCPQ.T_PriceItem child : item.childItems) {
  46.  
  47. // Get child item pricing fields
  48. enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcField = child.fields.get('enxcpq__mrc__c');
  49. enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcListField = child.fields.get('enxcpq__mrc_list__c');
  50. enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcTotalField = child.fields.get('enxcpq__mrc_total__c');
  51. enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrcListTotalField = child.fields.get('enxcpq__mrc_list_total__c');
  52. enxCPQ.T_PriceItem.ItemFieldValueWrapper childQuantity = child.fields.get('quantity');
  53.  
  54. Decimal childMrcList = Decimal.valueOf(childMrcListField.value);
  55.  
  56. // Multiply MRC by SIM Count attribute value
  57. if (item.fields.containsKey('sim_cards_quantity__c') && item.fields.get('sim_cards_quantity__c').value != null) {
  58. simCardsQuantity = Decimal.valueOf(item.fields.get('sim_cards_quantity__c').value);
  59. }
  60.  
  61.  
  62.  
  63. for (ExpandedBundleElementWrapper bundleElementWrapper : bundleElementWrappersList) {
  64. if (child.masterProductId == bundleElementWrapper.masterProductId) {
  65. childMrcList += bundleElementWrapper.mrcToAdd;
  66.  
  67. // Create Charge
  68. enxCPQ.T_PriceItemCharge charge = new enxCPQ.T_PriceItemCharge();
  69. charge.chargeId = bundleElementWrapper.masterProductId;
  70. charge.name = bundleElementWrapper.productName;
  71. charge.chargeName = bundleElementWrapper.productName;
  72. charge.sortingOrder = -1;
  73. charge.chargeListPrice = childMrcList;
  74. charge.chargeListPriceTotal = childMrcList * simCardsQuantity;
  75. charge.chargeUnitPrice = childMrcList;
  76. charge.chargeUnitPriceTotal = childMrcList * simCardsQuantity;
  77. charge.chargeModel = 'Flat Fee';
  78. charge.chargeType = 'Recurring';
  79. charge.syncPrice = true;
  80. charge.unitOfMeasure = 'SIM';
  81. charge.billingFrequency = 'Monthly';
  82. charge.multiplierValue = simCardsQuantity;
  83. charges.put(charge.chargeId, charge);
  84.  
  85. break;
  86. }
  87. }
  88.  
  89. if (areFieldsAvailable(child)) {
  90. childMrcField.value = String.valueOf(childMrcList);
  91. childMrcListField.value = String.valueOf(childMrcList);
  92. childMrcTotalField.value = String.valueOf(childMrcList * simCardsQuantity);
  93. childMrcListTotalField.value = String.valueOf(childMrcList * simCardsQuantity);
  94. childQuantity.value = String.valueOf(simCardsQuantity);
  95.  
  96. child.fields.put('enxcpq__mrc__c', childMrcField);
  97. child.fields.put('enxcpq__mrc_list__c', childMrcListField);
  98. child.fields.put('enxcpq__mrc_total__c', childMrcTotalField);
  99. child.fields.put('enxcpq__mrc_list_total__c', childMrcListTotalField);
  100. child.fields.put('quantity', childQuantity);
  101. }
  102. }
  103.  
  104. item.charges = charges;
  105. }
  106.  
  107. // DISCOUNTS START
  108. // -----------------------------------------------------------------
  109. Boolean percentDiscountIsUsed = true;
  110. Boolean amountDiscountIsUsed = (item.fields.containsKey('tech_percent_discount__c') && item.fields.get('tech_percent_discount__c').value == null) &&
  111. (item.fields.containsKey('tech_amount_discount__c') && item.fields.get('tech_amount_discount__c').value != null);
  112.  
  113. // Apply discount
  114. if (percentDiscountIsUsed) {
  115. String discountAsString = '50';
  116. applyDiscount(item, Decimal.valueOf(discountAsString), PERCENT_DISCOUNT);
  117. } else if (amountDiscountIsUsed) {
  118. String discountAsString = '50';
  119. applyDiscount(item, Decimal.valueOf(discountAsString), AMOUNT_DISCOUNT);
  120. }
  121. // DISCOUNTS END
  122. // -----------------------------------------------------------------
  123.  
  124. // SET SELF PRICE
  125. setSelfPriceFieldOnQuoteItem(item);
  126.  
  127.  
  128. }
  129.  
  130.  
  131. /************************************************************************
  132. Description: Method sets discounts according to discount type
  133. param 1: enxCPQ.T_PriceItem item - Quote Line Item as transfer object
  134. param 2: Decimal discount - Discount from item.fields on front side
  135. param 3: String discountType - Type set by attribute on front side
  136. ************************************************************************/
  137. private void applyDiscount(enxCPQ.T_PriceItem item, Decimal discount, String discountType) {
  138.  
  139.  
  140. if (discount != 0) {
  141.  
  142. // Update enxCPQ__Otc__c field
  143. if (item.fields.containsKey('enxcpq__otc__c') && item.fields.get('enxcpq__otc__c').value != null) {
  144. enxCPQ.T_PriceItem.ItemFieldValueWrapper itemOtc = item.fields.get('enxcpq__otc__c');
  145. Decimal itemOtcList = Decimal.valueOf(item.fields.get('enxcpq__otc_list__c').value);
  146.  
  147. if (discountType == AMOUNT_DISCOUNT) {
  148. itemOtc.value = String.valueOf(itemOtcList - discount);
  149. } else if (discountType == PERCENT_DISCOUNT) {
  150. itemOtc.value = String.valueOf(itemOtcList - (itemOtcList * discount/100));
  151. }
  152.  
  153. item.fields.put('enxcpq__otc__c', itemOtc);
  154. }
  155.  
  156. // Update enxCPQ__Mrc__c field
  157. if (item.fields.containsKey('enxcpq__mrc__c') && item.fields.get('enxcpq__mrc__c').value != null) {
  158. enxCPQ.T_PriceItem.ItemFieldValueWrapper itemMrc = item.fields.get('enxcpq__mrc__c');
  159. Decimal itemMrcList = Decimal.valueOf(item.fields.get('enxcpq__mrc_list__c').value);
  160.  
  161. if (discountType == AMOUNT_DISCOUNT) {
  162. itemMrc.value = String.valueOf(itemMrcList - discount);
  163. } else if (discountType == PERCENT_DISCOUNT) {
  164. itemMrc.value = String.valueOf(itemMrcList - (itemMrcList * discount/100));
  165. }
  166.  
  167. item.fields.put('enxcpq__mrc__c', itemMrc);
  168. }
  169.  
  170. // Update Reccuring Charges Fields
  171. Map<String, enxCPQ.T_PriceItemCharge> chargesMap = item.charges;
  172. for (String chargeId : chargesMap.keySet()) {
  173. enxCPQ.T_PriceItemCharge charge = chargesMap.get(chargeId);
  174.  
  175. if (charge.chargeType != 'Self Price') {
  176. if (discountType == AMOUNT_DISCOUNT) {
  177. charge.chargeUnitPrice = charge.chargeListPrice - discount;
  178. if (charge.chargeUnitPriceTotal != null) { charge.chargeUnitPriceTotal = charge.chargeUnitPriceTotal - discount; }
  179. } else if (discountType == PERCENT_DISCOUNT) {
  180. charge.chargeUnitPrice = charge.chargeListPrice - (charge.chargeListPrice * discount/100);
  181. if (charge.chargeUnitPriceTotal != null) { charge.chargeUnitPriceTotal = charge.chargeUnitPriceTotal - (charge.chargeUnitPriceTotal * discount/100); }
  182. }
  183. }
  184.  
  185. item.charges.put(chargeId, charge);
  186. }
  187.  
  188. // TL2-97
  189. if (item.childItems != null && !item.childItems.isEmpty()) {
  190. for (enxCPQ.T_PriceItem child : item.childItems) {
  191. if (child.fields.containsKey('enxcpq__mrc__c') && child.fields.get('enxcpq__mrc__c').value != null) {
  192. enxCPQ.T_PriceItem.ItemFieldValueWrapper childMrc = child.fields.get('enxcpq__mrc__c');
  193. Decimal childMrcList = Decimal.valueOf(child.fields.get('enxcpq__mrc_list__c').value);
  194.  
  195. if (discountType == AMOUNT_DISCOUNT) {
  196. childMrc.value = String.valueOf(childMrcList - discount);
  197. } else if (discountType == PERCENT_DISCOUNT) {
  198. childMrc.value = String.valueOf(childMrcList - (childMrcList * discount/100));
  199. }
  200.  
  201. // Decimal childMrcAfterDiscount = childMrcList - (childMrcList * discount/100);
  202. // childMrc.value = String.valueOf(childMrcAfterDiscount);
  203. // child.mrc = childMrcAfterDiscount;
  204.  
  205. child.fields.put('enxcpq__mrc__c', childMrc);
  206. }
  207. }
  208. }
  209. }
  210.  
  211.  
  212. }
  213.  
  214. /************************************************************************
  215. Description: Method sets TECH_Self_Price_Base__c field on QLI with value from Charge type Self Price
  216. param 1: enxCPQ.T_PriceItem item - Quote Line Item as transfer object
  217. ************************************************************************/
  218. private void setSelfPriceFieldOnQuoteItem(enxCPQ.T_PriceItem item) {
  219.  
  220.  
  221. Map<String, enxCPQ.T_PriceItemCharge> charges = item.charges;
  222. List<enxCPQ.T_PriceItemCharge> selfPriceCharges = new List<enxCPQ.T_PriceItemCharge> ();
  223.  
  224. if (!charges.isEmpty()) {
  225. for (enxCPQ.T_PriceItemCharge c : charges.values()) {
  226. if (c.chargeType == 'Self Price') {
  227. if (item.fields.containsKey('tech_self_price_base__c')) {
  228. item.fields.get('tech_self_price_base__c').value = String.valueOf(c.chargeListPrice);
  229. }
  230. }
  231. }
  232. }
  233.  
  234.  
  235. }
  236.  
  237. /************************************************************************
  238. Description: Check availibity of fields on T_PriceItem
  239. param 1: enxCPQ.T_PriceItem child - single child item of main calculated item
  240. ************************************************************************/
  241. private Boolean areFieldsAvailable(enxCPQ.T_PriceItem child) {
  242.  
  243.  
  244. Boolean result = true;
  245.  
  246. if (child.fields.get('enxcpq__mrc__c') == null) result = false;
  247. if (child.fields.get('enxcpq__mrc_list__c') == null) result = false;
  248. if (child.fields.get('enxcpq__mrc_total__c') == null) result = false;
  249. if (child.fields.get('enxcpq__mrc_list_total__c') == null) result = false;
  250. if (child.fields.get('quantity') == null) result = false;
  251.  
  252.  
  253. return result;
  254. }
  255.  
  256.  
  257. global class ExpandedBundleElementWrapper {
  258. String productName {get; set;}
  259. String masterProductId {get;set;}
  260. Decimal mrcToAdd {get;set;}
  261. List<PricebookEntry> connectedPricebookEntryList {get;set;}
  262.  
  263. global ExpandedBundleElementWrapper(enxCPQ.T_PriceItem item) {
  264. this.masterProductId = item.masterProductId;
  265. this.mrcToAdd = 0;
  266. connectedPricebookEntryList = new List<PricebookEntry>();
  267. }
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement