@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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +12 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/routes/admin/batch/create-batch-job.js +10 -7
- package/dist/api/routes/admin/batch/create-batch-job.js.map +1 -1
- package/dist/api/routes/admin/index.js +6 -4
- package/dist/api/routes/admin/index.js.map +1 -1
- package/dist/api/routes/admin/price-lists/create-price-list.d.ts +4 -0
- package/dist/api/routes/admin/price-lists/create-price-list.js +16 -1
- package/dist/api/routes/admin/price-lists/create-price-list.js.map +1 -1
- package/dist/api/routes/admin/price-lists/index.d.ts +2 -1
- package/dist/api/routes/admin/price-lists/index.js +8 -1
- package/dist/api/routes/admin/price-lists/index.js.map +1 -1
- package/dist/api/routes/admin/price-lists/update-price-list.d.ts +4 -0
- package/dist/api/routes/admin/price-lists/update-price-list.js +16 -1
- package/dist/api/routes/admin/price-lists/update-price-list.js.map +1 -1
- package/dist/api/routes/admin/products/update-variant.js +1 -0
- package/dist/api/routes/admin/products/update-variant.js.map +1 -1
- package/dist/api/routes/admin/regions/create-region.d.ts +4 -0
- package/dist/api/routes/admin/regions/create-region.js +16 -1
- package/dist/api/routes/admin/regions/create-region.js.map +1 -1
- package/dist/api/routes/admin/regions/index.d.ts +2 -1
- package/dist/api/routes/admin/regions/index.js +5 -1
- package/dist/api/routes/admin/regions/index.js.map +1 -1
- package/dist/api/routes/admin/regions/update-region.d.ts +4 -0
- package/dist/api/routes/admin/regions/update-region.js +16 -1
- package/dist/api/routes/admin/regions/update-region.js.map +1 -1
- package/dist/api/routes/admin/shipping-options/create-shipping-option.d.ts +4 -0
- package/dist/api/routes/admin/shipping-options/create-shipping-option.js +15 -0
- package/dist/api/routes/admin/shipping-options/create-shipping-option.js.map +1 -1
- package/dist/api/routes/admin/shipping-options/index.d.ts +2 -1
- package/dist/api/routes/admin/shipping-options/index.js +5 -1
- package/dist/api/routes/admin/shipping-options/index.js.map +1 -1
- package/dist/api/routes/admin/shipping-options/update-shipping-option.d.ts +4 -0
- package/dist/api/routes/admin/shipping-options/update-shipping-option.js +15 -0
- package/dist/api/routes/admin/shipping-options/update-shipping-option.js.map +1 -1
- package/dist/helpers/test-request.js +27 -4
- package/dist/helpers/test-request.js.map +1 -1
- package/dist/interfaces/price-selection-strategy.d.ts +4 -0
- package/dist/interfaces/price-selection-strategy.js.map +1 -1
- package/dist/loaders/feature-flags/index.d.ts +1 -0
- package/dist/loaders/feature-flags/index.js +18 -3
- package/dist/loaders/feature-flags/index.js.map +1 -1
- package/dist/loaders/feature-flags/order-editing.d.ts +3 -0
- package/dist/loaders/feature-flags/order-editing.js +10 -0
- package/dist/loaders/feature-flags/order-editing.js.map +1 -0
- package/dist/migrations/1663059812399-order_editing.d.ts +7 -0
- package/dist/migrations/1663059812399-order_editing.js +112 -0
- package/dist/migrations/1663059812399-order_editing.js.map +1 -0
- package/dist/models/currency.d.ts +4 -0
- package/dist/models/currency.js +12 -0
- package/dist/models/currency.js.map +1 -1
- package/dist/models/custom-shipping-option.d.ts +3 -0
- package/dist/models/custom-shipping-option.js +3 -0
- package/dist/models/custom-shipping-option.js.map +1 -1
- package/dist/models/line-item.d.ts +4 -0
- package/dist/models/line-item.js +12 -0
- package/dist/models/line-item.js.map +1 -1
- package/dist/models/money-amount.d.ts +2 -2
- package/dist/models/money-amount.js.map +1 -1
- package/dist/models/order-edit.d.ts +111 -0
- package/dist/models/order-edit.js +204 -0
- package/dist/models/order-edit.js.map +1 -0
- package/dist/models/order-item-change.d.ts +60 -0
- package/dist/models/order-item-change.js +143 -0
- package/dist/models/order-item-change.js.map +1 -0
- package/dist/models/order.d.ts +7 -0
- package/dist/models/order.js +16 -0
- package/dist/models/order.js.map +1 -1
- package/dist/models/price-list.d.ts +5 -0
- package/dist/models/price-list.js +13 -0
- package/dist/models/price-list.js.map +1 -1
- package/dist/models/region.d.ts +4 -0
- package/dist/models/region.js +12 -0
- package/dist/models/region.js.map +1 -1
- package/dist/models/shipping-method.d.ts +4 -0
- package/dist/models/shipping-method.js +12 -0
- package/dist/models/shipping-method.js.map +1 -1
- package/dist/models/shipping-option.d.ts +4 -0
- package/dist/models/shipping-option.js +12 -0
- package/dist/models/shipping-option.js.map +1 -1
- package/dist/repositories/currency.d.ts +1 -1
- package/dist/repositories/currency.js +2 -2
- package/dist/repositories/currency.js.map +1 -1
- package/dist/repositories/money-amount.d.ts +1 -1
- package/dist/repositories/money-amount.js +8 -4
- package/dist/repositories/money-amount.js.map +1 -1
- package/dist/repositories/order-edit.d.ts +4 -0
- package/dist/repositories/order-edit.js +38 -0
- package/dist/repositories/order-edit.js.map +1 -0
- package/dist/repositories/order-item-change.d.ts +4 -0
- package/dist/repositories/order-item-change.js +38 -0
- package/dist/repositories/order-item-change.js.map +1 -0
- package/dist/services/batch-job.js +4 -2
- package/dist/services/batch-job.js.map +1 -1
- package/dist/services/cart.js +83 -74
- package/dist/services/cart.js.map +1 -1
- package/dist/services/claim.js +45 -43
- package/dist/services/claim.js.map +1 -1
- package/dist/services/discount.d.ts +4 -1
- package/dist/services/discount.js +39 -20
- package/dist/services/discount.js.map +1 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +3 -3
- package/dist/services/index.js.map +1 -1
- package/dist/services/line-item-adjustment.d.ts +1 -4
- package/dist/services/line-item-adjustment.js +2 -1
- package/dist/services/line-item-adjustment.js.map +1 -1
- package/dist/services/line-item.d.ts +12 -7
- package/dist/services/line-item.js +22 -10
- package/dist/services/line-item.js.map +1 -1
- package/dist/services/notification.js +3 -2
- package/dist/services/notification.js.map +1 -1
- package/dist/services/order.js +214 -115
- package/dist/services/order.js.map +1 -1
- package/dist/services/price-list.d.ts +7 -4
- package/dist/services/price-list.js +25 -11
- package/dist/services/price-list.js.map +1 -1
- package/dist/services/pricing.d.ts +7 -4
- package/dist/services/pricing.js +39 -12
- package/dist/services/pricing.js.map +1 -1
- package/dist/services/product-variant.js +1 -1
- package/dist/services/product-variant.js.map +1 -1
- package/dist/services/region.d.ts +10 -7
- package/dist/services/region.js +22 -6
- package/dist/services/region.js.map +1 -1
- package/dist/services/return.js +23 -19
- package/dist/services/return.js.map +1 -1
- package/dist/services/shipping-option.d.ts +12 -8
- package/dist/services/shipping-option.js +23 -2
- package/dist/services/shipping-option.js.map +1 -1
- package/dist/services/store.d.ts +1 -2
- package/dist/services/store.js.map +1 -1
- package/dist/services/totals.d.ts +18 -20
- package/dist/services/totals.js +614 -358
- package/dist/services/totals.js.map +1 -1
- package/dist/strategies/__fixtures__/product-export-data.js +6 -2
- package/dist/strategies/__fixtures__/product-export-data.js.map +1 -1
- package/dist/strategies/batch-jobs/product/export.d.ts +6 -0
- package/dist/strategies/batch-jobs/product/export.js +11 -2
- package/dist/strategies/batch-jobs/product/export.js.map +1 -1
- package/dist/strategies/batch-jobs/product/import.d.ts +6 -5
- package/dist/strategies/batch-jobs/product/import.js +293 -197
- package/dist/strategies/batch-jobs/product/import.js.map +1 -1
- package/dist/strategies/batch-jobs/product/index.js +2 -2
- package/dist/strategies/batch-jobs/product/types.d.ts +8 -0
- package/dist/strategies/batch-jobs/product/types.js.map +1 -1
- package/dist/strategies/batch-jobs/product/utils.d.ts +1 -1
- package/dist/strategies/batch-jobs/product/utils.js +10 -1
- package/dist/strategies/batch-jobs/product/utils.js.map +1 -1
- package/dist/strategies/price-selection.d.ts +6 -2
- package/dist/strategies/price-selection.js +124 -10
- package/dist/strategies/price-selection.js.map +1 -1
- package/dist/strategies/tax-calculation.d.ts +7 -5
- package/dist/strategies/tax-calculation.js +47 -17
- package/dist/strategies/tax-calculation.js.map +1 -1
- package/dist/subscribers/batch-job.d.ts +4 -1
- package/dist/subscribers/batch-job.js +71 -44
- package/dist/subscribers/batch-job.js.map +1 -1
- package/dist/types/price-list.d.ts +2 -1
- package/dist/types/pricing.d.ts +5 -2
- package/dist/types/region.d.ts +2 -0
- package/dist/types/shipping-options.d.ts +2 -0
- package/dist/utils/feature-flag-decorators.js +4 -16
- package/dist/utils/feature-flag-decorators.js.map +1 -1
- package/dist/utils/flag-router.d.ts +1 -1
- package/dist/utils/flag-router.js.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -3
package/dist/services/totals.js
CHANGED
|
@@ -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
|
|
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 (
|
|
126
|
-
switch (
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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,
|
|
193
|
-
return __generator(this, function (
|
|
194
|
-
switch (
|
|
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*/,
|
|
209
|
-
if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate
|
|
210
|
-
totals.original_tax_total = Math.round(totals.
|
|
211
|
-
totals.tax_total = Math.round(totals.
|
|
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:
|
|
222
|
-
|
|
223
|
-
|
|
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 =
|
|
226
|
-
|
|
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
|
-
|
|
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
|
|
235
|
-
|
|
236
|
-
|
|
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
|
|
241
|
-
|
|
242
|
-
_b.sent();
|
|
255
|
+
case 5:
|
|
256
|
+
_b.original_tax_total = _c.sent();
|
|
243
257
|
totals.tax_total = totals.original_tax_total;
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
|
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 =
|
|
354
|
-
case
|
|
355
|
-
case
|
|
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
|
|
488
|
+
var _a;
|
|
375
489
|
if (options === void 0) { options = {}; }
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
var
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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
|
-
|
|
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
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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
|
-
|
|
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 ===
|
|
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
|
|
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
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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*/,
|
|
715
|
-
if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate
|
|
716
|
-
|
|
717
|
-
|
|
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) *
|
|
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*/,
|
|
723
|
-
case
|
|
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*/,
|
|
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*/,
|
|
731
|
-
case
|
|
732
|
-
if (!lineItem.is_return) return [3 /*break*/,
|
|
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*/,
|
|
738
|
-
case
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
764
|
-
_d.original_tax_total =
|
|
765
|
-
|
|
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 =
|
|
768
|
-
case
|
|
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
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
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
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
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
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
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
|
-
|
|
920
|
-
|
|
921
|
-
|
|
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.
|