UNPKG

@chiper-inc/ecommerce-lib

Version:
177 lines 7.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Price = void 0; const helpers_1 = require("./helpers"); class Price { constructor({ ico, iva, base, subtotal, total, measurementTotal, externalId, maxQuantity, discountedExternalId, expireDate, quantity, discount, }) { this.ico = ico; this.iva = iva; this.base = base; this.subtotal = subtotal; this.total = total; this.measurementTotal = measurementTotal; this.externalId = externalId; this.maxQuantity = maxQuantity; this.discountedExternalId = discountedExternalId; this.expireDate = expireDate; this._quantity = quantity; this.discount = discount; } get isExpired() { return !!this.expireDate && new Date() > this.expireDate; } /** * Set the price quantity until maxQuantity and returns the remaining quantity * @param quantity the quantity * @returns the remaining quantity */ set quantity(quantity) { if (quantity != undefined) { if (this.isExpired && quantity > 0) { throw new Error("Price is expired"); } if (this.maxQuantity !== undefined && this.maxQuantity !== null && this.maxQuantity !== 0 && quantity > this.maxQuantity) { throw new Error(`Quantity can't be higher than maxQuantity(${this.maxQuantity})`); } } this._quantity = quantity; } get quantity() { return this._quantity; } /** * Calculates the total based on quantity * * @returns the total * quantity */ get extendedTotal() { return this.quantity == null ? null : this.total * this.quantity; } /** * Calculates the subtotal based on quantity * * @returns the total * subtotal */ get extendedSubtotal() { return this.quantity == null ? null : this.subtotal * this.quantity; } static fromPricing(json) { const prices = []; for (const item of json) { const { values, ico, iva, scheduleEndDate, discountedMaximumQuantity, maximumQuantity, } = item; const maxHelper = new helpers_1.MaxQuantityHelper(maximumQuantity, discountedMaximumQuantity); for (const value of values) { const { externalId, discountedExternalId, startQuantity, endQuantity } = value; if (value.discountedTotal && value.discountedTotal < value.total) { const { discountedPrice: base, discountedSubtotal: subtotal, discountedTotal: total, discountedTotalPerUnit, total: regularTotal, } = value; const expireDate = scheduleEndDate ? new Date(scheduleEndDate) : undefined; const maxQuantity = expireDate && expireDate < new Date() ? 0 : maxHelper.discountedMaxQuantity(startQuantity, endQuantity); if (maxQuantity !== 0) { prices.push(new Price({ ico, iva, base, subtotal, total, externalId, maxQuantity, discountedExternalId, measurementTotal: discountedTotalPerUnit, expireDate: scheduleEndDate ? new Date(scheduleEndDate) : undefined, discount: (0, helpers_1.getDiscount)(regularTotal, total), })); } } const { price: base, subtotal, total, totalPerUnit } = value; const maxQuantity = maxHelper.maxQuantity(startQuantity, endQuantity); prices.push(new Price({ ico, iva, base, subtotal, total, measurementTotal: totalPerUnit, externalId, maxQuantity, })); } } return prices; } static fromCatalogue(json) { const prices = []; const maxHelper = new helpers_1.MaxQuantityHelper(json.maximumQuantity, json.discountedMaximumQuantity); for (const item of json.prices) { if (item.discountedTotal) { const ico = item.discountedSubtotal - item.discountedBase; const iva = (item.discountedTotal - ico) / item.discountedBase - 1; const expireDate = json.scheduleEndDate ? new Date(json.scheduleEndDate) : undefined; const maxQuantity = expireDate && expireDate < new Date() ? 0 : maxHelper.discountedMaxQuantity(item.startQuantity, item.endQuantity); if (maxQuantity !== 0) { prices.push(new Price({ ico, iva: Math.round(iva * 100) / 100, base: item.discountedBase, subtotal: item.discountedSubtotal, total: item.discountedTotal, measurementTotal: item.unitPrice, discount: item.discount, externalId: item.externalId, discountedExternalId: item.discountedExternalId, maxQuantity, expireDate, })); } } const ico = item.managerSubtotal - item.managerPrice; const iva = (item.managerTotal - ico) / item.managerPrice - 1; const maxQuantity = maxHelper.maxQuantity(item.startQuantity, item.endQuantity); prices.push(new Price({ ico, iva: Math.round(iva * 100) / 100, base: item.managerPrice, subtotal: item.managerSubtotal, total: item.managerTotal, externalId: item.externalId, measurementTotal: item.unitPrice, maxQuantity, })); } return prices; } toJSON(customerTotal) { return { customerTotal, ico: this.ico, iva: this.iva, base: this.base, subtotal: this.subtotal, managerSubtotal: this.subtotal, total: this.total, measurementTotal: this.measurementTotal, externalId: this.externalId, maxQuantity: this.maxQuantity, discountedExternalId: this.discountedExternalId ? this.discountedExternalId : null, expireDate: this.expireDate, quantity: this.quantity, discount: this.discount != null ? Math.round(this.discount * 100) / 100 : null, }; } } exports.Price = Price; //# sourceMappingURL=price.js.map