Files
runny 91b7233872 Merge branch 'dev' into runny827
feat(training): 新增精益培训视频与周任务管理

# Conflicts:
#	backend/app/models/__init__.py
2026-09-08 18:30:09 +08:00

62 lines
2.0 KiB
Python

"""在途调拨严格数量门禁。
用途:实现"收料数量 == ERP 账面库存 才可执行"的精确匹配规则,
禁止部分调拨、数量容差与隐式截断。
依赖:标准库 decimal。
逻辑:所有比较基于 Decimal 精确语义;非法数量(0/负数/非数字)在解析层拒绝。
"""
from __future__ import annotations
from decimal import Decimal, InvalidOperation
from typing import Any
# 匹配结果
MATCH_EXECUTABLE = "executable"
MATCH_QUANTITY_MISMATCH = "blocked_quantity_mismatch"
MATCH_INVENTORY_NOT_FOUND = "blocked_inventory_not_found"
class QuantityValidationError(ValueError):
"""收料数量非法(0、负数、非数字)。"""
def validate_receipt_quantity(value: Any) -> Decimal:
"""校验并归一化收料数量。
Args:
value: 数字或数字字符串。
Returns:
归一化后的 Decimal。
Raises:
QuantityValidationError: 非数字、0 或负数。
"""
try:
quantity = Decimal(str(value))
except (InvalidOperation, ValueError) as exc:
raise QuantityValidationError(f"invalid receipt quantity: {value!r}") from exc
if quantity <= 0:
raise QuantityValidationError(f"receipt quantity must be positive: {value!r}")
return quantity
def strict_quantities_equal(left: Any, right: Any) -> bool:
"""Decimal 精确相等比较(1.0 == 1.00)。"""
return Decimal(str(left)) == Decimal(str(right))
def classify_quantity_match(receipt_quantity: Any, erp_quantity: Any | None) -> str:
"""按严格相等规则分类数量匹配结果。
Args:
receipt_quantity: 收料数量。
erp_quantity: ERP 账面库存;None 表示无库存。
Returns:
'executable' | 'blocked_quantity_mismatch' | 'blocked_inventory_not_found'。
"""
if erp_quantity is None:
return MATCH_INVENTORY_NOT_FOUND
if strict_quantities_equal(receipt_quantity, erp_quantity):
return MATCH_EXECUTABLE
return MATCH_QUANTITY_MISMATCH