分拣标签二维码改用服务端提交回包的 container_code,不再使用 order_code;取不到容器码时不打印,避免生成配送小程序无法识别的标签。\n\n同时修正标签日期/时间等字段折行、空单位与市斤的重量口径、TSPL/ZPL 文本清洗,并补充 v1.0.64 回归用例。
248 lines
8.7 KiB
Python
248 lines
8.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""v1.0.64 回归:分拣标签二维码必须是「容器码」,不能再打订单号
|
||
|
||
现场(2026-11-09):
|
||
配送小程序扫标签提示「未找到条码对应商品或容器:
|
||
ORD_WEB_20261108_BR002_0005」——二维码里是订单号。
|
||
|
||
根因:
|
||
_do_print() 以前用 row['order_code'] or row['pick_code'] 当二维码内容;
|
||
服务端分拣提交成功的回包里其实有 data.container_code,但打印链路没接。
|
||
|
||
本测试覆盖:
|
||
① 从提交回包/待办行取 container_code
|
||
② 有容器码 → 标签队列/打印函数拿到容器码
|
||
③ 没有容器码 → 宁可不打,也不打订单号
|
||
④ 二维码内容只出现容器码,不出现订单号
|
||
⑤ 50×50mm 带二维码时日期不被折成两行
|
||
⑥ qty_word 对空单位/市斤判为「重量」
|
||
⑦ TSPL 文本里的双引号/换行不会破坏指令
|
||
⑧ 同一操作号不重复打印
|
||
"""
|
||
import io
|
||
import os
|
||
import sys
|
||
import queue
|
||
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
||
from core.flow import FlowEngine
|
||
from core.printing import label as LB
|
||
|
||
CFG = {
|
||
'flow': {'min_scale_kg': 0.05},
|
||
'scale': {'simulate': True},
|
||
'camera': {'simulate': True},
|
||
'server': {'mock': True},
|
||
'printer': {'enabled': True, 'printer_name': '测试机'},
|
||
}
|
||
|
||
ROW = {
|
||
'id': 'p1', 'item_name': '去皮蒜', 'unit': '袋',
|
||
'required_qty': 1, 'remaining_qty': 1,
|
||
'order_code': 'ORD_WEB_20261108_BR002_0005',
|
||
'pick_code': 'PK20261108001',
|
||
'container_code': 'CT20261108000123',
|
||
}
|
||
|
||
EVENTS = []
|
||
|
||
|
||
def emit(ev, payload=None):
|
||
EVENTS.append((ev, payload))
|
||
|
||
|
||
def new_eng(station='picking', cfg=None):
|
||
EVENTS[:] = []
|
||
e = FlowEngine(cfg or CFG, emit)
|
||
e.station = station
|
||
e._print_enabled = lambda: True
|
||
e._print_q = queue.Queue()
|
||
e._running = True
|
||
return e
|
||
|
||
|
||
def logs():
|
||
return [str(p) for ev, p in EVENTS if ev in ('log', 'print_result')]
|
||
|
||
|
||
def check(name, cond, extra=''):
|
||
global ok, fail
|
||
if cond:
|
||
ok += 1
|
||
print(' ✔ %s' % name)
|
||
else:
|
||
fail += 1
|
||
print(' ✘ %s %s' % (name, extra))
|
||
|
||
|
||
ok = fail = 0
|
||
|
||
print('=== 1. 容器码提取:data / 根节点 / 行 ===')
|
||
e = new_eng()
|
||
check('data.container_code',
|
||
e._container_code_of({'data': {'container_code': 'CT-D'}}) == 'CT-D')
|
||
check('根节点 container_code',
|
||
e._container_code_of({'container_code': 'CT-R'}) == 'CT-R')
|
||
check('data.result.container_code',
|
||
e._container_code_of(
|
||
{'data': {'result': {'container_code': 'CT-N'}}}) == 'CT-N')
|
||
check('container.code',
|
||
e._container_code_of(
|
||
{'data': {'container': {'code': 'CT-C'}}}) == 'CT-C')
|
||
check('行上的 container_code',
|
||
e._container_code_of(ROW) == 'CT20261108000123')
|
||
|
||
print('=== 2. 有容器码:队列里只能是容器码 ===')
|
||
e = new_eng()
|
||
e._print_async(dict(ROW), 1.0, container_code='CT-FROM-RESP',
|
||
op='op-1')
|
||
it = e._print_q.get_nowait()
|
||
check('二维码 = 提交回包的容器码', it['qr'] == 'CT-FROM-RESP', it)
|
||
check('来源标记 container_code', it['qr_source'] == 'container_code', it)
|
||
check('二维码不是订单号', 'ORD_WEB' not in it['qr'], it['qr'])
|
||
|
||
print('=== 3. 没有容器码:宁可不打,也不打订单号 ===')
|
||
e = new_eng()
|
||
row = {k: v for k, v in ROW.items() if k != 'container_code'}
|
||
e._print_async(row, 1.0, op='op-2')
|
||
check('没有入打印队列', e._print_q.qsize() == 0)
|
||
check('回了 print_result=False',
|
||
any(ev == 'print_result' and p[0] is False for ev, p in EVENTS),
|
||
logs())
|
||
|
||
print('=== 4. qr_fallback=pick_code 才允许显式兜底 ===')
|
||
cfg = dict(CFG)
|
||
cfg['printer'] = dict(CFG['printer'], qr_fallback='pick_code')
|
||
e = new_eng(cfg=cfg)
|
||
e._print_async(row, 1.0, op='op-3')
|
||
it = e._print_q.get_nowait()
|
||
check('兜底用 pick_code', it['qr'] == 'PK20261108001', it)
|
||
check('来源写明兜底', 'pick_code' in it['qr_source'], it)
|
||
|
||
print('=== 5. 打印机收到的二维码参数 ===')
|
||
e = new_eng()
|
||
sent = {}
|
||
e.printer.print_picking = lambda c, n, w, u, qr='': (
|
||
sent.update({'customer': c, 'name': n, 'weight': w,
|
||
'unit': u, 'qr': qr}) or (True, 'ok'))
|
||
e._do_print({'row': dict(ROW), 'business': 1.0, 'kg': 0.0,
|
||
'station': 'picking', 'qr': 'CT-PRINT'})
|
||
check('print_picking 收到容器码', sent.get('qr') == 'CT-PRINT', sent)
|
||
check('品名没串', sent.get('name') == '去皮蒜', sent)
|
||
check('数量/单位没串', sent.get('weight') == 1.0 and sent.get('unit') == '袋',
|
||
sent)
|
||
|
||
print('=== 6. TSPL 里二维码内容只出现容器码 ===')
|
||
data = LB.build_picking_label_tspl(
|
||
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
|
||
'王记饭店', '去皮蒜', 1.0, '袋', '11-08 20:39', 'CT20261108000123')
|
||
text = data.decode('gbk', 'replace')
|
||
check('QRCODE 里是容器码', 'QRCODE' in text and 'CT20261108000123' in text, text)
|
||
check('没有把订单号打进二维码', 'ORD_WEB' not in text, text)
|
||
|
||
print('=== 6b. ZPL 也走同一份版面与容器码 ===')
|
||
zpl = LB.build_zpl(
|
||
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
|
||
'分拣标签',
|
||
['客户: 李府家宴', '品名: 大葱', '重量: 35.22 斤', '日期: 11-06 10:00'],
|
||
'CT20261108000123').decode('utf-8')
|
||
check('ZPL 里有容器码', 'CT20261108000123' in zpl, zpl)
|
||
check('ZPL 里没有订单号', 'ORD_WEB' not in zpl, zpl)
|
||
check('ZPL 日期没被拆开',
|
||
'日期: 11-06 10:00' in zpl
|
||
and '^FD0:00^FS' not in zpl
|
||
and '11-06 1^FS' not in zpl, zpl)
|
||
|
||
print('=== 7. 50×50mm 带二维码:日期必须单行 ===')
|
||
p = LB.plan_label(
|
||
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
|
||
'分拣标签',
|
||
['客户: 李府家宴', '品名: 大葱', '重量: 35.22 斤', '日期: 11-06 10:00'],
|
||
qr_data='CT20261108000123')
|
||
rows = [x['text'] for x in p['lines']]
|
||
check('日期没被拆成 11-06 1 / 0:00',
|
||
'日期: 11-06 10:00' in rows, rows)
|
||
check('没有孤立的 0:00', '0:00' not in rows, rows)
|
||
check('字号仍尽量大(>=20)', int(p['font']) >= 20, p['font'])
|
||
|
||
print('=== 8. 空单位/市斤判为重量 ===')
|
||
check('空单位 → 重量', LB.qty_word('') == '重量', LB.qty_word(''))
|
||
check('市斤 → 重量', LB.qty_word('市斤') == '重量', LB.qty_word('市斤'))
|
||
check('斤 → 重量', LB.qty_word('斤') == '重量', LB.qty_word('斤'))
|
||
check('袋 → 数量', LB.qty_word('袋') == '数量', LB.qty_word('袋'))
|
||
|
||
print('=== 9. 文本里的双引号/换行不能破坏 TSPL ===')
|
||
data = LB.build_picking_label_tspl(
|
||
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
|
||
'王记"水果', '上海青\n(小棠菜)', 5.62, '斤', '11-06 10:00',
|
||
'CT20261108000123')
|
||
text = data.decode('gbk', 'replace')
|
||
check('换行被清掉', '\n(小棠菜)' not in text, text)
|
||
check('双引号被换成单引号', '王记\'水果' in text, text)
|
||
check('QRCODE 行仍然完整', 'QRCODE' in text and 'CT20261108000123' in text,
|
||
text)
|
||
|
||
print('=== 10. 同一操作号只打印一次 ===')
|
||
e = new_eng()
|
||
e._print_async(dict(ROW), 1.0, container_code='CT-DUP', op='op-dup')
|
||
e._print_async(dict(ROW), 1.0, container_code='CT-DUP', op='op-dup')
|
||
check('队列里只有 1 张', e._print_q.qsize() == 1)
|
||
check('第二次跳过有日志',
|
||
any('已打印过标签' in x for x in logs()), logs())
|
||
|
||
print('=== 11. 全链路:提交回包 → 打印队列 ===')
|
||
|
||
|
||
class FakeJournal:
|
||
def __init__(self):
|
||
self.rec = {}
|
||
|
||
def begin(self, op, kind, target, qty, weight, unit):
|
||
self.rec[op] = {'operationId': op}
|
||
return self.rec[op]
|
||
|
||
def update(self, op, **kw):
|
||
self.rec.setdefault(op, {}).update(kw)
|
||
return True
|
||
|
||
def conclude(self, op, state, msg=''):
|
||
self.rec.setdefault(op, {})['state'] = state
|
||
return True
|
||
|
||
def has_unresolved(self):
|
||
return False
|
||
|
||
|
||
class FakeApi:
|
||
user_id = 'u'
|
||
base_url = 'http://x'
|
||
|
||
def commit_picking(self, row, qty, weight, op, photo_expected):
|
||
return {'operation_status': 'committed_by_this_operation',
|
||
'client_operation_id': op,
|
||
'data': {'pick_status': 20,
|
||
'container_code': 'CT-E2E'}}
|
||
|
||
|
||
e = new_eng()
|
||
e.journal = FakeJournal()
|
||
e.api = FakeApi()
|
||
e._record = lambda *a, **k: None
|
||
e.frames.capture_confirmed = lambda: {
|
||
'qualified': False, 'path': None, 'sha256': '', 'reason': 'test'}
|
||
e._frozen_kg = 1.0
|
||
e._frozen_row = dict(ROW, container_code='')
|
||
row_no_ct = dict(ROW, container_code='')
|
||
e._submit_worker(row_no_ct, 1.0, 'AI')
|
||
it = e._print_q.get_nowait()
|
||
check('提交回包里的 container_code 进了打印队列',
|
||
it['qr'] == 'CT-E2E', it)
|
||
check('来源标记 container_code', it['qr_source'] == 'container_code', it)
|
||
check('没有退化成订单号', 'ORD_WEB' not in it['qr'], it['qr'])
|
||
|
||
print()
|
||
print('%d 项通过,%d 项失败' % (ok, fail))
|
||
sys.exit(1 if fail else 0)
|