import assert from 'node:assert/strict'; import { ui } from './funds-ui.mjs'; export async function locks(context, { owner, assistant, yesterday }) { const { action, field, fill, navigate, write, submit } = ui(context); const quotes = {}; async function selectPool(page, control, label, match) { await context.step('锁价' + label, async () => { const [response] = await Promise.all([ page.waitForResponse( r => r.url().endsWith('/api/material-pools/quote') && match(r.request().postDataJSON().entries), ), control.selectOption('pool'), ]); assert.equal(response.status(), 200); const input = response.request().postDataJSON(), result = await response.json(); assert.equal(result.entries.length, input.entries.length); quotes[label] = { input, result }; }); } const endpoint = tail => new RegExp('/price-locks/[^/]+/' + tail + '$'); const lockAction = async (page, customer, name) => { await navigate(page, 'price-locks'); await action(page.locator('.price-lock-list tr').filter({ hasText: customer }), name).click(); }; async function sale(page, name, date, multiple, pool) { await navigate(page, 'price-locks'); await action(page, 'new-price-lock').click(); const form = page.locator('#price-lock-create-form'); await fill(form, { customer: name, date }); if (multiple) await action(page, 'add-lock-line').click(); const rows = multiple ? await form.locator('[data-price-lock-sale]').all() : [form]; for (const [index, row] of rows.entries()) { await fill(row, { name: index ? '足金手链' : '足金999', quantity: index ? '3' : '2', goldPrice: '900', laborRate: '10', }); if (pool && index === 1) { await selectPool( page, field(row, 'material_source'), '多行预留试算', entries => entries.length === 2 && entries[0].mg === 2000 && entries[1].mg === 3000, ); } else if (pool) await selectPool( page, field(row, 'material_source'), '第一件个人料与公共料预留试算', entries => entries.length === 1 && entries[0].mg === 2000, ); else { await field(row, 'material_rate').fill('850'); await field(row, 'material_verified').check(); } } await field(form, 'received_amount').fill(multiple ? '2500' : '500'); await field(form, 'payment_verified').check(); await page.waitForLoadState('networkidle'); await write('创建' + name, page, '/api/price-locks', () => submit(form)); await action(page, 'close-price-lock').click(); } async function deliver(name, page, form) { const original = await form.getAttribute('data-id'); const details = page.waitForResponse( response => response.request().method() === 'GET' && /^\/api\/orders\/[^/]+$/.test(new URL(response.url()).pathname), ); const [saved, read] = await Promise.all([ write(name, page, endpoint('deliver'), () => submit(form), { after: () => page.locator('#order-dialog[open]').waitFor(), }), details, ]); const result = await saved.json(), order = await read.json(); assert.equal(order.id, result.id); assert.equal(order.price_lock_id, original, '交付后打开的销售单必须关联本次原锁价单'); } for (const pool of [false, true]) { if (pool) { // 加入真实个人回收料,触发锁价时按归属查询个人料优先、公料补足的业务报价。 await context.session.post('owner', '/api/recoveries', { date: context.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, }); assert.deepEqual(context.session.problems, []); await navigate(owner, 'material-pools'); await owner.reload(); } const name = pool ? '料池多件锁价' : '手工多件锁价'; await sale(owner, name, context.day, true, pool); let customer = name; if (!pool) { await lockAction(owner, name, 'edit-price-lock'); const form = owner.locator('#price-lock-create-form'); customer = name + '已修改'; await fill(form, { customer, pending_edit_reason: '修订客户称呼' }); await field(form, 'pending_edit_verified').check(); await write('直接更正整笔多件锁价', owner, endpoint('edit'), () => submit(form)); } await lockAction(owner, customer, 'deliver-price-lock'); const form = owner.locator('#price-lock-delivery-form'); await form.locator('[name=customer_type][value=new]').check(); const rows = await form.locator('[data-price-lock-delivery-item]').all(); assert.equal(rows.length, 2); for (const [index, row] of rows.entries()) { await fill(row, { quantity: index ? '3.300' : '1.900', 'cost-labor': '25.50' }); await field(row, 'pocket').selectOption('现货'); if (!pool) await field(row, 'original_stock').selectOption('stocked'); if (!index) continue; if (pool) { await selectPool( owner, field(row, 'extra_source'), '交付补重试算', entries => entries.length === 1 && entries[0].lineIndex === 1 && entries[0].mg === 300, ); await row.locator('[data-lock-part="extra-material"] .note:visible').filter({ hasText: '¥' }).waitFor(); } else { await field(row, 'extra_rate').fill('860'); await field(row, 'extra_payer').selectOption('owner'); await field(row, 'extra_stock').selectOption('receive'); await field(row, 'extra_verified').check(); } } await field(form, 'settlement_verified').check(); await field(form, 'delivery_verified').check(); await deliver(name + '交付,一件退料一件补料', owner, form); } await sale(assistant, '单件足重锁价', context.day, false, false); await lockAction(assistant, '单件足重锁价', 'deliver-price-lock'); const single = assistant.locator('#price-lock-delivery-form'); await single.locator('[name=customer_type][value=new]').check(); await fill(single, { quantity: '2.000', 'cost-labor': '12.30' }); await field(single, 'pocket').selectOption('现货'); await field(single, 'original_stock').selectOption('receive'); await field(single, 'settlement_verified').check(); await field(single, 'delivery_verified').check(); await deliver('足重单行锁价按原料成本交付入库并打开销售单', assistant, single); await sale(owner, '当天作废锁价', context.day, false, false); await lockAction(owner, '当天作废锁价', 'cancel-price-lock'); const cancel = owner.locator('#price-lock-cancel-form'); await fill(cancel, { reason: '当天录错' }); await field(cancel, 'verified').check(); await write('直接作废当天锁价', owner, endpoint('cancel'), () => submit(cancel)); for (const [kind, result] of [ ['edit', 'approve'], ['cancel', 'reject'], ['cancel', 'approve'], ]) { const verb = kind === 'edit' ? '修改' : '作废'; const decision = result === 'approve' ? '同意' : '驳回'; const name = '历史锁价' + verb + decision; await sale(assistant, name, yesterday, false, false); await lockAction(assistant, name, kind + '-price-lock'); const form = assistant.locator(kind === 'edit' ? '#price-lock-create-form' : '#price-lock-cancel-form'); if (kind === 'edit') await fill(form, { customer: name + '改名', pending_edit_reason: '纸单客户名更正' }); else await fill(form, { reason: '原始凭据核对' }); await field(form, kind === 'edit' ? 'pending_edit_verified' : 'verified').check(); await write('店员申请' + verb + '历史锁价,待' + decision, assistant, endpoint('change-requests'), () => submit(form), ); await owner.reload(); await navigate(owner, 'price-locks'); await action(owner, 'price-lock-changes-open').click(); await action( owner.locator('.price-lock-change-list tr').filter({ hasText: name }), 'price-lock-change-open', ).click(); const review = owner.locator('#price-lock-review-form'); if (result === 'approve') await field(review, 'verified').check(); await fill(review, { review_note: result === 'approve' ? '凭据相符' : '原单保持' }); await write( '店主' + decision + '历史锁价' + verb, owner, new RegExp('/price-lock-changes/[^/]+/' + result + '$'), () => review.locator('button[value="' + result + '"]').click(), ); } return { 料池报价: quotes }; }