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 setRate(4); await session.post('owner', '/api/stock', { kind: 'increase', date: day, pocket: '现货', grams: '20', note: '费率对照', }); const historical = await 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', }, }); 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, 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); if (!existingId || version === 'old') { const received = (data.netSettlement / 100).toFixed(2); const fee = (expectedCents / 100).toFixed(2); assert.equal( preview, `内部信用卡手续费:¥${received} × ${expectedRate}‰ = ¥${fee},由麦麦承担。客户结算金额不变。`, ); } 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, 234); }); await section(owner, ctx, 'recoveries'); await owner .locator('[data-action=recovery-open][data-id="' + historical.id + '"]') .first() .click(); await action(owner.locator('#recovery-dialog[open]'), 'recovery-edit-open').click(); const edit = await step('修改旧费率信用卡置换并核对预览和入账费用', async () => { await field('change_reason').fill('更正旧单工费'); await field('labor_rate').fill('250'); return capture(historical.id, 4, 100); }); return { create, edit }; }