import assert from 'node:assert/strict'; import { section } from './operations-helpers.mjs'; export default async function recoveryQuote(ctx) { const { day, session } = ctx; await session.post('owner', '/api/recoveries', { date: day, mode: 'direct', customer: '个人回收料来源', member: 'owner', salesperson: '麦麦', old_lines: [{ name: '旧料', incoming_weight: '0.400', melted_weight: '0.400', purity: '100' }], buy_rate: '800', payer: 'owner', payment_account: '麦麦微信', verified: true, }); await session.post('owner', '/api/material-purchases', { date: day, grams: '8', total: '7200', payer: 'owner', note: '公共料来源', }); assert.deepEqual(session.problems, []); const owner = await ctx.pageFor('owner'); await section(owner, ctx, 'recoveries'); await owner.locator('[data-action=recovery-mode][data-mode=exchange]').click(); const form = owner.locator('#recovery-form'); const field = name => form.locator('[name="' + name + '"]'); let original; function captureQuote(request) { if (new URL(request.url()).pathname === '/api/recoveries/quote' && request.method() === 'POST') original = request.postDataJSON(); } owner.on('request', captureQuote); await ctx.step('置换试算列出个人和公共料各自的用量、成本与前后余额', async () => { await field('customer').fill('核对两种扣料来源'); await form.locator('[name=customer_type][value=new]').check(); await field('incoming_weight').fill('2'); await field('new_quantity').fill('3'); await field('gold_price').fill('920'); await field('cost_labor_value').fill('15'); await field('labor_rate').fill('20'); await field('material_source').selectOption('pool'); await field('material_pool_id').selectOption(day); await owner .locator('#recovery-quote-status') .getByText(/结算已核对/) .waitFor(); const shown = await owner.locator('#recovery-material-note').innerText(); const expected = [ '麦麦个人回收料 0.400g · ¥320.00(加权约 ¥800.00/克)', '公共料 0.600g · ¥540.00(加权约 ¥900.00/克)', '本次前可用:个人 0.400g,公共 8.000g', '本次后余:个人回收料 0.000g,公共料 7.400g', ]; assert.deepEqual( expected.filter(part => !shown.includes(part)), [], '实际置换料池试算:' + shown, ); }); owner.off('request', captureQuote); const status = owner.locator('#recovery-quote-status'); const note = owner.locator('#recovery-material-note'); await ctx.step('余额读取失败时继续展示扣料来源和已核对的结算', async () => { const balanceUrl = ctx.base + '/api/material-pools/quote'; const unavailable = route => route.fulfill({ status: 503, json: { error: '对照余额暂不可用' } }); await owner.route(balanceUrl, unavailable); try { await field('new_quantity').fill('3.200'); await note.getByText(/个人与公共余量暂未读到,请刷新核对/).waitFor(); await status.getByText(/结算已核对/).waitFor(); const shown = await note.innerText(); assert.ok(shown.includes('个人回收料 0.400g · ¥320.00') && shown.includes('公共料 0.800g · ¥720.00'), shown); } finally { await owner.unroute(balanceUrl, unavailable); } }); await ctx.step('分来源成本合计与服务端总额不符时提示重新核对', async () => { const quoteUrl = ctx.base + '/api/recoveries/quote'; const inconsistent = async route => { const res = await route.fetch(); try { const reply = await res.json(); assert.ok(Number.isSafeInteger(reply.materialCost)); await route.fulfill({ json: { ...reply, materialCost: reply.materialCost + 1 } }); } finally { await res.dispose(); } }; await owner.route(quoteUrl, inconsistent); try { await field('new_quantity').fill('3.500'); await status.getByText(/置换用料来源与当前归属不一致,请重新核对/).waitFor(); } finally { await owner.unroute(quoteUrl, inconsistent); } }); await session.post('owner', '/api/stock', { date: day, kind: 'increase', grams: '3', pocket: '现货', note: '原置换的出货库存', }); const historical = await session.post('owner', '/api/recoveries', { ...original, verified: true }); assert.deepEqual(session.problems, []); await section(owner, ctx, 'recoveries'); await owner .locator('[data-action=recovery-open][data-id="' + historical.id + '"]') .first() .click(); await owner.locator('#recovery-dialog[open] [data-action=recovery-edit-open]').click(); await ctx.step('修改已有置换单时排除原单耗用并列出扣料前后结余', async () => { await field('change_reason').fill('复核原料池'); await field('new_quantity').fill('3.200'); await note.getByText(/原单耗用已排除/).waitFor(); const shown = await note.innerText(); assert.ok(shown.includes('个人回收料 0.400g · ¥320.00') && shown.includes('公共料 0.800g · ¥720.00'), shown); assert.ok(shown.includes('本次前可用:个人 0.400g,公共 8.000g'), shown); assert.ok(shown.includes('本次后余:个人回收料 0.000g,公共料 7.200g'), shown); }); }