Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. importScript( "app_cc2_asda:library/c2_asda/webservice/vieworderdetails/libPopulateDTOFromDwareOrder.ds" );
  4. importScript("bc_ecm:library/utility/logging/libLog.ds");
  5. importScript( "app_ecm:util/libDateHelper.ds" );
  6. importScript( "bc_ecm:library/order/libOrderCalculations.ds");
  7.  
  8.  
  9.  
  10. var guard = require('~/cartridge/scripts/guard');
  11. var ISML = require('dw/template/ISML');
  12. var ContentMgr = require('dw/content/ContentMgr');
  13. var ProductLineItem = require('dw/order/ProductLineItem');
  14. var PromotionMgr = require('dw/campaign/PromotionMgr');
  15.  
  16.  
  17. function Show() {
  18.     var orderDetailsDTO = getOrderDetailsDTO();
  19.     var vatReceipt = {};
  20.  
  21.     vatReceipt.billingInformation = getCustomerBillingInformationFromOrderDTO(orderDetailsDTO);
  22.     vatReceipt.productsInformation = getListOfProductsFromOrderDTO(orderDetailsDTO);
  23.     vatReceipt.orderInformation = getOrderInformationFromOrderDTO(orderDetailsDTO);
  24.     vatReceipt.orderPromotions = getOrderPromotions(orderDetailsDTO);
  25.  
  26.     ISML.renderTemplate('bill_vat_details.isml', {"output" : vatReceipt});
  27. }
  28.  
  29. function getOrderDetailsDTO() {
  30.     var _logger  = new Log();
  31.     var dwOrder = require('dw/order/OrderMgr').getOrder('00011503_dev293');
  32.     var orderDetailsDTO = new PopulateDTOFromDwareOrderHelper(_logger, dwOrder);
  33.     return orderDetailsDTO.getOrderDetailsDTO();
  34. }
  35.  
  36. function getCustomerBillingInformationFromOrderDTO(orderDTO) {
  37.     return {
  38.         title: orderDTO._dwareOrder.getBillingAddress().getTitle(),
  39.         fullName: orderDTO._dwareOrder.getBillingAddress().getFullName(),
  40.         address1: orderDTO._dwareOrder.getBillingAddress().getAddress1(),
  41.         address2: orderDTO._dwareOrder.getBillingAddress().getAddress2(),
  42.         city: orderDTO._dwareOrder.getBillingAddress().getCity(),
  43.         county: orderDTO._dwareOrder.getBillingAddress().getStateCode(),
  44.         postalCode: orderDTO._dwareOrder.currentOrder.getBillingAddress().getPostalCode(),
  45.         country: orderDTO._dwareOrder.getBillingAddress().getCountryCode().value
  46.     };
  47. }
  48.  
  49. function getListOfProductsFromOrderDTO(orderDTO) {
  50.     var listOfProducts = {};
  51.     var plis = orderDTO._dwareOrder.getProductLineItems().iterator();
  52.  
  53.     while(plis.hasNext()){
  54.         var productLineItem = plis.next();
  55.         listOfProducts[productLineItem.getProductID()] = getProductInformationFromProductLineItem(productLineItem);
  56.     }
  57.  
  58.     return listOfProducts;
  59. }
  60.  
  61. function getProductInformationFromProductLineItem(productLineItem) {
  62.     return {
  63.         productID: productLineItem.getProductID(),
  64.         productName: productLineItem.getProductName(),
  65.         quantity: productLineItem.getQuantity().value,
  66.         totalPrice: productLineItem.getProratedPrice().decimalValue.toString(),
  67.         vatPercentage: productLineItem.getTaxRate()*100,
  68.         excludingVatPrice: productLineItem.getAdjustedNetPrice().decimalValue.toString(),
  69.         vatPrice: productLineItem.getAdjustedTax().decimalValue.toString(),
  70.         productPromotion: getProductPromotions(productLineItem)
  71.     }
  72. }
  73.  
  74. function getOrderInformationFromOrderDTO(orderDTO) {
  75.     var asdaVatContent = ContentMgr.getContent('ACCOUNT_SETTINGS_YOUR_ORDERS_VAT_REGISTRATION_NUMBER').custom.body;
  76.     return {
  77.         orderNumber:orderDTO._dwareOrder.getOrderNo(),
  78.         orderDate: DateHelper.formatDate( new Date(orderDTO._dwareOrder.originalOrder.creationDate), 'dd/MM/YYYY'),
  79.         invoiceDate: DateHelper.formatDate( new Date(), 'dd/MM/YYYY'),
  80.         vatRegistrationNumber: asdaVatContent.toString().replace('The Asda VAT registration number is ', '').replace('.', '')
  81.     }
  82. }
  83.  
  84. function getOrderPromotions(orderDTO) {
  85.     var orderPromotions = {};
  86.     var orderPromotionsArrayList = new OrderCalculations().getOrderPromotionsText(orderDTO._dwareOrder);
  87.  
  88.     for(promotion in orderPromotionsArrayList) {
  89.         orderPromotions[orderPromotionsArrayList[promotion]] = {
  90.             "Name": orderPromotionsArrayList[promotion],
  91.             "Discount": new OrderCalculations().getOrderLevelDiscounts(orderDTO._dwareOrder)
  92.         }
  93.     }
  94.  
  95.  
  96.     return orderPromotions;
  97. }
  98.  
  99. function getProductPromotions(productLineItem) {
  100.     var productPromotions = {};
  101.     var productPromotionsArrayList = new OrderCalculations().getLinePromotionsText(productLineItem);
  102.     var productPriceAdjustments = productLineItem.getPriceAdjustments();
  103.     var productAdjustmentsItr = productPriceAdjustments.iterator();
  104.  
  105.     var totalProductAdjustments = 0;
  106.  
  107.  
  108.     while(productAdjustmentsItr.hasNext()) {
  109.         var priceAdjustment = productAdjustmentsItr.next();
  110.         totalProductAdjustments = totalProductAdjustments + priceAdjustment.grossPrice;
  111.     }
  112.  
  113.     for(promotion in productPromotionsArrayList) {
  114.         productPromotions[productPromotionsArrayList[promotion]] = {
  115.             "Name": productPromotionsArrayList[promotion],
  116.             "Discount": totalProductAdjustments
  117.         }
  118.     }
  119.  
  120.     return productPromotions;
  121. }
  122. module.exports.Show = guard.ensure(['get'], Show);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement