抽出开单表单纯数据模型 order-entry-model.js(旧界面不加载,行为不变)
把旧页面依附 DOM 的商品行同步、附加项目报价、进货成本、费用行、常用成本勾选、 日购料选择、收款人联动、合计与开单校验整理为纯函数,输出与旧页面 collectDraft 相同的请求体,供新版界面使用。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
40bff0be94
commit
bdffbcab85
3 files changed
+1927
No files matched your search
@@ -0,0 +1,304 @@
|
||||
'use strict';
|
||||
// 共用开单模型:与旧页面相同的同步、收集与校验规则(附加项目报价、进货成本、费用行、常用成本、收款人、合计)。
|
||||
const test = require('node:test'),
|
||||
assert = require('node:assert/strict');
|
||||
const M = require('../public/order-entry-model');
|
||||
|
||||
function setup(overrides = {}) {
|
||||
let n = 0;
|
||||
const uuid = () => '00000000-0000-4000-8000-' + String(++n).padStart(12, '0');
|
||||
const st = {
|
||||
today: '2026-11-20',
|
||||
user: { id: 'u1', name: '麦麦', role: 'owner', member: 'owner', operatorName: '麦麦' },
|
||||
members: [
|
||||
{ member: 'owner', label: '麦麦' },
|
||||
{ member: 'assistant', label: '娜娜' },
|
||||
{ member: 'assistant2', label: '小昊' },
|
||||
],
|
||||
accounts: ['麦麦微信', '麦麦信用卡', '助理微信', '助理2微信'],
|
||||
shopSettings: {},
|
||||
...overrides,
|
||||
};
|
||||
const ctx = { st, uuid };
|
||||
return { st, ctx, uuid };
|
||||
}
|
||||
const sync = (form, ctx) => M.syncForm(form, ctx);
|
||||
function editLine(form, index, patch) {
|
||||
return M.updateLine(form, index, { ...form.lines[index], ...patch });
|
||||
}
|
||||
function editPurchase(form, index, patch) {
|
||||
const line = form.lines[index];
|
||||
return M.updateLine(form, index, { ...line, purchase: { ...line.purchase, ...patch } });
|
||||
}
|
||||
|
||||
test('新单默认:按克计价、现货货盘、整单取整到元、进货料价按克、付款人为本人', () => {
|
||||
const { ctx } = setup();
|
||||
const form = M.newForm(ctx);
|
||||
assert.equal(form.rounding_mode, 'yuan');
|
||||
assert.equal(form.feeCostMode, 'checkboxes');
|
||||
const line = form.lines[0];
|
||||
assert.deepEqual(
|
||||
[line.nameType, line.pricingMode, line.unit, line.pocket, line.pieces],
|
||||
['足金999', 'auto', 'g', '现货', '1'],
|
||||
);
|
||||
assert.equal(line.purchase.materialMode, 'gram');
|
||||
assert.equal(line.purchase.laborMode, 'total');
|
||||
assert.equal(line.purchase.materialPayer, 'owner');
|
||||
assert.equal(form.salesperson, '麦麦');
|
||||
assert.equal(M.readPaymentAccount(form), '麦麦微信');
|
||||
});
|
||||
|
||||
test('克工费:销售额、按克料费与合计;整单四舍五入(x.49 舍、x.50 入)', () => {
|
||||
const { ctx } = setup();
|
||||
let form = M.newForm(ctx);
|
||||
form = editLine(form, 0, { quantity: '2.5', goldPrice: '950', laborRate: '30' });
|
||||
form = editPurchase(form, 0, { materialRate: '880', labor: '25' });
|
||||
let r = sync(form, ctx);
|
||||
assert.equal(r.draft.lines[0].sale, '2450.00');
|
||||
assert.deepEqual(r.draft.lines[0].costParts, { material: '2200.00', labor: '25', accessories: '0', other: '0' });
|
||||
assert.deepEqual(r.draft.lines[0].costMaterial, { mode: 'gram', rate: '880' });
|
||||
assert.equal(r.summary.totalText, '¥2,450');
|
||||
r = sync({ ...r.form, discount: '0.51' }, ctx);
|
||||
assert.equal(r.summary.total, 244900);
|
||||
assert.match(r.summary.roundingHint, /差额 -0\.49元|2,449/);
|
||||
r = sync({ ...r.form, discount: '0.50' }, ctx);
|
||||
assert.equal(r.summary.total, 245000);
|
||||
});
|
||||
|
||||
test('件工费与直接金额', () => {
|
||||
const { ctx } = setup();
|
||||
let form = M.newForm(ctx);
|
||||
form = editLine(form, 0, { quantity: '3', goldPrice: '900', laborMode: 'piece', laborRate: '50', pieces: '3' });
|
||||
let r = sync(form, ctx);
|
||||
assert.equal(r.draft.lines[0].sale, '2850.00');
|
||||
form = M.updateLine(r.form, 0, M.setLineField(r.form.lines[0], 'unit', '件'));
|
||||
assert.equal(form.lines[0].pricingMode, 'manual');
|
||||
assert.equal(form.lines[0].pocket, '');
|
||||
form = editLine(form, 0, { quantity: '2', baseSale: '388' });
|
||||
r = sync(form, ctx);
|
||||
assert.equal(r.draft.lines[0].sale, '388.00');
|
||||
assert.equal(r.form.lines[0].purchase.materialMode, 'total', '按件时料费改为总额');
|
||||
});
|
||||
|
||||
test('附加项目:新选择预填报价,切换类目记住各自金额,手填0与清空不被覆盖', () => {
|
||||
const { uuid } = setup();
|
||||
let c = M.newCharge({}, uuid);
|
||||
assert.deepEqual([c.type, c.amount], ['编绳', '55']);
|
||||
c = M.setChargeAmount(c, '0');
|
||||
c = M.changeChargeType(c, '证书');
|
||||
assert.equal(c.amount, '10');
|
||||
c = M.setChargeAmount(c, '');
|
||||
c = M.changeChargeType(c, '编绳');
|
||||
assert.equal(c.amount, '0', '回到编绳仍是手填的0');
|
||||
c = M.changeChargeType(c, '证书');
|
||||
assert.equal(c.amount, '', '回到证书仍是清空');
|
||||
c = M.changeChargeType(c, '附加费');
|
||||
assert.equal(c.amount, '');
|
||||
assert.equal(M.newCharge({ name: '包装', amount: null }, uuid).amount, '');
|
||||
});
|
||||
|
||||
test('附加项目带入成本行:证书按店铺默认、编绳待填,快递与包装改由常用成本勾选;手改后标记已改', () => {
|
||||
const { ctx, uuid } = setup({ shopSettings: { feeDefaults: { certificate: '5' } } });
|
||||
let form = M.newForm(ctx);
|
||||
form = editLine(form, 0, { quantity: '1', goldPrice: '900', laborRate: '20' });
|
||||
for (let i = 0; i < 4; i++) form = M.addCharge(form, 0, uuid);
|
||||
const types = ['编绳', '证书', '包装', '快递'];
|
||||
form = M.updateLine(form, 0, {
|
||||
...form.lines[0],
|
||||
charges: form.lines[0].charges.map((c, i) => M.changeChargeType(c, types[i])),
|
||||
});
|
||||
let r = sync(form, ctx);
|
||||
assert.deepEqual(
|
||||
r.form.feeRows.map(row => [row.note, row.amount, row.edited]),
|
||||
[
|
||||
['编绳', '', false],
|
||||
['证书', '5', false],
|
||||
],
|
||||
);
|
||||
form = M.editFeeRow(r.form, 0, 'amount', '6');
|
||||
form = M.togglePreset(form, 'jd', true, ctx.st, uuid);
|
||||
form = M.togglePreset(form, 'yt', true, ctx.st, uuid);
|
||||
r = sync(form, ctx);
|
||||
assert.equal(r.form.presets.checked.jd, false, '快递二选一');
|
||||
assert.deepEqual(
|
||||
r.draft.fee_items.map(f => [f.key.split(':')[0], f.note, f.amount, f.payer, f.edited]),
|
||||
[
|
||||
['charge', '编绳', '6', 'owner', true],
|
||||
['charge', '证书', '5', 'owner', false],
|
||||
['shipping', '圆通快递', '6', 'owner', true],
|
||||
],
|
||||
);
|
||||
assert.equal(r.draft.lines[0].sale, '1003.00');
|
||||
});
|
||||
|
||||
test('移除附加项目成本行后不再自动生成,恢复后重新带入', () => {
|
||||
const { ctx, uuid } = setup();
|
||||
let form = M.addCharge(M.newForm(ctx), 0, uuid);
|
||||
let r = sync(form, ctx);
|
||||
assert.equal(r.form.feeRows.length, 1);
|
||||
r = sync(M.removeFeeRow(r.form, 0), ctx);
|
||||
assert.equal(r.form.feeRows.length, 0);
|
||||
assert.equal(r.draft.lines[0].charges[0].costRemoved, true);
|
||||
r = sync(M.restoreChargeCost(r.form, 0, 0), ctx);
|
||||
assert.equal(r.form.feeRows.length, 1);
|
||||
});
|
||||
|
||||
test('盒子按数量计费,数量无效时金额留空', () => {
|
||||
const { ctx, uuid } = setup();
|
||||
let form = M.togglePreset(M.newForm(ctx), 'box', true, ctx.st, uuid);
|
||||
form = M.setPresetQuantity(form, 'box', '3');
|
||||
let r = sync(form, ctx);
|
||||
assert.deepEqual(r.draft.fee_items[0], {
|
||||
key: 'box',
|
||||
note: '盒子(3个 × 3元/个)',
|
||||
amount: '9',
|
||||
payer: 'owner',
|
||||
status: 'estimated',
|
||||
edited: true,
|
||||
});
|
||||
r = sync(M.setPresetQuantity(r.form, 'box', '0'), ctx);
|
||||
assert.equal(r.draft.fee_items[0].amount, '');
|
||||
assert.throws(() => M.validateFormFields(r.form, ctx.st), /盒子数量/);
|
||||
});
|
||||
|
||||
test('进货成本:按克与总额分别记住,按件时不把每克料价当作总额', () => {
|
||||
const { ctx } = setup();
|
||||
let form = editLine(M.newForm(ctx), 0, { quantity: '2', goldPrice: '900', laborRate: '10' });
|
||||
form = editPurchase(form, 0, { materialMode: 'total', material: '1700' });
|
||||
let r = sync(form, ctx);
|
||||
r = sync(editPurchase(r.form, 0, { materialMode: 'gram', materialRate: '860' }), ctx);
|
||||
assert.equal(r.form.lines[0].purchase.material, '1720.00');
|
||||
r = sync(editPurchase(r.form, 0, { materialMode: 'total' }), ctx);
|
||||
assert.equal(r.form.lines[0].purchase.material, '1700', '切回总额恢复原总额');
|
||||
r = sync(editPurchase(r.form, 0, { laborMode: 'gram', laborRate: '12.5' }), ctx);
|
||||
assert.equal(r.draft.lines[0].costParts.labor, '25.00');
|
||||
assert.deepEqual(r.draft.lines[0].costLabor, { mode: 'gram', rate: '12.5' });
|
||||
});
|
||||
|
||||
test('店主改订单归属:收款人与未改过的费用付款人跟随;信用卡固定由店主收款并保留原收款人', () => {
|
||||
const { ctx, uuid } = setup();
|
||||
let r = sync(M.addCharge(M.newForm(ctx), 0, uuid), ctx);
|
||||
r = sync(M.changeMember(r.form, 'assistant', ctx.st), ctx);
|
||||
assert.equal(r.draft.member, 'assistant');
|
||||
assert.equal(r.draft.payment_account, '助理微信');
|
||||
assert.equal(r.form.feeRows[0].payer, 'assistant');
|
||||
r = sync({ ...r.form, payment: { ...r.form.payment, method: '信用卡' } }, ctx);
|
||||
assert.equal(r.draft.payment_account, '麦麦信用卡');
|
||||
r = sync({ ...r.form, payment: { ...r.form.payment, method: '微信' } }, ctx);
|
||||
assert.equal(r.draft.payment_account, '助理微信');
|
||||
});
|
||||
|
||||
test('信用卡手续费按成交合计 6‰ 预览', () => {
|
||||
const { ctx } = setup();
|
||||
let form = editLine(M.newForm(ctx), 0, { pricingMode: 'manual', baseSale: '1000' });
|
||||
form = { ...form, payment: { ...form.payment, method: '信用卡' } };
|
||||
const r = sync(form, ctx);
|
||||
assert.equal(r.summary.card.amount, 600);
|
||||
assert.match(r.summary.feesText, /¥6\.00/);
|
||||
});
|
||||
|
||||
test('校验:客户、开单人、内部备注至少3个字符(#计入、空白不计),客户类型必选', () => {
|
||||
const { ctx, st } = setup();
|
||||
const base = { customer: '甲', salesperson: '麦麦', internal_note: '#补货' };
|
||||
assert.equal(M.validateOrderIdentity(base).internal_note, '#补货');
|
||||
assert.throws(() => M.validateOrderIdentity({ ...base, customer: ' ' }), /客户称呼/);
|
||||
assert.throws(() => M.validateOrderIdentity({ ...base, salesperson: '' }), /开单人/);
|
||||
assert.throws(() => M.validateOrderIdentity({ ...base, internal_note: '# 补' }), /至少填写3个字符/);
|
||||
const form = M.newForm(ctx);
|
||||
assert.throws(() => M.validateFormFields(form, st), /客户类型/);
|
||||
const r = sync(form, ctx);
|
||||
assert.throws(() => M.validateIssueDraft({ ...r.draft, ...base }, st), /开单前请补齐:商品 1的克重/数量/);
|
||||
});
|
||||
|
||||
test('旧草稿订单转为表单:保留原单号、取整方式与付款人,不显示常用成本勾选', () => {
|
||||
const { ctx } = setup();
|
||||
const order = {
|
||||
id: '11111111-2222-4333-8444-555555555555',
|
||||
revision: 1,
|
||||
status: 'draft',
|
||||
date: '2026-11-20',
|
||||
customer: '草稿客户',
|
||||
salesperson: '麦麦',
|
||||
paper_number: '20261120001',
|
||||
paper_date: '2026-11-20',
|
||||
rounding_mode: 'cent',
|
||||
discount: 0,
|
||||
payment_account: '助理微信',
|
||||
member: 'owner',
|
||||
customer_type: 'new',
|
||||
fee_items: [],
|
||||
lines: [
|
||||
{
|
||||
name: '足金999',
|
||||
unit: 'g',
|
||||
quantity: 1500,
|
||||
pocket: '现货',
|
||||
payer: 'owner',
|
||||
pricingMode: 'auto',
|
||||
goldPrice: 90000,
|
||||
laborRate: 2000,
|
||||
laborMode: 'gram',
|
||||
pieces: 1,
|
||||
sale: 138000,
|
||||
baseSale: 138000,
|
||||
charges: [],
|
||||
cost: null,
|
||||
costParts: { material: 120000, labor: null, accessories: 0, other: 0 },
|
||||
costPayers: { material: 'owner', labor: 'assistant' },
|
||||
costMaterial: { mode: 'total' },
|
||||
costLabor: { mode: 'total' },
|
||||
},
|
||||
],
|
||||
};
|
||||
const form = M.newForm(ctx, M.draftInputs(order));
|
||||
assert.equal(form.presets, null);
|
||||
const r = sync(form, ctx);
|
||||
assert.equal(r.draft.id, order.id);
|
||||
assert.equal(r.draft.revision, 1);
|
||||
assert.equal(r.draft.rounding_mode, 'cent');
|
||||
assert.equal(r.draft.fee_cost_mode, 'legacy');
|
||||
assert.equal(r.draft.lines[0].quantity, '1.500');
|
||||
assert.equal(r.draft.lines[0].costParts.material, '1200.00');
|
||||
assert.deepEqual(r.draft.lines[0].costPayers, { material: 'owner', labor: 'assistant' });
|
||||
assert.equal(r.summary.totalText, '¥1,380.00');
|
||||
});
|
||||
|
||||
test('公式预填:先填空白行再追加,附加费带入', () => {
|
||||
const { ctx } = setup();
|
||||
const form = M.newForm(ctx);
|
||||
const lines = [
|
||||
{
|
||||
unit: 'g',
|
||||
pricingMode: 'auto',
|
||||
quantity: '0.79',
|
||||
goldPrice: '822',
|
||||
laborMode: 'piece',
|
||||
laborRate: '120',
|
||||
pieces: '1',
|
||||
charges: [],
|
||||
},
|
||||
{
|
||||
unit: 'g',
|
||||
pricingMode: 'auto',
|
||||
quantity: '0.58',
|
||||
goldPrice: '822',
|
||||
laborMode: 'gram',
|
||||
laborRate: '45',
|
||||
pieces: '1',
|
||||
charges: [{ name: '附加费', amount: '55' }],
|
||||
},
|
||||
];
|
||||
const { form: next, filled } = M.applyFormulaLines(form, lines, ctx);
|
||||
assert.deepEqual(filled, [1, 2]);
|
||||
const r = sync(next, ctx);
|
||||
assert.deepEqual(
|
||||
r.draft.lines.map(l => l.sale),
|
||||
['769.38', '557.86'],
|
||||
);
|
||||
});
|
||||
|
||||
test('内部备注快捷标签:按 # 后的词筛选,插入时替换正在输入的标签', () => {
|
||||
assert.deepEqual(M.filterNoteTags(['#补货', '#客户自提'], '已联系 #补'), ['#补货']);
|
||||
assert.equal(M.insertNoteTag('已联系 #补', '#补货'), '已联系 #补货 ');
|
||||
assert.equal(M.insertNoteTag('已联系', '#补货'), '已联系 #补货 ');
|
||||
});
|
||||
Reference in new issue
Block a user