/** * 小程序「送货单**图片**」(canvas 出图)契约 + 金额大写**两侧对拍**。 * * 开发者 2026-10-15 需求:分享出去的是**一张图片**(参照「秒账」的分享卡片), * 与免登录 H5 送货单同字段:编号、产品、单位、总数量、「单价=销售价」、金额、备注,加合计与金额大写, * + 订货电话/地址 + 制单人/收货人(签字);**不打印成本价**、**商品名取下单原文**、 * **不打印收款记录**(2026-10-15 裁决)。 * * 设计:docs/design/20261015-小程序送货单图片(canvas出图分享).md * 入口已于 2026-10-15 二次修改:**订单列表卡片**(不再是详情页)——开发者:「按钮别放到详情里面, * 放到订单列表的外面就行」。 */ import { createRequire } from 'module'; import { readFileSync } from 'fs'; import { describe, expect, it } from 'vitest'; import { amountInWords as serverAmountInWords } from '../src/utils/amountInWords'; const requireCjs = createRequire(import.meta.url); const miniWords = requireCjs('../../miniprogram/utils/amountInWords.js') as { amountInWords: (v: unknown) => string; }; const utilSrc = readFileSync( new URL('../../miniprogram/utils/deliveryNoteImage.js', import.meta.url), 'utf8' ); const pageJs = readFileSync( new URL('../../miniprogram/components/order-manage-panel/index.js', import.meta.url), 'utf8' ); const pageWxml = readFileSync( new URL('../../miniprogram/components/order-manage-panel/index.wxml', import.meta.url), 'utf8' ); const pageWxss = readFileSync( new URL('../../miniprogram/components/order-manage-panel/index.wxss', import.meta.url), 'utf8' ); const detailJs = readFileSync( new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.js', import.meta.url), 'utf8' ); const detailWxml = readFileSync( new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.wxml', import.meta.url), 'utf8' ); const mobileRoutes = readFileSync( new URL('../src/routes/mobile/mobileAdminRoutes.ts', import.meta.url), 'utf8' ); const outboundBatchService = readFileSync( new URL('../src/services/orderOutboundBatchService.ts', import.meta.url), 'utf8' ); /** 用假 canvas 实跑出图,回传所有 `fillText`(验「渲染成几列」比读源码更硬) */ function renderNote(note: { items: Array> }): { texts: Array<{ text: string; x: number; y: number }>; height: number; } { const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as { drawDeliveryNote: (canvas: unknown, note: unknown) => number; }; const texts: Array<{ text: string; x: number; y: number }> = []; const ctx = { fillRect() {}, strokeRect() {}, beginPath() {}, moveTo() {}, lineTo() {}, stroke() {}, fillText(text: unknown, x: number, y: number) { texts.push({ text: String(text), x, y }); }, measureText(text: unknown) { return { width: String(text).length * 12 }; }, }; const canvas = { width: 0, height: 0, getContext: () => ctx }; const height = util.drawDeliveryNote(canvas, note); return { texts, height }; } describe('金额大写:小程序副本与服务端逐字一致', () => { it('同一批边界值两侧输出完全相同(改动任一侧都会被拦下)', () => { const cases = [ 0, 0.05, 0.5, 8, 12.3, 100, 948, 948.05, 948.5, 1000, 1001, 10002, 100000, 100000001, 1234567.89, 99999999.99, 'abc', Number.NaN, -8, ]; for (const value of cases) { expect(miniWords.amountInWords(value), `值 ${String(value)}`).toBe( serverAmountInWords(value) ); } }); it('与送货单截图同款:948 ⇒ 玖佰肆拾捌元整', () => { expect(miniWords.amountInWords(948)).toBe('玖佰肆拾捌元整'); }); }); describe('出图版式(与 H5 送货单同字段)', () => { it('画的是送货单:标题/单号/销售日期/客户 + 表头(含生产时间)+ 合计', () => { expect(utilSrc).toContain("ctx.fillText('送货单'"); expect(utilSrc).toContain("'NO.' + String(note.order_code"); expect(utilSrc).toContain("'销售日期:'"); expect(utilSrc).toContain("'客户:'"); for (const label of ['编号', '产品', '单位', '生产时间', '总数量', '单价', '金额', '备注', '合计']) { expect(utilSrc).toContain(`'${label}'`); } }); it('金额大写 + 电话/地址/制单人/收货人签字都在,订单号与日期取自真实数据', () => { expect(utilSrc).toContain('amountInWords(totalAmount)'); expect(utilSrc).toContain("'订货电话:'"); expect(utilSrc).toContain("'订货地址:'"); expect(utilSrc).toContain("'制单人:'"); expect(utilSrc).toContain("'收货人(签字):'"); }); it('绝不打印成本价(单价是销售价)', () => { expect(utilSrc).not.toContain('cost_price'); expect(utilSrc).not.toContain('unit_cost'); }); it('不打印收款记录(2026-10-15 裁决:库里没有按单收款,不编数字)', () => { expect(utilSrc).not.toContain('已收款'); expect(utilSrc).not.toContain('未收款'); }); it('图片高度随行数增长(长单不会被截断)', () => { const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as { deliveryNoteHeight: (n: number) => number; }; expect(util.deliveryNoteHeight(3)).toBeLessThan(util.deliveryNoteHeight(12)); }); it('列宽:8 列(含生产时间)/ 7 列(保险退回)都合计 920(W - 2*MARGIN,不会溢出)', () => { const matched = utilSrc.match( /const widths = showProduction[\s\S]*?\? \[([^\]]+)\][\s\S]*?: \[([^\]]+)\]/ ); expect(matched).toBeTruthy(); const parse = (raw: string) => raw.split(',').map((value) => Number(value.trim())); const withProduction = parse(String(matched![1])); const withoutProduction = parse(String(matched![2])); expect(withProduction).toHaveLength(8); expect(withoutProduction).toHaveLength(7); expect(withProduction.reduce((sum, value) => sum + value, 0)).toBe(920); expect(withoutProduction.reduce((sum, value) => sum + value, 0)).toBe(920); // 列序钉死:生产时间放**最后一列**(备注之后,2026-10-20 开发者指定) expect(utilSrc).toContain( "const header = ['编号', '产品', '单位', '总数量', '单价', '金额', '备注'];" ); expect(utilSrc).toContain("if (showProduction) header.push('生产时间');"); // 右对齐列跟着列序走:总数量/单价/金额 = 下标 3/4/5(两种版式相同) expect(utilSrc).toContain('const rightAligned = i === 3 || i === 4 || i === 5;'); }); it('保险:后端没给(或整单没有)生产日期 ⇒ 整列不渲染,退回原 7 列版式', () => { const { texts } = renderNote({ items: [ { item_name: 'A', unit: '斤', quantity: 1, price: 1, amount: 1, production_dates: [] }, { item_name: 'B', unit: '斤', quantity: 2, price: 2, amount: 4 }, ], }); expect(texts.some((item) => item.text === '生产时间')).toBe(false); expect(texts.some((item) => item.text === '金额')).toBe(true); // 原 7 列的产品列 x = 40 + 70 + 8(左内边距)= 118(8 列时是 108) expect(texts.find((item) => item.text === '产品')?.x).toBe(118); }); it('只有部分行有日期 ⇒ 仍出该列,无日期的行留空', () => { const { texts } = renderNote({ items: [ { item_name: 'A', unit: '斤', quantity: 1, price: 1, amount: 1, production_dates: ['2026-10-01'] }, { item_name: 'B', unit: '斤', quantity: 2, price: 2, amount: 4, production_dates: [] }, ], }); expect(texts.filter((item) => item.text === '生产时间')).toHaveLength(1); expect(texts.filter((item) => item.text === '2026-10-01')).toHaveLength(1); // 生产时间在最右一列(备注之后) const remark = texts.find((item) => item.text === '备注'); const production = texts.find((item) => item.text === '生产时间'); expect(production?.x).toBeGreaterThan(remark?.x ?? 0); // 8 列的产品列 x = 40 + 60 + 8(左内边距)= 108 expect(texts.find((item) => item.text === '产品')?.x).toBe(108); }); it('多批次把行撑高:deliveryNoteHeight 按每行日期数增长(单批次不改变旧行高)', () => { const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as { deliveryNoteHeight: (value: number | Array>) => number; }; const one = [{ item_name: 'A', production_dates: ['2026-10-01'] }]; const three = [ { item_name: 'A', production_dates: ['2026-10-01', '2026-10-02', '2026-10-03'] }, ]; expect(util.deliveryNoteHeight(three)).toBeGreaterThan(util.deliveryNoteHeight(one)); // 单批次 ⇒ 仍是默认行高;空单与 1 行同高(不因新列变矮/变高) expect(util.deliveryNoteHeight(one)).toBe(util.deliveryNoteHeight(1)); expect(util.deliveryNoteHeight([])).toBe(util.deliveryNoteHeight(1)); }); it('建单据:批次生产日期透传为 production_dates(未出库为空数组,已退货行不打印)', () => { const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as { buildDeliveryNotePayload: ( order: Record, items: Array> ) => { items: Array<{ production_dates: string[] }> }; }; const note = util.buildDeliveryNotePayload({ order_code: 'X' }, [ { item_name: 'A', batch_production_dates: ['2026-10-01', '2026-10-02'] }, { item_name: 'B' }, { item_name: 'C', batch_production_dates: ['2026-10-03'], is_returned: true }, ]); expect(note.items.map((item) => item.production_dates)).toEqual([ ['2026-10-01', '2026-10-02'], [], ]); }); }); describe('订单列表卡片接线(2026-10-15 二次修改:入口从详情页移到列表)', () => { it('列表卡片挂了隐藏画布并调用出图工具', () => { expect(pageWxml).toContain('type="2d" id="deliveryNoteCanvas"'); expect(pageWxss).toContain('.print-canvas'); expect(pageJs).toContain("require('../../utils/deliveryNoteImage')"); expect(pageJs).toContain("exportDeliveryNoteImage("); expect(pageJs).toContain("'#deliveryNoteCanvas'"); expect(pageJs).toContain('buildDeliveryNotePayload(order, items)'); }); it('按钮在列表卡片上(catchtap 不冒泡到开详情);独立进货单不给(不是给客户的货)', () => { expect(pageWxml).toContain('送货单图片'); expect(pageWxml).toContain('catchtap="onDeliveryNoteImage"'); expect(pageWxml).toMatch(/wx:if="\{\{item\.customer_code !== '__INDEPENDENT_INBOUND__'\}\}"/); }); it('与配送/推进按钮**同一行**(放在左组 .actions-main 里,不新开一行)', () => { expect(pageWxml).toContain(''); expect(pageWxml).not.toContain('actions--note'); expect(pageWxss).toContain('.actions-main {'); }); it('详情页不再有送货单图片入口(按钮/画布/处理函数都清掉)', () => { expect(detailWxml).not.toContain('送货单图片'); expect(detailWxml).not.toContain('deliveryNoteCanvas'); expect(detailJs).not.toContain('onDeliveryNoteImage'); expect(detailJs).not.toContain('buildDeliveryNotePayload'); }); it('出图后给:预览 / 保存到相册 / 分享给好友(不支持图片分享菜单时降级提示)', () => { expect(pageJs).toContain("itemList: ['预览', '保存到相册', '分享给好友']"); expect(pageJs).toContain('wx.saveImageToPhotosAlbum('); expect(pageJs).toContain('wx.previewImage('); expect(pageJs).toContain("typeof wx.showShareImageMenu === 'function'"); expect(pageJs).toContain('长按图片可保存或转发'); // 无相册权限要能引导去设置,而不是静默失败 expect(pageJs).toContain('wx.openSetting()'); }); it('数据口径:下单原文 + 散客单显示「散客录单」 + 已退货行不打印', () => { expect(utilSrc).toContain("item_name: item.item_name || item.sku_name || ''"); expect(utilSrc).toContain("? '散客录单'"); expect(utilSrc).toContain('.filter((item) => !item.is_returned)'); }); }); describe('后端补的两个字段', () => { it('移动端订单详情返回 contact_phone 与 created_by_name(只加字段)', () => { expect(mobileRoutes).toContain("NULLIF(TRIM(c.contact_phone), '') AS contact_phone"); expect(mobileRoutes).toContain('AS created_by_name'); expect(mobileRoutes).toContain('LEFT JOIN public.profiles p ON p.id = o.created_by'); }); it('送货单「生产时间」取自本单实际用掉的批次(出库流水 ⋈ 批次生产日期)', () => { // 事实源必须是出库流水(未出库就没有流水 ⇒ 不猜批次) expect(outboundBatchService).toContain( 'JOIN inventory inv ON inv.batch_no = il.batch_no AND inv.sku_code = il.sku_code' ); expect(outboundBatchService).toContain( "il.operation_type IN ('picking_out', 'delivery_loading_out', 'retail_out')" ); expect(outboundBatchService).toContain('AND inv.production_date IS NOT NULL'); expect(outboundBatchService).toContain('ORDER BY il.sku_code, inv.production_date ASC'); // 只加字段:每个明细行带 batch_production_dates(去重升序的数组) expect(mobileRoutes).toContain('listOrderBatchProductionDates(orderCode)'); expect(mobileRoutes).toContain( "batch_production_dates: productionDatesBySku.get(String(row.sku_code || '')) || []," ); }); });