UNPKG

@pisell/pisellos

Version:

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

97 lines (92 loc) 3.71 kB
import Decimal from 'decimal.js'; export var calculatePriceDetails = function calculatePriceDetails(shopInfo, items) { var subtotal = new Decimal(calculateSubtotal(items)); var totalTaxFee = new Decimal(calculateTaxFee(shopInfo, items)); // 计算总价 var total = shopInfo !== null && shopInfo !== void 0 && shopInfo.is_price_include_tax ? subtotal : subtotal.plus(totalTaxFee); // 计算定金 var deposit = calculateDeposit(items); return { subtotal: subtotal.toFixed(2), total: total.toFixed(2), taxTitle: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_title, totalTaxFee: totalTaxFee.toFixed(2), isPriceIncludeTax: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax, deposit: deposit }; }; /** * 计算商品小计(不含其他费用) * @param items - 购物车商品数组 * @returns 商品总价字符串,保留2位小数 */ export var calculateSubtotal = function calculateSubtotal(items) { if (!(items !== null && items !== void 0 && items.length)) { return '0.00'; } var subtotal = items.reduce(function (sum, item) { var cartItemTotalPrice = new Decimal(item.summaryTotal || 0); return sum.plus(cartItemTotalPrice); }, new Decimal(0)); return subtotal.toFixed(2); }; /** * @title: 单个商品的税费 * @description: * 单个商品的税费 = 商品销售单价(折扣后) * 税率 * is_charge_tax /( 1+ 税率 * is_price_include_tax) * $taxFee = $price * $taxRate * $isChargeTax / (1 + $taxRate * $isPriceIncludeTax); * @return {*} * @Author: xiangfeng.xue */ export var calculateTaxFee = function calculateTaxFee(shopInfo, items) { if (!(items !== null && items !== void 0 && items.length)) { return '0.00'; } var _ref = shopInfo || {}, is_price_include_tax = _ref.is_price_include_tax, tax_rate = _ref.tax_rate; var totalTaxFee = items.reduce(function (sum, item) { var cartItemTotalPrice = new Decimal(item.summaryTotal || 0); var taxRate = new Decimal(tax_rate || 0).div(100); var productTaxRate = cartItemTotalPrice.times(taxRate).times((item === null || item === void 0 ? void 0 : item.is_charge_tax) || 0).div(taxRate.times(is_price_include_tax || 0).plus(1)); return sum.plus(productTaxRate); }, new Decimal(0)); return totalTaxFee; }; /** * @title: 计算定金 * @param items - 购物车商品数组 * @returns 定金字符串,保留2位小数 */ export var calculateDeposit = function calculateDeposit(items) { if (!(items !== null && items !== void 0 && items.length)) { return undefined; } var protocols = []; var hasDeposit = false; var total = items.reduce(function (sum, item) { if (item !== null && item !== void 0 && item.deposit) { var _item$deposit, _item$deposit2; hasDeposit = true; var cartItemTotalPrice = new Decimal((item === null || item === void 0 || (_item$deposit = item.deposit) === null || _item$deposit === void 0 ? void 0 : _item$deposit.total) || 0); // 将协议数据去重合并 item === null || item === void 0 || (_item$deposit2 = item.deposit) === null || _item$deposit2 === void 0 || (_item$deposit2 = _item$deposit2.protocols) === null || _item$deposit2 === void 0 || _item$deposit2.forEach(function (protocol) { if (protocols.findIndex(function (p) { return p.id === protocol.id; }) === -1) { protocols.push(protocol); } }); return sum.plus(cartItemTotalPrice); } return sum; }, new Decimal(0)); if (hasDeposit) { return { total: total.toFixed(2), protocols: protocols, hasDeposit: hasDeposit }; } return undefined; };