fix(pl): 内部站点清空合同,并隐藏已释放关联证件。
避免山峡/云栋装送段残留外部合同选证,以及 released 用量行仍出现在计划详情关联证件中。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
eab882f92c
commit
2045e7de89
6 files changed
+109
-4
No files matched your search
@@ -831,6 +831,15 @@ def assert_stop_contract_required(
|
||||
raise HTTPException(status_code=422, detail=f"请为{side_label}选择合同")
|
||||
|
||||
|
||||
def _clear_internal_stop_product_contracts(stop) -> None:
|
||||
"""山峡/云栋段不占用外部合同;客户端若仍回传产品行合同字段,保存前强制清空。"""
|
||||
if stop is None or not is_internal_customer_name(getattr(stop, 'customer_name', None)):
|
||||
return
|
||||
for prod in getattr(stop, 'products', None) or []:
|
||||
prod.contract_id = None
|
||||
prod.contract_product_id = None
|
||||
|
||||
|
||||
def _resolve_stop_contracts(
|
||||
db: Session,
|
||||
tenant_no: str,
|
||||
@@ -975,6 +984,7 @@ def validate_plan_stops_write(
|
||||
"装货合同",
|
||||
expected_supplier_name=entity_name,
|
||||
)
|
||||
_clear_internal_stop_product_contracts(load)
|
||||
|
||||
resolved_deliveries: list[PlanDeliveryBody] = []
|
||||
for idx, delivery in enumerate(deliveries, start=1):
|
||||
@@ -990,6 +1000,7 @@ def validate_plan_stops_write(
|
||||
f"送货段#{idx}合同",
|
||||
expected_supplier_name=entity_name,
|
||||
)
|
||||
_clear_internal_stop_product_contracts(delivery)
|
||||
resolved_deliveries.append(delivery)
|
||||
|
||||
enrich_stop_addresses_from_library(db, tenant_no, load, resolved_deliveries)
|
||||
@@ -1443,6 +1454,7 @@ def replace_plan_stops(
|
||||
stop.customer_name, kind, body.generate_dibang, body.dibang_direction)
|
||||
stop.same_city = 1 if int(getattr(body, "same_city", 0) or 0) == 1 else 0
|
||||
stop.del_flag, stop.update_by, stop.update_time = 0, user_id, now
|
||||
internal_stop = is_internal_customer_name(body.customer_name)
|
||||
model = PlPlanLoadProduct if kind == 'load' else PlPlanDeliveryProduct
|
||||
for idx, (row, product, linked) in enumerate(product_jobs or []):
|
||||
if row is None:
|
||||
@@ -1453,6 +1465,10 @@ def replace_plan_stops(
|
||||
for field in product_fields:
|
||||
if field in product.model_fields_set or row.id is None:
|
||||
setattr(row, field, getattr(product, field))
|
||||
if internal_stop:
|
||||
# 内部客户段即使客户端漏清/省略合同字段,落库也不得保留外部合同绑定。
|
||||
row.contract_id = None
|
||||
row.contract_product_id = None
|
||||
row.plan_product_id = linked
|
||||
row.order_num, row.update_by, row.update_time = idx, user_id, now
|
||||
for row in removed:
|
||||
|
||||
@@ -336,7 +336,18 @@ def get_plan_quota(db, user, plan_id):
|
||||
if row.contract_product_id in cert['productIds']]})
|
||||
if product.is_toxic == 1:
|
||||
options.append(item)
|
||||
usages = db.query(PlPlanCertUsage).filter_by(tenant_no=user.tenant_no, plan_id=plan_id, del_flag=0).order_by(PlPlanCertUsage.create_time.desc()).all()
|
||||
# 关联证件展示只含仍占用/结转中的用量;released 已从配置摘除,不再算作当前关联。
|
||||
usages = (
|
||||
db.query(PlPlanCertUsage)
|
||||
.filter(
|
||||
PlPlanCertUsage.tenant_no == user.tenant_no,
|
||||
PlPlanCertUsage.plan_id == plan_id,
|
||||
PlPlanCertUsage.del_flag == 0,
|
||||
PlPlanCertUsage.usage_status != 'released',
|
||||
)
|
||||
.order_by(PlPlanCertUsage.create_time.desc())
|
||||
.all()
|
||||
)
|
||||
version = resolve_measurement_version(db, user, plan)
|
||||
from app.services.pl_cert_quota_rule_config_service import (
|
||||
VERSION_LEGACY,
|
||||
|
||||
@@ -185,9 +185,9 @@ def _execute(db,user,r,digest,plan):
|
||||
raise ValueError('已执行用证不得释放预占,请先结转实际量')
|
||||
for k,u in usages.items():
|
||||
certs[k].reserved_qty-=u.reserved_qty; u.reserved_qty=ZERO
|
||||
u.usage_status='released'
|
||||
u.usage_status='released'; u.del_flag=1
|
||||
for item in items[k]:
|
||||
item.reserved_qty=ZERO; item.item_status='released'
|
||||
item.reserved_qty=ZERO; item.item_status='released'; item.del_flag=1
|
||||
else:
|
||||
supplement=_settle(db,user,r,certs,usages,items,plan)
|
||||
for k,u in usages.items():
|
||||
@@ -343,7 +343,12 @@ def _reserve(db,user,r,plan,certs,usages,items,selected,preview=False,lock_previ
|
||||
if item.actual_cert_qty is not None or item.item_status in ('in_transit','settled'): raise ValueError('已执行明细不能移除')
|
||||
item.del_flag=1; item.reserved_qty=ZERO; item.item_status='released'
|
||||
new_qty=sum((a.permit_quantity for a in allocations),ZERO)
|
||||
c.reserved_qty+=new_qty-u.reserved_qty; u.reserved_qty=new_qty; u.usage_status='reserved' if allocations else 'released'
|
||||
c.reserved_qty+=new_qty-u.reserved_qty; u.reserved_qty=new_qty
|
||||
# 无分配时标记释放并软删,详情「关联证件」不再展示;再预占同证时复用本行并恢复 del_flag。
|
||||
if allocations:
|
||||
u.usage_status='reserved'; u.del_flag=0
|
||||
else:
|
||||
u.usage_status='released'; u.del_flag=1
|
||||
u.cert_revision=c.revision; u.confirmed_by=user.id; u.confirmed_time=datetime.now()
|
||||
u.confirmation_snapshot={'executed':False,'vehicleNo':r.vehicle_no,
|
||||
'measurementVersion': next((s.snapshot.get('measurementVersion', 'legacy_single_side') for s in r.sources), 'legacy_single_side')}
|
||||
|
||||
@@ -42,6 +42,28 @@ def test_internal_stop_skips_belongs_company_contract_check():
|
||||
assert links == []
|
||||
|
||||
|
||||
def test_internal_stop_clears_product_contract_fields():
|
||||
from types import SimpleNamespace
|
||||
from app.services.pl_plan_stop_service import _clear_internal_stop_product_contracts
|
||||
|
||||
stop = SimpleNamespace(
|
||||
customer_name="安庆市山峡化工有限公司",
|
||||
products=[
|
||||
SimpleNamespace(contract_id="c-dongheng", contract_product_id="line-1"),
|
||||
SimpleNamespace(contract_id="c-2", contract_product_id="line-2"),
|
||||
],
|
||||
)
|
||||
_clear_internal_stop_product_contracts(stop)
|
||||
assert all(p.contract_id is None and p.contract_product_id is None for p in stop.products)
|
||||
|
||||
external = SimpleNamespace(
|
||||
customer_name="安庆市鑫富化工有限责任公司",
|
||||
products=[SimpleNamespace(contract_id="keep", contract_product_id="line")],
|
||||
)
|
||||
_clear_internal_stop_product_contracts(external)
|
||||
assert external.products[0].contract_id == "keep"
|
||||
|
||||
|
||||
def test_belongs_company_contract_error_is_not_swallowed():
|
||||
mismatch = HTTPException(status_code=422, detail="装货合同的交易主体与归属公司不一致")
|
||||
empty = HTTPException(status_code=422, detail="请先选择归属公司")
|
||||
|
||||
@@ -353,3 +353,49 @@ def test_plan_quota_usages_include_cert_attachment_urls(case):
|
||||
assert usages['p1']['attachmentUrls'] == 'https://example.com/buy.jpg'
|
||||
assert usages['p1']['certNo'] == 'G-BUY'
|
||||
assert usages['t1']['attachmentUrls'] in ('', None)
|
||||
|
||||
|
||||
def test_plan_quota_usages_hide_released_rows(case, monkeypatch):
|
||||
"""详情关联证件不展示已释放用量,避免改客户后仍显示旧购买证。"""
|
||||
from app.models.crm_models import CrmContract
|
||||
from app.models.pl_quota_models import (
|
||||
PlCertQuotaRuleConfig, PlPlanCertUsage, PlPlanCertUsageItem, PlPurchaseCert,
|
||||
)
|
||||
from app.services.pl_quota_planning import get_plan_quota
|
||||
c = case
|
||||
for model in (CrmContract, PlCertQuotaRuleConfig, PlPlanCertUsage, PlPlanCertUsageItem, PlPurchaseCert):
|
||||
model.__table__.create(c.db.bind, checkfirst=True)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_cert_quota_rule_config_service.plan_applies_customer_category_rule',
|
||||
lambda *a, **k: False,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_cert_quota_rule_config_service.resolve_rule_version_for_arrange',
|
||||
lambda *a, **k: 'legacy',
|
||||
)
|
||||
# 本用例只断言 usages 过滤;清空站点合同避免 get_plan_quota 拉合同表
|
||||
c.db.get(c.m.PlPlanLoadProduct, 'lp').contract_id = None
|
||||
c.db.get(c.m.PlPlanDeliveryProduct, 'dp').contract_id = None
|
||||
c.db.add(PlPurchaseCert(
|
||||
id='p-keep', cert_no='G-KEEP', product_name_on_cert='硫酸', valid_use_text='多次有效',
|
||||
buyer_name='买方', seller_name='卖方', total_qty=33, quantity_unit='吨',
|
||||
tenant_no='t', status='active',
|
||||
))
|
||||
c.db.add(PlPurchaseCert(
|
||||
id='p-old', cert_no='G-OLD', product_name_on_cert='硫酸', valid_use_text='多次有效',
|
||||
buyer_name='买方', seller_name='卖方', total_qty=33, quantity_unit='吨',
|
||||
tenant_no='t', status='active',
|
||||
))
|
||||
c.db.add(PlPlanCertUsage(
|
||||
id='u-keep', tenant_no='t', plan_id='plan', plan_no_snapshot='PLAN',
|
||||
cert_type='purchase', cert_id='p-keep', cert_no_snapshot='G-KEEP', cert_revision=1,
|
||||
quantity_unit='吨', reserved_qty=5, used_qty=0, usage_status='reserved', del_flag=0,
|
||||
))
|
||||
c.db.add(PlPlanCertUsage(
|
||||
id='u-released', tenant_no='t', plan_id='plan', plan_no_snapshot='PLAN',
|
||||
cert_type='purchase', cert_id='p-old', cert_no_snapshot='G-OLD', cert_revision=1,
|
||||
quantity_unit='吨', reserved_qty=0, used_qty=0, usage_status='released', del_flag=0,
|
||||
))
|
||||
c.db.flush()
|
||||
cert_nos = [u['certNo'] for u in get_plan_quota(c.db, c.user, 'plan')['usages']]
|
||||
assert cert_nos == ['G-KEEP']
|
||||
@@ -330,11 +330,16 @@ def test_no_cert_operation_is_still_idempotent(case):
|
||||
def test_removal_and_restore_preserve_item_id(case):
|
||||
c=case; run(c)
|
||||
ids={i.id for i in c.db.query(c.m.PlPlanCertUsageItem).all()}
|
||||
usage_ids={u.id for u in c.db.query(c.m.PlPlanCertUsage).all()}
|
||||
run(c,action='resize',operation_id='clear',sources=[],purchase_cert_ids=[],transport_cert_ids=[])
|
||||
assert all(i.del_flag==1 for i in c.db.query(c.m.PlPlanCertUsageItem).all())
|
||||
# 清空分配后用量行标记 released 并软删,详情关联证件不再展示
|
||||
assert all(u.usage_status=='released' and u.del_flag==1 for u in c.db.query(c.m.PlPlanCertUsage).all())
|
||||
run(c,action='resize',operation_id='restore')
|
||||
assert {i.id for i in c.db.query(c.m.PlPlanCertUsageItem).all()}==ids
|
||||
assert {u.id for u in c.db.query(c.m.PlPlanCertUsage).all()}==usage_ids
|
||||
assert all(i.del_flag==0 for i in c.db.query(c.m.PlPlanCertUsageItem).all())
|
||||
assert all(u.usage_status=='reserved' and u.del_flag==0 for u in c.db.query(c.m.PlPlanCertUsage).all())
|
||||
|
||||
def _second_pair(c, cross=False):
|
||||
for kind,model,old_id,new_id,total in [('purchase',c.m.PlPurchaseCert,'p','p2',100),('transport',c.m.PlTransportCert,'t','t2',100)]:
|
||||
|
||||
Reference in new issue
Block a user