fix(pl): 客户品类安排运输时采用已保存单位换算。

安排自动选证不再忽略计划段次用证中的 manualAllocations,避免瓶升等异单位已填换算仍无法安排。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-16 10:56:29 +08:00
1 parent f865d8ed99
commit 7e515d9ccd
3 files changed
+212 -7

No files matched your search

@@ -1154,3 +1154,144 @@ def test_archive_cover_block_message_customer_rule_wording(monkeypatch):
)
assert '客户名下' in message
assert '合同' not in message
def test_whole_cover_uses_saved_manual_when_units_need_conversion(monkeypatch):
"""计划段次用证已填瓶→升换算时,同市自动选证应采用该证面量,不能因无法自动换算失败。"""
from app.services import pl_quota_customer_select as sel
from app.schemas.pl.quota_planning import ManualQuantity
purchase = _ok_purchase(
id='78bde6ea',
cert_no='G34280697302194',
quantity_unit='sheng',
total_qty=Decimal('2.5'),
)
db = _mock_select_db([purchase], [], [])
monkeypatch.setattr(sel, 'category_ids_for_product', lambda *a, **k: {'cat1'})
source = _remote_source(
requires_transport=False,
quantity=Decimal('5'),
unit='ping',
source_stop_type='delivery',
source_stop_id='d1',
source_stop_product_id='012423E0',
)
saved = [
ManualQuantity(
permit_kind='purchase',
source_id=source.source_id,
permit_id='78bde6ea',
source_quantity=Decimal('5'),
permit_quantity=Decimal('2.5'),
)
]
purchase_ids, transport_ids, manuals = sel.select_whole_cover_certs(
db,
SimpleNamespace(tenant_no='t'),
[source],
vehicle_no='皖H21522',
on_date=date(2026, 11, 16),
manual_allocations=saved,
)
assert purchase_ids == ['78bde6ea']
assert transport_ids == []
assert len(manuals) == 1
assert manuals[0].permit_id == '78bde6ea'
assert manuals[0].permit_quantity == Decimal('2.5')
assert manuals[0].source_quantity == Decimal('5')
def test_whole_cover_hetero_unit_without_manual_still_blocks(monkeypatch):
from app.services import pl_quota_customer_select as sel
purchase = _ok_purchase(quantity_unit='sheng', total_qty=Decimal('2.5'))
db = _mock_select_db([purchase], [], [])
monkeypatch.setattr(sel, 'category_ids_for_product', lambda *a, **k: {'cat1'})
with pytest.raises(HTTPException) as exc:
sel.select_whole_cover_certs(
db,
SimpleNamespace(tenant_no='t'),
[_remote_source(requires_transport=False, quantity=Decimal('5'), unit='ping')],
vehicle_no='皖H21522',
on_date=date(2026, 11, 16),
)
assert exc.value.status_code == 409
assert '无法自动换算' in str(exc.value.detail)
def test_build_usage_request_passes_saved_manuals_to_customer_select(monkeypatch):
"""安排预占在客户品类规则下应把已保存换算传给自动选证。"""
from app.services import pl_quota_planning as planning
from app.schemas.pl.quota_planning import ManualQuantity, 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('5'),
unit='ping',
fingerprint='f' * 64,
snapshot={'customerId': 'cust1'},
requires_transport=False,
)
saved = [
ManualQuantity(
permit_kind='purchase',
source_id=source.source_id,
permit_id='buy1',
source_quantity=Decimal('5'),
permit_quantity=Decimal('2.5'),
)
]
plan = SimpleNamespace(
id='p1',
plan_date=date(2026, 11, 16),
license_plate='皖H21522',
transport_status='pending',
archive_status='unarchived',
cert_quota_rule_version='customer_category',
cert_configuration={
'measurementVersion': 'per_stop_v2',
'manualAllocations': [row.model_dump(by_alias=True) for row in saved],
},
)
captured = {}
def fake_select(db, user, sources, *, vehicle_no, on_date, manual_allocations=()):
captured['manual_allocations'] = list(manual_allocations)
return ['buy1'], [], list(manual_allocations)
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.plan_uses_customer_category',
lambda p: True,
)
monkeypatch.setattr(
'app.services.pl_quota_customer_select.select_whole_cover_certs',
fake_select,
)
monkeypatch.setattr(planning, '_get_row', lambda *a, **k: SimpleNamespace(id='buy1', revision=1))
request, config = planning.build_usage_request(
MagicMock(),
SimpleNamespace(id='u', tenant_no='t', dept_id=''),
'p1',
PlanQuotaOperation(operationId='op1'),
'reserve',
check_conditions=False,
)
assert captured['manual_allocations']
assert captured['manual_allocations'][0].permit_id == 'buy1'
assert config.purchase_cert_ids == ['buy1']
assert request.manual_allocations[0].permit_quantity == Decimal('2.5')