'use strict'; // 只接受约定的销售公式,不执行输入代码。金额复用开单的整数计算。 (function (root) { /** @type {Pick} */ const Calc = typeof module === 'object' && module.exports ? require('./calculations') : root.LedgerCalc; /** @param {string} value */ const decimal = value => { const [whole, fraction = ''] = value.split('.'); return ( (whole.replace(/^0+(?=\d)/, '') || '0') + (fraction.replace(/0+$/, '') ? '.' + fraction.replace(/0+$/, '') : '') ); }; /** @param {string} input */ const normalize = input => input .normalize('NFKC') .replace(/\\\*/g, '*') .replace(/[×✕xX]/g, '*') .replace(/\s/g, '') .replace(/\\+$/, '') .replace(/元$/, ''); const number = '(\\d+(?:\\.\\d+)?)', extras = '((?:\\+\\d+(?:\\.\\d+)?)*)', ending = '(?:=' + number + ')?$', weight = number + '(?:克|g)?\\*'; const gramPattern = new RegExp('^' + weight + '\\(' + number + '\\+' + number + '\\)' + extras + ending, 'i'); const piecePattern = new RegExp('^' + weight + number + '\\+' + number + extras + ending, 'i'); /** @param {string} value */ const money = value => Calc.cents(decimal(value), '公式金额'); /** @param {string} input @param {{roundingMode?: 'cent'|'yuan'}} [options] */ function parse(input, { roundingMode = 'yuan' } = {}) { if (typeof input !== 'string' || input.length > 300) throw new Error('每行请填写300字以内的销售公式'); const formula = normalize(input), gram = gramPattern.exec(formula), match = gram || piecePattern.exec(formula); if (!match) throw new Error('请填写“克重×(金价+克工费)+附加费”或“克重×金价+件工费”;可在末尾写=金额'); const [, weight, gold, labor, extra, stated] = match; const quantities = extra ? extra.slice(1).split('+') : []; if (quantities.length > 100) throw new Error('每件商品最多100项附加费'); /** @type {import('../web/src/shared/orderEntryTypes').FormulaLine} */ const line = { unit: 'g', pricingMode: 'auto', quantity: decimal(weight), goldPrice: decimal(gold), laborMode: gram ? 'gram' : 'piece', laborRate: decimal(labor), pieces: '1', charges: quantities.map(value => ({ name: '附加费', amount: decimal(value) })), }; const sale = Calc.sale(line), total = Calc.orderTotal([sale.sale], 0, roundingMode), statedTotal = stated === undefined ? null : money(stated); return { formula, line, subtotal: sale.sale, total: total.total, roundingMode, roundingAdjustment: total.rounding_adjustment, statedTotal, mismatch: statedTotal !== null && statedTotal !== total.total, }; } /** @param {string} input @param {{roundingMode?: 'cent'|'yuan'}} [options] */ function parseBatch(input, { roundingMode = 'yuan' } = {}) { if (typeof input !== 'string' || input.length > 18000) throw new Error('请填写18000字以内的公式,每件商品一行'); const rows = input .split(/\r\n?|\n/) .map(value => value.trim()) .filter(Boolean); if (!rows.length || rows.length > 61) throw new Error('请填写1~60件商品公式,可在最后另写一行合计'); /** @type {ReturnType[]} */ const items = [], warnings = []; let summary = null; for (const [index, raw] of rows.entries()) { const normalized = normalize(raw).replace(/^(?:合计|总价|总计|总金额):?/, ''); const sumMatch = normalized.match(/^(\d+(?:\.\d+)?(?:\+\d+(?:\.\d+)?)+)(?:=(\d+(?:\.\d+)?))?$/); if (sumMatch) { if (!items.length || index !== rows.length - 1) throw new Error('合计公式只能放在全部商品之后的最后一行'); const amounts = sumMatch[1].split('+').map(money), sum = amounts.reduce((a, b) => a + b, 0); if (!Number.isSafeInteger(sum) || sum > 100000000000) throw new Error('合计金额超出支持范围'); summary = { amounts, sum, statedTotal: sumMatch[2] === undefined ? sum : money(sumMatch[2]) }; if ( amounts.length !== items.length || amounts.some((value, i) => value !== (items[i]?.statedTotal ?? items[i]?.total)) ) warnings.push('合计行的加数与各商品金额不对应,请核对是否漏了一件或重复相加。'); if (summary.statedTotal !== sum) warnings.push('合计行左右金额不相等,请核对。'); } else { if (items.length >= 60) throw new Error('每单最多60件商品'); let item; try { item = parse(raw, { roundingMode }); } catch (err) { throw new Error('第' + (index + 1) + '行:' + (err instanceof Error ? err.message : String(err))); } items.push(item); if (item.mismatch && item.statedTotal !== null) warnings.push( '商品 ' + items.length + ' 写的是 ' + (item.statedTotal / 100).toFixed(2) + '元,按当前取整规则计算为 ' + (item.total / 100).toFixed(2) + '元。', ); } } const total = Calc.orderTotal( items.map(item => item.subtotal), 0, roundingMode, ); if (summary && summary.statedTotal !== total.total) warnings.push( '公式合计为 ' + (summary.statedTotal / 100).toFixed(2) + '元;系统先合计各件精确金额,再整单取整,结果为 ' + (total.total / 100).toFixed(2) + '元。', ); return { items, lines: items.map(item => item.line), summary, subtotal: total.subtotal, total: total.total, roundingMode, roundingAdjustment: total.rounding_adjustment, warnings, mismatch: warnings.length > 0, }; } const api = { parse, parseBatch }; if (typeof module === 'object' && module.exports) module.exports = api; else root.OrderFormula = api; })(typeof globalThis === 'object' ? globalThis : /** @type {typeof globalThis} */ (/** @type {unknown} */ (this)));