import assert from 'node:assert/strict'; import { action, postFrom, section } from './operations-helpers.mjs'; export async function inventoryWrites(ctx, owner, staff) { const { step, day } = ctx; const manager = owner.locator('#pocket-manager'); const editor = owner.locator('#pocket-editor-dialog[open]'); await section(owner, ctx, 'stock'); await action(owner, 'pocket-manager-toggle').click(); const initial = '对照原料盘'; const renamed = '对照成对货盘'; await action(manager, 'pocket-manager-add').click(); await step('库存新增普通货盘', async () => { await editor.locator('[name=name]').fill(initial); await postFrom(owner, '/api/pockets', () => editor.locator('[type=submit]').click()); await editor.waitFor({ state: 'hidden' }); }); const pocketRow = name => manager.locator('tr').filter({ hasText: name }); const edit = pocketRow(initial).locator('[data-action=pocket-manager-edit]'); const key = await edit.getAttribute('data-pocket-key'); const update = '/api/pockets/' + encodeURIComponent(key) + '/update'; const form = p => p.locator('#stock-form'); const movement = (p, note) => p.locator('tr[data-movement-id]').filter({ hasText: note }).first(); async function add(p, name, { kind = 'increase', pocket = key, grams, stockCount, to, date = day }) { const f = form(p); await f.locator('[name=kind]').selectOption(kind); await f.locator('[name=pocket]').selectOption(pocket); if (to) await f.locator('[name=to]').selectOption(to); if (grams) await f.locator('[name=grams]').fill(grams); if (stockCount) await f.locator('[name=stockCount]').fill(stockCount); await f.locator('[name=date]').fill(date); await f.locator('[name=note]').fill(name); const [request, result] = await Promise.all([ p.waitForRequest(r => r.method() === 'POST' && new URL(r.url()).pathname === '/api/stock'), postFrom(p, '/api/stock', () => f.locator('button').click()), ]); assert.deepEqual(request.postDataJSON(), { kind, date, pocket, grams: grams || '0', note: name, ...(to && { to }), ...(stockCount && { stockCount: Number(stockCount) }), }); if (date === day) await movement(p, name).waitFor(); await p.waitForFunction(() => document.querySelector('#stock-form [name=note]')?.value === ''); return result; } await step('库存普通货盘登记启用对数前的流水', () => add(owner, '盘点前的克重', { grams: '3.500' })); await section(owner, ctx, 'stock'); await action(owner, 'pocket-manager-toggle').click(); await edit.click(); await step('库存货盘重命名并盘点启用对数', async () => { await editor.locator('[name=name]').fill(renamed); await editor.locator('[name=enableCounts]').check(); await editor.locator('[name=initialCount]').fill('4'); await editor.locator('[name=countVerified]').check(); await postFrom(owner, update, () => editor.locator('[type=submit]').click()); await pocketRow(renamed).waitFor(); }); for (const [state, label] of [ [false, '停用'], [true, '启用'], ]) { await step('库存' + label + '已有货盘', async () => { await postFrom(owner, update, () => pocketRow(renamed) .locator('[data-action=pocket-manager-status][data-active=' + state + ']') .click(), ); await pocketRow(renamed) .getByText(state ? '已启用' : '已停用', { exact: true }) .waitFor(); }); } await action(manager, 'pocket-manager-add').click(); const target = await step('库存新增零基准对数货盘', async () => { await editor.locator('[name=name]').fill('对照第二对数盘'); await editor.locator('[name=enableCounts]').check(); await editor.locator('[name=initialCount]').fill('0'); await editor.locator('[name=countVerified]').check(); await postFrom(owner, '/api/pockets', () => editor.locator('[type=submit]').click()); return pocketRow('对照第二对数盘').locator('[data-action=pocket-manager-edit]').getAttribute('data-pocket-key'); }); await step('库存增加克重及对数', () => add(owner, '增加对数', { grams: '12.345', stockCount: '2' })); await step('库存减少克重及对数', () => add(owner, '减少对数', { kind: 'decrease', grams: '1.001', stockCount: '1' })); await step('库存从对数盘平移到普通货盘', () => add(owner, '平移的组', { kind: 'transfer', grams: '2.250', stockCount: '1', to: '现货' }), ); await step('库存只调整对数', () => add(owner, '实物重新清点', { kind: 'adjustment', stockCount: '-1' })); await step('库存两个对数盘间平移克重及对数', () => add(owner, '两个对数盘间平移', { kind: 'transfer', grams: '0.750', stockCount: '1', to: target }), ); const changes = (p, note, kind) => movement(p, note).locator('[data-action=inventory-change-' + kind + ']'); async function modify(p, note, kind, reason, newNote) { await changes(p, note, kind).click(); const f = p.locator('#inventory-change-form'); const id = await f.getAttribute('data-id'); if (newNote) await f.locator('[name=note]').fill(newNote); await f.locator('[name=reason]').fill(reason); const confirmation = f.locator('[name=legacyCountVerified]:visible'); if (await confirmation.count()) await confirmation.check(); const result = await postFrom( p, '/api/inventory/movements/' + encodeURIComponent(id) + '/' + kind.replace('edit', 'update'), () => f.locator('[type=submit]').click(), ); await f.waitFor({ state: 'hidden' }); return result; } await step('库存盘点后更正只有克重的历史流水', async () => { await changes(owner, '盘点前的克重', 'edit').click(); const f = owner.locator('#inventory-change-form'); const id = await f.getAttribute('data-id'); await f.locator('[name=legacyCountVerified]').check(); await f.locator('[name=grams]').fill('3.250'); await f.locator('[name=reason]').fill('更正盘点前的重量'); await postFrom(owner, '/api/inventory/movements/' + encodeURIComponent(id) + '/update', () => f.locator('[type=submit]').click(), ); await f.waitFor({ state: 'hidden' }); }); await step('库存店主更正已有平移组', () => modify(owner, '平移的组', 'edit', '更正平移备注', '已更正平移')); await step('库存店主删除完整平移组', () => modify(owner, '已更正平移', 'delete', '撤回整组平移')); await step('库存保存当日复核快照', async () => { await postFrom(owner, '/api/inventory/review', () => action(owner, 'inventory-review').click()); await owner.locator('.stock-review-status').getByText('店主复核', { exact: true }).waitFor(); }); // 店员自己登记的历史记录可发起更正,删除即使当天也要复核。 const yesterday = new Date(Date.parse(day + 'T12:00Z') - 86400000).toISOString().slice(0, 10); await section(staff, ctx, 'stock'); await step('库存店员登记当日库存变动', () => add(staff, '店员当日记录', { grams: '0.750', pocket: '现货' })); await step('库存店员直接更正自己当日流水', async () => { const changed = await modify(staff, '店员当日记录', 'edit', '复核当天备注', '店员当日记录已复核'); assert.equal(changed.pending, false); }); await step('库存店员登记历史库存变动', () => add(staff, '店员历史记录', { date: yesterday, pocket: '现货', grams: '3.250' }), ); await staff.locator('#inventory-movements-from').fill(yesterday); await staff.locator('#inventory-movements-from').press('Tab'); let request; await step('库存店员申请修改旧流水', async () => { request = await modify(staff, '店员历史记录', 'edit', '请复核修改', '申请更正库存'); }); async function decide(op, reason) { await action(owner, 'inventory-change-refresh').click(); await action(owner, 'inventory-change-open-request').click(); const review = owner.locator('#inventory-change-review-form'); const id = await review.getAttribute('data-id'); if (request.requestId) assert.equal(id, request.requestId); await review.locator('[name=note]').fill(reason); const button = op === 'approve' ? review.locator('[type=submit]') : action(review, 'inventory-change-reject'); const result = await postFrom(owner, '/api/inventory/changes/' + encodeURIComponent(id) + '/' + op, () => button.click(), ); await review.waitFor({ state: 'hidden' }); return result; } await step('库存店主驳回流水更正申请', () => decide('reject', '维持原记录')); async function reloadHistory() { await section(staff, ctx, 'stock'); await staff.locator('#inventory-movements-from').fill(yesterday); await staff.locator('#inventory-movements-from').press('Tab'); } await reloadHistory(); await step('库存店员重新提交流水更正', async () => { request = await modify(staff, '店员历史记录', 'edit', '复核后重新申请', '核实后更正'); }); await step('库存店主批准流水更正', () => decide('approve', '已复核修改')); await reloadHistory(); await step('库存店员申请删除已更正的旧流水', async () => { request = await modify(staff, '核实后更正', 'delete', '实际需要撤回'); }); await step('库存店主批准删除申请', () => decide('approve', '核对后撤回')); }