UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

108 lines (106 loc) 4.5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/modules/Summary/utils.ts var utils_exports = {}; __export(utils_exports, { calculateDeposit: () => calculateDeposit, calculatePriceDetails: () => calculatePriceDetails, calculateSubtotal: () => calculateSubtotal, calculateTaxFee: () => calculateTaxFee }); module.exports = __toCommonJS(utils_exports); var import_decimal = __toESM(require("decimal.js")); var calculatePriceDetails = (shopInfo, items) => { const subtotal = new import_decimal.default(calculateSubtotal(items)); const totalTaxFee = new import_decimal.default(calculateTaxFee(shopInfo, items)); const total = (shopInfo == null ? void 0 : shopInfo.is_price_include_tax) ? subtotal : subtotal.plus(totalTaxFee); const deposit = calculateDeposit(items); return { subtotal: subtotal.toFixed(2), total: total.toFixed(2), taxTitle: shopInfo == null ? void 0 : shopInfo.tax_title, totalTaxFee: totalTaxFee.toFixed(2), isPriceIncludeTax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax, deposit }; }; var calculateSubtotal = (items) => { if (!(items == null ? void 0 : items.length)) { return "0.00"; } const subtotal = items.reduce((sum, item) => { const cartItemTotalPrice = new import_decimal.default(item.summaryTotal || 0); return sum.plus(cartItemTotalPrice); }, new import_decimal.default(0)); return subtotal.toFixed(2); }; var calculateTaxFee = (shopInfo, items) => { if (!(items == null ? void 0 : items.length)) { return "0.00"; } const { is_price_include_tax, tax_rate } = shopInfo || {}; const totalTaxFee = items.reduce((sum, item) => { const cartItemTotalPrice = new import_decimal.default(item.summaryTotal || 0); const taxRate = new import_decimal.default(tax_rate || 0).div(100); const productTaxRate = cartItemTotalPrice.times(taxRate).times((item == null ? void 0 : item.is_charge_tax) || 0).div(taxRate.times(is_price_include_tax || 0).plus(1)); return sum.plus(productTaxRate); }, new import_decimal.default(0)); return totalTaxFee; }; var calculateDeposit = (items) => { if (!(items == null ? void 0 : items.length)) { return void 0; } const protocols = []; let hasDeposit = false; const total = items.reduce((sum, item) => { var _a, _b, _c; if (item == null ? void 0 : item.deposit) { hasDeposit = true; const cartItemTotalPrice = new import_decimal.default(((_a = item == null ? void 0 : item.deposit) == null ? void 0 : _a.total) || 0); (_c = (_b = item == null ? void 0 : item.deposit) == null ? void 0 : _b.protocols) == null ? void 0 : _c.forEach((protocol) => { if (protocols.findIndex((p) => p.id === protocol.id) === -1) { protocols.push(protocol); } }); return sum.plus(cartItemTotalPrice); } return sum; }, new import_decimal.default(0)); if (hasDeposit) { return { total: total.toFixed(2), protocols, hasDeposit }; } return void 0; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { calculateDeposit, calculatePriceDetails, calculateSubtotal, calculateTaxFee });