fix(pl): 未安排计划按生效时间走新规则选证。
安排预检不再把空版本当成 legacy 去查合同绑证,与证书齐备待完善判定一致。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e9f3602459
commit
8ac835fa96
3 files changed
+175
-11
No files matched your search
@@ -99,14 +99,15 @@ def stamp_plan_rule_version(db: Session, user, plan) -> str:
|
||||
|
||||
|
||||
def plan_uses_customer_category(plan) -> bool:
|
||||
"""仅看已落库版本;空版本视为 legacy。安排前请用 plan_applies_customer_category_rule。"""
|
||||
return (getattr(plan, 'cert_quota_rule_version', None) or VERSION_LEGACY) == VERSION_CUSTOMER_CATEGORY
|
||||
|
||||
|
||||
def plan_requires_cert_fulfill(db: Session, tenant_no: str, plan) -> bool:
|
||||
"""齐备后是否须进待完善:已打新规则,或尚未安排但按当前时刻将落入新规则。
|
||||
def plan_applies_customer_category_rule(db: Session, tenant_no: str, plan) -> bool:
|
||||
"""业务是否按新规则:已打 customer_category,或尚未安排且按当前时刻将落入新规则。
|
||||
|
||||
安排前 `cert_quota_rule_version` 常为空;不能把空当成 legacy,否则会漏写 pending。
|
||||
已安排并打成 legacy 的计划仍按旧规则,不进待完善。
|
||||
安排前 `cert_quota_rule_version` 常为空;不能把空当成 legacy,否则会误走合同绑证、
|
||||
漏写齐备待完善。已首次安排并打成 legacy 的计划保持旧规则。
|
||||
"""
|
||||
if plan_uses_customer_category(plan):
|
||||
return True
|
||||
@@ -115,6 +116,11 @@ def plan_requires_cert_fulfill(db: Session, tenant_no: str, plan) -> bool:
|
||||
return resolve_rule_version_for_arrange(db, tenant_no) == VERSION_CUSTOMER_CATEGORY
|
||||
|
||||
|
||||
def plan_requires_cert_fulfill(db: Session, tenant_no: str, plan) -> bool:
|
||||
"""齐备后是否须进待完善;与安排是否走新规则同一判定。"""
|
||||
return plan_applies_customer_category_rule(db, tenant_no, plan)
|
||||
|
||||
|
||||
def infer_arranged_at(db: Session, plan) -> Optional[datetime]:
|
||||
"""历史缺字段时用关联运输单创建时间推断首次安排。"""
|
||||
stamped = getattr(plan, 'cert_quota_arranged_at', None)
|
||||
|
||||
@@ -131,8 +131,8 @@ def build_usage_request(db, user, plan_id, operation, action, *, lock=False, che
|
||||
sources, actuals = [], {}
|
||||
else:
|
||||
sources, actuals = extract_sources(db, user, plan_id, mapping, actual=action in ('settle', 'actual_adjust'), lock=lock, preview_vehicle=preview_vehicle, measurement_version=config.measurement_version)
|
||||
from app.services.pl_cert_quota_rule_config_service import plan_uses_customer_category
|
||||
customer_rule = plan_uses_customer_category(plan)
|
||||
from app.services.pl_cert_quota_rule_config_service import plan_applies_customer_category_rule
|
||||
customer_rule = plan_applies_customer_category_rule(db, user.tenant_no, plan)
|
||||
if action in ('reserve', 'resize') and not explicit and sources:
|
||||
if customer_rule:
|
||||
# 新规则:禁止手工选证;按客户+品类整笔自动选证
|
||||
@@ -228,10 +228,10 @@ def preview_plan(db, user, plan_id, config, preview_vehicle=None):
|
||||
|
||||
def save_configuration(db, user, plan_id, config):
|
||||
from app.services.pl_plan_service import record_plan_change
|
||||
from app.services.pl_cert_quota_rule_config_service import plan_uses_customer_category
|
||||
from app.services.pl_cert_quota_rule_config_service import plan_applies_customer_category_rule
|
||||
config = config if isinstance(config, PlanQuotaConfiguration) else PlanQuotaConfiguration.model_validate(config)
|
||||
plan = _plan(db, user, plan_id, lock=True)
|
||||
if plan_uses_customer_category(plan):
|
||||
if plan_applies_customer_category_rule(db, user.tenant_no, plan):
|
||||
raise HTTPException(409, '新规则下安排仅自动选证,不支持手工保存用证配置')
|
||||
if plan.transport_status == 'arranged' or plan.archive_status == 'archived':
|
||||
raise HTTPException(409, '已安排计划请通过调整运输在同一事务重算用证')
|
||||
@@ -338,11 +338,22 @@ def get_plan_quota(db, user, plan_id):
|
||||
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()
|
||||
version = resolve_measurement_version(db, user, plan)
|
||||
from app.services.pl_cert_quota_rule_config_service import plan_uses_customer_category
|
||||
customer_rule = plan_uses_customer_category(plan)
|
||||
from app.services.pl_cert_quota_rule_config_service import (
|
||||
VERSION_LEGACY,
|
||||
plan_applies_customer_category_rule,
|
||||
resolve_rule_version_for_arrange,
|
||||
)
|
||||
customer_rule = plan_applies_customer_category_rule(db, user.tenant_no, plan)
|
||||
stamped = getattr(plan, 'cert_quota_rule_version', None)
|
||||
if stamped:
|
||||
rule_version = stamped
|
||||
elif getattr(plan, 'cert_quota_arranged_at', None) is None:
|
||||
rule_version = resolve_rule_version_for_arrange(db, user.tenant_no)
|
||||
else:
|
||||
rule_version = VERSION_LEGACY
|
||||
result = {'planId': plan.id, 'planNo': plan.plan_no, 'licensePlate': plan.license_plate,
|
||||
'quotaCertWaived': int(getattr(plan, 'quota_cert_waived', 0) or 0) == 1,
|
||||
'certQuotaRuleVersion': getattr(plan, 'cert_quota_rule_version', None) or 'legacy',
|
||||
'certQuotaRuleVersion': rule_version or VERSION_LEGACY,
|
||||
'measurementVersion': version,
|
||||
'configuration': {**(plan.cert_configuration or {}), 'measurementVersion': version}, 'sourceOptions': options,
|
||||
'stopProducts': stop_products,
|
||||
|
||||
@@ -1373,3 +1373,150 @@ def test_build_usage_request_passes_saved_manuals_to_customer_select(monkeypatch
|
||||
assert captured['manual_allocations'][0].permit_id == 'buy1'
|
||||
assert config.purchase_cert_ids == ['buy1']
|
||||
assert request.manual_allocations[0].permit_quantity == Decimal('2.5')
|
||||
|
||||
|
||||
def test_unarranged_plan_build_usage_uses_customer_rule_by_effective_at(monkeypatch):
|
||||
"""尚未安排且版本为空时,按生效时间走新规则自动选证,不查合同绑证。"""
|
||||
from app.services import pl_quota_planning as planning
|
||||
from app.schemas.pl.quota_planning import PlanQuotaOperation
|
||||
from app.schemas.pl.quota_usage import UsageSource
|
||||
|
||||
source = UsageSource(
|
||||
source_stop_type='delivery',
|
||||
source_stop_id='d1',
|
||||
source_stop_product_id='sp1',
|
||||
plan_product_id='pp1',
|
||||
contract_id='c1',
|
||||
contract_product_id='cp1',
|
||||
product_id='prod1',
|
||||
quantity=Decimal('33'),
|
||||
unit='dun',
|
||||
fingerprint='f' * 64,
|
||||
snapshot={'customerId': 'cust1'},
|
||||
requires_transport=True,
|
||||
)
|
||||
plan = SimpleNamespace(
|
||||
id='p1',
|
||||
plan_date=date(2026, 11, 14),
|
||||
license_plate='皖R48299',
|
||||
transport_status='pending',
|
||||
archive_status='unarchived',
|
||||
cert_quota_rule_version=None,
|
||||
cert_quota_arranged_at=None,
|
||||
cert_configuration={'measurementVersion': 'per_stop_v2'},
|
||||
tenant_no='t',
|
||||
)
|
||||
called = {'customer_select': 0, 'contract_bind': 0}
|
||||
|
||||
def fake_select(*args, **kwargs):
|
||||
called['customer_select'] += 1
|
||||
return ['buy1'], ['move1'], []
|
||||
|
||||
def fake_contract_bind(*args, **kwargs):
|
||||
called['contract_bind'] += 1
|
||||
raise AssertionError('未安排计划不应再走合同绑证')
|
||||
|
||||
monkeypatch.setattr(planning, '_plan', lambda *a, **k: plan)
|
||||
monkeypatch.setattr(planning, 'extract_sources', lambda *a, **k: ([source], {}))
|
||||
monkeypatch.setattr(planning, 'resolve_measurement_version', lambda *a, **k: 'per_stop_v2')
|
||||
monkeypatch.setattr(planning, 'plan_execution_lock_reason', lambda *a, **k: None)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_cert_quota_rule_config_service.get_effective_at',
|
||||
lambda *a, **k: datetime(2026, 11, 15, 20, 0, 0),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_quota_customer_select.select_whole_cover_certs',
|
||||
fake_select,
|
||||
)
|
||||
monkeypatch.setattr(planning, 'resolve_contract_bound_certs', fake_contract_bind)
|
||||
monkeypatch.setattr(
|
||||
planning,
|
||||
'_get_row',
|
||||
lambda *a, **k: SimpleNamespace(id='x', revision=1),
|
||||
)
|
||||
|
||||
request, config = planning.build_usage_request(
|
||||
MagicMock(),
|
||||
SimpleNamespace(id='u', tenant_no='t', dept_id=''),
|
||||
'p1',
|
||||
PlanQuotaOperation(operationId='arrange-check'),
|
||||
'reserve',
|
||||
check_conditions=False,
|
||||
)
|
||||
assert called['customer_select'] == 1
|
||||
assert called['contract_bind'] == 0
|
||||
assert config.purchase_cert_ids == ['buy1']
|
||||
assert config.transport_cert_ids == ['move1']
|
||||
assert request.purchase_cert_ids == ['buy1']
|
||||
|
||||
|
||||
def test_arranged_legacy_plan_build_usage_keeps_contract_bind(monkeypatch):
|
||||
"""已首次安排并打成 legacy 的计划,安排/改派预占仍走合同绑证。"""
|
||||
from app.services import pl_quota_planning as planning
|
||||
from app.schemas.pl.quota_planning import PlanQuotaOperation
|
||||
from app.schemas.pl.quota_usage import UsageSource
|
||||
|
||||
source = UsageSource(
|
||||
source_stop_type='delivery',
|
||||
source_stop_id='d1',
|
||||
source_stop_product_id='sp1',
|
||||
plan_product_id='pp1',
|
||||
contract_id='c1',
|
||||
contract_product_id='cp1',
|
||||
product_id='prod1',
|
||||
quantity=Decimal('33'),
|
||||
unit='dun',
|
||||
fingerprint='f' * 64,
|
||||
snapshot={'customerId': 'cust1'},
|
||||
requires_transport=True,
|
||||
)
|
||||
plan = SimpleNamespace(
|
||||
id='p1',
|
||||
plan_date=date(2026, 11, 14),
|
||||
license_plate='皖R48299',
|
||||
transport_status='arranged',
|
||||
archive_status='unarchived',
|
||||
cert_quota_rule_version='legacy',
|
||||
cert_quota_arranged_at=datetime(2026, 11, 14, 10, 0, 0),
|
||||
cert_configuration={'measurementVersion': 'per_stop_v2'},
|
||||
tenant_no='t',
|
||||
)
|
||||
called = {'customer_select': 0, 'contract_bind': 0}
|
||||
|
||||
def fake_select(*args, **kwargs):
|
||||
called['customer_select'] += 1
|
||||
return ['buy1'], ['move1'], []
|
||||
|
||||
def fake_contract_bind(*args, **kwargs):
|
||||
called['contract_bind'] += 1
|
||||
return ['buy1'], ['move1']
|
||||
|
||||
monkeypatch.setattr(planning, '_plan', lambda *a, **k: plan)
|
||||
monkeypatch.setattr(planning, 'extract_sources', lambda *a, **k: ([source], {}))
|
||||
monkeypatch.setattr(planning, 'resolve_measurement_version', lambda *a, **k: 'per_stop_v2')
|
||||
monkeypatch.setattr(planning, 'plan_execution_lock_reason', lambda *a, **k: None)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_cert_quota_rule_config_service.get_effective_at',
|
||||
lambda *a, **k: datetime(2026, 11, 15, 20, 0, 0),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.pl_quota_customer_select.select_whole_cover_certs',
|
||||
fake_select,
|
||||
)
|
||||
monkeypatch.setattr(planning, 'resolve_contract_bound_certs', fake_contract_bind)
|
||||
monkeypatch.setattr(
|
||||
planning,
|
||||
'_get_row',
|
||||
lambda *a, **k: SimpleNamespace(id='x', revision=1),
|
||||
)
|
||||
|
||||
planning.build_usage_request(
|
||||
MagicMock(),
|
||||
SimpleNamespace(id='u', tenant_no='t', dept_id=''),
|
||||
'p1',
|
||||
PlanQuotaOperation(operationId='arrange-check'),
|
||||
'resize',
|
||||
check_conditions=False,
|
||||
)
|
||||
assert called['contract_bind'] == 1
|
||||
assert called['customer_select'] == 0
|
||||
Reference in new issue
Block a user