@medusajs/utils
Version:
Medusa utilities functions shared by Medusa core and Modules
145 lines • 8.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decorateCartTotals = decorateCartTotals;
const big_number_1 = require("../big-number");
const credit_lines_1 = require("../credit-lines");
const line_item_1 = require("../line-item");
const math_1 = require("../math");
const shipping_method_1 = require("../shipping-method");
const transform_properties_to_bignumber_1 = require("../transform-properties-to-bignumber");
function decorateCartTotals(cartLike, config = {}) {
(0, transform_properties_to_bignumber_1.transformPropertiesToBigNumber)(cartLike);
const optionalFields = {
"detail.fulfilled_quantity": "fulfilled_total",
"detail.shipped_quantity": "shipped_total",
"detail.return_requested_quantity": "return_requested_total",
"detail.return_received_quantity": "return_received_total",
"detail.return_dismissed_quantity": "return_dismissed_total",
"detail.written_off_quantity": "write_off_total",
};
const creditLines = cartLike.credit_lines ?? [];
const items = (cartLike.items ?? []);
const shippingMethods = (cartLike.shipping_methods ??
[]);
const includeTax = config?.includeTaxes || cartLike.region?.automatic_taxes;
const itemsTotals = (0, line_item_1.getLineItemsTotals)(items, {
includeTax,
extraQuantityFields: optionalFields,
});
const shippingMethodsTotals = (0, shipping_method_1.getShippingMethodsTotals)(shippingMethods, {
includeTax,
});
const extraTotals = {};
// TODO: Remove this once we have a way to calculate the tax rate for credit lines
const creditLinesSumTax = math_1.MathBN.convert(0);
const creditLinesSumTaxRate = math_1.MathBN.div(creditLinesSumTax, 100);
let subtotal = math_1.MathBN.convert(0);
let discountTotal = math_1.MathBN.convert(0);
let discountSubtotal = math_1.MathBN.convert(0);
let discountTaxTotal = math_1.MathBN.convert(0);
let itemsSubtotal = math_1.MathBN.convert(0);
let itemsTotal = math_1.MathBN.convert(0);
let itemsOriginalTotal = math_1.MathBN.convert(0);
let itemsOriginalSubtotal = math_1.MathBN.convert(0);
let itemsTaxTotal = math_1.MathBN.convert(0);
let itemsOriginalTaxTotal = math_1.MathBN.convert(0);
let shippingSubtotal = math_1.MathBN.convert(0);
let shippingTotal = math_1.MathBN.convert(0);
let shippingOriginalTotal = math_1.MathBN.convert(0);
let shippingOriginalSubtotal = math_1.MathBN.convert(0);
let shippingTaxTotal = math_1.MathBN.convert(0);
let shippingOriginalTaxTotal = math_1.MathBN.convert(0);
const cartItems = items.map((item, index) => {
const itemTotals = Object.assign(item, itemsTotals[item.id ?? index] ?? {});
const itemSubtotal = itemTotals.subtotal;
const itemTotal = math_1.MathBN.convert(itemTotals.total);
const itemOriginalTotal = math_1.MathBN.convert(itemTotals.original_total);
const itemTaxTotal = math_1.MathBN.convert(itemTotals.tax_total);
const itemOriginalTaxTotal = math_1.MathBN.convert(itemTotals.original_tax_total);
const itemDiscountTotal = math_1.MathBN.convert(itemTotals.discount_total);
const itemDiscountSubTotal = math_1.MathBN.convert(itemTotals.discount_subtotal);
const itemDiscountTaxTotal = math_1.MathBN.convert(itemTotals.discount_tax_total);
subtotal = math_1.MathBN.add(subtotal, itemSubtotal);
discountTotal = math_1.MathBN.add(discountTotal, itemDiscountTotal);
discountSubtotal = math_1.MathBN.add(discountSubtotal, itemDiscountSubTotal);
discountTaxTotal = math_1.MathBN.add(discountTaxTotal, itemDiscountTaxTotal);
itemsTotal = math_1.MathBN.add(itemsTotal, itemTotal);
itemsOriginalTotal = math_1.MathBN.add(itemsOriginalTotal, itemOriginalTotal);
itemsOriginalSubtotal = math_1.MathBN.add(itemsOriginalSubtotal, itemSubtotal);
itemsSubtotal = math_1.MathBN.add(itemsSubtotal, itemSubtotal);
itemsTaxTotal = math_1.MathBN.add(itemsTaxTotal, itemTaxTotal);
itemsOriginalTaxTotal = math_1.MathBN.add(itemsOriginalTaxTotal, itemOriginalTaxTotal);
for (const key of Object.values(optionalFields)) {
if (key in itemTotals) {
extraTotals[key] ??= math_1.MathBN.convert(0);
extraTotals[key] = math_1.MathBN.add(extraTotals[key], itemTotals[key] ?? 0);
}
}
return itemTotals;
});
const cartShippingMethods = shippingMethods.map((shippingMethod, index) => {
const shippingMethodTotals = Object.assign(shippingMethod, shippingMethodsTotals[shippingMethod.id ?? index] ?? {});
subtotal = math_1.MathBN.add(subtotal, shippingMethodTotals.subtotal);
shippingSubtotal = math_1.MathBN.add(shippingSubtotal, shippingMethodTotals.subtotal);
shippingTotal = math_1.MathBN.add(shippingTotal, shippingMethodTotals.total);
shippingOriginalTotal = math_1.MathBN.add(shippingOriginalTotal, shippingMethodTotals.original_total);
shippingOriginalSubtotal = math_1.MathBN.add(shippingOriginalSubtotal, shippingMethodTotals.subtotal);
shippingTaxTotal = math_1.MathBN.add(shippingTaxTotal, shippingMethodTotals.tax_total);
shippingOriginalTaxTotal = math_1.MathBN.add(shippingOriginalTaxTotal, shippingMethodTotals.original_tax_total);
discountTotal = math_1.MathBN.add(discountTotal, shippingMethodTotals.discount_total);
discountSubtotal = math_1.MathBN.add(discountSubtotal, shippingMethodTotals.discount_subtotal);
discountTaxTotal = math_1.MathBN.add(discountTaxTotal, shippingMethodTotals.discount_tax_total);
return shippingMethodTotals;
});
const { creditLinesTotal, creditLinesSubtotal, creditLinesTaxTotal } = (0, credit_lines_1.calculateCreditLinesTotal)({
creditLines,
includesTax: false,
taxRate: creditLinesSumTaxRate,
});
const taxTotal = math_1.MathBN.add(itemsTaxTotal, shippingTaxTotal);
const originalTaxTotal = math_1.MathBN.add(itemsOriginalTaxTotal, shippingOriginalTaxTotal);
// TODO: Gift Card calculations
const originalTotal = math_1.MathBN.add(itemsOriginalTotal, shippingOriginalTotal);
// TODO: subtract (cart.gift_card_total + cart.gift_card_tax_total)
const tempTotal = math_1.MathBN.add(subtotal, taxTotal);
const total = math_1.MathBN.sub(tempTotal, discountSubtotal, creditLinesTotal);
const cart = cartLike;
cart.total = new big_number_1.BigNumber(total);
cart.subtotal = new big_number_1.BigNumber(subtotal);
cart.tax_total = new big_number_1.BigNumber(taxTotal);
cart.discount_total = new big_number_1.BigNumber(discountTotal);
cart.discount_subtotal = new big_number_1.BigNumber(discountSubtotal);
cart.discount_tax_total = new big_number_1.BigNumber(discountTaxTotal);
cart.credit_line_total = new big_number_1.BigNumber(creditLinesTotal);
cart.credit_line_subtotal = new big_number_1.BigNumber(creditLinesSubtotal);
cart.credit_line_tax_total = new big_number_1.BigNumber(creditLinesTaxTotal);
// cart.gift_card_total = giftCardTotal.total || 0
// cart.gift_card_tax_total = giftCardTotal.tax_total || 0
cart.original_total = new big_number_1.BigNumber(originalTotal);
cart.original_tax_total = new big_number_1.BigNumber(originalTaxTotal);
// cart.original_gift_card_total =
// cart.original_gift_card_tax_total =
if (cartLike.items) {
cart.items = cartItems;
cart.item_total = new big_number_1.BigNumber(itemsTotal);
cart.item_subtotal = new big_number_1.BigNumber(itemsSubtotal);
cart.item_tax_total = new big_number_1.BigNumber(itemsTaxTotal);
cart.original_item_total = new big_number_1.BigNumber(itemsOriginalTotal);
cart.original_item_subtotal = new big_number_1.BigNumber(itemsOriginalSubtotal);
cart.original_item_tax_total = new big_number_1.BigNumber(itemsOriginalTaxTotal);
for (const key of Object.keys(extraTotals)) {
cart[key] = new big_number_1.BigNumber(extraTotals[key]);
}
}
if (cart.shipping_methods) {
cart.shipping_methods = cartShippingMethods;
cart.shipping_total = new big_number_1.BigNumber(shippingTotal);
cart.shipping_subtotal = new big_number_1.BigNumber(shippingSubtotal);
cart.shipping_tax_total = new big_number_1.BigNumber(shippingTaxTotal);
cart.original_shipping_tax_total = new big_number_1.BigNumber(shippingOriginalTaxTotal);
cart.original_shipping_subtotal = new big_number_1.BigNumber(shippingOriginalSubtotal);
cart.original_shipping_total = new big_number_1.BigNumber(shippingOriginalTotal);
}
return cart;
}
//# sourceMappingURL=index.js.map