Files

249 lines
9.1 KiB
Python

# -*- coding: utf-8 -*-
"""v1.0.53 回归:合并入库(一堆货一次称重,按剩余量收掉多张订单)
现场(2026-11-05 19:25):
大葱 17.61kg = 35.22 斤,名单里 4 单(4 / 15 / 12 / 4 斤,合计 35 斤)。
操作员要的是「这一堆把 4 单一起收掉」,实际却:
同名 4 行,实收 35.22 斤 对「ORD_..._0003」(应 15.00 斤)
→ 超收 30% 转人工 → 人工确认 → 35.22 斤全记到应 15 斤那单,
另外 3 单一直挂着,账实差越攒越大。
根因:_choose_same_name 用服务端 weigh_match_tolerance_value 判断「这行装不
装得下」,而现场该值是 0(容差关闭)→ within_tolerance 恒真 → 任何数量都
算某一单装得下 → 永远挑「应量最接近」的那一单,永远走不到分摊。
本文件验证:
① 一单装不下、几单合计装得下 → 按剩余量分摊,先结小单
② 服务端容差=0 时也能合并(根因回归)
③ 一单装得下 → 保持原行为(挑一行,不分摊)
④ 实收远超合计 → 仍由超收倍数护栏转人工
⑤ 只有一行 / 单位不一致 → 不合并
不需要秤/摄像头/网络。
"""
import sys
import io
import os
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
CFG = {
'flow': {'min_scale_kg': 0.05, 'stable_ms': 400, 'settle_ms': 650,
'stable_delta_kg': 0.03, 'done_timeout_s': 12,
'done_lift_off_kg': 0.2, 'confirm_before_submit': True,
'auto_submit': True, 'same_name_auto': True,
'merge_same_name': True, 'over_receipt_ratio': 3,
'weight_pick_pct': 40},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 60, 'auto_over_pct': 30,
'same_kind_guard': False},
'server': {'mock': True},
'noweigh': {'picking_allow_weight_unit': True},
}
EVENTS = []
def emit(ev, p=None):
EVENTS.append((ev, p))
eng = FlowEngine(CFG, emit)
eng.refresh_pending = lambda initial=False: None
eng.logged_in = True
eng._running = True
# 现场口径:服务端把重量容差关了
eng.server_config = {'weigh_match_tolerance_mode': 'percent',
'weigh_match_tolerance_value': 0}
class FakeJournal:
def begin(self, *a, **k):
return {'op': 'op'}
def update(self, *a, **k):
pass
def conclude(self, *a, **k):
pass
def has_unresolved(self):
return False
class FakeRecog:
def __init__(self, name='大葱', conf=98, alt='无', alt_conf=None):
self.name, self.conf, self.alt = name, conf, alt
self.alt_conf = alt_conf
def identify_ex(self, path, names, timeout=8):
return {'name': self.name, 'conf': self.conf, 'alt': self.alt,
'alt_conf': self.alt_conf}
def cong_rows():
"""现场 4 单大葱:4 / 15 / 12 / 4 斤,合计 35"""
return [
{'id': 'c1', 'item_name': '大葱', 'unit': '斤', 'required_qty': 4,
'prepare_qty': 4, 'remaining_qty': 4, 'order_code': 'ORD_...0001',
'sku_code': 'S1'},
{'id': 'c2', 'item_name': '大葱', 'unit': '斤', 'required_qty': 15,
'prepare_qty': 15, 'remaining_qty': 15, 'order_code': 'ORD_...0003',
'sku_code': 'S1'},
{'id': 'c3', 'item_name': '大葱', 'unit': '斤', 'required_qty': 12,
'prepare_qty': 12, 'remaining_qty': 12, 'order_code': 'ORD_...0008',
'sku_code': 'S1'},
{'id': 'c4', 'item_name': '大葱', 'unit': '斤', 'required_qty': 4,
'prepare_qty': 4, 'remaining_qty': 4, 'order_code': 'ORD_...0004',
'sku_code': 'S1'},
]
def reset(rows, kg, over_pct=30):
CFG['vision']['auto_over_pct'] = over_pct
EVENTS.clear()
eng.rows = [dict(r) for r in rows]
eng.state = 'IDLE'
eng._focus = None
eng._count_mode = False
eng._count_qty = 0.0
eng._frozen_kg = kg
eng._frozen_row = None
eng._aborted = False
eng._running = True
eng._last_ok = None
eng._plan = []
eng.journal = FakeJournal()
eng._cycle_conf = 98
eng._cycle_detected = ''
eng._cycle_recog_path = 'fake.jpg'
eng.recog = FakeRecog()
eng.submitted = []
eng._start_submit = lambda row, biz, src: (
eng.submitted.append((row['id'], biz, src)),
eng._set_state('SUBMIT'))
def last_focus():
for e, p in reversed(EVENTS):
if e == 'focus':
return p
return None
def manual_warn():
f = last_focus()
return (f or {}).get('warn')
def logs():
return [str(p) for e, p in EVENTS if e == 'log']
ok = fail = 0
def check(name, cond, extra=''):
global ok, fail
if cond:
ok += 1
print(' ✓', name)
else:
fail += 1
print(' ✗', name, extra)
print('=== 1. 现场复现:35.22 斤 vs 4 单合计 35 斤 → 合并入库 ===')
reset(cong_rows(), kg=17.61) # 17.61kg = 35.22 斤
eng._run_recognition()
print(' submitted =', eng.submitted, '| plan =',
[(r['id'], q) for r, q in eng._plan])
check('自动提交了第一单(不再转人工)', len(eng.submitted) == 1)
check('提交方式标成「合并」',
bool(eng.submitted) and eng.submitted[0][2] == '合并')
check('第一单是最小的 4 斤(先清小单)',
bool(eng.submitted) and abs(eng.submitted[0][1] - 4.0) < 1e-6)
check('队列里还有 3 单', len(eng._plan) == 3)
check('先把 4 单合计 35 斤都排上',
abs(sum(q for _, q in eng._plan) + eng.submitted[0][1] - 35.0) < 1e-6,
str([(r['id'], q) for r, q in eng._plan]))
check('日志说清「4 单合计 35 斤、按剩余量分摊」',
any('合计应 35.00' in x and '按剩余量分摊' in x for x in logs()),
logs()[:3])
check('界面看得到「合并入库:这堆 35.22 斤 … 分给 4 单」',
any('合并入库:这堆 35.22' in x and '分给 4 单' in x for x in logs()),
logs()[:4])
check('0.22 斤尾差按「称重误差内」提示,不吓现场',
any('尾差 0.22' in x and '可忽略' in x for x in logs()),
logs()[-2:])
print('=== 2. 一单装得下的量(3.98 斤)→ 保持原行为,只收一单 ===')
reset(cong_rows(), kg=1.99) # 3.98 斤
eng._run_recognition()
print(' submitted =', eng.submitted, '| plan =', eng._plan)
check('不分摊(排队为空)', eng._plan == [])
check('停在确认条,指向应 4 斤那单',
eng.state == 'CONFIRM'
and str((last_focus() or {}).get('row_id')) in ('c1', 'c4'),
'%s / %s' % (eng.state, (last_focus() or {}).get('row_id')))
print('=== 3. 实收远超合计(100 斤)→ 仍由超收倍数护栏拦住 ===')
reset(cong_rows(), kg=50.0) # 100 斤,合计 35 斤的 2.86 倍
eng._run_recognition()
print(' submitted =', eng.submitted, '| state =', eng.state)
check('没有自动提交', eng.submitted == [])
check('停在确认条等人(未直接分摊)', eng.state == 'CONFIRM')
print('=== 4. 单位不一致 → 不合并,提示人工 ===')
rows = cong_rows()
rows[1] = dict(rows[1], unit='袋')
reset(rows, kg=17.61)
eng._run_recognition()
print(' submitted =', eng.submitted, '| warn =', manual_warn())
check('不自动提交', eng.submitted == [])
check('提示里说明没法自动分摊',
'没法自动分摊' in str(manual_warn() or ''), manual_warn())
print('=== 5. 只有一行 → 不涉及合并 ===')
reset([cong_rows()[1]], kg=7.0) # 14 斤,单行应 15 斤
eng._run_recognition()
print(' submitted =', eng.submitted, '| plan =', eng._plan)
check('停在确认条,指向这一单',
eng.state == 'CONFIRM' and (last_focus() or {}).get('row_id') == 'c2',
'%s / %s' % (eng.state, (last_focus() or {}).get('row_id')))
check('不排队', eng._plan == [])
print('=== 6. auto_over_pct=0 → 阈值收紧到「超过最大单即合并」 ===')
reset(cong_rows(), kg=8.0, over_pct=0) # 16 斤 > 最大单 15 斤
eng._run_recognition()
print(' submitted =', eng.submitted)
check('16 斤一单装不下(应 15)→ 走分摊', len(eng.submitted) == 1)
print('=== 7. 同名行数超过上限(merge_max_rows)→ 转人工 ===')
CFG['flow']['merge_max_rows'] = 2
reset(cong_rows(), kg=17.61)
eng._run_recognition()
print(' submitted =', eng.submitted, '| state =', eng.state)
check('超过上限不自动分摊', eng.submitted == [] and eng.state == 'CONFIRM')
CFG['flow']['merge_max_rows'] = 8
print('=== 8. 识别有疑问(次选也有 60% 把握)→ 不自动分摊 ===')
_rows8 = cong_rows() + [
{'id': 'x1', 'item_name': '小葱', 'unit': '斤', 'required_qty': 5,
'prepare_qty': 5, 'remaining_qty': 5, 'order_code': 'ORD_...0009',
'sku_code': 'S2'}]
reset(_rows8, kg=17.61)
eng.recog = FakeRecog(name='大葱', conf=75, alt='小葱', alt_conf=60)
eng._run_recognition()
print(' submitted =', eng.submitted, '| state =', eng.state)
check('不自动分摊 N 单', eng.submitted == [] and eng.state == 'CONFIRM')
check('提示里说明「要合并 4 单,先确认识别」',
'先确认识别' in str(manual_warn() or ''), manual_warn())
print()
print('通过 %d 项,失败 %d 项' % (ok, fail))
sys.exit(1 if fail else 0)