import assert from 'node:assert/strict'; import { assertOrderChangeBody } from './orders-coverage.mjs'; import { exerciseOrderConflicts } from './orders-conflicts.mjs'; import { exerciseOrderReviews } from './orders-reviews.mjs'; import { exerciseDeliveredLocks } from './orders-locks.mjs'; export default async function orders({ version, base, day, session, pageFor, step, passwords }) { const yesterday = '2026-11-19'; const entry = base + (version === 'old' ? '/' : '/next/'); const pages = {}; const route = id => '/api/orders/' + id; const field = (form, name) => form.locator('[name="' + name + '"]').first(); const action = (page, name) => page.locator('[data-action="' + name + '"]:visible').first(); const detail = page => page.locator('#order-dialog[open]'); const post = async (actor, path, data) => { const response = await session.postRaw(actor, path, data); assert.equal(response.status, 200, path + ' ' + JSON.stringify(response.body)); return response.body; }; const order = (customer, date = day, extra = {}) => ({ date, customer, customer_type: 'new', member: 'assistant', internal_note: '订单写入对照', salesperson: '娜娜', payment_account: '助理支付宝', rounding_mode: 'cent', lines: [ { name: '足金项链', unit: 'g', quantity: '1.5', pocket: '现货', payer: 'assistant', sale: '1000', costParts: { material: '720', labor: '58', accessories: '2', other: '4' }, costPayers: { material: 'assistant2', labor: 'assistant' }, }, ], fee_items: [], ...extra, }); const issue = async (name, date = day) => (await post('assistant', '/api/orders/issue', order(name, date))).id; async function open(actor, id, period = 'today') { const page = (pages[actor] ||= await pageFor(actor)); await page.goto(entry); await page .locator('nav:visible') .locator(version === 'old' ? '[data-tab="orders"]' : 'a[href="/next/orders"]') .first() .click(); await action(page, 'order-period').waitFor(); await page .locator('[data-action="order-period"][data-period="' + period + '"]:visible') .first() .click(); await page .locator('[data-action="open-order"][data-id="' + id + '"]:visible') .first() .click(); await detail(page).locator('.order-detail-identifiers').waitFor(); return page; } async function write(label, page, endpoint, click, after) { await step(label, async () => { const [result] = await Promise.all([ page.waitForResponse( r => r.request().method() === 'POST' && (typeof endpoint === 'string' ? new URL(r.url()).pathname === endpoint : endpoint.test(new URL(r.url()).pathname)), ), click(), ]); assert.equal(result.status(), 200, label + ' ' + (await result.text()).slice(0, 500)); if (/^\/api\/orders\/[^/]+\/change$/.test(new URL(result.url()).pathname)) assertOrderChangeBody(label, result.request().postDataJSON()); if (after) await after(); await page.waitForLoadState('networkidle'); }); } async function submit(label, page, form, endpoint) { await write( label, page, endpoint, () => form.locator('button[type="submit"], button.primary:not([type])').first().click(), () => form.waitFor({ state: 'hidden' }), ); } async function legacyForm(page, kind) { if (kind === 'return') { const details = detail(page).locator('details.order-legacy-actions:not([open]) > summary'); if (await details.count()) await details.click(); } await action(page, 'show-' + kind).click(); const form = page.locator('#action-form[data-kind="' + kind + '"]'); await form.waitFor(); return form; } const helpers = { day, yesterday, session, post, open, field, action, write, submit, legacyForm, route, issue, order, step, base, passwords, }; await post('owner', '/api/stock', { date: yesterday, kind: 'opening', pocket: '现货', grams: '100', note: '订单对照库存期初', }); await post('owner', '/api/pockets', { name: '订单对数盘', countUnit: 'pair', initialCount: 0, countVerified: true }); const historical = order('旧单补账'); historical.lines.push({ name: '银饰', unit: '件', quantity: '1', pocket: '', payer: 'owner', sale: '100', costParts: { material: '30', labor: '5', accessories: '0', other: '0' }, costPayers: { material: 'owner', labor: 'assistant' }, }); historical.fee_items = [{ key: 'shipping', note: '快递费', payer: 'assistant', amount: '5', status: 'estimated' }]; const id = (await post('assistant', '/api/orders', historical)).id; await post('assistant', route(id) + '/confirm', { date: day }); const owner = await open('owner', id); let form = await legacyForm(owner, 'cost'); await field(form, 'cost-0-materialMode').selectOption('gram'); await field(form, 'cost-0-materialRate').fill('480.85'); await field(form, 'cost-0-laborMode').selectOption('gram'); await field(form, 'cost-0-laborRate').fill('10.10'); await field(form, 'cost-1-accessories').fill('2.50'); assert.equal(await field(form, 'cost-0-material').inputValue(), '721.28'); await field(form, 'cost-0-materialMode').selectOption('total'); assert.equal(await field(form, 'cost-0-material').inputValue(), '720.00'); await submit('旧单补齐多行料费和工费', owner, form, route(id) + '/cost'); form = await legacyForm(owner, 'fees'); await field(form, 'verifiedAmount').fill('6.50'); await field(form, 'verifiedPayer').selectOption('owner'); await field(form, 'verified').check(); await submit('旧单补记实际费用及付款人', owner, form, route(id) + '/fees'); form = await legacyForm(owner, 'receipt'); await field(form, 'payment_recipient').selectOption('助理2'); await field(form, 'payment_method').selectOption('信用卡'); assert.equal(await field(form, 'payment_recipient').isDisabled(), true); await field(form, 'payment_method').selectOption('微信'); assert.equal(await field(form, 'payment_recipient').inputValue(), '助理2'); await field(form, 'payment_method').selectOption('信用卡'); await field(form, 'credit_card_fee_payer').selectOption('assistant2'); await field(form, 'verified').check(); await submit('旧单信用卡收款并计入手续费', owner, form, route(id) + '/receipt'); form = await legacyForm(owner, 'correct'); await field(form, 'account').selectOption('麦麦信用卡'); await field(form, 'amount').fill('1100'); await field(form, 'credit_card_fee_payer').selectOption('owner'); await field(form, 'reason').fill('核正手续费付款人'); await field(form, 'verified').check(); await submit('店主更正旧单收款流水', owner, form, route(id) + '/correct'); form = await legacyForm(owner, 'complete'); await field(form, 'verified').check(); await submit('补齐旧单账务并扣库存', owner, form, route(id) + '/complete'); form = await legacyForm(owner, 'review'); await submit('店主复核旧单', owner, form, route(id) + '/review'); let preview; await write('打开订单客户打印预览并登记审计', owner, route(id) + '/print', async () => { [preview] = await Promise.all([owner.context().waitForEvent('page'), action(owner, 'print').click()]); }); await write( '订单客户点击打印并更新打印状态', preview, '/api/print-status/order/' + id, () => preview.locator('#print').click(), () => preview.getByText('✓ 已打印', { exact: true }).waitFor(), ); await preview.close(); const assistant = await open('assistant', id); form = await legacyForm(assistant, 'expense'); await field(form, 'amount').fill('3.12'); await field(form, 'note').fill('补记费用'); await field(form, 'change_reason').fill('已核实旧单'); await field(form, 'verified').check(); await field(form, 'date').fill(yesterday); assert.equal(await field(form, 'verified').isChecked(), false); assert.equal(await form.getAttribute('data-order-direct'), 'false'); await field(form, 'date').fill(day); await field(form, 'verified').check(); await submit('助理同日直接登记额外费用', assistant, form, route(id) + '/expense'); form = await legacyForm(assistant, 'return'); const pair = await field(form, 'return-0').locator('option').filter({ hasText: '订单对数盘' }).getAttribute('value'); await field(form, 'return-0').selectOption(pair); await field(form, 'returnCount-0').fill('2'); await field(form, 'return-0').selectOption('现货'); assert.equal(await field(form, 'returnCount-0').isDisabled(), true); await field(form, 'return-0').selectOption(pair); await field(form, 'reason').fill('核实实物和对数'); await field(form, 'verified').check(); await submit('助理同日整单退回按对货盘', assistant, form, route(id) + '/return'); form = await legacyForm(assistant, 'refund'); await field(form, 'change_reason').fill('已核实退款'); await field(form, 'verified').check(); await submit('助理同日直接登记全额退款', assistant, form, route(id) + '/refund'); const sale = await issue('原单售后扣款'); await open('owner', sale); await action(owner, 'order-after-sale-open').click(); const after = owner.locator('#order-after-sale-form'); await field(after, 'reason').fill('实物已退回'); await field(after, 'refund_amount').fill('990'); await field(after, 'deduction_reason').fill('约定扣回邮费'); await action(owner, 'order-after-sale-add-expense').click(); await field(after, 'expense_note').fill('退件运费'); await field(after, 'expense_amount').fill('6.50'); await write( '原单售后试算扣款与费用', owner, route(sale) + '/after-sale-preview', () => action(owner, 'order-after-sale-preview').click(), () => after.getByText('预览已更新,请核对后勾选确认。').waitFor(), ); await field(after, 'verified').check(); await submit('原单售后登记退款和退货', owner, after, route(sale) + '/return'); await exerciseOrderReviews(helpers); await exerciseDeliveredLocks(helpers); await exerciseOrderConflicts(helpers); }