rerobe-js-orm 3.0.43 → 3.1.1

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.
@@ -5,7 +5,7 @@ const Order_1 = require("../../models/Order");
5
5
  const order_constants_1 = require("../../constants/order-constants");
6
6
  class OrderFromFormState extends OrderFactory_1.default {
7
7
  createOrder(fs) {
8
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
8
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
9
9
  const currencyCode = fs.fields.currencyCode.inputValue || 'SEK';
10
10
  const customer = fs.fields.customer.selectedValue || {};
11
11
  const customerAttr = {
@@ -98,7 +98,7 @@ class OrderFromFormState extends OrderFactory_1.default {
98
98
  fulfillmentStatus: fs.fields.fulfillmentStatus.selectedValue,
99
99
  statusUrl: ((_j = fs.props) === null || _j === void 0 ? void 0 : _j.statusUrl) || '',
100
100
  };
101
- const orderAttributes = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, totalPricesAttr), presentmentTotalPricesAttr), discountsAttr), shippingAndLocationAttr), customerAttr), statusesAttr), { id: ((_k = fs.props) === null || _k === void 0 ? void 0 : _k.id) || '', documentId: ((_l = fs.props) === null || _l === void 0 ? void 0 : _l.documentId) || '', name: ((_m = fs.props) === null || _m === void 0 ? void 0 : _m.name) || '', orderNumber: (_o = fs.props) === null || _o === void 0 ? void 0 : _o.orderNumber, ribbnOrderNumber: (_p = fs.props) === null || _p === void 0 ? void 0 : _p.ribbnOrderNumber, merchantId: ((_q = fs.props) === null || _q === void 0 ? void 0 : _q.merchantId) || '', currencyCode: fs.fields.currencyCode.inputValue, lineItems: fs.fields.lineItems.selectedValues || [], fulfillments: ((_r = fs.props) === null || _r === void 0 ? void 0 : _r.fulfillments) || [], refunds: ((_s = fs.props) === null || _s === void 0 ? void 0 : _s.refunds) || [], paymentType: fs.fields.paymentType.selectedValue, paymentMethod: fs.fields.paymentMethod.selectedValue || '', paymentDetails: fs.fields.paymentDetails.selectedValue || {}, notes: fs.fields.notes.inputValue || '', tags: fs.fields.tags.selectedValues || [], cancelReason: fs.fields.cancelReason.selectedValue || '', canceledAt: ((_t = fs.props) === null || _t === void 0 ? void 0 : _t.canceledAt) || '', processedAt: ((_u = fs.props) === null || _u === void 0 ? void 0 : _u.processedAt) || new Date().toUTCString(), createdAtTimestamp: 0, updatedAtTimestamp: 0 });
101
+ const orderAttributes = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, totalPricesAttr), presentmentTotalPricesAttr), discountsAttr), shippingAndLocationAttr), customerAttr), statusesAttr), { id: ((_k = fs.props) === null || _k === void 0 ? void 0 : _k.id) || '', documentId: ((_l = fs.props) === null || _l === void 0 ? void 0 : _l.documentId) || '', name: ((_m = fs.props) === null || _m === void 0 ? void 0 : _m.name) || '', orderNumber: (_o = fs.props) === null || _o === void 0 ? void 0 : _o.orderNumber, ribbnOrderNumber: (_p = fs.props) === null || _p === void 0 ? void 0 : _p.ribbnOrderNumber, merchantId: ((_q = fs.props) === null || _q === void 0 ? void 0 : _q.merchantId) || '', currencyCode: fs.fields.currencyCode.inputValue, lineItems: fs.fields.lineItems.selectedValues || [], fulfillments: ((_r = fs.props) === null || _r === void 0 ? void 0 : _r.fulfillments) || [], refunds: ((_s = fs.props) === null || _s === void 0 ? void 0 : _s.refunds) || [], paymentType: fs.fields.paymentType.selectedValue, paymentMethod: fs.fields.paymentMethod.selectedValue || '', paymentDetails: fs.fields.paymentDetails.selectedValue || {}, notes: fs.fields.notes.inputValue || '', tags: fs.fields.tags.selectedValues || [], cancelReason: fs.fields.cancelReason.selectedValue || '', canceledAt: ((_t = fs.props) === null || _t === void 0 ? void 0 : _t.canceledAt) || '', processedAt: ((_u = fs.props) === null || _u === void 0 ? void 0 : _u.processedAt) || new Date().toUTCString(), adjustments: ((_v = fs.props) === null || _v === void 0 ? void 0 : _v.adjustments) || [], createdAtTimestamp: 0, updatedAtTimestamp: 0 });
102
102
  return new Order_1.default(Object.assign({}, orderAttributes));
103
103
  }
104
104
  }
@@ -4,24 +4,45 @@ const Refund_1 = require("../../models/Refund");
4
4
  const RefundFactory_1 = require("./RefundFactory");
5
5
  class RefundFromFormState extends RefundFactory_1.default {
6
6
  createRefund(fs) {
7
- var _a, _b;
8
- let currencyCode = fs.props.totalRefunded.currencyCode || '';
9
- if (!currencyCode && fs.fields.refundLineItems.selectedValues.length) {
10
- currencyCode = ((_a = fs.fields.refundLineItems.selectedValues[0].subtotalRefund) === null || _a === void 0 ? void 0 : _a.currencyCode) || '';
11
- }
12
- const subtotalRefundedAmount = fs.fields.subtotalRefunded.inputValue;
7
+ var _a, _b, _c, _d, _e, _f;
8
+ const currencyCode = fs.props.totalRefunded.currencyCode ||
9
+ ((_b = (_a = fs.fields.refundLineItems.selectedValues[0]) === null || _a === void 0 ? void 0 : _a.subtotalRefund) === null || _b === void 0 ? void 0 : _b.currencyCode) ||
10
+ '';
11
+ const currencyCodePresentment = fs.props.totalRefundedPresentment.currencyCode ||
12
+ ((_e = (_d = (_c = fs.fields.refundLineItems.selectedValues[0]) === null || _c === void 0 ? void 0 : _c.lineItem) === null || _d === void 0 ? void 0 : _d.presentmentUnitPrice) === null || _e === void 0 ? void 0 : _e.currencyCode) ||
13
+ '';
13
14
  const preparedRefundLineItems = !fs.fields.refundLineItems.selectedValues.length
14
15
  ? []
15
16
  : fs.fields.refundLineItems.selectedValues.filter(({ quantityRefund, quantityCancel }) => quantityRefund + quantityCancel > 0);
16
17
  const refundAttributes = {
17
- documentId: ((_b = fs.props) === null || _b === void 0 ? void 0 : _b.documentId) || '',
18
+ documentId: ((_f = fs.props) === null || _f === void 0 ? void 0 : _f.documentId) || '',
18
19
  totalRefunded: { amount: fs.fields.totalRefunded.inputValue || 0, currencyCode },
19
- subtotalRefunded: { amount: subtotalRefundedAmount, currencyCode },
20
+ subtotalRefunded: { amount: fs.fields.subtotalRefunded.inputValue || 0, currencyCode },
20
21
  refundDiscount: { amount: fs.fields.refundDiscount.inputValue || 0, currencyCode },
21
22
  refundShipping: { amount: fs.fields.refundShipping.inputValue || 0, currencyCode },
22
23
  totalTax: { amount: fs.fields.totalTax.inputValue || 0, currencyCode },
23
24
  refundLineItems: preparedRefundLineItems,
24
25
  note: fs.fields.note.inputValue || '',
26
+ totalRefundedPresentment: {
27
+ amount: fs.fields.totalRefundedPresentment.inputValue || 0,
28
+ currencyCode: currencyCodePresentment,
29
+ },
30
+ subtotalRefundedPresentment: {
31
+ amount: fs.fields.subtotalRefundedPresentment.inputValue || 0,
32
+ currencyCode: currencyCodePresentment,
33
+ },
34
+ refundDiscountPresentment: {
35
+ amount: fs.fields.refundDiscountPresentment.inputValue || 0,
36
+ currencyCode: currencyCodePresentment,
37
+ },
38
+ refundShippingPresentment: {
39
+ amount: fs.fields.refundShippingPresentment.inputValue || 0,
40
+ currencyCode: currencyCodePresentment,
41
+ },
42
+ totalTaxPresentment: {
43
+ amount: fs.fields.totalTaxPresentment.inputValue || 0,
44
+ currencyCode: currencyCodePresentment,
45
+ },
25
46
  };
26
47
  return new Refund_1.default(Object.assign({}, refundAttributes));
27
48
  }
@@ -6,12 +6,13 @@ export default class RefundFormState extends FormState {
6
6
  constructor(props?: any);
7
7
  createRefund(): Refund;
8
8
  updateTotalRefunded(): void;
9
- getUpdatedRefundLineItem(lineItem: ReRobeOrderLineItem, quantityRefund: number | string, quantityCancel: number | string, unitOrderDiscountAmount: number): RefundLineItem;
9
+ getUpdatedRefundLineItem(lineItem: ReRobeOrderLineItem, quantityRefund: number | string, quantityCancel: number | string, unitOrderDiscountAmount: number, unitOrderDiscountPresentmentAmount: number): RefundLineItem;
10
10
  refundLineItemChangeHandler(val: {
11
11
  lineItemLike: ReRobeOrderLineItem | RefundLineItem;
12
12
  quantityRefund: number | string;
13
13
  quantityCancel: number | string;
14
14
  unitOrderDiscount: number;
15
+ unitOrderDiscountPresentment?: number;
15
16
  }): void;
16
17
  private fieldFactory;
17
18
  }
@@ -14,6 +14,11 @@ class RefundFormState extends FormState_1.default {
14
14
  this.fields.totalTax = this.fieldFactory('textInput', 'totalTax');
15
15
  this.fields.refundLineItems = this.fieldFactory('multiSelect', 'refundLineItems');
16
16
  this.fields.note = this.fieldFactory('textInput', 'note');
17
+ this.fields.totalRefundedPresentment = this.fieldFactory('textInput', 'totalRefundedPresentment');
18
+ this.fields.subtotalRefundedPresentment = this.fieldFactory('textInput', 'subtotalRefundedPresentment');
19
+ this.fields.refundDiscountPresentment = this.fieldFactory('textInput', 'refundDiscountPresentment');
20
+ this.fields.refundShippingPresentment = this.fieldFactory('textInput', 'refundShippingPresentment');
21
+ this.fields.totalTaxPresentment = this.fieldFactory('textInput', 'totalTaxPresentment');
17
22
  }
18
23
  createRefund() {
19
24
  const refundFactory = new RefundFromFormState_1.default();
@@ -24,15 +29,24 @@ class RefundFormState extends FormState_1.default {
24
29
  Number(this.fields.refundDiscount.inputValue) +
25
30
  Number(this.fields.refundShipping.inputValue) +
26
31
  Number(this.fields.totalTax.inputValue)).toFixed(2);
32
+ this.fields.totalRefundedPresentment.inputValue = Number(Number(this.fields.subtotalRefundedPresentment.inputValue) -
33
+ Number(this.fields.refundDiscountPresentment.inputValue) +
34
+ Number(this.fields.refundShippingPresentment.inputValue) +
35
+ Number(this.fields.totalTaxPresentment.inputValue)).toFixed(2);
27
36
  }
28
- getUpdatedRefundLineItem(lineItem, quantityRefund, quantityCancel, unitOrderDiscountAmount) {
29
- var _a;
37
+ getUpdatedRefundLineItem(lineItem, quantityRefund, quantityCancel, unitOrderDiscountAmount, unitOrderDiscountPresentmentAmount) {
38
+ var _a, _b;
30
39
  const lineItemPriceAmount = Number((_a = lineItem.originalUnitPrice) === null || _a === void 0 ? void 0 : _a.amount);
40
+ const lineItemPricePresentmentAmount = Number(((_b = lineItem.presentmentUnitPrice) === null || _b === void 0 ? void 0 : _b.amount) || 0);
31
41
  const quantityRefundValue = Number(quantityRefund || 0);
32
42
  const quantityCancelValue = Number(quantityCancel || 0);
33
43
  const discountTotalRefundAmount = Number(unitOrderDiscountAmount * (quantityRefundValue + quantityCancelValue));
34
44
  const subtotalRefundWithoutDiscount = Number((lineItemPriceAmount - unitOrderDiscountAmount) * quantityRefundValue);
35
45
  const subtotalCanceledWithoutDiscount = Number((lineItemPriceAmount - unitOrderDiscountAmount) * quantityCancelValue);
46
+ // Presentment prices
47
+ const discountTotalRefundPresentmentAmount = Number(unitOrderDiscountPresentmentAmount * (quantityRefundValue + quantityCancelValue));
48
+ const subtotalRefundWithoutPresentmentDiscount = Number((lineItemPricePresentmentAmount - unitOrderDiscountPresentmentAmount) * quantityRefundValue);
49
+ const subtotalCanceledWithoutPresentmentDiscount = Number((lineItemPricePresentmentAmount - unitOrderDiscountPresentmentAmount) * quantityCancelValue);
36
50
  return {
37
51
  lineItem,
38
52
  quantityRefund: quantityRefundValue,
@@ -40,26 +54,30 @@ class RefundFormState extends FormState_1.default {
40
54
  discountRefund: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: discountTotalRefundAmount }),
41
55
  subtotalRefund: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: subtotalRefundWithoutDiscount }),
42
56
  subtotalCanceled: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: subtotalCanceledWithoutDiscount }),
57
+ // Presentment prices
58
+ discountRefundPresentment: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: discountTotalRefundAmount }),
59
+ subtotalRefundPresentment: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: subtotalRefundWithoutDiscount }),
60
+ subtotalCanceledPresentment: Object.assign(Object.assign({}, lineItem.originalUnitPrice), { amount: subtotalCanceledWithoutDiscount }),
43
61
  };
44
62
  }
45
63
  refundLineItemChangeHandler(val) {
46
64
  const currentLineItems = this.fields.refundLineItems.selectedValues;
47
65
  const unitOrderDiscountAmount = val.unitOrderDiscount || 0;
66
+ const unitOrderDiscountPresentmentAmount = val.unitOrderDiscountPresentment || 0;
48
67
  // Add new product to line items
49
68
  if (val.lineItemLike.productId) {
50
69
  const lineItem = val.lineItemLike;
51
- const newRefundLineItem = this.getUpdatedRefundLineItem(lineItem, val.quantityRefund, val.quantityCancel, unitOrderDiscountAmount);
52
- this.fields.refundLineItems.selectedValues = [...currentLineItems, newRefundLineItem];
70
+ const newRefundLineItem = this.getUpdatedRefundLineItem(lineItem, val.quantityRefund, val.quantityCancel, unitOrderDiscountAmount, unitOrderDiscountPresentmentAmount);
71
+ this.fields.refundLineItems.selectedValues = this.utilities.uniqObjArray([...currentLineItems, newRefundLineItem], 'lineItem.productId');
53
72
  }
54
73
  // Update quantity of a line item
55
74
  if ((val.quantityRefund || val.quantityCancel) && val.lineItemLike.lineItem) {
56
75
  const refundLineItem = val.lineItemLike;
57
76
  const { lineItem, quantityRefund: prevQuantityRefund, quantityCancel: prevQuantityCancel } = refundLineItem;
58
- const updatedRefundLineItem = this.getUpdatedRefundLineItem(lineItem, val.quantityRefund || prevQuantityRefund, val.quantityCancel || prevQuantityCancel, unitOrderDiscountAmount);
59
- const lineItemToUpdateIndex = currentLineItems.findIndex((li) => li.lineItem.productId === refundLineItem.lineItem.productId);
60
- const lineItemsClone = [...currentLineItems];
61
- lineItemsClone.splice(lineItemToUpdateIndex, 1, updatedRefundLineItem);
62
- this.fields.refundLineItems.selectedValues = lineItemsClone;
77
+ const updatedRefundLineItem = this.getUpdatedRefundLineItem(lineItem, val.quantityRefund || prevQuantityRefund, val.quantityCancel || prevQuantityCancel, unitOrderDiscountAmount, unitOrderDiscountPresentmentAmount);
78
+ this.fields.refundLineItems.selectedValues = currentLineItems.map((li) => {
79
+ return li.lineItem.productId === refundLineItem.lineItem.productId ? updatedRefundLineItem : li;
80
+ });
63
81
  }
64
82
  // Update discount price
65
83
  this.fields.refundDiscount.inputValue = this.fields.refundLineItems.selectedValues
@@ -69,6 +87,13 @@ class RefundFormState extends FormState_1.default {
69
87
  this.fields.subtotalRefunded.inputValue = this.fields.refundLineItems.selectedValues
70
88
  .map((item) => { var _a; return Number(Number((_a = item.lineItem.originalUnitPrice) === null || _a === void 0 ? void 0 : _a.amount) * (item.quantityRefund + item.quantityCancel)); })
71
89
  .reduce((a, b) => a + b, 0);
90
+ // Update discount and subtotal presentment prices
91
+ this.fields.refundDiscountPresentment.inputValue = this.fields.refundLineItems.selectedValues
92
+ .map((item) => { var _a; return Number((_a = item.discountRefundPresentment) === null || _a === void 0 ? void 0 : _a.amount); })
93
+ .reduce((a, b) => a + b, 0);
94
+ this.fields.subtotalRefundedPresentment.inputValue = this.fields.refundLineItems.selectedValues
95
+ .map((item) => { var _a; return Number(Number((_a = item.lineItem.presentmentUnitPrice) === null || _a === void 0 ? void 0 : _a.amount) * (item.quantityRefund + item.quantityCancel)); })
96
+ .reduce((a, b) => a + b, 0);
72
97
  // Update total price
73
98
  this.updateTotalRefunded();
74
99
  }
@@ -78,14 +103,25 @@ class RefundFormState extends FormState_1.default {
78
103
  let inputValue = this.props[fieldKey] || '';
79
104
  let onChangeHandler = (val) => this.textInputChangeHandler(fieldKey, String(val));
80
105
  const amountTypeFields = ['totalRefunded', 'subtotalRefunded', 'refundDiscount', 'refundShipping', 'totalTax'];
81
- if (amountTypeFields.includes(fieldKey.toString())) {
82
- // @ts-ignore
106
+ const presentmentAmountTypeFields = [
107
+ 'totalRefundedPresentment',
108
+ 'subtotalRefundedPresentment',
109
+ 'refundDiscountPresentment',
110
+ 'refundShippingPresentment',
111
+ 'totalTaxPresentment',
112
+ ];
113
+ if ([...amountTypeFields, ...presentmentAmountTypeFields].includes(fieldKey.toString())) {
83
114
  inputValue = this.props[fieldKey] ? this.props[fieldKey].amount : 0;
84
115
  }
116
+ // all amountTypeFields without totalRefunded and totalRefundedPresentment
85
117
  if (fieldKey === 'subtotalRefunded' ||
86
118
  fieldKey === 'refundDiscount' ||
87
119
  fieldKey === 'refundShipping' ||
88
- fieldKey === 'totalTax') {
120
+ fieldKey === 'totalTax' ||
121
+ fieldKey === 'subtotalRefundedPresentment' ||
122
+ fieldKey === 'refundDiscountPresentment' ||
123
+ fieldKey === 'refundShippingPresentment' ||
124
+ fieldKey === 'totalTaxPresentment') {
89
125
  onChangeHandler = (val) => {
90
126
  this.fields[fieldKey].inputValue = String(val);
91
127
  this.updateTotalRefunded();
@@ -13,4 +13,9 @@ export default class OrderHelpers {
13
13
  getCreditDiscount(lineItems: ReRobeOrderLineItem[] | undefined, userAvailableCredit: number | string): number;
14
14
  getSaleDiscount(lineItems?: ReRobeOrderLineItem[]): number;
15
15
  getDiscountAmount(lineItems?: ReRobeOrderLineItem[], userAvailableCredit?: number | string): number;
16
+ getLineItemUnitOrderDiscount({ originalUnitPrice, totalDiscount, subtotalPrice, }: {
17
+ originalUnitPrice: Money;
18
+ totalDiscount?: Money;
19
+ subtotalPrice?: Money;
20
+ }): number;
16
21
  }
@@ -93,10 +93,10 @@ class OrderHelpers {
93
93
  return Number(price).toFixed(2);
94
94
  }
95
95
  getAmountSumByField(array, fieldKey) {
96
- return array.map((item) => Number(item[fieldKey].amount)).reduce((a, b) => a + b, 0);
96
+ return array.map((item) => { var _a; return Number(((_a = item[fieldKey]) === null || _a === void 0 ? void 0 : _a.amount) || 0); }).reduce((a, b) => a + b, 0);
97
97
  }
98
98
  getOrderInfoWithRefundsAndFulfillments(order) {
99
- var _a;
99
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
100
100
  const orderRefunds = order.refunds ? [...order.refunds] : [];
101
101
  const orderFulfillment = order.fulfillments ? [...order.fulfillments] : [];
102
102
  const preparedOrder = {
@@ -104,17 +104,27 @@ class OrderHelpers {
104
104
  name: order.name || '',
105
105
  currencyCode: order.currencyCode,
106
106
  fulfillmentStatus: order.fulfillmentStatus || '',
107
+ // Total prices
107
108
  totalDiscount: order.totalDiscount ? Object.assign({}, order.totalDiscount) : { amount: 0 },
108
109
  totalTax: order.totalTax ? Object.assign({}, order.totalTax) : { amount: 0 },
109
110
  subtotalPrice: order.subtotalPrice ? Object.assign({}, order.subtotalPrice) : { amount: 0 },
110
111
  totalRefunded: order.totalRefunded ? Object.assign({}, order.totalRefunded) : { amount: 0 },
111
112
  totalShippingPrice: order.totalShippingPrice ? Object.assign({}, order.totalShippingPrice) : { amount: 0 },
112
113
  totalPrice: order.totalPrice ? Object.assign({}, order.totalPrice) : { amount: 0 },
114
+ // Total prices presentment prices
115
+ totalDiscountPresentment: order.totalDiscountPresentment ? Object.assign({}, order.totalDiscountPresentment) : { amount: 0 },
116
+ totalTaxPresentment: order.totalTaxPresentment ? Object.assign({}, order.totalTaxPresentment) : { amount: 0 },
117
+ subtotalPricePresentment: order.subtotalPricePresentment ? Object.assign({}, order.subtotalPricePresentment) : { amount: 0 },
118
+ totalRefundedPresentment: order.totalRefundedPresentment ? Object.assign({}, order.totalRefundedPresentment) : { amount: 0 },
119
+ totalShippingPricePresentment: order.totalShippingPricePresentment
120
+ ? Object.assign({}, order.totalShippingPricePresentment) : { amount: 0 },
121
+ totalPricePresentment: order.totalPricePresentment ? Object.assign({}, order.totalPricePresentment) : { amount: 0 },
122
+ // Additional order info
113
123
  activeLineItems: !order.lineItems || !order.lineItems.length
114
124
  ? []
115
125
  : order.lineItems.map((item) => (Object.assign(Object.assign({}, item), { unfulfilledQuantity: Number(item.quantity), fulfilledQuantity: 0, refundedQuantity: 0, canceledQuantity: 0 }))),
116
- discountType: 'Custom discount',
117
- shippingRate: 'Custom shipping rate',
126
+ discountType: ((_a = order.appliedDiscount) === null || _a === void 0 ? void 0 : _a.title) || 'Custom discount',
127
+ shippingRate: ((_c = (_b = order.fulfillmentLocations) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.shippingRateName) || 'Custom shipping rate',
118
128
  unfulfilledItems: [],
119
129
  fulfilledItems: [],
120
130
  refundedItems: [],
@@ -124,6 +134,7 @@ class OrderHelpers {
124
134
  if (!preparedOrder.activeLineItems.length) {
125
135
  return preparedOrder;
126
136
  }
137
+ // Get unfulfilledQuantity and fulfilledQuantity for each lineItem
127
138
  if (orderFulfillment.length) {
128
139
  preparedOrder.activeLineItems = preparedOrder.activeLineItems.map((item) => {
129
140
  const currentItem = item.productId;
@@ -144,14 +155,32 @@ class OrderHelpers {
144
155
  if (!orderRefunds.length) {
145
156
  return preparedOrder;
146
157
  }
158
+ // Subtract existed refunds prices from total prices
147
159
  preparedOrder.totalDiscount.amount =
148
160
  Number(order.totalDiscount.amount) - this.getAmountSumByField(orderRefunds, 'refundDiscount');
161
+ preparedOrder.totalTax.amount =
162
+ Number(((_d = order.totalTax) === null || _d === void 0 ? void 0 : _d.amount) || 0) - this.getAmountSumByField(orderRefunds, 'totalTaxPresentment');
149
163
  preparedOrder.subtotalPrice.amount =
150
164
  Number(order.subtotalPrice.amount) - this.getAmountSumByField(orderRefunds, 'subtotalRefunded');
151
165
  preparedOrder.totalShippingPrice.amount =
152
- Number(((_a = order.totalShippingPrice) === null || _a === void 0 ? void 0 : _a.amount) || 0) - this.getAmountSumByField(orderRefunds, 'refundShipping');
166
+ Number(((_e = order.totalShippingPrice) === null || _e === void 0 ? void 0 : _e.amount) || 0) - this.getAmountSumByField(orderRefunds, 'refundShipping');
153
167
  preparedOrder.totalPrice.amount =
154
168
  Number(order.totalPrice.amount) - this.getAmountSumByField(orderRefunds, 'totalRefunded');
169
+ // Subtract existed refunds prices from total presentment prices
170
+ preparedOrder.totalDiscountPresentment.amount =
171
+ Number(((_f = order.totalDiscountPresentment) === null || _f === void 0 ? void 0 : _f.amount) || 0) -
172
+ this.getAmountSumByField(orderRefunds, 'refundDiscountPresentment');
173
+ preparedOrder.totalTaxPresentment.amount =
174
+ Number(((_g = order.totalTaxPresentment) === null || _g === void 0 ? void 0 : _g.amount) || 0) - this.getAmountSumByField(orderRefunds, 'totalTaxPresentment');
175
+ preparedOrder.subtotalPricePresentment.amount =
176
+ Number(((_h = order.subtotalPricePresentment) === null || _h === void 0 ? void 0 : _h.amount) || 0) -
177
+ this.getAmountSumByField(orderRefunds, 'subtotalRefundedPresentment');
178
+ preparedOrder.totalShippingPricePresentment.amount =
179
+ Number(((_j = order.totalShippingPricePresentment) === null || _j === void 0 ? void 0 : _j.amount) || 0) -
180
+ this.getAmountSumByField(orderRefunds, 'refundShippingPresentment');
181
+ preparedOrder.totalPricePresentment.amount =
182
+ Number(((_k = order.totalPricePresentment) === null || _k === void 0 ? void 0 : _k.amount) || 0) -
183
+ this.getAmountSumByField(orderRefunds, 'totalRefundedPresentment');
155
184
  const lineItemsWithQuantityInfo = preparedOrder.activeLineItems.map((item) => {
156
185
  const currentItem = item.productId;
157
186
  const refundedAndCanceledQuantity = orderRefunds.reduce((result, { refundLineItems }) => {
@@ -168,6 +197,7 @@ class OrderHelpers {
168
197
  // 4. quantity = unfulfilled quantity (prev quantity - sum of order.fulfillment) - cancelled
169
198
  return Object.assign(Object.assign(Object.assign({}, item), { fulfilledQuantity: Number(item.fulfilledQuantity) - refundedAndCanceledQuantity.refundedQuantity, unfulfilledQuantity: Number(item.unfulfilledQuantity) - refundedAndCanceledQuantity.canceledQuantity }), refundedAndCanceledQuantity);
170
199
  });
200
+ // Active lineItems - not refunded and not removed(not canceled) positions
171
201
  preparedOrder.activeLineItems = lineItemsWithQuantityInfo.filter(({ unfulfilledQuantity, fulfilledQuantity }) => unfulfilledQuantity + fulfilledQuantity > 0);
172
202
  preparedOrder.unfulfilledItems = lineItemsWithQuantityInfo.filter(({ unfulfilledQuantity }) => unfulfilledQuantity > 0);
173
203
  preparedOrder.fulfilledItems = lineItemsWithQuantityInfo.filter(({ fulfilledQuantity }) => fulfilledQuantity > 0);
@@ -240,5 +270,15 @@ class OrderHelpers {
240
270
  const discount = Number(Number(creditDiscount + saleDiscount).toFixed(2));
241
271
  return discount;
242
272
  }
273
+ // Calculate amount of discount that each lineItem unit gets (used for Refund)
274
+ getLineItemUnitOrderDiscount({ originalUnitPrice, totalDiscount, subtotalPrice, }) {
275
+ const td = Number((totalDiscount === null || totalDiscount === void 0 ? void 0 : totalDiscount.amount) || 0);
276
+ const subT = Number((subtotalPrice === null || subtotalPrice === void 0 ? void 0 : subtotalPrice.amount) || 0);
277
+ const unitPrice = Number(originalUnitPrice.amount || 0);
278
+ if (subT === 0) {
279
+ return td;
280
+ }
281
+ return (td / subT) * unitPrice;
282
+ }
243
283
  }
244
284
  exports.default = OrderHelpers;
@@ -11,6 +11,8 @@ export default class CustomerNotification extends Base {
11
11
  message: string;
12
12
  imgUrl?: string;
13
13
  recipientMap: RecipientMap[];
14
+ isRead?: boolean;
15
+ skipSend?: boolean;
14
16
  constructor(props?: any);
15
17
  toObj(): CustomerNotificationAttributes;
16
18
  }
@@ -15,6 +15,8 @@ class CustomerNotification extends Base_1.default {
15
15
  this.message = (props === null || props === void 0 ? void 0 : props.message) || '';
16
16
  this.imgUrl = (props === null || props === void 0 ? void 0 : props.imgUrl) || '';
17
17
  this.recipientMap = (props === null || props === void 0 ? void 0 : props.recipientMap) || [];
18
+ this.isRead = (props === null || props === void 0 ? void 0 : props.isRead) || false;
19
+ this.skipSend = (props === null || props === void 0 ? void 0 : props.skipSend) || false;
18
20
  }
19
21
  toObj() {
20
22
  return {
@@ -29,6 +31,8 @@ class CustomerNotification extends Base_1.default {
29
31
  message: this.message,
30
32
  imgUrl: this.imgUrl,
31
33
  recipientMap: this.recipientMap,
34
+ isRead: this.isRead,
35
+ skipSend: this.skipSend,
32
36
  };
33
37
  }
34
38
  }
@@ -99,6 +99,7 @@ export default class Order extends Base {
99
99
  additionalDiscountPresentment: Money;
100
100
  createdAtTimestamp: number;
101
101
  updatedAtTimestamp: number;
102
+ adjustments?: OrderAdjustment[];
102
103
  constructor(props?: any);
103
104
  toObj(): ReRobeOrderObj;
104
105
  generateSchemaForTypesense(name?: string): {
@@ -126,6 +126,7 @@ class Order extends Base_1.default {
126
126
  };
127
127
  this.createdAtTimestamp = (props === null || props === void 0 ? void 0 : props.createdAtTimestamp) || 0;
128
128
  this.updatedAtTimestamp = (props === null || props === void 0 ? void 0 : props.updatedAtTimestamp) || 0;
129
+ this.adjustments = (props === null || props === void 0 ? void 0 : props.adjustments) || [];
129
130
  }
130
131
  toObj() {
131
132
  const orderObj = {
@@ -185,6 +186,7 @@ class Order extends Base_1.default {
185
186
  creditDiscountPresentment: this.creditDiscountPresentment,
186
187
  saleDiscountPresentment: this.saleDiscountPresentment,
187
188
  additionalDiscountPresentment: this.additionalDiscountPresentment,
189
+ adjustments: this.adjustments,
188
190
  createdAtTimestamp: this.createdAtTimestamp,
189
191
  updatedAtTimestamp: this.updatedAtTimestamp,
190
192
  };
@@ -8,6 +8,15 @@ export default class Refund extends Base {
8
8
  totalTax?: Money;
9
9
  refundLineItems: RefundLineItem[];
10
10
  note?: string;
11
+ totalRefundedPresentment: Money;
12
+ subtotalRefundedPresentment?: Money;
13
+ refundDiscountPresentment?: Money;
14
+ refundShippingPresentment?: Money;
15
+ totalTaxPresentment?: Money;
16
+ createdAtTimestamp?: number;
17
+ updatedAtTimestamp?: number;
18
+ createdAt?: string;
19
+ updatedAt?: string;
11
20
  constructor(props?: any);
12
21
  toObj(): OrderRefund;
13
22
  }
@@ -4,24 +4,23 @@ const Base_1 = require("../Base");
4
4
  class Refund extends Base_1.default {
5
5
  constructor(props) {
6
6
  super();
7
- this.documentId = (props === null || props === void 0 ? void 0 : props.documentId) || '';
8
- this.totalRefunded = (props === null || props === void 0 ? void 0 : props.totalRefunded) || {
9
- amount: 0,
10
- };
11
- this.subtotalRefunded = (props === null || props === void 0 ? void 0 : props.subtotalRefunded) || {
12
- amount: 0,
13
- };
14
- this.refundDiscount = (props === null || props === void 0 ? void 0 : props.refundDiscount) || {
15
- amount: 0,
16
- };
17
- this.refundShipping = (props === null || props === void 0 ? void 0 : props.refundShipping) || {
18
- amount: 0,
19
- };
20
- this.totalTax = (props === null || props === void 0 ? void 0 : props.totalTax) || {
21
- amount: 0,
22
- };
7
+ this.documentId = (props === null || props === void 0 ? void 0 : props.documentId) || this.utilities.makeRandId(28);
8
+ this.totalRefunded = (props === null || props === void 0 ? void 0 : props.totalRefunded) || { amount: 0 };
9
+ this.subtotalRefunded = (props === null || props === void 0 ? void 0 : props.subtotalRefunded) || { amount: 0 };
10
+ this.refundDiscount = (props === null || props === void 0 ? void 0 : props.refundDiscount) || { amount: 0 };
11
+ this.refundShipping = (props === null || props === void 0 ? void 0 : props.refundShipping) || { amount: 0 };
12
+ this.totalTax = (props === null || props === void 0 ? void 0 : props.totalTax) || { amount: 0 };
23
13
  this.refundLineItems = (props === null || props === void 0 ? void 0 : props.refundLineItems) || [];
24
14
  this.note = (props === null || props === void 0 ? void 0 : props.note) || '';
15
+ this.totalRefundedPresentment = (props === null || props === void 0 ? void 0 : props.totalRefundedPresentment) || { amount: 0 };
16
+ this.subtotalRefundedPresentment = (props === null || props === void 0 ? void 0 : props.subtotalRefundedPresentment) || { amount: 0 };
17
+ this.refundDiscountPresentment = (props === null || props === void 0 ? void 0 : props.refundDiscountPresentment) || { amount: 0 };
18
+ this.refundShippingPresentment = (props === null || props === void 0 ? void 0 : props.refundShippingPresentment) || { amount: 0 };
19
+ this.totalTaxPresentment = (props === null || props === void 0 ? void 0 : props.totalTaxPresentment) || { amount: 0 };
20
+ this.createdAtTimestamp = (props === null || props === void 0 ? void 0 : props.createdAtTimestamp) || 0;
21
+ this.updatedAtTimestamp = (props === null || props === void 0 ? void 0 : props.updatedAtTimestamp) || 0;
22
+ this.createdAt = (props === null || props === void 0 ? void 0 : props.createdAt) || '';
23
+ this.updatedAt = (props === null || props === void 0 ? void 0 : props.updatedAt) || '';
25
24
  }
26
25
  toObj() {
27
26
  const refundObj = {
@@ -33,7 +32,23 @@ class Refund extends Base_1.default {
33
32
  totalTax: this.totalTax,
34
33
  refundLineItems: this.refundLineItems,
35
34
  note: this.note,
35
+ totalRefundedPresentment: this.totalRefundedPresentment,
36
+ subtotalRefundedPresentment: this.subtotalRefundedPresentment,
37
+ refundDiscountPresentment: this.refundDiscountPresentment,
38
+ refundShippingPresentment: this.refundShippingPresentment,
39
+ totalTaxPresentment: this.totalTaxPresentment,
40
+ createdAtTimestamp: this.createdAtTimestamp,
41
+ createdAt: this.createdAt,
36
42
  };
43
+ if (this.createdAt) {
44
+ refundObj.createdAt = this.createdAt;
45
+ }
46
+ if (this.updatedAtTimestamp) {
47
+ refundObj.updatedAtTimestamp = this.updatedAtTimestamp;
48
+ }
49
+ if (this.updatedAt) {
50
+ refundObj.updatedAt = this.updatedAt;
51
+ }
37
52
  return refundObj;
38
53
  }
39
54
  }
@@ -14,6 +14,8 @@ declare type CustomerNotificationAttributes = {
14
14
  message: string;
15
15
  imgUrl?: string;
16
16
  recipientMap: RecipientMap[];
17
+ isRead?: boolean;
18
+ skipSend?: boolean;
17
19
  };
18
20
  declare type ORDER_CONFIRMATION_TYPE = 'ORDER_CONFIRMATION';
19
21
  declare type ORDER_CANCELLATION_TYPE = 'ORDER_CANCELLATION';
@@ -77,6 +77,7 @@ declare type ReRobeOrderObj = {
77
77
  creditDiscountPresentment?: Money;
78
78
  saleDiscountPresentment?: Money;
79
79
  additionalDiscountPresentment?: Money;
80
+ adjustments?: OrderAdjustment[];
80
81
  createdAtTimestamp: number;
81
82
  updatedAtTimestamp: number;
82
83
  };
@@ -99,6 +100,12 @@ declare type OrderWithRefundsAndFulfillmentsInfoObj = {
99
100
  fulfilledItems: ReRobeOrderLineItem[];
100
101
  refundedItems: ReRobeOrderLineItem[];
101
102
  canceledItems: ReRobeOrderLineItem[];
103
+ subtotalPricePresentment: Money;
104
+ totalDiscountPresentment: Money;
105
+ totalPricePresentment: Money;
106
+ totalRefundedPresentment: Money;
107
+ totalShippingPricePresentment: Money;
108
+ totalTaxPresentment: Money;
102
109
  };
103
110
  declare type OrderInput = {
104
111
  id?: string | null;
@@ -330,6 +337,13 @@ declare type OrderRefund = {
330
337
  totalTax?: Money;
331
338
  refundLineItems: RefundLineItem[];
332
339
  note?: string;
340
+ totalRefundedPresentment: Money;
341
+ subtotalRefundedPresentment?: Money;
342
+ refundDiscountPresentment?: Money;
343
+ refundShippingPresentment?: Money;
344
+ totalTaxPresentment?: Money;
345
+ createdAtTimestamp?: number;
346
+ updatedAtTimestamp?: number;
333
347
  };
334
348
  declare type RefundLineItem = {
335
349
  lineItem: ReRobeOrderLineItem;
@@ -338,6 +352,9 @@ declare type RefundLineItem = {
338
352
  discountRefund?: Money;
339
353
  subtotalRefund?: Money;
340
354
  subtotalCanceled?: Money;
355
+ discountRefundPresentment?: Money;
356
+ subtotalRefundPresentment?: Money;
357
+ subtotalCanceledPresentment?: Money;
341
358
  };
342
359
  declare type TaxLine = {
343
360
  title: string;
@@ -397,6 +414,8 @@ declare type FulfillmentAdmin = {
397
414
  totalQuantity?: number;
398
415
  trackingInfo?: FulfillmentTrackingInfo;
399
416
  updatedAt?: string;
417
+ createdAtTimestamp?: number;
418
+ updatedAtTimestamp?: number;
400
419
  };
401
420
  declare type FulfillmentService = {
402
421
  id: string | number;
@@ -741,6 +760,11 @@ declare type OrderRefundFormFields = {
741
760
  totalTax: TextInputFormField<string | number>;
742
761
  refundLineItems: MultiSelectFormField<RefundLineItem>;
743
762
  note: TextInputFormField<string>;
763
+ totalRefundedPresentment: TextInputFormField<string | number>;
764
+ subtotalRefundedPresentment: TextInputFormField<string | number>;
765
+ refundDiscountPresentment: TextInputFormField<string | number>;
766
+ refundShippingPresentment: TextInputFormField<string | number>;
767
+ totalTaxPresentment: TextInputFormField<string | number>;
744
768
  };
745
769
  declare type TypesenseOrderObj = {
746
770
  id: string;
@@ -769,3 +793,10 @@ declare type TypesenseOrderObj = {
769
793
  createdAtHour: string;
770
794
  createdAtYear: string;
771
795
  };
796
+ declare type OrderAdjustment = {
797
+ createdAtTimestamp?: number;
798
+ updatedAtTimestamp?: number;
799
+ totalAdjustmentPrice: Money;
800
+ totalAdjustmentPricePresentment: Money;
801
+ note?: string;
802
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "3.0.43",
3
+ "version": "3.1.1",
4
4
  "description": "ReRobe's Javascript ORM Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",