|
@@ -698,21 +698,29 @@ def _update_existing_stop_inventory(
|
|
|
"""就地强制同步出入库正文(含已过磅),并回写关联过磅单业务字段。"""
|
|
"""就地强制同步出入库正文(含已过磅),并回写关联过磅单业务字段。"""
|
|
|
label = inventory.batch_no or inventory.id
|
|
label = inventory.batch_no or inventory.id
|
|
|
|
|
|
|
|
- # —— 强制更新 partner / vehicle / 产品快照 / quantity / date(跨月改挂)——
|
|
|
|
|
- if partner and (inventory.partner_company or "").strip() != partner:
|
|
|
|
|
|
|
+ old_partner = (inventory.partner_company or "").strip()
|
|
|
|
|
+ old_product_id = (inventory.product_id or "").strip()
|
|
|
|
|
+ old_spec = (inventory.specification or "").strip()
|
|
|
|
|
+ old_plate = (inventory.vehicle_no or "").strip()
|
|
|
|
|
+ old_date = _normalize_to_date(inventory.date)
|
|
|
|
|
+
|
|
|
|
|
+ # —— 强制更新 partner / vehicle / 产品快照 / quantity ——
|
|
|
|
|
+ if partner and old_partner != partner:
|
|
|
inventory.partner_company = partner
|
|
inventory.partner_company = partner
|
|
|
|
|
|
|
|
plate = (license_plate or "").strip()
|
|
plate = (license_plate or "").strip()
|
|
|
- if plate and (inventory.vehicle_no or "").strip() != plate:
|
|
|
|
|
|
|
+ plate_changed = bool(plate and old_plate != plate)
|
|
|
|
|
+ if plate_changed:
|
|
|
inventory.vehicle_no = plate
|
|
inventory.vehicle_no = plate
|
|
|
|
|
|
|
|
- new_product_id = (product.product_id or "").strip()
|
|
|
|
|
- if new_product_id:
|
|
|
|
|
|
|
+ incoming_product_id = (product.product_id or "").strip()
|
|
|
|
|
+ if incoming_product_id:
|
|
|
snapshot = resolve_crm_product(
|
|
snapshot = resolve_crm_product(
|
|
|
- db, tenant_no, new_product_id, require_on_shelf=False
|
|
|
|
|
|
|
+ db, tenant_no, incoming_product_id, require_on_shelf=False
|
|
|
)
|
|
)
|
|
|
apply_product_snapshot_to_inventory(inventory, snapshot)
|
|
apply_product_snapshot_to_inventory(inventory, snapshot)
|
|
|
|
|
|
|
|
|
|
+ # 数量差量先作用于当前挂靠月计划(改挂前);改挂时会整额扣回旧计划
|
|
|
new_qty = float(product.quantity)
|
|
new_qty = float(product.quantity)
|
|
|
old_qty = _resolve_link_contributed_qty(link, inventory)
|
|
old_qty = _resolve_link_contributed_qty(link, inventory)
|
|
|
if abs(new_qty - old_qty) > 1e-9:
|
|
if abs(new_qty - old_qty) > 1e-9:
|
|
@@ -732,7 +740,57 @@ def _update_existing_stop_inventory(
|
|
|
inventory.quantity = Decimal(str(new_qty))
|
|
inventory.quantity = Decimal(str(new_qty))
|
|
|
link.contributed_qty = Decimal(str(new_qty))
|
|
link.contributed_qty = Decimal(str(new_qty))
|
|
|
|
|
|
|
|
- old_date = _normalize_to_date(inventory.date)
|
|
|
|
|
|
|
+ new_partner = (inventory.partner_company or "").strip()
|
|
|
|
|
+ new_product_id = (inventory.product_id or "").strip()
|
|
|
|
|
+ new_spec = (inventory.specification or "").strip()
|
|
|
|
|
+ need_remount = (
|
|
|
|
|
+ (partner and old_partner != new_partner)
|
|
|
|
|
+ or (old_product_id and new_product_id and old_product_id != new_product_id)
|
|
|
|
|
+ or (old_spec != new_spec and bool(new_product_id or old_product_id))
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ remounted = False
|
|
|
|
|
+ if need_remount:
|
|
|
|
|
+ qty = _resolve_link_contributed_qty(link, inventory)
|
|
|
|
|
+ _subtract_plan_contribution(
|
|
|
|
|
+ db,
|
|
|
|
|
+ tenant_no=tenant_no,
|
|
|
|
|
+ link=link,
|
|
|
|
|
+ inventory=inventory,
|
|
|
|
|
+ user_id=user_id,
|
|
|
|
|
+ )
|
|
|
|
|
+ try:
|
|
|
|
|
+ new_db_plan = _ensure_monthly_db_plan_for_inventory(
|
|
|
|
|
+ db,
|
|
|
|
|
+ tenant_no=tenant_no,
|
|
|
|
|
+ plan=plan,
|
|
|
|
|
+ inventory=inventory,
|
|
|
|
|
+ dibang_date=dibang_date,
|
|
|
|
|
+ contributed_qty=qty,
|
|
|
|
|
+ user_id=user_id,
|
|
|
|
|
+ dept_id=dept_id or "",
|
|
|
|
|
+ )
|
|
|
|
|
+ link.db_purchase_plan_id = new_db_plan.id
|
|
|
|
|
+ inventory.purchase_plan_id = new_db_plan.id
|
|
|
|
|
+ inventory.plan_no = (new_db_plan.plan_no or "").strip() or inventory.plan_no
|
|
|
|
|
+ remounted = True
|
|
|
|
|
+ except ValueError as exc:
|
|
|
|
|
+ warnings.append(f"出入库单 {label} 往来/产品改挂失败:{exc}")
|
|
|
|
|
+ elif plate_changed and link.db_purchase_plan_id:
|
|
|
|
|
+ db_plan = (
|
|
|
|
|
+ db.query(DbPurchasePlan)
|
|
|
|
|
+ .filter(
|
|
|
|
|
+ DbPurchasePlan.id == link.db_purchase_plan_id,
|
|
|
|
|
+ DbPurchasePlan.tenant_no == tenant_no,
|
|
|
|
|
+ DbPurchasePlan.del_flag == 0,
|
|
|
|
|
+ )
|
|
|
|
|
+ .first()
|
|
|
|
|
+ )
|
|
|
|
|
+ if db_plan:
|
|
|
|
|
+ db_plan.vehicle_no = plate
|
|
|
|
|
+ db_plan.update_by = user_id
|
|
|
|
|
+
|
|
|
|
|
+ # —— date:同月仅改日期;跨月改挂(本轮已因往来/产品改挂则不再二次扣量)——
|
|
|
if old_date != dibang_date:
|
|
if old_date != dibang_date:
|
|
|
same_month = (
|
|
same_month = (
|
|
|
old_date is not None
|
|
old_date is not None
|
|
@@ -740,7 +798,7 @@ def _update_existing_stop_inventory(
|
|
|
and old_date.month == dibang_date.month
|
|
and old_date.month == dibang_date.month
|
|
|
)
|
|
)
|
|
|
inventory.date = dibang_date
|
|
inventory.date = dibang_date
|
|
|
- if not same_month:
|
|
|
|
|
|
|
+ if not same_month and not remounted:
|
|
|
qty = _resolve_link_contributed_qty(link, inventory)
|
|
qty = _resolve_link_contributed_qty(link, inventory)
|
|
|
_subtract_plan_contribution(
|
|
_subtract_plan_contribution(
|
|
|
db,
|
|
db,
|