@medusajs/medusa 1.3.9 → 1.4.0

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.
Files changed (173) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +12 -0
  3. package/dist/api/index.d.ts +1 -0
  4. package/dist/api/index.js +1 -0
  5. package/dist/api/index.js.map +1 -1
  6. package/dist/api/routes/admin/batch/create-batch-job.js +10 -7
  7. package/dist/api/routes/admin/batch/create-batch-job.js.map +1 -1
  8. package/dist/api/routes/admin/index.js +6 -4
  9. package/dist/api/routes/admin/index.js.map +1 -1
  10. package/dist/api/routes/admin/price-lists/create-price-list.d.ts +4 -0
  11. package/dist/api/routes/admin/price-lists/create-price-list.js +16 -1
  12. package/dist/api/routes/admin/price-lists/create-price-list.js.map +1 -1
  13. package/dist/api/routes/admin/price-lists/index.d.ts +2 -1
  14. package/dist/api/routes/admin/price-lists/index.js +8 -1
  15. package/dist/api/routes/admin/price-lists/index.js.map +1 -1
  16. package/dist/api/routes/admin/price-lists/update-price-list.d.ts +4 -0
  17. package/dist/api/routes/admin/price-lists/update-price-list.js +16 -1
  18. package/dist/api/routes/admin/price-lists/update-price-list.js.map +1 -1
  19. package/dist/api/routes/admin/products/update-variant.js +1 -0
  20. package/dist/api/routes/admin/products/update-variant.js.map +1 -1
  21. package/dist/api/routes/admin/regions/create-region.d.ts +4 -0
  22. package/dist/api/routes/admin/regions/create-region.js +16 -1
  23. package/dist/api/routes/admin/regions/create-region.js.map +1 -1
  24. package/dist/api/routes/admin/regions/index.d.ts +2 -1
  25. package/dist/api/routes/admin/regions/index.js +5 -1
  26. package/dist/api/routes/admin/regions/index.js.map +1 -1
  27. package/dist/api/routes/admin/regions/update-region.d.ts +4 -0
  28. package/dist/api/routes/admin/regions/update-region.js +16 -1
  29. package/dist/api/routes/admin/regions/update-region.js.map +1 -1
  30. package/dist/api/routes/admin/shipping-options/create-shipping-option.d.ts +4 -0
  31. package/dist/api/routes/admin/shipping-options/create-shipping-option.js +15 -0
  32. package/dist/api/routes/admin/shipping-options/create-shipping-option.js.map +1 -1
  33. package/dist/api/routes/admin/shipping-options/index.d.ts +2 -1
  34. package/dist/api/routes/admin/shipping-options/index.js +5 -1
  35. package/dist/api/routes/admin/shipping-options/index.js.map +1 -1
  36. package/dist/api/routes/admin/shipping-options/update-shipping-option.d.ts +4 -0
  37. package/dist/api/routes/admin/shipping-options/update-shipping-option.js +15 -0
  38. package/dist/api/routes/admin/shipping-options/update-shipping-option.js.map +1 -1
  39. package/dist/helpers/test-request.js +27 -4
  40. package/dist/helpers/test-request.js.map +1 -1
  41. package/dist/interfaces/price-selection-strategy.d.ts +4 -0
  42. package/dist/interfaces/price-selection-strategy.js.map +1 -1
  43. package/dist/loaders/feature-flags/index.d.ts +1 -0
  44. package/dist/loaders/feature-flags/index.js +18 -3
  45. package/dist/loaders/feature-flags/index.js.map +1 -1
  46. package/dist/loaders/feature-flags/order-editing.d.ts +3 -0
  47. package/dist/loaders/feature-flags/order-editing.js +10 -0
  48. package/dist/loaders/feature-flags/order-editing.js.map +1 -0
  49. package/dist/migrations/1663059812399-order_editing.d.ts +7 -0
  50. package/dist/migrations/1663059812399-order_editing.js +112 -0
  51. package/dist/migrations/1663059812399-order_editing.js.map +1 -0
  52. package/dist/models/currency.d.ts +4 -0
  53. package/dist/models/currency.js +12 -0
  54. package/dist/models/currency.js.map +1 -1
  55. package/dist/models/custom-shipping-option.d.ts +3 -0
  56. package/dist/models/custom-shipping-option.js +3 -0
  57. package/dist/models/custom-shipping-option.js.map +1 -1
  58. package/dist/models/line-item.d.ts +4 -0
  59. package/dist/models/line-item.js +12 -0
  60. package/dist/models/line-item.js.map +1 -1
  61. package/dist/models/money-amount.d.ts +2 -2
  62. package/dist/models/money-amount.js.map +1 -1
  63. package/dist/models/order-edit.d.ts +111 -0
  64. package/dist/models/order-edit.js +204 -0
  65. package/dist/models/order-edit.js.map +1 -0
  66. package/dist/models/order-item-change.d.ts +60 -0
  67. package/dist/models/order-item-change.js +143 -0
  68. package/dist/models/order-item-change.js.map +1 -0
  69. package/dist/models/order.d.ts +7 -0
  70. package/dist/models/order.js +16 -0
  71. package/dist/models/order.js.map +1 -1
  72. package/dist/models/price-list.d.ts +5 -0
  73. package/dist/models/price-list.js +13 -0
  74. package/dist/models/price-list.js.map +1 -1
  75. package/dist/models/region.d.ts +4 -0
  76. package/dist/models/region.js +12 -0
  77. package/dist/models/region.js.map +1 -1
  78. package/dist/models/shipping-method.d.ts +4 -0
  79. package/dist/models/shipping-method.js +12 -0
  80. package/dist/models/shipping-method.js.map +1 -1
  81. package/dist/models/shipping-option.d.ts +4 -0
  82. package/dist/models/shipping-option.js +12 -0
  83. package/dist/models/shipping-option.js.map +1 -1
  84. package/dist/repositories/currency.d.ts +1 -1
  85. package/dist/repositories/currency.js +2 -2
  86. package/dist/repositories/currency.js.map +1 -1
  87. package/dist/repositories/money-amount.d.ts +1 -1
  88. package/dist/repositories/money-amount.js +8 -4
  89. package/dist/repositories/money-amount.js.map +1 -1
  90. package/dist/repositories/order-edit.d.ts +4 -0
  91. package/dist/repositories/order-edit.js +38 -0
  92. package/dist/repositories/order-edit.js.map +1 -0
  93. package/dist/repositories/order-item-change.d.ts +4 -0
  94. package/dist/repositories/order-item-change.js +38 -0
  95. package/dist/repositories/order-item-change.js.map +1 -0
  96. package/dist/services/batch-job.js +4 -2
  97. package/dist/services/batch-job.js.map +1 -1
  98. package/dist/services/cart.js +83 -74
  99. package/dist/services/cart.js.map +1 -1
  100. package/dist/services/claim.js +45 -43
  101. package/dist/services/claim.js.map +1 -1
  102. package/dist/services/discount.d.ts +4 -1
  103. package/dist/services/discount.js +39 -20
  104. package/dist/services/discount.js.map +1 -1
  105. package/dist/services/index.d.ts +1 -1
  106. package/dist/services/index.js +3 -3
  107. package/dist/services/index.js.map +1 -1
  108. package/dist/services/line-item-adjustment.d.ts +1 -4
  109. package/dist/services/line-item-adjustment.js +2 -1
  110. package/dist/services/line-item-adjustment.js.map +1 -1
  111. package/dist/services/line-item.d.ts +12 -7
  112. package/dist/services/line-item.js +22 -10
  113. package/dist/services/line-item.js.map +1 -1
  114. package/dist/services/notification.js +3 -2
  115. package/dist/services/notification.js.map +1 -1
  116. package/dist/services/order.js +214 -115
  117. package/dist/services/order.js.map +1 -1
  118. package/dist/services/price-list.d.ts +7 -4
  119. package/dist/services/price-list.js +25 -11
  120. package/dist/services/price-list.js.map +1 -1
  121. package/dist/services/pricing.d.ts +7 -4
  122. package/dist/services/pricing.js +39 -12
  123. package/dist/services/pricing.js.map +1 -1
  124. package/dist/services/product-variant.js +1 -1
  125. package/dist/services/product-variant.js.map +1 -1
  126. package/dist/services/region.d.ts +10 -7
  127. package/dist/services/region.js +22 -6
  128. package/dist/services/region.js.map +1 -1
  129. package/dist/services/return.js +23 -19
  130. package/dist/services/return.js.map +1 -1
  131. package/dist/services/shipping-option.d.ts +12 -8
  132. package/dist/services/shipping-option.js +23 -2
  133. package/dist/services/shipping-option.js.map +1 -1
  134. package/dist/services/store.d.ts +1 -2
  135. package/dist/services/store.js.map +1 -1
  136. package/dist/services/totals.d.ts +18 -20
  137. package/dist/services/totals.js +614 -358
  138. package/dist/services/totals.js.map +1 -1
  139. package/dist/strategies/__fixtures__/product-export-data.js +6 -2
  140. package/dist/strategies/__fixtures__/product-export-data.js.map +1 -1
  141. package/dist/strategies/batch-jobs/product/export.d.ts +6 -0
  142. package/dist/strategies/batch-jobs/product/export.js +11 -2
  143. package/dist/strategies/batch-jobs/product/export.js.map +1 -1
  144. package/dist/strategies/batch-jobs/product/import.d.ts +6 -5
  145. package/dist/strategies/batch-jobs/product/import.js +293 -197
  146. package/dist/strategies/batch-jobs/product/import.js.map +1 -1
  147. package/dist/strategies/batch-jobs/product/index.js +2 -2
  148. package/dist/strategies/batch-jobs/product/types.d.ts +8 -0
  149. package/dist/strategies/batch-jobs/product/types.js.map +1 -1
  150. package/dist/strategies/batch-jobs/product/utils.d.ts +1 -1
  151. package/dist/strategies/batch-jobs/product/utils.js +10 -1
  152. package/dist/strategies/batch-jobs/product/utils.js.map +1 -1
  153. package/dist/strategies/price-selection.d.ts +6 -2
  154. package/dist/strategies/price-selection.js +124 -10
  155. package/dist/strategies/price-selection.js.map +1 -1
  156. package/dist/strategies/tax-calculation.d.ts +7 -5
  157. package/dist/strategies/tax-calculation.js +47 -17
  158. package/dist/strategies/tax-calculation.js.map +1 -1
  159. package/dist/subscribers/batch-job.d.ts +4 -1
  160. package/dist/subscribers/batch-job.js +71 -44
  161. package/dist/subscribers/batch-job.js.map +1 -1
  162. package/dist/types/price-list.d.ts +2 -1
  163. package/dist/types/pricing.d.ts +5 -2
  164. package/dist/types/region.d.ts +2 -0
  165. package/dist/types/shipping-options.d.ts +2 -0
  166. package/dist/utils/feature-flag-decorators.js +4 -16
  167. package/dist/utils/feature-flag-decorators.js.map +1 -1
  168. package/dist/utils/flag-router.d.ts +1 -1
  169. package/dist/utils/flag-router.js.map +1 -1
  170. package/dist/utils/index.d.ts +2 -0
  171. package/dist/utils/index.js +2 -0
  172. package/dist/utils/index.js.map +1 -1
  173. package/package.json +1 -3
@@ -86,13 +86,17 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
86
86
  }
87
87
  return to.concat(ar || Array.prototype.slice.call(from));
88
88
  };
89
+ var __importDefault = (this && this.__importDefault) || function (mod) {
90
+ return (mod && mod.__esModule) ? mod : { "default": mod };
91
+ };
89
92
  Object.defineProperty(exports, "__esModule", { value: true });
90
93
  var medusa_core_utils_1 = require("medusa-core-utils");
91
94
  var interfaces_1 = require("../interfaces");
92
- var discount_rule_1 = require("../models/discount-rule");
95
+ var models_1 = require("../models");
93
96
  var cart_1 = require("../types/cart");
94
97
  var orders_1 = require("../types/orders");
95
98
  var utils_1 = require("../utils");
99
+ var tax_inclusive_pricing_1 = __importDefault(require("../loaders/feature-flags/tax-inclusive-pricing"));
96
100
  /**
97
101
  * A service that calculates total and subtotals for orders, carts etc..
98
102
  * @implements {BaseService}
@@ -100,16 +104,18 @@ var utils_1 = require("../utils");
100
104
  var TotalsService = /** @class */ (function (_super) {
101
105
  __extends(TotalsService, _super);
102
106
  function TotalsService(_a) {
103
- var manager = _a.manager, taxProviderService = _a.taxProviderService, taxCalculationStrategy = _a.taxCalculationStrategy;
107
+ var manager = _a.manager, taxProviderService = _a.taxProviderService, taxCalculationStrategy = _a.taxCalculationStrategy, featureFlagRouter = _a.featureFlagRouter;
104
108
  var _this = _super.call(this, {
105
109
  taxProviderService: taxProviderService,
106
110
  taxCalculationStrategy: taxCalculationStrategy,
107
111
  manager: manager,
112
+ featureFlagRouter: featureFlagRouter,
108
113
  }) || this;
109
114
  _this.manager_ = manager;
110
115
  _this.taxProviderService_ = taxProviderService;
111
116
  _this.taxCalculationStrategy_ = taxCalculationStrategy;
112
117
  _this.manager_ = manager;
118
+ _this.featureFlagRouter_ = featureFlagRouter;
113
119
  return _this;
114
120
  }
115
121
  /**
@@ -121,19 +127,30 @@ var TotalsService = /** @class */ (function (_super) {
121
127
  TotalsService.prototype.getTotal = function (cartOrOrder, options) {
122
128
  if (options === void 0) { options = {}; }
123
129
  return __awaiter(this, void 0, void 0, function () {
124
- var subtotal, taxTotal, discountTotal, giftCardTotal, shippingTotal;
125
- return __generator(this, function (_a) {
126
- switch (_a.label) {
127
- case 0:
128
- subtotal = this.getSubtotal(cartOrOrder);
129
- return [4 /*yield*/, this.getTaxTotal(cartOrOrder, options.force_taxes)];
130
+ var subtotal, taxTotal, discountTotal, giftCardTotal, _a, shippingTotal;
131
+ return __generator(this, function (_b) {
132
+ switch (_b.label) {
133
+ case 0: return [4 /*yield*/, this.getSubtotal(cartOrOrder)];
130
134
  case 1:
131
- taxTotal = (_a.sent()) || 0;
132
- discountTotal = this.getDiscountTotal(cartOrOrder);
133
- giftCardTotal = options.exclude_gift_cards
134
- ? { total: 0 }
135
- : this.getGiftCardTotal(cartOrOrder);
136
- shippingTotal = this.getShippingTotal(cartOrOrder);
135
+ subtotal = _b.sent();
136
+ return [4 /*yield*/, this.getTaxTotal(cartOrOrder, options.force_taxes)];
137
+ case 2:
138
+ taxTotal = (_b.sent()) || 0;
139
+ return [4 /*yield*/, this.getDiscountTotal(cartOrOrder)];
140
+ case 3:
141
+ discountTotal = _b.sent();
142
+ if (!options.exclude_gift_cards) return [3 /*break*/, 4];
143
+ _a = { total: 0 };
144
+ return [3 /*break*/, 6];
145
+ case 4: return [4 /*yield*/, this.getGiftCardTotal(cartOrOrder)];
146
+ case 5:
147
+ _a = _b.sent();
148
+ _b.label = 6;
149
+ case 6:
150
+ giftCardTotal = _a;
151
+ return [4 /*yield*/, this.getShippingTotal(cartOrOrder)];
152
+ case 7:
153
+ shippingTotal = _b.sent();
137
154
  return [2 /*return*/, (subtotal + taxTotal + shippingTotal - discountTotal - giftCardTotal.total)];
138
155
  }
139
156
  });
@@ -187,69 +204,71 @@ var TotalsService = /** @class */ (function (_super) {
187
204
  * @returns An object that breaks down the totals for the shipping method
188
205
  */
189
206
  TotalsService.prototype.getShippingMethodTotals = function (shippingMethod, cartOrOrder, opts) {
207
+ var _a;
190
208
  if (opts === void 0) { opts = {}; }
191
209
  return __awaiter(this, void 0, void 0, function () {
192
- var calculationContext, totals, taxLines, orderLines, _a;
193
- return __generator(this, function (_b) {
194
- switch (_b.label) {
195
- case 0:
196
- calculationContext = this.getCalculationContext(cartOrOrder, {
210
+ var calculationContext, totals, orderLines, includesTax, _b, hasFreeShipping;
211
+ return __generator(this, function (_c) {
212
+ switch (_c.label) {
213
+ case 0: return [4 /*yield*/, this.getCalculationContext(cartOrOrder, {
197
214
  exclude_shipping: true,
198
- });
215
+ })];
216
+ case 1:
217
+ calculationContext = _c.sent();
199
218
  calculationContext.shipping_methods = [shippingMethod];
200
219
  totals = {
201
220
  price: shippingMethod.price,
202
221
  original_total: shippingMethod.price,
203
222
  total: shippingMethod.price,
223
+ subtotal: shippingMethod.price,
204
224
  original_tax_total: 0,
205
225
  tax_total: 0,
206
226
  tax_lines: shippingMethod.tax_lines || [],
207
227
  };
208
- if (!opts.include_tax) return [3 /*break*/, 7];
209
- if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate !== null)) return [3 /*break*/, 1];
210
- totals.original_tax_total = Math.round(totals.original_tax_total * (cartOrOrder.tax_rate / 100));
211
- totals.tax_total = Math.round(totals.original_tax_total * (cartOrOrder.tax_rate / 100));
212
- return [3 /*break*/, 5];
213
- case 1:
214
- taxLines = void 0;
215
- if (!(opts.use_tax_lines || (0, orders_1.isOrder)(cartOrOrder))) return [3 /*break*/, 2];
216
- if (typeof shippingMethod.tax_lines === "undefined") {
217
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax Lines must be joined on shipping method to calculate taxes");
218
- }
219
- taxLines = shippingMethod.tax_lines;
228
+ if (!opts.include_tax) return [3 /*break*/, 6];
229
+ if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate != null)) return [3 /*break*/, 2];
230
+ totals.original_tax_total = Math.round(totals.price * (cartOrOrder.tax_rate / 100));
231
+ totals.tax_total = Math.round(totals.price * (cartOrOrder.tax_rate / 100));
220
232
  return [3 /*break*/, 4];
221
- case 2: return [4 /*yield*/, this.taxProviderService_
222
- .withTransaction(this.manager_)
223
- .getTaxLines(cartOrOrder.items, calculationContext)];
233
+ case 2:
234
+ if (!(totals.tax_lines.length === 0)) return [3 /*break*/, 4];
235
+ return [4 /*yield*/, this.taxProviderService_
236
+ .withTransaction(this.manager_)
237
+ .getTaxLines(cartOrOrder.items, calculationContext)];
224
238
  case 3:
225
- orderLines = _b.sent();
226
- taxLines = orderLines.filter(function (ol) {
239
+ orderLines = _c.sent();
240
+ totals.tax_lines = orderLines.filter(function (ol) {
227
241
  if ("shipping_method_id" in ol) {
228
242
  return ol.shipping_method_id === shippingMethod.id;
229
243
  }
230
244
  return false;
231
245
  });
232
- _b.label = 4;
246
+ if (totals.tax_lines.length === 0 && (0, orders_1.isOrder)(cartOrOrder)) {
247
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax Lines must be joined on shipping method to calculate taxes");
248
+ }
249
+ _c.label = 4;
233
250
  case 4:
234
- totals.tax_lines = taxLines;
235
- _b.label = 5;
236
- case 5:
237
- if (!(totals.tax_lines.length > 0)) return [3 /*break*/, 7];
238
- _a = totals;
251
+ if (!(totals.tax_lines.length > 0)) return [3 /*break*/, 6];
252
+ includesTax = this.featureFlagRouter_.isFeatureEnabled(tax_inclusive_pricing_1.default.key) && shippingMethod.includes_tax;
253
+ _b = totals;
239
254
  return [4 /*yield*/, this.taxCalculationStrategy_.calculate([], totals.tax_lines, calculationContext)];
240
- case 6:
241
- _a.original_tax_total =
242
- _b.sent();
255
+ case 5:
256
+ _b.original_tax_total = _c.sent();
243
257
  totals.tax_total = totals.original_tax_total;
244
- totals.original_total += totals.original_tax_total;
245
- totals.total += totals.tax_total;
246
- _b.label = 7;
247
- case 7:
248
- if (cartOrOrder.discounts) {
249
- if (cartOrOrder.discounts.some(function (d) { return d.rule.type === "free_shipping"; })) {
250
- totals.total = 0;
251
- totals.tax_total = 0;
252
- }
258
+ if (includesTax) {
259
+ totals.subtotal -= totals.tax_total;
260
+ }
261
+ else {
262
+ totals.original_total += totals.original_tax_total;
263
+ totals.total += totals.tax_total;
264
+ }
265
+ _c.label = 6;
266
+ case 6:
267
+ hasFreeShipping = (_a = cartOrOrder.discounts) === null || _a === void 0 ? void 0 : _a.some(function (d) { return d.rule.type === models_1.DiscountRuleType.FREE_SHIPPING; });
268
+ if (hasFreeShipping) {
269
+ totals.total = 0;
270
+ totals.subtotal = 0;
271
+ totals.tax_total = 0;
253
272
  }
254
273
  return [2 /*return*/, totals];
255
274
  }
@@ -264,21 +283,71 @@ var TotalsService = /** @class */ (function (_super) {
264
283
  */
265
284
  TotalsService.prototype.getSubtotal = function (cartOrOrder, opts) {
266
285
  if (opts === void 0) { opts = {}; }
267
- var subtotal = 0;
268
- if (!cartOrOrder.items) {
269
- return subtotal;
270
- }
271
- cartOrOrder.items.map(function (item) {
272
- if (opts.excludeNonDiscounts) {
273
- if (item.allow_discounts) {
274
- subtotal += item.unit_price * item.quantity;
286
+ return __awaiter(this, void 0, void 0, function () {
287
+ var subtotal, getLineItemSubtotal, _a, _b, item, _c, _d, e_2_1;
288
+ var e_2, _e;
289
+ var _this = this;
290
+ return __generator(this, function (_f) {
291
+ switch (_f.label) {
292
+ case 0:
293
+ subtotal = 0;
294
+ if (!cartOrOrder.items) {
295
+ return [2 /*return*/, subtotal];
296
+ }
297
+ getLineItemSubtotal = function (item) { return __awaiter(_this, void 0, void 0, function () {
298
+ var totals;
299
+ return __generator(this, function (_a) {
300
+ switch (_a.label) {
301
+ case 0: return [4 /*yield*/, this.getLineItemTotals(item, cartOrOrder, {
302
+ include_tax: true,
303
+ exclude_gift_cards: true,
304
+ })];
305
+ case 1:
306
+ totals = _a.sent();
307
+ return [2 /*return*/, totals.subtotal];
308
+ }
309
+ });
310
+ }); };
311
+ _f.label = 1;
312
+ case 1:
313
+ _f.trys.push([1, 9, 10, 11]);
314
+ _a = __values(cartOrOrder.items), _b = _a.next();
315
+ _f.label = 2;
316
+ case 2:
317
+ if (!!_b.done) return [3 /*break*/, 8];
318
+ item = _b.value;
319
+ if (!opts.excludeNonDiscounts) return [3 /*break*/, 5];
320
+ if (!item.allow_discounts) return [3 /*break*/, 4];
321
+ _c = subtotal;
322
+ return [4 /*yield*/, getLineItemSubtotal(item)];
323
+ case 3:
324
+ subtotal = _c + _f.sent();
325
+ _f.label = 4;
326
+ case 4: return [3 /*break*/, 7];
327
+ case 5:
328
+ _d = subtotal;
329
+ return [4 /*yield*/, getLineItemSubtotal(item)];
330
+ case 6:
331
+ subtotal = _d + _f.sent();
332
+ _f.label = 7;
333
+ case 7:
334
+ _b = _a.next();
335
+ return [3 /*break*/, 2];
336
+ case 8: return [3 /*break*/, 11];
337
+ case 9:
338
+ e_2_1 = _f.sent();
339
+ e_2 = { error: e_2_1 };
340
+ return [3 /*break*/, 11];
341
+ case 10:
342
+ try {
343
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
344
+ }
345
+ finally { if (e_2) throw e_2.error; }
346
+ return [7 /*endfinally*/];
347
+ case 11: return [2 /*return*/, this.rounded(subtotal)];
275
348
  }
276
- }
277
- else {
278
- subtotal += item.unit_price * item.quantity;
279
- }
349
+ });
280
350
  });
281
- return this.rounded(subtotal);
282
351
  };
283
352
  /**
284
353
  * Calculates shipping total
@@ -286,10 +355,47 @@ var TotalsService = /** @class */ (function (_super) {
286
355
  * @return shipping total
287
356
  */
288
357
  TotalsService.prototype.getShippingTotal = function (cartOrOrder) {
289
- var shipping_methods = cartOrOrder.shipping_methods;
290
- return shipping_methods.reduce(function (acc, next) {
291
- return acc + next.price;
292
- }, 0);
358
+ return __awaiter(this, void 0, void 0, function () {
359
+ var shipping_methods, total, shipping_methods_1, shipping_methods_1_1, shippingMethod, totals, e_3_1;
360
+ var e_3, _a;
361
+ return __generator(this, function (_b) {
362
+ switch (_b.label) {
363
+ case 0:
364
+ shipping_methods = cartOrOrder.shipping_methods;
365
+ total = 0;
366
+ _b.label = 1;
367
+ case 1:
368
+ _b.trys.push([1, 6, 7, 8]);
369
+ shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
370
+ _b.label = 2;
371
+ case 2:
372
+ if (!!shipping_methods_1_1.done) return [3 /*break*/, 5];
373
+ shippingMethod = shipping_methods_1_1.value;
374
+ return [4 /*yield*/, this.getShippingMethodTotals(shippingMethod, cartOrOrder, {
375
+ include_tax: true,
376
+ })];
377
+ case 3:
378
+ totals = _b.sent();
379
+ total += totals.subtotal;
380
+ _b.label = 4;
381
+ case 4:
382
+ shipping_methods_1_1 = shipping_methods_1.next();
383
+ return [3 /*break*/, 2];
384
+ case 5: return [3 /*break*/, 8];
385
+ case 6:
386
+ e_3_1 = _b.sent();
387
+ e_3 = { error: e_3_1 };
388
+ return [3 /*break*/, 8];
389
+ case 7:
390
+ try {
391
+ if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
392
+ }
393
+ finally { if (e_3) throw e_3.error; }
394
+ return [7 /*endfinally*/];
395
+ case 8: return [2 /*return*/, total];
396
+ }
397
+ });
398
+ });
293
399
  };
294
400
  /**
295
401
  * Calculates tax total
@@ -311,32 +417,40 @@ var TotalsService = /** @class */ (function (_super) {
311
417
  !cartOrOrder.region.automatic_taxes) {
312
418
  return [2 /*return*/, null];
313
419
  }
314
- calculationContext = this.getCalculationContext(cartOrOrder);
315
- giftCardTotal = this.getGiftCardTotal(cartOrOrder);
316
- if (!(0, orders_1.isOrder)(cartOrOrder)) return [3 /*break*/, 1];
420
+ return [4 /*yield*/, this.getCalculationContext(cartOrOrder)];
421
+ case 1:
422
+ calculationContext = _a.sent();
423
+ return [4 /*yield*/, this.getGiftCardTotal(cartOrOrder)];
424
+ case 2:
425
+ giftCardTotal = _a.sent();
426
+ if (!(0, orders_1.isOrder)(cartOrOrder)) return [3 /*break*/, 8];
317
427
  taxLinesJoined = cartOrOrder.items.every(function (i) {
318
428
  return (0, utils_1.isDefined)(i.tax_lines);
319
429
  });
320
430
  if (!taxLinesJoined) {
321
431
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Order tax calculations must have tax lines joined on line items");
322
432
  }
323
- if (cartOrOrder.tax_rate === null) {
324
- taxLines = cartOrOrder.items.flatMap(function (li) { return li.tax_lines; });
325
- shippingTaxLines = cartOrOrder.shipping_methods.flatMap(function (sm) { return sm.tax_lines; });
326
- taxLines = taxLines.concat(shippingTaxLines);
327
- }
328
- else {
329
- subtotal = this.getSubtotal(cartOrOrder);
330
- shippingTotal = this.getShippingTotal(cartOrOrder);
331
- discountTotal = this.getDiscountTotal(cartOrOrder);
332
- return [2 /*return*/, this.rounded((subtotal - discountTotal - giftCardTotal.total + shippingTotal) *
333
- (cartOrOrder.tax_rate / 100))];
334
- }
335
- return [3 /*break*/, 3];
336
- case 1: return [4 /*yield*/, this.taxProviderService_
433
+ if (!(cartOrOrder.tax_rate === null)) return [3 /*break*/, 3];
434
+ taxLines = cartOrOrder.items.flatMap(function (li) { return li.tax_lines; });
435
+ shippingTaxLines = cartOrOrder.shipping_methods.flatMap(function (sm) { return sm.tax_lines; });
436
+ taxLines = taxLines.concat(shippingTaxLines);
437
+ return [3 /*break*/, 7];
438
+ case 3: return [4 /*yield*/, this.getSubtotal(cartOrOrder)];
439
+ case 4:
440
+ subtotal = _a.sent();
441
+ return [4 /*yield*/, this.getShippingTotal(cartOrOrder)];
442
+ case 5:
443
+ shippingTotal = _a.sent();
444
+ return [4 /*yield*/, this.getDiscountTotal(cartOrOrder)];
445
+ case 6:
446
+ discountTotal = _a.sent();
447
+ return [2 /*return*/, this.rounded((subtotal - discountTotal - giftCardTotal.total + shippingTotal) *
448
+ (cartOrOrder.tax_rate / 100))];
449
+ case 7: return [3 /*break*/, 10];
450
+ case 8: return [4 /*yield*/, this.taxProviderService_
337
451
  .withTransaction(this.manager_)
338
452
  .getTaxLines(cartOrOrder.items, calculationContext)];
339
- case 2:
453
+ case 9:
340
454
  taxLines = _a.sent();
341
455
  if (cartOrOrder.type === "swap") {
342
456
  returnTaxLines = cartOrOrder.items.flatMap(function (i) {
@@ -350,9 +464,9 @@ var TotalsService = /** @class */ (function (_super) {
350
464
  });
351
465
  taxLines = taxLines.concat(returnTaxLines);
352
466
  }
353
- _a.label = 3;
354
- case 3: return [4 /*yield*/, this.taxCalculationStrategy_.calculate(cartOrOrder.items, taxLines, calculationContext)];
355
- case 4:
467
+ _a.label = 10;
468
+ case 10: return [4 /*yield*/, this.taxCalculationStrategy_.calculate(cartOrOrder.items, taxLines, calculationContext)];
469
+ case 11:
356
470
  toReturn = _a.sent();
357
471
  if (cartOrOrder.region.gift_cards_taxable) {
358
472
  return [2 /*return*/, this.rounded(toReturn - giftCardTotal.tax_total)];
@@ -371,89 +485,104 @@ var TotalsService = /** @class */ (function (_super) {
371
485
  * @return the allocation map for the line items in the cart or order.
372
486
  */
373
487
  TotalsService.prototype.getAllocationMap = function (orderOrCart, options) {
374
- var e_2, _a, e_3, _b;
488
+ var _a;
375
489
  if (options === void 0) { options = {}; }
376
- var allocationMap = {};
377
- if (!options.exclude_discounts) {
378
- var lineDiscounts = [];
379
- var discount = orderOrCart.discounts.find(function (_a) {
380
- var rule = _a.rule;
381
- return rule.type !== "free_shipping";
382
- });
383
- if (discount) {
384
- lineDiscounts = this.getLineDiscounts(orderOrCart, discount);
385
- }
386
- try {
387
- for (var lineDiscounts_1 = __values(lineDiscounts), lineDiscounts_1_1 = lineDiscounts_1.next(); !lineDiscounts_1_1.done; lineDiscounts_1_1 = lineDiscounts_1.next()) {
388
- var ld = lineDiscounts_1_1.value;
389
- if (allocationMap[ld.item.id]) {
390
- allocationMap[ld.item.id].discount = {
391
- amount: ld.amount,
392
- unit_amount: Math.round(ld.amount / ld.item.quantity),
393
- };
394
- }
395
- else {
396
- allocationMap[ld.item.id] = {
397
- discount: {
398
- amount: ld.amount,
399
- unit_amount: Math.round(ld.amount / ld.item.quantity),
400
- },
401
- };
402
- }
403
- }
404
- }
405
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
406
- finally {
407
- try {
408
- if (lineDiscounts_1_1 && !lineDiscounts_1_1.done && (_a = lineDiscounts_1.return)) _a.call(lineDiscounts_1);
409
- }
410
- finally { if (e_2) throw e_2.error; }
411
- }
412
- }
413
- if (!options.exclude_gift_cards) {
414
- var lineGiftCards = [];
415
- if (orderOrCart.gift_cards && orderOrCart.gift_cards.length) {
416
- var subtotal = this.getSubtotal(orderOrCart);
417
- var giftCardTotal = this.getGiftCardTotal(orderOrCart);
418
- // If the fixed discount exceeds the subtotal we should
419
- // calculate a 100% discount
420
- var nominator = Math.min(giftCardTotal.total, subtotal);
421
- var percentage_1 = nominator / subtotal;
422
- lineGiftCards = orderOrCart.items.map(function (l) {
423
- return {
424
- item: l,
425
- amount: Math.round(l.unit_price * l.quantity * percentage_1),
426
- };
427
- });
428
- }
429
- try {
430
- for (var lineGiftCards_1 = __values(lineGiftCards), lineGiftCards_1_1 = lineGiftCards_1.next(); !lineGiftCards_1_1.done; lineGiftCards_1_1 = lineGiftCards_1.next()) {
431
- var lgc = lineGiftCards_1_1.value;
432
- if (allocationMap[lgc.item.id]) {
433
- allocationMap[lgc.item.id].gift_card = {
434
- amount: lgc.amount,
435
- unit_amount: Math.round(lgc.amount / lgc.item.quantity),
436
- };
437
- }
438
- else {
439
- allocationMap[lgc.item.id] = {
440
- gift_card: {
441
- amount: lgc.amount,
442
- unit_amount: Math.round(lgc.amount / lgc.item.quantity),
443
- },
444
- };
445
- }
446
- }
447
- }
448
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
449
- finally {
450
- try {
451
- if (lineGiftCards_1_1 && !lineGiftCards_1_1.done && (_b = lineGiftCards_1.return)) _b.call(lineGiftCards_1);
490
+ return __awaiter(this, void 0, void 0, function () {
491
+ var allocationMap, lineDiscounts, discount, lineDiscounts_1, lineDiscounts_1_1, ld, lineGiftCards, subtotal, giftCardTotal, nominator, percentage_1, lineGiftCards_1, lineGiftCards_1_1, lgc;
492
+ var e_4, _b, e_5, _c;
493
+ return __generator(this, function (_d) {
494
+ switch (_d.label) {
495
+ case 0:
496
+ allocationMap = {};
497
+ if (!options.exclude_discounts) {
498
+ lineDiscounts = [];
499
+ discount = (_a = orderOrCart.discounts) === null || _a === void 0 ? void 0 : _a.find(function (_a) {
500
+ var rule = _a.rule;
501
+ return rule.type !== models_1.DiscountRuleType.FREE_SHIPPING;
502
+ });
503
+ if (discount) {
504
+ lineDiscounts = this.getLineDiscounts(orderOrCart, discount);
505
+ }
506
+ try {
507
+ for (lineDiscounts_1 = __values(lineDiscounts), lineDiscounts_1_1 = lineDiscounts_1.next(); !lineDiscounts_1_1.done; lineDiscounts_1_1 = lineDiscounts_1.next()) {
508
+ ld = lineDiscounts_1_1.value;
509
+ if (allocationMap[ld.item.id]) {
510
+ allocationMap[ld.item.id].discount = {
511
+ amount: ld.amount,
512
+ unit_amount: Math.round(ld.amount / ld.item.quantity),
513
+ };
514
+ }
515
+ else {
516
+ allocationMap[ld.item.id] = {
517
+ discount: {
518
+ amount: ld.amount,
519
+ unit_amount: Math.round(ld.amount / ld.item.quantity),
520
+ },
521
+ };
522
+ }
523
+ }
524
+ }
525
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
526
+ finally {
527
+ try {
528
+ if (lineDiscounts_1_1 && !lineDiscounts_1_1.done && (_b = lineDiscounts_1.return)) _b.call(lineDiscounts_1);
529
+ }
530
+ finally { if (e_4) throw e_4.error; }
531
+ }
532
+ }
533
+ if (!!options.exclude_gift_cards) return [3 /*break*/, 4];
534
+ lineGiftCards = [];
535
+ if (!(orderOrCart.gift_cards && orderOrCart.gift_cards.length)) return [3 /*break*/, 3];
536
+ return [4 /*yield*/, this.getSubtotal(orderOrCart)];
537
+ case 1:
538
+ subtotal = _d.sent();
539
+ return [4 /*yield*/, this.getGiftCardTotal(orderOrCart)
540
+ // If the fixed discount exceeds the subtotal we should
541
+ // calculate a 100% discount
542
+ ];
543
+ case 2:
544
+ giftCardTotal = _d.sent();
545
+ nominator = Math.min(giftCardTotal.total, subtotal);
546
+ percentage_1 = nominator / subtotal;
547
+ lineGiftCards = orderOrCart.items.map(function (l) {
548
+ return {
549
+ item: l,
550
+ amount: Math.round(l.unit_price * l.quantity * percentage_1),
551
+ };
552
+ });
553
+ _d.label = 3;
554
+ case 3:
555
+ try {
556
+ for (lineGiftCards_1 = __values(lineGiftCards), lineGiftCards_1_1 = lineGiftCards_1.next(); !lineGiftCards_1_1.done; lineGiftCards_1_1 = lineGiftCards_1.next()) {
557
+ lgc = lineGiftCards_1_1.value;
558
+ if (allocationMap[lgc.item.id]) {
559
+ allocationMap[lgc.item.id].gift_card = {
560
+ amount: lgc.amount,
561
+ unit_amount: Math.round(lgc.amount / lgc.item.quantity),
562
+ };
563
+ }
564
+ else {
565
+ allocationMap[lgc.item.id] = {
566
+ gift_card: {
567
+ amount: lgc.amount,
568
+ unit_amount: Math.round(lgc.amount / lgc.item.quantity),
569
+ },
570
+ };
571
+ }
572
+ }
573
+ }
574
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
575
+ finally {
576
+ try {
577
+ if (lineGiftCards_1_1 && !lineGiftCards_1_1.done && (_c = lineGiftCards_1.return)) _c.call(lineGiftCards_1);
578
+ }
579
+ finally { if (e_5) throw e_5.error; }
580
+ }
581
+ _d.label = 4;
582
+ case 4: return [2 /*return*/, allocationMap];
452
583
  }
453
- finally { if (e_3) throw e_3.error; }
454
- }
455
- }
456
- return allocationMap;
584
+ });
585
+ });
457
586
  };
458
587
  /**
459
588
  * Gets the total refund amount for an order.
@@ -474,30 +603,63 @@ var TotalsService = /** @class */ (function (_super) {
474
603
  * @return the line item refund amount.
475
604
  */
476
605
  TotalsService.prototype.getLineItemRefund = function (order, lineItem) {
477
- var _this = this;
478
606
  var _a, _b;
479
- var allocationMap = this.getAllocationMap(order);
480
- var discountAmount = (((_b = (_a = allocationMap[lineItem.id]) === null || _a === void 0 ? void 0 : _a.discount) === null || _b === void 0 ? void 0 : _b.unit_amount) || 0) *
481
- lineItem.quantity;
482
- var lineSubtotal = lineItem.unit_price * lineItem.quantity - discountAmount;
483
- /*
484
- * Used for backcompat with old tax system
485
- */
486
- if (order.tax_rate !== null) {
487
- var taxRate = order.tax_rate / 100;
488
- return this.rounded(lineSubtotal * (1 + taxRate));
489
- }
490
- /*
491
- * New tax system uses the tax lines registerd on the line items
492
- */
493
- if (typeof lineItem.tax_lines === "undefined") {
494
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax calculation did not receive tax_lines");
495
- }
496
- var taxTotal = lineItem.tax_lines.reduce(function (acc, next) {
497
- var taxRate = next.rate / 100;
498
- return acc + _this.rounded(lineSubtotal * taxRate);
499
- }, 0);
500
- return lineSubtotal + taxTotal;
607
+ return __awaiter(this, void 0, void 0, function () {
608
+ var allocationMap, includesTax, discountAmount, lineSubtotal, taxAmountIncludedInPrice_1, taxRate_1, taxRate, taxAmountIncludedInPrice, taxTotal;
609
+ var _this = this;
610
+ return __generator(this, function (_c) {
611
+ switch (_c.label) {
612
+ case 0: return [4 /*yield*/, this.getAllocationMap(order)];
613
+ case 1:
614
+ allocationMap = _c.sent();
615
+ includesTax = this.featureFlagRouter_.isFeatureEnabled(tax_inclusive_pricing_1.default.key) && lineItem.includes_tax;
616
+ discountAmount = (((_b = (_a = allocationMap[lineItem.id]) === null || _a === void 0 ? void 0 : _a.discount) === null || _b === void 0 ? void 0 : _b.unit_amount) || 0) *
617
+ lineItem.quantity;
618
+ lineSubtotal = lineItem.unit_price * lineItem.quantity - discountAmount;
619
+ /*
620
+ * Used for backcompat with old tax system
621
+ */
622
+ if (order.tax_rate !== null) {
623
+ taxAmountIncludedInPrice_1 = !includesTax
624
+ ? 0
625
+ : Math.round((0, utils_1.calculatePriceTaxAmount)({
626
+ price: lineItem.unit_price,
627
+ taxRate: order.tax_rate / 100,
628
+ includesTax: includesTax,
629
+ }));
630
+ lineSubtotal =
631
+ (lineItem.unit_price - taxAmountIncludedInPrice_1) * lineItem.quantity -
632
+ discountAmount;
633
+ taxRate_1 = order.tax_rate / 100;
634
+ return [2 /*return*/, this.rounded(lineSubtotal * (1 + taxRate_1))];
635
+ }
636
+ /*
637
+ * New tax system uses the tax lines registerd on the line items
638
+ */
639
+ if (typeof lineItem.tax_lines === "undefined") {
640
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax calculation did not receive tax_lines");
641
+ }
642
+ taxRate = lineItem.tax_lines.reduce(function (acc, next) {
643
+ return acc + next.rate / 100;
644
+ }, 0);
645
+ taxAmountIncludedInPrice = !includesTax
646
+ ? 0
647
+ : Math.round((0, utils_1.calculatePriceTaxAmount)({
648
+ price: lineItem.unit_price,
649
+ taxRate: taxRate,
650
+ includesTax: includesTax,
651
+ }));
652
+ lineSubtotal =
653
+ (lineItem.unit_price - taxAmountIncludedInPrice) * lineItem.quantity -
654
+ discountAmount;
655
+ taxTotal = lineItem.tax_lines.reduce(function (acc, next) {
656
+ var taxRate = next.rate / 100;
657
+ return acc + _this.rounded(lineSubtotal * taxRate);
658
+ }, 0);
659
+ return [2 /*return*/, lineSubtotal + taxTotal];
660
+ }
661
+ });
662
+ });
501
663
  };
502
664
  /**
503
665
  * Calculates refund total of line items.
@@ -508,49 +670,81 @@ var TotalsService = /** @class */ (function (_super) {
508
670
  * @return the calculated subtotal
509
671
  */
510
672
  TotalsService.prototype.getRefundTotal = function (order, lineItems) {
511
- var e_4, _a, e_5, _b;
512
- var _this = this;
513
- var itemIds = order.items.map(function (i) { return i.id; });
514
- // in case we swap a swap, we need to include swap items
515
- if (order.swaps && order.swaps.length) {
516
- try {
517
- for (var _c = __values(order.swaps), _d = _c.next(); !_d.done; _d = _c.next()) {
518
- var s = _d.value;
519
- var swapItemIds = s.additional_items.map(function (el) { return el.id; });
520
- itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(swapItemIds), false);
521
- }
522
- }
523
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
524
- finally {
525
- try {
526
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
527
- }
528
- finally { if (e_4) throw e_4.error; }
529
- }
530
- }
531
- if (order.claims && order.claims.length) {
532
- try {
533
- for (var _e = __values(order.claims), _f = _e.next(); !_f.done; _f = _e.next()) {
534
- var c = _f.value;
535
- var claimItemIds = c.additional_items.map(function (el) { return el.id; });
536
- itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(claimItemIds), false);
537
- }
538
- }
539
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
540
- finally {
541
- try {
542
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
673
+ return __awaiter(this, void 0, void 0, function () {
674
+ var itemIds, _a, _b, s, swapItemIds, _c, _d, c, claimItemIds, refunds, lineItems_1, lineItems_1_1, item, refund, e_6_1;
675
+ var e_7, _e, e_8, _f, e_6, _g;
676
+ return __generator(this, function (_h) {
677
+ switch (_h.label) {
678
+ case 0:
679
+ itemIds = order.items.map(function (i) { return i.id; });
680
+ // in case we swap a swap, we need to include swap items
681
+ if (order.swaps && order.swaps.length) {
682
+ try {
683
+ for (_a = __values(order.swaps), _b = _a.next(); !_b.done; _b = _a.next()) {
684
+ s = _b.value;
685
+ swapItemIds = s.additional_items.map(function (el) { return el.id; });
686
+ itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(swapItemIds), false);
687
+ }
688
+ }
689
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
690
+ finally {
691
+ try {
692
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
693
+ }
694
+ finally { if (e_7) throw e_7.error; }
695
+ }
696
+ }
697
+ if (order.claims && order.claims.length) {
698
+ try {
699
+ for (_c = __values(order.claims), _d = _c.next(); !_d.done; _d = _c.next()) {
700
+ c = _d.value;
701
+ claimItemIds = c.additional_items.map(function (el) { return el.id; });
702
+ itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(claimItemIds), false);
703
+ }
704
+ }
705
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
706
+ finally {
707
+ try {
708
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
709
+ }
710
+ finally { if (e_8) throw e_8.error; }
711
+ }
712
+ }
713
+ refunds = [];
714
+ _h.label = 1;
715
+ case 1:
716
+ _h.trys.push([1, 6, 7, 8]);
717
+ lineItems_1 = __values(lineItems), lineItems_1_1 = lineItems_1.next();
718
+ _h.label = 2;
719
+ case 2:
720
+ if (!!lineItems_1_1.done) return [3 /*break*/, 5];
721
+ item = lineItems_1_1.value;
722
+ if (!itemIds.includes(item.id)) {
723
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Line item does not exist on order");
724
+ }
725
+ return [4 /*yield*/, this.getLineItemRefund(order, item)];
726
+ case 3:
727
+ refund = _h.sent();
728
+ refunds.push(refund);
729
+ _h.label = 4;
730
+ case 4:
731
+ lineItems_1_1 = lineItems_1.next();
732
+ return [3 /*break*/, 2];
733
+ case 5: return [3 /*break*/, 8];
734
+ case 6:
735
+ e_6_1 = _h.sent();
736
+ e_6 = { error: e_6_1 };
737
+ return [3 /*break*/, 8];
738
+ case 7:
739
+ try {
740
+ if (lineItems_1_1 && !lineItems_1_1.done && (_g = lineItems_1.return)) _g.call(lineItems_1);
741
+ }
742
+ finally { if (e_6) throw e_6.error; }
743
+ return [7 /*endfinally*/];
744
+ case 8: return [2 /*return*/, this.rounded(refunds.reduce(function (acc, next) { return acc + next; }, 0))];
543
745
  }
544
- finally { if (e_5) throw e_5.error; }
545
- }
546
- }
547
- var refunds = lineItems.map(function (i) {
548
- if (!itemIds.includes(i.id)) {
549
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Line item does not exist on order");
550
- }
551
- return _this.getLineItemRefund(order, i);
746
+ });
552
747
  });
553
- return this.rounded(refunds.reduce(function (acc, next) { return acc + next; }, 0));
554
748
  };
555
749
  /**
556
750
  * Calculates either fixed or percentage discount of a variant
@@ -569,7 +763,7 @@ var TotalsService = /** @class */ (function (_super) {
569
763
  amount: 0,
570
764
  };
571
765
  }
572
- if (discountType === discount_rule_1.DiscountRuleType.PERCENTAGE) {
766
+ if (discountType === models_1.DiscountRuleType.PERCENTAGE) {
573
767
  return {
574
768
  lineItem: lineItem,
575
769
  variant: variant,
@@ -606,7 +800,8 @@ var TotalsService = /** @class */ (function (_super) {
606
800
  return discounts;
607
801
  };
608
802
  TotalsService.prototype.getLineItemDiscountAdjustment = function (lineItem, discount) {
609
- var matchingDiscount = lineItem.adjustments.find(function (adjustment) { return adjustment.discount_id === discount.id; });
803
+ var _a;
804
+ var matchingDiscount = (_a = lineItem.adjustments) === null || _a === void 0 ? void 0 : _a.find(function (adjustment) { return adjustment.discount_id === discount.id; });
610
805
  if (!matchingDiscount) {
611
806
  return 0;
612
807
  }
@@ -631,7 +826,7 @@ var TotalsService = /** @class */ (function (_super) {
631
826
  * order
632
827
  */
633
828
  TotalsService.prototype.getLineDiscounts = function (cartOrOrder, discount) {
634
- var e_6, _a, e_7, _b;
829
+ var e_9, _a, e_10, _b;
635
830
  var merged = __spreadArray([], __read(cartOrOrder.items), false);
636
831
  // merge items from order with items from order swaps
637
832
  if ("swaps" in cartOrOrder && cartOrOrder.swaps.length) {
@@ -641,12 +836,12 @@ var TotalsService = /** @class */ (function (_super) {
641
836
  merged = __spreadArray(__spreadArray([], __read(merged), false), __read(s.additional_items), false);
642
837
  }
643
838
  }
644
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
839
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
645
840
  finally {
646
841
  try {
647
842
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
648
843
  }
649
- finally { if (e_6) throw e_6.error; }
844
+ finally { if (e_9) throw e_9.error; }
650
845
  }
651
846
  }
652
847
  if ("claims" in cartOrOrder && cartOrOrder.claims.length) {
@@ -656,12 +851,12 @@ var TotalsService = /** @class */ (function (_super) {
656
851
  merged = __spreadArray(__spreadArray([], __read(merged), false), __read(c.additional_items), false);
657
852
  }
658
853
  }
659
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
854
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
660
855
  finally {
661
856
  try {
662
857
  if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
663
858
  }
664
- finally { if (e_7) throw e_7.error; }
859
+ finally { if (e_10) throw e_10.error; }
665
860
  }
666
861
  }
667
862
  return merged.map(function (item) {
@@ -688,15 +883,22 @@ var TotalsService = /** @class */ (function (_super) {
688
883
  var _a, _b;
689
884
  if (options === void 0) { options = {}; }
690
885
  return __awaiter(this, void 0, void 0, function () {
691
- var calculationContext, lineItemAllocation, subtotal, gift_card_total, discount_total, lineItemTotals, taxLines, orderLines, _c, _d;
886
+ var calculationContext, lineItemAllocation, subtotal, gift_card_total, discount_total, lineItemTotals, taxRate, includesTax, taxIncludedInPrice, taxLines, orderLines, _c, _d;
692
887
  return __generator(this, function (_e) {
693
888
  switch (_e.label) {
694
- case 0:
695
- calculationContext = this.getCalculationContext(cartOrOrder, {
889
+ case 0: return [4 /*yield*/, this.getCalculationContext(cartOrOrder, {
696
890
  exclude_shipping: true,
697
- });
891
+ exclude_gift_cards: options.exclude_gift_cards,
892
+ })];
893
+ case 1:
894
+ calculationContext = _e.sent();
698
895
  lineItemAllocation = calculationContext.allocation_map[lineItem.id] || {};
699
896
  subtotal = lineItem.unit_price * lineItem.quantity;
897
+ if (this.featureFlagRouter_.isFeatureEnabled(tax_inclusive_pricing_1.default.key) &&
898
+ lineItem.includes_tax &&
899
+ options.include_tax) {
900
+ subtotal = 0; // in that case we need to know the tax rate to compute it later
901
+ }
700
902
  gift_card_total = ((_a = lineItemAllocation.gift_card) === null || _a === void 0 ? void 0 : _a.amount) || 0;
701
903
  discount_total = (((_b = lineItemAllocation.discount) === null || _b === void 0 ? void 0 : _b.unit_amount) || 0) * lineItem.quantity;
702
904
  lineItemTotals = {
@@ -711,34 +913,45 @@ var TotalsService = /** @class */ (function (_super) {
711
913
  tax_total: 0,
712
914
  tax_lines: lineItem.tax_lines || [],
713
915
  };
714
- if (!options.include_tax) return [3 /*break*/, 6];
715
- if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate !== null)) return [3 /*break*/, 1];
716
- lineItemTotals.original_tax_total =
717
- subtotal * (cartOrOrder.tax_rate / 100);
916
+ if (!options.include_tax) return [3 /*break*/, 7];
917
+ if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate != null)) return [3 /*break*/, 2];
918
+ taxRate = cartOrOrder.tax_rate / 100;
919
+ includesTax = this.featureFlagRouter_.isFeatureEnabled(tax_inclusive_pricing_1.default.key) && lineItem.includes_tax;
920
+ taxIncludedInPrice = !lineItem.includes_tax
921
+ ? 0
922
+ : Math.round((0, utils_1.calculatePriceTaxAmount)({
923
+ price: lineItem.unit_price,
924
+ taxRate: taxRate,
925
+ includesTax: includesTax,
926
+ }));
927
+ lineItemTotals.subtotal =
928
+ (lineItem.unit_price - taxIncludedInPrice) * lineItem.quantity;
929
+ lineItemTotals.total = lineItemTotals.subtotal;
930
+ lineItemTotals.original_tax_total = lineItemTotals.subtotal * taxRate;
718
931
  lineItemTotals.tax_total =
719
- (subtotal - discount_total) * (cartOrOrder.tax_rate / 100);
932
+ (lineItemTotals.subtotal - discount_total) * taxRate;
720
933
  lineItemTotals.total += lineItemTotals.tax_total;
721
934
  lineItemTotals.original_total += lineItemTotals.original_tax_total;
722
- return [3 /*break*/, 6];
723
- case 1:
935
+ return [3 /*break*/, 7];
936
+ case 2:
724
937
  taxLines = void 0;
725
- if (!(options.use_tax_lines || (0, orders_1.isOrder)(cartOrOrder))) return [3 /*break*/, 2];
938
+ if (!(options.use_tax_lines || (0, orders_1.isOrder)(cartOrOrder))) return [3 /*break*/, 3];
726
939
  if (typeof lineItem.tax_lines === "undefined") {
727
940
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax Lines must be joined on items to calculate taxes");
728
941
  }
729
942
  taxLines = lineItem.tax_lines;
730
- return [3 /*break*/, 5];
731
- case 2:
732
- if (!lineItem.is_return) return [3 /*break*/, 3];
943
+ return [3 /*break*/, 6];
944
+ case 3:
945
+ if (!lineItem.is_return) return [3 /*break*/, 4];
733
946
  if (typeof lineItem.tax_lines === "undefined") {
734
947
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Return Line Items must join tax lines");
735
948
  }
736
949
  taxLines = lineItem.tax_lines;
737
- return [3 /*break*/, 5];
738
- case 3: return [4 /*yield*/, this.taxProviderService_
950
+ return [3 /*break*/, 6];
951
+ case 4: return [4 /*yield*/, this.taxProviderService_
739
952
  .withTransaction(this.manager_)
740
953
  .getTaxLines(cartOrOrder.items, calculationContext)];
741
- case 4:
954
+ case 5:
742
955
  orderLines = _e.sent();
743
956
  taxLines = orderLines.filter(function (ol) {
744
957
  if ("item_id" in ol) {
@@ -746,26 +959,33 @@ var TotalsService = /** @class */ (function (_super) {
746
959
  }
747
960
  return false;
748
961
  });
749
- _e.label = 5;
750
- case 5:
751
- lineItemTotals.tax_lines = taxLines;
752
962
  _e.label = 6;
753
963
  case 6:
754
- if (!(lineItemTotals.tax_lines.length > 0)) return [3 /*break*/, 9];
964
+ lineItemTotals.tax_lines = taxLines;
965
+ _e.label = 7;
966
+ case 7:
967
+ if (!(lineItemTotals.tax_lines.length > 0)) return [3 /*break*/, 10];
755
968
  _c = lineItemTotals;
756
969
  return [4 /*yield*/, this.taxCalculationStrategy_.calculate([lineItem], lineItemTotals.tax_lines, calculationContext)];
757
- case 7:
970
+ case 8:
758
971
  _c.tax_total = _e.sent();
759
- lineItemTotals.total += lineItemTotals.tax_total;
760
972
  calculationContext.allocation_map = {}; // Don't account for discounts
761
973
  _d = lineItemTotals;
762
974
  return [4 /*yield*/, this.taxCalculationStrategy_.calculate([lineItem], lineItemTotals.tax_lines, calculationContext)];
763
- case 8:
764
- _d.original_tax_total =
765
- _e.sent();
975
+ case 9:
976
+ _d.original_tax_total = _e.sent();
977
+ if (this.featureFlagRouter_.isFeatureEnabled(tax_inclusive_pricing_1.default.key) &&
978
+ lineItem.includes_tax) {
979
+ lineItemTotals.subtotal +=
980
+ lineItem.unit_price * lineItem.quantity -
981
+ lineItemTotals.original_tax_total;
982
+ lineItemTotals.total += lineItemTotals.subtotal;
983
+ lineItemTotals.original_total += lineItemTotals.subtotal;
984
+ }
985
+ lineItemTotals.total += lineItemTotals.tax_total;
766
986
  lineItemTotals.original_total += lineItemTotals.original_tax_total;
767
- _e.label = 9;
768
- case 9: return [2 /*return*/, lineItemTotals];
987
+ _e.label = 10;
988
+ case 10: return [2 /*return*/, lineItemTotals];
769
989
  }
770
990
  });
771
991
  });
@@ -813,16 +1033,22 @@ var TotalsService = /** @class */ (function (_super) {
813
1033
  TotalsService.prototype.getGiftCardableAmount = function (cartOrOrder) {
814
1034
  var _a;
815
1035
  return __awaiter(this, void 0, void 0, function () {
1036
+ var subtotal, discountTotal;
816
1037
  return __generator(this, function (_b) {
817
1038
  switch (_b.label) {
818
1039
  case 0:
819
- if ((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable) {
820
- return [2 /*return*/, this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder)];
821
- }
822
- return [4 /*yield*/, this.getTotal(cartOrOrder, {
823
- exclude_gift_cards: true,
824
- })];
825
- case 1: return [2 /*return*/, _b.sent()];
1040
+ if (!((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable)) return [3 /*break*/, 3];
1041
+ return [4 /*yield*/, this.getSubtotal(cartOrOrder)];
1042
+ case 1:
1043
+ subtotal = _b.sent();
1044
+ return [4 /*yield*/, this.getDiscountTotal(cartOrOrder)];
1045
+ case 2:
1046
+ discountTotal = _b.sent();
1047
+ return [2 /*return*/, subtotal - discountTotal];
1048
+ case 3: return [4 /*yield*/, this.getTotal(cartOrOrder, {
1049
+ exclude_gift_cards: true,
1050
+ })];
1051
+ case 4: return [2 /*return*/, _b.sent()];
826
1052
  }
827
1053
  });
828
1054
  });
@@ -834,50 +1060,63 @@ var TotalsService = /** @class */ (function (_super) {
834
1060
  */
835
1061
  TotalsService.prototype.getGiftCardTotal = function (cartOrOrder) {
836
1062
  var _a;
837
- var giftCardable = this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder);
838
- if ("gift_card_transactions" in cartOrOrder) {
839
- // gift_card_transactions only exist on orders so we can
840
- // safely calculate the total based on the gift card transactions
841
- return cartOrOrder.gift_card_transactions.reduce(function (acc, next) {
842
- var _a;
843
- var taxMultiplier = (next.tax_rate || 0) / 100;
844
- // Previously we did not record whether a gift card was taxable or not.
845
- // All gift cards where is_taxable === null are from the old system,
846
- // where we defaulted to taxable gift cards.
847
- //
848
- // This is a backwards compatability fix for orders that were created
849
- // before we added the gift card tax rate.
850
- if (next.is_taxable === null &&
851
- ((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable)) {
852
- taxMultiplier = cartOrOrder.region.tax_rate / 100;
1063
+ return __awaiter(this, void 0, void 0, function () {
1064
+ var subtotal, discountTotal, giftCardable, toReturn, orderGiftCardAmount;
1065
+ return __generator(this, function (_b) {
1066
+ switch (_b.label) {
1067
+ case 0: return [4 /*yield*/, this.getSubtotal(cartOrOrder)];
1068
+ case 1:
1069
+ subtotal = _b.sent();
1070
+ return [4 /*yield*/, this.getDiscountTotal(cartOrOrder)];
1071
+ case 2:
1072
+ discountTotal = _b.sent();
1073
+ giftCardable = subtotal - discountTotal;
1074
+ if ("gift_card_transactions" in cartOrOrder) {
1075
+ // gift_card_transactions only exist on orders so we can
1076
+ // safely calculate the total based on the gift card transactions
1077
+ return [2 /*return*/, cartOrOrder.gift_card_transactions.reduce(function (acc, next) {
1078
+ var _a;
1079
+ var taxMultiplier = (next.tax_rate || 0) / 100;
1080
+ // Previously we did not record whether a gift card was taxable or not.
1081
+ // All gift cards where is_taxable === null are from the old system,
1082
+ // where we defaulted to taxable gift cards.
1083
+ //
1084
+ // This is a backwards compatability fix for orders that were created
1085
+ // before we added the gift card tax rate.
1086
+ if (next.is_taxable === null &&
1087
+ ((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable)) {
1088
+ taxMultiplier = cartOrOrder.region.tax_rate / 100;
1089
+ }
1090
+ return {
1091
+ total: acc.total + next.amount,
1092
+ tax_total: acc.tax_total + next.amount * taxMultiplier,
1093
+ };
1094
+ }, {
1095
+ total: 0,
1096
+ tax_total: 0,
1097
+ })];
1098
+ }
1099
+ if (!cartOrOrder.gift_cards || !cartOrOrder.gift_cards.length) {
1100
+ return [2 /*return*/, {
1101
+ total: 0,
1102
+ tax_total: 0,
1103
+ }];
1104
+ }
1105
+ toReturn = cartOrOrder.gift_cards.reduce(function (acc, next) { return acc + next.balance; }, 0);
1106
+ orderGiftCardAmount = Math.min(giftCardable, toReturn);
1107
+ if ((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable) {
1108
+ return [2 /*return*/, {
1109
+ total: orderGiftCardAmount,
1110
+ tax_total: (orderGiftCardAmount * cartOrOrder.region.tax_rate) / 100,
1111
+ }];
1112
+ }
1113
+ return [2 /*return*/, {
1114
+ total: orderGiftCardAmount,
1115
+ tax_total: 0,
1116
+ }];
853
1117
  }
854
- return {
855
- total: acc.total + next.amount,
856
- tax_total: acc.tax_total + next.amount * taxMultiplier,
857
- };
858
- }, {
859
- total: 0,
860
- tax_total: 0,
861
1118
  });
862
- }
863
- if (!cartOrOrder.gift_cards || !cartOrOrder.gift_cards.length) {
864
- return {
865
- total: 0,
866
- tax_total: 0,
867
- };
868
- }
869
- var toReturn = cartOrOrder.gift_cards.reduce(function (acc, next) { return acc + next.balance; }, 0);
870
- var orderGiftCardAmount = Math.min(giftCardable, toReturn);
871
- if ((_a = cartOrOrder.region) === null || _a === void 0 ? void 0 : _a.gift_cards_taxable) {
872
- return {
873
- total: orderGiftCardAmount,
874
- tax_total: (orderGiftCardAmount * cartOrOrder.region.tax_rate) / 100,
875
- };
876
- }
877
- return {
878
- total: orderGiftCardAmount,
879
- tax_total: 0,
880
- };
1119
+ });
881
1120
  };
882
1121
  /**
883
1122
  * Calculates the total discount amount for each of the different supported
@@ -886,26 +1125,34 @@ var TotalsService = /** @class */ (function (_super) {
886
1125
  * @return the total discounts amount
887
1126
  */
888
1127
  TotalsService.prototype.getDiscountTotal = function (cartOrOrder) {
889
- var subtotal = this.getSubtotal(cartOrOrder, {
890
- excludeNonDiscounts: true,
891
- });
892
- if (!cartOrOrder.discounts || !cartOrOrder.discounts.length) {
893
- return 0;
894
- }
895
- // we only support having free shipping and one other discount, so first
896
- // find the discount, which is not free shipping.
897
- var discount = cartOrOrder.discounts.find(function (_a) {
898
- var rule = _a.rule;
899
- return rule.type !== "free_shipping";
1128
+ var _a;
1129
+ return __awaiter(this, void 0, void 0, function () {
1130
+ var subtotal, discount, discountTotal;
1131
+ return __generator(this, function (_b) {
1132
+ switch (_b.label) {
1133
+ case 0: return [4 /*yield*/, this.getSubtotal(cartOrOrder, {
1134
+ excludeNonDiscounts: true,
1135
+ })
1136
+ // we only support having free shipping and one other discount, so first
1137
+ // find the discount, which is not free shipping.
1138
+ ];
1139
+ case 1:
1140
+ subtotal = _b.sent();
1141
+ discount = (_a = cartOrOrder.discounts) === null || _a === void 0 ? void 0 : _a.find(function (_a) {
1142
+ var rule = _a.rule;
1143
+ return rule.type !== models_1.DiscountRuleType.FREE_SHIPPING;
1144
+ });
1145
+ if (!discount) {
1146
+ return [2 /*return*/, 0];
1147
+ }
1148
+ discountTotal = this.getLineItemAdjustmentsTotal(cartOrOrder);
1149
+ if (subtotal < 0) {
1150
+ return [2 /*return*/, this.rounded(Math.max(subtotal, discountTotal))];
1151
+ }
1152
+ return [2 /*return*/, this.rounded(Math.min(subtotal, discountTotal))];
1153
+ }
1154
+ });
900
1155
  });
901
- if (!discount) {
902
- return 0;
903
- }
904
- var discountTotal = this.getLineItemAdjustmentsTotal(cartOrOrder);
905
- if (subtotal < 0) {
906
- return this.rounded(Math.max(subtotal, discountTotal));
907
- }
908
- return this.rounded(Math.min(subtotal, discountTotal));
909
1156
  };
910
1157
  /**
911
1158
  * Prepares the calculation context for a tax total calculation.
@@ -916,23 +1163,32 @@ var TotalsService = /** @class */ (function (_super) {
916
1163
  TotalsService.prototype.getCalculationContext = function (cartOrOrder, options) {
917
1164
  var _a;
918
1165
  if (options === void 0) { options = {}; }
919
- var allocationMap = this.getAllocationMap(cartOrOrder, {
920
- exclude_gift_cards: options.exclude_gift_cards,
921
- exclude_discounts: options.exclude_discounts,
1166
+ return __awaiter(this, void 0, void 0, function () {
1167
+ var allocationMap, shippingMethods;
1168
+ return __generator(this, function (_b) {
1169
+ switch (_b.label) {
1170
+ case 0: return [4 /*yield*/, this.getAllocationMap(cartOrOrder, {
1171
+ exclude_gift_cards: options.exclude_gift_cards,
1172
+ exclude_discounts: options.exclude_discounts,
1173
+ })];
1174
+ case 1:
1175
+ allocationMap = _b.sent();
1176
+ shippingMethods = [];
1177
+ // Default to include shipping methods
1178
+ if (!options.exclude_shipping) {
1179
+ shippingMethods = cartOrOrder.shipping_methods || [];
1180
+ }
1181
+ return [2 /*return*/, {
1182
+ shipping_address: cartOrOrder.shipping_address,
1183
+ shipping_methods: shippingMethods,
1184
+ customer: cartOrOrder.customer,
1185
+ region: cartOrOrder.region,
1186
+ is_return: (_a = options.is_return) !== null && _a !== void 0 ? _a : false,
1187
+ allocation_map: allocationMap,
1188
+ }];
1189
+ }
1190
+ });
922
1191
  });
923
- var shippingMethods = [];
924
- // Default to include shipping methods
925
- if (!options.exclude_shipping) {
926
- shippingMethods = cartOrOrder.shipping_methods || [];
927
- }
928
- return {
929
- shipping_address: cartOrOrder.shipping_address,
930
- shipping_methods: shippingMethods,
931
- customer: cartOrOrder.customer,
932
- region: cartOrOrder.region,
933
- is_return: (_a = options.is_return) !== null && _a !== void 0 ? _a : false,
934
- allocation_map: allocationMap,
935
- };
936
1192
  };
937
1193
  /**
938
1194
  * Rounds a number using Math.round.