@pmcretail/mapper-library 0.0.38 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5406,8 +5406,10 @@ if (false) {
5406
5406
  class WebReturnMapperBuilderClass {
5407
5407
  /**
5408
5408
  * @param {?} document
5409
+ * @param {?=} currencyPipe
5409
5410
  */
5410
- constructor(document) {
5411
+ constructor(document, currencyPipe) {
5412
+ this.currencyPipe = currencyPipe;
5411
5413
  this.webReturnObject = {
5412
5414
  id: '',
5413
5415
  orgCode: "",
@@ -5436,6 +5438,7 @@ class WebReturnMapperBuilderClass {
5436
5438
  if (document) {
5437
5439
  this.document = document;
5438
5440
  }
5441
+ this.numberUtilService = new NumberUtilService(currencyPipe);
5439
5442
  }
5440
5443
  /**
5441
5444
  * @template THIS
@@ -5472,7 +5475,7 @@ class WebReturnMapperBuilderClass {
5472
5475
  (/** @type {?} */ (this)).webReturnObject['customer']['shippingAddress']['postCode'] = ((/** @type {?} */ (this)).document.shipping_address && (/** @type {?} */ (this)).document.shipping_address.postcode) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.shipping_address.postcode) : '';
5473
5476
  (/** @type {?} */ (this)).webReturnObject['customer']['shippingAddress']['country'] = ((/** @type {?} */ (this)).document.shipping_address && (/** @type {?} */ (this)).document.shipping_address.country) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.shipping_address.country) : '';
5474
5477
  // basketSummary
5475
- (/** @type {?} */ (this)).webReturnObject.basketSummary['discountTotal'] = Number((/** @type {?} */ (this)).document.discount_amount ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.discount_amount) : 0);
5478
+ (/** @type {?} */ (this)).webReturnObject.basketSummary['discountTotal'] = Math.abs(Number((/** @type {?} */ (this)).document.discount_amount ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.discount_amount) : 0));
5476
5479
  (/** @type {?} */ (this)).webReturnObject.basketSummary['VATTotal'] = Number((/** @type {?} */ (this)).document.base_total_paid ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.base_total_paid) : 0);
5477
5480
  (/** @type {?} */ (this)).webReturnObject.basketSummary['saleTotal'] = Number((/** @type {?} */ (this)).document.base_total_invoiced ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.base_total_invoiced) : 0);
5478
5481
  (/** @type {?} */ (this)).webReturnObject.basketSummary['basketTotal'] = Number((/** @type {?} */ (this)).document.base_total_paid ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.base_total_paid) : 0);
@@ -5486,23 +5489,25 @@ class WebReturnMapperBuilderClass {
5486
5489
  // }
5487
5490
  // this.webReturnObject.basket['lineDiscount'].push(lineDiscount);
5488
5491
  // basket transaction discount
5489
- (/** @type {?} */ (this)).webReturnObject.basket['transactionDiscount'] = [];
5490
- /** @type {?} */
5491
- let transactionDiscount = {
5492
- discountAmount: ((/** @type {?} */ (this)).document && (/** @type {?} */ (this)).document.discount_amount) ? Number((/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.discount_amount)) : 0
5493
- };
5494
- (/** @type {?} */ (this)).webReturnObject.basket['transactionDiscount'].push(transactionDiscount);
5495
5492
  // basket products
5496
5493
  (/** @type {?} */ (this)).webReturnObject.basket['products'] = [];
5494
+ (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'] = [];
5495
+ (/** @type {?} */ (this)).webReturnObject.basket['transactionDiscount'] = [];
5496
+ (/** @type {?} */ (this)).webReturnObject.basket['transactionPromotion'] = [];
5497
+ // let transactionDiscount = {
5498
+ // discountAmount: (this.document && this.document.discount_amount) ? Number(this.getText(this.document.discount_amount)) : 0
5499
+ // }
5500
+ // this.webReturnObject.basket['transactionDiscount'].push(transactionDiscount);
5497
5501
  /** @type {?} */
5498
5502
  let product = {};
5499
5503
  if ((/** @type {?} */ (this)).document.items && (/** @type {?} */ (this)).document.items.item) {
5500
5504
  if ((/** @type {?} */ (this)).document.items.item instanceof Array) {
5501
5505
  (/** @type {?} */ (this)).document.items.item.forEach((/**
5502
5506
  * @param {?} element
5507
+ * @param {?} index
5503
5508
  * @return {?}
5504
5509
  */
5505
- element => {
5510
+ (element, index) => {
5506
5511
  /** @type {?} */
5507
5512
  let qty = (/** @type {?} */ (this)).getText(element.qty_ordered) ? Number((/** @type {?} */ (this)).getText(element.qty_ordered)) : 0;
5508
5513
  /** @type {?} */
@@ -5516,11 +5521,30 @@ class WebReturnMapperBuilderClass {
5516
5521
  refundedQuantity: (element.qty_refunded) ? Number((/** @type {?} */ (this)).getText(element.qty_refunded)) : 0,
5517
5522
  //price: (element.original_price) ? Number(this.getText(element.original_price)) : 0,
5518
5523
  price: price,
5519
- itemLineId: (element.item_id) ? Number((/** @type {?} */ (this)).getText(element.item_id)) : 0,
5524
+ itemLineId: (index + 1).toString(),
5520
5525
  // totalLinePrice: (element.row_invoiced) ? Number(this.getText(element.row_invoiced)) : 0,
5521
5526
  totalLinePrice: ((qty - refundQty) * price),
5522
5527
  };
5528
+ /** @type {?} */
5529
+ let promotionAmount = (/** @type {?} */ (this)).getText(element.discount_amount) ? Number((/** @type {?} */ (this)).getText(element.discount_amount)) : 0;
5530
+ /** @type {?} */
5531
+ let appliedIDs = (/** @type {?} */ (this)).getText(element.applied_rule_ids) ? (/** @type {?} */ (this)).getText(element.applied_rule_ids) : '';
5523
5532
  if (product && product.price != 0) {
5533
+ if (promotionAmount) {
5534
+ /** @type {?} */
5535
+ let trigger = (/** @type {?} */ ({}));
5536
+ trigger.itemLineId = product.itemLineId;
5537
+ trigger.quantity = product.quantity ? product.quantity : 0;
5538
+ /** @type {?} */
5539
+ let promotion = {
5540
+ promotionAmount: promotionAmount,
5541
+ promotionId: appliedIDs,
5542
+ itemLineId: product.itemLineId,
5543
+ promotionLinePrice: product.totalLinePrice - promotionAmount,
5544
+ trigger: trigger
5545
+ };
5546
+ (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'].push(promotion);
5547
+ }
5524
5548
  (/** @type {?} */ (this)).webReturnObject.basket['products'].push(product);
5525
5549
  }
5526
5550
  }));
@@ -5539,17 +5563,189 @@ class WebReturnMapperBuilderClass {
5539
5563
  refundedQuantity: ((/** @type {?} */ (this)).document.items && (/** @type {?} */ (this)).document.items.item && (/** @type {?} */ (this)).document.items.item.qty_refunded) ? Number((/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.qty_refunded)) : 0,
5540
5564
  // price: (this.document.items && this.document.items.item && this.document.items.item.original_price) ? Number(this.getText(this.document.items.item.original_price)) : 0,
5541
5565
  price: price,
5542
- itemLineId: ((/** @type {?} */ (this)).document.items && (/** @type {?} */ (this)).document.items.item && (/** @type {?} */ (this)).document.items.item.item_id) ? Number((/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.item_id)) : 0,
5566
+ itemLineId: "1",
5543
5567
  // totalLinePrice: (this.document.items && this.document.items.item && this.document.items.item.row_invoiced) ? Number(this.getText(this.document.items.item.row_invoiced)) : 0,
5544
5568
  totalLinePrice: ((qty - refundQty) * price),
5545
5569
  };
5570
+ /** @type {?} */
5571
+ let promotionAmount = (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.discount_amount) ? Number((/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.discount_amount)) : 0;
5572
+ /** @type {?} */
5573
+ let appliedIDs = (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.applied_rule_ids) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.items.item.applied_rule_ids) : '';
5546
5574
  if (product && product.price != 0) {
5575
+ if (promotionAmount) {
5576
+ /** @type {?} */
5577
+ let trigger = (/** @type {?} */ ({}));
5578
+ trigger.itemLineId = product.itemLineId;
5579
+ trigger.quantity = product.quantity ? product.quantity : 0;
5580
+ /** @type {?} */
5581
+ let promotion = {
5582
+ promotionAmount: promotionAmount,
5583
+ promotionId: appliedIDs,
5584
+ itemLineId: product.itemLineId,
5585
+ promotionLinePrice: product.totalLinePrice - promotionAmount,
5586
+ trigger: trigger
5587
+ };
5588
+ (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'].push(promotion);
5589
+ }
5547
5590
  (/** @type {?} */ (this)).webReturnObject.basket['products'].push(product);
5548
5591
  }
5549
5592
  }
5593
+ /** @type {?} */
5594
+ let couponCode = (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.coupon_code) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.coupon_code) : 0;
5595
+ /** @type {?} */
5596
+ let appliedIDs = (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.applied_rule_ids) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.applied_rule_ids) : '';
5597
+ /** @type {?} */
5598
+ let discountDescription = (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.discount_description) ? (/** @type {?} */ (this)).getText((/** @type {?} */ (this)).document.discount_description) : '';
5599
+ /** @type {?} */
5600
+ let couponTriggerPromotions = (/** @type {?} */ ([]));
5601
+ if (couponCode && appliedIDs) {
5602
+ /** @type {?} */
5603
+ let transactionPromotion = (/** @type {?} */ ({}));
5604
+ if ((/** @type {?} */ (this)).webReturnObject.basket['linePromotion'] && (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'].length > 0) {
5605
+ (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'].forEach((/**
5606
+ * @param {?} promotion
5607
+ * @return {?}
5608
+ */
5609
+ (promotion) => {
5610
+ if (promotion.promotionId === appliedIDs) {
5611
+ /** @type {?} */
5612
+ let trigger = (/** @type {?} */ ({}));
5613
+ trigger.itemLineId = promotion.itemLineId;
5614
+ trigger.quantity = promotion.trigger.quantity;
5615
+ trigger.totalQuantity = promotion.trigger.quantity;
5616
+ trigger.promotionAmount = promotion.promotionAmount;
5617
+ trigger.refundedReason = couponCode;
5618
+ trigger.refundedReasonDescription = discountDescription;
5619
+ trigger.promotionId = promotion.promotionId;
5620
+ couponTriggerPromotions.push(trigger);
5621
+ (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'] = (/** @type {?} */ (this)).webReturnObject.basket['linePromotion'].filter((/**
5622
+ * @param {?} x
5623
+ * @return {?}
5624
+ */
5625
+ (x) => x.itemLineId !== promotion.itemLineId));
5626
+ }
5627
+ }));
5628
+ }
5629
+ if (couponTriggerPromotions.length > 0) {
5630
+ transactionPromotion.displayText = couponTriggerPromotions[0].refundedReasonDescription;
5631
+ transactionPromotion.name = couponTriggerPromotions[0].refundedReason;
5632
+ transactionPromotion.promotionAmount = -(couponTriggerPromotions.reduce((/**
5633
+ * @param {?} n
5634
+ * @param {?} __1
5635
+ * @return {?}
5636
+ */
5637
+ (n, { promotionAmount }) => n + promotionAmount), 0));
5638
+ transactionPromotion.promotionId = couponTriggerPromotions[0].promotionId;
5639
+ transactionPromotion.promotionInfo = couponTriggerPromotions[0].refundedReasonDescription;
5640
+ transactionPromotion.couponCode = couponCode;
5641
+ transactionPromotion.itemLineIds = [];
5642
+ transactionPromotion.type = PROMOTION_TYPE.COUPON;
5643
+ transactionPromotion.subLevelType = PROMOTION_LEVEL.LINE;
5644
+ transactionPromotion.trigger = couponTriggerPromotions;
5645
+ }
5646
+ console.log("e-basket: update LinePromotion With Coupon", JSON.stringify(transactionPromotion));
5647
+ if (!(/** @type {?} */ (this)).webReturnObject.basket.transactionPromotion) {
5648
+ (/** @type {?} */ (this)).webReturnObject.basket.transactionPromotion = [];
5649
+ }
5650
+ if (transactionPromotion && transactionPromotion.promotionId) {
5651
+ (/** @type {?} */ (this)).webReturnObject.basket.transactionPromotion.push(transactionPromotion);
5652
+ }
5653
+ }
5654
+ (/** @type {?} */ (this)).updateTransactionPromotionPrtionCalc();
5550
5655
  }
5551
5656
  return (/** @type {?} */ (this));
5552
5657
  }
5658
+ /**
5659
+ * @param {?} product
5660
+ * @return {?}
5661
+ */
5662
+ validateProductItemDiscount(product) {
5663
+ if (!product.itemDiscounts) {
5664
+ product.itemDiscounts = {
5665
+ trnDiscountsPortion: 0,
5666
+ trnPromotionsPortion: 0,
5667
+ refundPortion: 0,
5668
+ };
5669
+ }
5670
+ return product;
5671
+ }
5672
+ /**
5673
+ * @param {?} txDoc
5674
+ * @param {?} promotion
5675
+ * @return {?}
5676
+ */
5677
+ transactionPromotionProductPortionCalc(txDoc, promotion) {
5678
+ console.log(`ebasket: transactionPromotionProductPortionCalc: trnsactionPromotion: ${JSON.stringify(promotion)}`);
5679
+ if (promotion.type === PROMOTION_TYPE.COUPON && promotion.subLevelType === PROMOTION_LEVEL.LINE) {
5680
+ txDoc.basket.products.forEach((/**
5681
+ * @param {?} product
5682
+ * @return {?}
5683
+ */
5684
+ (product) => {
5685
+ product = this.validateProductItemDiscount(product);
5686
+ if (promotion.trigger && promotion.trigger.length > 0) {
5687
+ promotion.trigger.forEach((/**
5688
+ * @param {?} t
5689
+ * @return {?}
5690
+ */
5691
+ (t) => {
5692
+ if (t.itemLineId === product.itemLineId.toString()) {
5693
+ product.itemDiscounts.trnPromotionsPortion += t.promotionAmount;
5694
+ }
5695
+ }));
5696
+ }
5697
+ }));
5698
+ }
5699
+ else {
5700
+ txDoc.basket.products.forEach((/**
5701
+ * @param {?} product
5702
+ * @return {?}
5703
+ */
5704
+ (product) => {
5705
+ product = this.validateProductItemDiscount(product);
5706
+ if (promotion.itemLineIds.includes(+product.itemLineId)) {
5707
+ /** @type {?} */
5708
+ let discountAmount = 0;
5709
+ if (this.document.items.item instanceof Array) {
5710
+ /** @type {?} */
5711
+ const productItem = this.document.items.item.find((/**
5712
+ * @param {?} productItem
5713
+ * @return {?}
5714
+ */
5715
+ productItem => productItem.sku === product.SKU));
5716
+ discountAmount = productItem.discount_amount ? +productItem.discount_amount : 0;
5717
+ }
5718
+ else {
5719
+ discountAmount = this.document.items.item.sku === product.SKU && this.document.items.item.discount_amount ?
5720
+ +this.document.items.item.discount_amount : 0;
5721
+ }
5722
+ /** @type {?} */
5723
+ const transactionPromotionPortion = discountAmount;
5724
+ product.itemDiscounts.trnPromotionsPortion += this.numberUtilService.trimTo2Decimal2(transactionPromotionPortion);
5725
+ }
5726
+ }));
5727
+ }
5728
+ return txDoc;
5729
+ }
5730
+ /**
5731
+ * @return {?}
5732
+ */
5733
+ updateTransactionPromotionPrtionCalc() {
5734
+ try {
5735
+ if (this.webReturnObject.basket.transactionPromotion && this.webReturnObject.basket.transactionPromotion.length > 0) {
5736
+ this.webReturnObject.basket.transactionPromotion.forEach((/**
5737
+ * @param {?} tr
5738
+ * @return {?}
5739
+ */
5740
+ tr => {
5741
+ this.transactionPromotionProductPortionCalc(this.webReturnObject, tr);
5742
+ }));
5743
+ }
5744
+ }
5745
+ catch (error) {
5746
+ console.log("updateTransactionPromotionPrtionCalc => failed:", JSON.stringify(error));
5747
+ }
5748
+ }
5553
5749
  /**
5554
5750
  * @template THIS
5555
5751
  * @this {THIS}
@@ -5692,6 +5888,13 @@ if (false) {
5692
5888
  * @private
5693
5889
  */
5694
5890
  WebReturnMapperBuilderClass.prototype.webReturnObject;
5891
+ /** @type {?} */
5892
+ WebReturnMapperBuilderClass.prototype.numberUtilService;
5893
+ /**
5894
+ * @type {?}
5895
+ * @private
5896
+ */
5897
+ WebReturnMapperBuilderClass.prototype.currencyPipe;
5695
5898
  }
5696
5899
 
5697
5900
  /**