UNPKG

@pisell/pisellos

Version:

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

153 lines (145 loc) 7.27 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } import Decimal from 'decimal.js'; export var calculatePriceDetails = function calculatePriceDetails(shopInfo, items) { var subtotal = new Decimal(calculateSubtotal(items)); var subOriginTotal = new Decimal(calculateOriginSubtotal(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 originTotal = shopInfo !== null && shopInfo !== void 0 && shopInfo.is_price_include_tax ? subOriginTotal : subOriginTotal.plus(totalTaxFee); // 计算定金 var deposit = calculateDeposit(items); return { subtotal: subtotal.toFixed(2), total: total.toFixed(2), originTotal: originTotal.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 item */ export var getBundleDiscountList = function getBundleDiscountList(bundle) { if (!bundle) { return []; } var discountList = []; bundle.forEach(function (d) { if (d.discount_list && Array.isArray(d.discount_list)) { discountList.push.apply(discountList, _toConsumableArray(d.discount_list.filter(function (item) { return !item.id; }))); } }); return discountList; }; var getProductDiscountProductDiscountDifference = function getProductDiscountProductDiscountDifference(item) { var _item$_origin, _item$_origin2; var mainDiscountList = ((_item$_origin = item._origin) === null || _item$_origin === void 0 || (_item$_origin = _item$_origin.product) === null || _item$_origin === void 0 ? void 0 : _item$_origin.discount_list) || []; var bundleDiscountList = getBundleDiscountList(((_item$_origin2 = item._origin) === null || _item$_origin2 === void 0 || (_item$_origin2 = _item$_origin2.product) === null || _item$_origin2 === void 0 ? void 0 : _item$_origin2.product_bundle) || []); var discountList = [].concat(_toConsumableArray(mainDiscountList), _toConsumableArray(bundleDiscountList)); return discountList.reduce(function (pre, cur) { return pre + (cur.metadata.product_discount_difference || 0); }, 0); }; /** * 计算商品小计(不含其他费用) * @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); var productDiscountProductDiscountDifference = getProductDiscountProductDiscountDifference(item); return sum.plus(cartItemTotalPrice).sub(productDiscountProductDiscountDifference); }, new Decimal(0)); return subtotal.toFixed(2); }; /** * 计算商品小计(不含其他费用 不包含折扣卡商品券抵扣金额) * @param items - 购物车商品数组 * @returns 商品总价字符串,保留2位小数 */ export var calculateOriginSubtotal = function calculateOriginSubtotal(items) { if (!(items !== null && items !== void 0 && items.length)) { return '0.00'; } var subtotal = items.reduce(function (sum, item) { var cartItemTotalPrice = new Decimal(item.summaryOriginTotal || 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; };