Files
weiguang-jewelry-ledger/新账系统/e2e/writes/historical-cardfee.mjs
T

150 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict';
import { DatabaseSync } from 'node:sqlite';
import { action, section, postFrom } from './operations-helpers.mjs';
// 单独与固定旧版对照。店铺调价后,改单的手续费应与原单的费率快照相符。
export default async function historicalCardFee(ctx) {
const { day, session, dbPath, version, step } = ctx;
async function setRate(rate) {
const { revision } = await session.get('owner', '/api/shop-settings');
await session.post('owner', '/api/shop-settings', { revision, creditCardRatePermille: rate });
}
await session.post('owner', '/api/stock', {
kind: 'increase',
date: day,
pocket: '现货',
grams: '20',
note: '费率对照',
});
async function seedHistorical(rate, customer) {
await setRate(rate);
return session.post('owner', '/api/recoveries', {
date: day,
mode: 'exchange',
customer,
customer_type: 'new',
salesperson: '麦麦',
member: 'owner',
payer: 'owner',
payment_account: '麦麦信用卡',
verified: true,
buy_rate: '0',
old_lines: [{ name: '旧料', incoming_weight: '1', melted_weight: '1', purity: '100' }],
exchange: {
name: '足金999',
quantity: '1',
pocket: '现货',
gold_price: '0',
labor_rate: '200',
labor_mode: 'total',
depreciation_rate: '0',
cost_labor: { mode: 'total', amount: '15' },
cost_payer: 'owner',
discount: '0',
rounding_mode: 'cent',
},
});
}
const historical = await seedHistorical(4, '历史卡费对照');
const noFee = await seedHistorical(0, '历史免手续费对照');
await setRate(9);
assert.deepEqual(session.problems, []);
const owner = await ctx.pageFor('owner');
const form = owner.locator('#recovery-form');
const field = name => form.locator('[name="' + name + '"]');
async function capture(existingId, expectedRate, expectedReceived, expectedCents) {
await field('verified').check();
await owner
.locator('#recovery-quote-status')
.getByText(/结算已核对/)
.waitFor();
const notice = version === 'old' ? owner.locator('#recovery-card-fee') : form.getByText(/^内部信用卡手续费/);
const preview = await notice.innerText();
const endpoint = existingId ? '/api/recoveries/' + existingId + '/update' : '/api/recoveries';
const [request, result] = await Promise.all([
owner.waitForRequest(r => new URL(r.url()).pathname === endpoint && r.method() === 'POST'),
postFrom(owner, endpoint, () => form.locator('button[type=submit]').click()),
]);
const input = request.postDataJSON();
assert.equal(input.payment_account, '麦麦信用卡');
assert.equal(Object.hasOwn(input, 'credit_card_rate_permille'), false);
const db = new DatabaseSync(dbPath, { readOnly: true });
try {
const record = db.prepare('SELECT data, order_id FROM recoveries WHERE id=?').get(existingId || result.id);
const data = JSON.parse(record.data);
const order = db.prepare('SELECT fee_items FROM orders WHERE id=?').get(record.order_id);
const fees = JSON.parse(order.fee_items).filter(
row => row.system === 'credit_card' || row.key?.startsWith('card_fee:'),
);
assert.equal(data.credit_card_rate_permille, expectedRate);
assert.equal(data.cardFee, expectedCents);
assert.equal(data.netSettlement, expectedReceived);
const receipts = db
.prepare(
"SELECT SUM(amount) AS total FROM payments WHERE order_id=? AND kind='receipt' AND account='麦麦信用卡'",
)
.get(record.order_id);
assert.equal(receipts.total, expectedReceived);
const expenses = db
.prepare('SELECT COUNT(*) AS count, COALESCE(SUM(amount), 0) AS total FROM expenses WHERE order_id=?')
.get(record.order_id);
assert.equal(expenses.total, expectedCents);
if (expectedRate === 0) assert.equal(expenses.count, 0, '历史 0‰ 单应保留费率快照且不产生费用流水');
assert.deepEqual(
fees.map(row => [row.rate_permille, row.amount]),
[[expectedRate, expectedCents]],
);
return {
preview,
rate: data.credit_card_rate_permille,
received: data.netSettlement,
fee: data.cardFee,
orderFee: fees[0].amount,
};
} finally {
db.close();
}
}
await section(owner, ctx, 'recoveries');
await owner.locator('[data-action=recovery-mode][data-mode=exchange]').click();
const create = await step('现行费率新建信用卡置换并核对净收款和入账费用', async () => {
await field('customer').fill('当日卡费对照');
await form.locator('[name=customer_type][value=new]').check();
await field('incoming_weight').fill('1');
await field('new_quantity').fill('1');
await field('labor_mode').selectOption('total');
await field('labor_rate').fill('250');
await field('cost_labor_value').fill('15');
await field('payment_method').selectOption('信用卡');
return capture(null, 9, 26000, 234);
});
async function editHistorical(id, rate, cents, label) {
await section(owner, ctx, 'recoveries');
await owner
.locator('[data-action=recovery-open][data-id="' + id + '"]')
.first()
.click();
await action(owner.locator('#recovery-dialog[open]'), 'recovery-edit-open').click();
return step(label, async () => {
await field('change_reason').fill('更正旧单工费');
await field('labor_rate').fill('250');
return capture(id, rate, 25000, cents);
});
}
const edit = await editHistorical(historical.id, 4, 100, '修改旧费率信用卡置换并核对预览和入账费用');
const editZero = await editHistorical(noFee.id, 0, 0, '修改零费率信用卡置换并核对免手续费及净收款');
// 三笔先各自入账,再一起验证三张表单的预览,使 4‰ 和 0‰ 回归能在一次运行中分别复现。
const records = { create, edit, editZero };
const rows = Object.values(records);
assert.deepEqual(
rows.map(record => record.preview),
rows.map(
({ rate, received, fee }) =>
`内部信用卡手续费:¥${(received / 100).toFixed(2)} × ${rate}‰ = ¥${(fee / 100).toFixed(2)},由麦麦承担。客户结算金额不变。`,
),
'每笔浏览器预览必须与已核实的实收、费率快照及关联手续费相符',
);
return records;
}