fix(pl): sync dibang inventory and weighing fields after rearrange

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-09-06 19:43:32 +08:00
1 parent cd64463c0a
commit 4dde13e7d7
1 file changed
+57 -37
@@ -425,6 +425,52 @@ def _inventory_has_weighing_slip(db: Session, inventory_id: str) -> bool:
)
def _sync_weighing_slip_business_fields(
db: Session,
*,
inventory: DbInboundOutbound,
user_id: Optional[str],
) -> None:
"""按出入库正文回写关联过磅单业务字段;称重数字一律不动。"""
slips = (
db.query(DbWeighingSlip)
.filter(
DbWeighingSlip.inbound_outbound_id == inventory.id,
DbWeighingSlip.del_flag == 0,
DbWeighingSlip.is_voided == 0,
)
.all()
)
if not slips:
return
partner = (inventory.partner_company or "").strip()
business_type = (inventory.business_type or "").strip()
for slip in slips:
plate = (inventory.vehicle_no or "").strip()
if plate:
slip.vehicle_no = plate
if inventory.product_id:
slip.product_id = inventory.product_id
if inventory.product:
slip.cargo_name = inventory.product
slip.specification = inventory.specification
if business_type:
slip.business_type = business_type
if inventory.plan_no:
slip.plan_no = inventory.plan_no
if inventory.batch_no:
slip.batch_no = inventory.batch_no
if inventory.purchase_plan_id:
slip.purchase_plan_id = inventory.purchase_plan_id
if partner:
if business_type == "入库":
slip.supplier = partner
else:
slip.consignee = partner
slip.update_by = user_id
def revoke_regeneratable_dibang_for_transport(
db: Session,
*,
@@ -654,45 +700,10 @@ def _update_existing_stop_inventory(
dept_id: str,
warnings: list[str],
) -> DibangInventoryLinkResult:
"""就地更新已有出入库可变字段;不可编辑时写 warnings,不抛错。"""
inv_product_id = (inventory.product_id or "").strip()
new_product_id = (product.product_id or "").strip()
if inv_product_id and new_product_id and inv_product_id != new_product_id:
label = inventory.batch_no or inventory.id
raise ValueError(
f"出入库单 {label} 已关联产品,禁止更换为其他产品。请取消地磅后重新勾选,或保留原产品。"
)
"""就地强制同步出入库正文(含已过磅),并回写关联过磅单业务字段。"""
label = inventory.batch_no or inventory.id
editable = _inventory_mutable_editable(db, inventory)
if not editable:
status = (inventory.status or "").strip() or "未知状态"
if _inventory_has_weighing_slip(db, inventory.id):
reason = "已关联地磅单"
else:
reason = f"状态:{status}"
warnings.append(
f"出入库单 {label}({reason})未同步往来单位/车牌/数量等可变字段,请在地磅管理中手工处理"
)
# 备注中计划编号文案仍可校正
desired_remark = f"运输计划 {plan.plan_no}"
if (inventory.remark or "").strip() != desired_remark:
inventory.remark = desired_remark
inventory.update_by = user_id
if (link.remark or "").strip() != desired_remark:
link.remark = desired_remark
link.update_by = user_id
return DibangInventoryLinkResult(
plan_product_id=product.plan_product_id,
product_name=product.product_name or "",
db_purchase_plan_id=link.db_purchase_plan_id or "",
db_inventory_id=inventory.id,
batch_no=inventory.batch_no or "",
plan_no=(inventory.plan_no or plan.plan_no) or "",
)
# —— 可编辑:更新 partner / vehicle / quantity / date(跨月改挂)——
# —— 强制更新 partner / vehicle / 产品快照 / quantity / date(跨月改挂)——
if partner and (inventory.partner_company or "").strip() != partner:
inventory.partner_company = partner
@@ -700,6 +711,13 @@ def _update_existing_stop_inventory(
if plate and (inventory.vehicle_no or "").strip() != plate:
inventory.vehicle_no = plate
new_product_id = (product.product_id or "").strip()
if new_product_id:
snapshot = resolve_crm_product(
db, tenant_no, new_product_id, require_on_shelf=False
)
apply_product_snapshot_to_inventory(inventory, snapshot)
new_qty = float(product.quantity)
old_qty = _resolve_link_contributed_qty(link, inventory)
if abs(new_qty - old_qty) > 1e-9:
@@ -759,6 +777,8 @@ def _update_existing_stop_inventory(
inventory.update_by = user_id
link.update_by = user_id
_sync_weighing_slip_business_fields(db, inventory=inventory, user_id=user_id)
return DibangInventoryLinkResult(
plan_product_id=product.plan_product_id,
product_name=product.product_name or "",