Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. global with sharing class PricingVariableMapHookImplementation implements vlocity_cmt.VlocityOpenInterface{
  2.  
  3. global Boolean invokeMethod(String methodName, Map<String, Object> input, Map<String, Object> output, Map<String, Object> options)
  4. {
  5. try
  6. {
  7. System.debug('____ PricingVariableMapHookImplementation ' + methodName);
  8. System.debug('____ Input: ' + input);
  9. if (methodName.equalsIgnoreCase('calculate.PreInvoke'))
  10. {
  11. // dump the input
  12. System.debug('--- calculate.PreInvoke input: ' + JSON.serialize(input));
  13. }
  14. else if (methodName.equalsIgnoreCase('calculate.PostInvoke'))
  15. {
  16. // log the output
  17. System.debug('--- calculate.PostInvoke output: ' + JSON.serialize(output));
  18.  
  19. // Retrieve the pricing variable map from the output
  20. Map<String, Object> pricingVariableCodeToValueMap = (Map<String, Object>)output.get('pricingVariableMap');
  21.  
  22. // Retrieve the pricing log data map from the output
  23. Map<String, Object> pricingLogDataMap = (Map<String, Object>)output.get('pricingLogDataMap');
  24.  
  25. // Retrieve the isRoot flag from the input
  26. Boolean isRoot = (Boolean)input.get('isRoot');
  27.  
  28. // Apply Warranty discount and adjust OT_STD_PRC_TOTAL
  29.  
  30. // Get the LINE_QUANTITY pricing variable value
  31. Decimal LINE_QUANTITY = (Decimal)pricingVariableCodeToValueMap.get('LINE_QUANTITY');
  32.  
  33. // Get the OT_STD_PRC pricing variable value
  34. Decimal OT_STD_PRC = (Decimal)pricingVariableCodeToValueMap.get('OT_STD_PRC');
  35.  
  36. // Get the OT_STD_PRC_CALC pricing variable value
  37. Decimal OT_STD_PRC_CALC = (Decimal)pricingVariableCodeToValueMap.get('OT_STD_PRC_CALC');
  38.  
  39. // Get the OT_STD_PRC_DISC_PCT_MAN pricing variable value
  40. Decimal OT_STD_PRC_DISC_PCT_MAN = (Decimal)pricingVariableCodeToValueMap.get('OT_STD_PRC_DISC_PCT_MAN');
  41.  
  42. // Get the OT_STD_PRC_TOTAL pricing variable value
  43. Decimal OT_STD_PRC_TOTAL = (Decimal)pricingVariableCodeToValueMap.get('OT_STD_PRC_TOTAL');
  44.  
  45. // Set the Warranty Disc amount from the line in cart having Product2.Product Family = ‘Movil’ AND Product2.Type = ‘Dispositivo’ and OrderItem.Action = ‘Disconnect’
  46.  
  47. Decimal WarrantyDis = 0.00;
  48.  
  49. System.debug('--- WarrantyDis:'+WarrantyDis);
  50.  
  51. String itemID = (String)input.get('itemId');
  52. OrderItem originOrderItem = new OrderItem();
  53. OrderItem finalOrderItem = new OrderItem();
  54. originOrderItem = [SELECT id, OrderId FROM OrderItem WHERE id =: ItemID];
  55. Order selectedOrder = new Order();
  56. selectedOrder = [SELECT Gestion__c, Id FROM Order WHERE Id =: originOrderItem.OrderId];
  57. if(selectedOrder.Gestion__c=='Cambio en Garantia'){
  58. List <OrderItem> orderItems = new List <OrderItem>();
  59. orderItems = [SELECT id, OrderId, Product2Id FROM OrderItem WHERE OrderId =: selectedOrder.id];
  60. for (orderItem item : orderItems){
  61. if (item.id != ItemID){
  62. finalOrderItem = item;
  63. }
  64. }
  65. List <vlocity_cmt__PriceListEntry__c> priceListEntries = new List <vlocity_cmt__PriceListEntry__c>();
  66. priceListEntries = [SELECT vlocity_cmt__IsBasePrice__c,vlocity_cmt__ProductId__c,Price_Amount__c, vlocity_cmt__IsActive__c, Pricing_Variable_Code__c, vlocity_cmt__EffectiveFromDate__c, vlocity_cmt__EffectiveUntilDate__c FROM vlocity_cmt__PriceListEntry__c WHERE vlocity_cmt__IsBasePrice__c = true AND vlocity_cmt__ProductId__c =: finalOrderItem.Product2Id AND vlocity_cmt__IsActive__c = true AND Pricing_Variable_Code__c = 'OT_STD_PRC' AND vlocity_cmt__EffectiveFromDate__c < TODAY AND (vlocity_cmt__EffectiveUntilDate__c > TODAY OR vlocity_cmt__EffectiveUntilDate__c = null)];
  67. if(priceListEntries.size()>0){
  68. System.debug('Entro 1');
  69. vlocity_cmt__PriceListEntry__c plEntry = new vlocity_cmt__PriceListEntry__c();
  70. plEntry = priceListEntries[0];
  71. WarrantyDis = plEntry.Price_Amount__c;
  72. System.debug('WarrantyDis: '+WarrantyDis);
  73. }
  74. }
  75.  
  76.  
  77. // Override the default calculation formula. The formula being overridden is:
  78.  
  79. // OT_STD_PRC_CALC = OT_STD_PRC - (OT_STD_PRC * OT_STD_PRC_DISC_PCT_MAN / 100);
  80.  
  81. if(OT_STD_PRC - (OT_STD_PRC * OT_STD_PRC_DISC_PCT_MAN / 100) - WarrantyDis<0){
  82. OT_STD_PRC_CALC = 0;
  83. }else{
  84. OT_STD_PRC_CALC = OT_STD_PRC - (OT_STD_PRC * OT_STD_PRC_DISC_PCT_MAN / 100) - WarrantyDis;}
  85.  
  86. // Get the ROLLUP_OT_MNTH_STD_PRC_TOTAL pricing variable value
  87. Decimal ROLLUP_OT_STD_PRC_TOTAL = (Decimal)pricingVariableCodeToValueMap.get('ROLLUP_OT_STD_PRC_TOTAL');
  88.  
  89. // Calculate line level total
  90. OT_STD_PRC_TOTAL = (OT_STD_PRC_CALC + ROLLUP_OT_STD_PRC_TOTAL) * LINE_QUANTITY;
  91.  
  92. // Save the new OT_STD_PRC_TOTAL rounded to 2 decimals in the pricing variable map of the line item
  93. pricingVariableCodeToValueMap.put('OT_STD_PRC_TOTAL', OT_STD_PRC_TOTAL.setScale(2, RoundingMode.HALF_UP));
  94.  
  95. // Set the new pricing details information for the OT_STD_PRC_TOTAL
  96. pricingLogDataMap.put('OT_STD_PRC_TOTAL',
  97. new Map<String, Object>{
  98. 'format'=>'[{0} ({1}) x {2} ({3})] + Rollup {4} ({5})',
  99. 'data'=>new List<String>{'OT_STD_PRC_CALC', // replaces {0}
  100. String.valueOf(OT_STD_PRC_CALC), // replaces {1}
  101. 'LINE_QUANTITY', // replaces {2}
  102. String.valueOf(LINE_QUANTITY), // replaces {3}
  103. 'OT_STD_PRC_TOTAL', // replaces {4}
  104. String.valueOf(ROLLUP_OT_STD_PRC_TOTAL) // replaces {5}
  105. }});
  106.  
  107. // If this is the root, then set the EFF_OT_STD_PRC_TOTAL of the line item to be the same as
  108. // OT_STD_PRC_TOTAL value. EFF_OT_STD_PRC_TOTAL participates in a rollup summary field in the parent header
  109. if (isRoot)
  110. {
  111. Decimal EFF_OT_STD_PRC_TOTAL = OT_STD_PRC_TOTAL;
  112. pricingVariableCodeToValueMap.put('EFF_OT_STD_PRC_TOTAL', EFF_OT_STD_PRC_TOTAL.setScale(2, RoundingMode.HALF_UP));
  113. }
  114. }
  115. return true;
  116. }
  117. catch (Exception ex)
  118. {
  119. System.debug('--- Exception: ' + ex.getMessage());
  120. System.debug('--- Stack Trace: ' + ex.getStackTraceString());
  121. throw ex;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement