fix(pl): 修复客户品类证件额度选证与回显问题。

优先消耗有到期日的证件,补齐品类产品选择与运输证适用品类回显,避免新规则安排与建档展示失败。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-15 07:13:42 +08:00
1 parent b0c1bca683
commit e8cda33a3c
10 files changed
+330 -44

No files matched your search

@@ -238,6 +238,84 @@ def test_legacy_waive_still_skips_settle(monkeypatch):
assert result.get('waived') is True
def test_null_valid_to_sorted_after_dated_certs():
"""无到期日的证不能排在有到期日之前(MySQL NULL ASC 默认会错序)。"""
from app.services import pl_quota_customer_select as sel
from app.schemas.pl.quota_usage import UsageSource
from app.models.pl_quota_models import PlPurchaseCert, PlCertQtyLog
source = UsageSource(
source_stop_type='delivery',
source_stop_id='s1',
source_stop_product_id='sp1',
plan_product_id='pp1',
contract_id='c1',
contract_product_id='cp1',
product_id='prod1',
quantity=Decimal('10'),
unit='吨',
fingerprint='z' * 64,
snapshot={'customerId': 'cust1'},
requires_transport=False,
)
unbounded = SimpleNamespace(
id='buy-null', customer_id='cust1', category_id='cat1', status='active',
verified_by='u', verified_time=datetime.now(), valid_from=None, valid_to=None,
total_qty=Decimal('100'), used_qty=Decimal('0'), reserved_qty=Decimal('0'),
quantity_unit='吨', opening_single_use_consumed=0,
)
dated = SimpleNamespace(
id='buy-dated', customer_id='cust1', category_id='cat1', status='active',
verified_by='u', verified_time=datetime.now(), valid_from=None, valid_to=date(2026, 12, 1),
total_qty=Decimal('100'), used_qty=Decimal('0'), reserved_qty=Decimal('0'),
quantity_unit='吨', opening_single_use_consumed=0,
)
class PurchaseQuery:
def __init__(self):
self._order = None
def filter(self, *a, **k):
return self
def order_by(self, *exprs):
self._order = exprs
return self
def all(self):
# 应先按 valid_to IS NULL 升序(非空在前),再按 valid_to ASC
assert self._order is not None and len(self._order) >= 2
return [dated, unbounded]
class LogQuery:
def filter_by(self, **k):
return self
def first(self):
return SimpleNamespace(id='opening')
db = MagicMock()
def q_side(model):
if model is PlPurchaseCert:
return PurchaseQuery()
if model is PlCertQtyLog:
return LogQuery()
return MagicMock()
db.query.side_effect = q_side
orig = sel.category_ids_for_product
sel.category_ids_for_product = lambda db, tenant, pid: {'cat1'}
try:
purchase_ids, _, manuals = sel.select_whole_cover_certs(
db, SimpleNamespace(tenant_no='t'), [source], vehicle_no='皖A1', on_date=date(2026, 11, 14),
)
finally:
sel.category_ids_for_product = orig
assert purchase_ids == ['buy-dated']
assert manuals[0].permit_id == 'buy-dated'
def test_same_city_source_skips_transport_requirement():
"""同市来源 requires_transport=False,整笔选证可不带运输证。"""
from app.services import pl_quota_customer_select as sel