Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e515d9ccd |
No files matched your search
@@ -80,6 +80,49 @@ def _permit_qty_for(source_qty: Decimal, source_unit: str, cert_unit: str) -> De
|
|||||||
return (source_qty * Decimal(str(ratio))).quantize(Decimal('0.000001'))
|
return (source_qty * Decimal(str(ratio))).quantize(Decimal('0.000001'))
|
||||||
|
|
||||||
|
|
||||||
|
def _manual_field(row, *names):
|
||||||
|
if isinstance(row, dict):
|
||||||
|
for name in names:
|
||||||
|
if name in row and row[name] is not None:
|
||||||
|
return row[name]
|
||||||
|
return None
|
||||||
|
for name in names:
|
||||||
|
value = getattr(row, name, None)
|
||||||
|
if value is not None:
|
||||||
|
return value
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _saved_permit_qty(manuals, kind, source_id, permit_id, source_qty: Decimal):
|
||||||
|
"""计划段次用证已保存的人工换算;来源量变化时按原比例缩放。"""
|
||||||
|
for row in manuals or ():
|
||||||
|
if (
|
||||||
|
str(_manual_field(row, 'permit_kind', 'permitKind') or '') != kind
|
||||||
|
or str(_manual_field(row, 'source_id', 'sourceId') or '') != str(source_id)
|
||||||
|
or str(_manual_field(row, 'permit_id', 'permitId') or '') != str(permit_id)
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
saved_src = Decimal(str(_manual_field(row, 'source_quantity', 'sourceQuantity') or '0'))
|
||||||
|
saved_permit = Decimal(str(_manual_field(row, 'permit_quantity', 'permitQuantity') or '0'))
|
||||||
|
if saved_src <= 0 or saved_permit <= 0:
|
||||||
|
return None
|
||||||
|
current = Decimal(source_qty)
|
||||||
|
if saved_src == current:
|
||||||
|
return saved_permit
|
||||||
|
return (saved_permit * current / saved_src).quantize(Decimal('0.000001'))
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _need_permit_qty(source_qty, source_unit, cert_unit, manuals, kind, source_id, permit_id):
|
||||||
|
try:
|
||||||
|
return _permit_qty_for(Decimal(source_qty), source_unit, cert_unit)
|
||||||
|
except HTTPException:
|
||||||
|
manual_qty = _saved_permit_qty(manuals, kind, source_id, permit_id, Decimal(source_qty))
|
||||||
|
if manual_qty is None:
|
||||||
|
raise
|
||||||
|
return manual_qty
|
||||||
|
|
||||||
|
|
||||||
def _customer_id_from_source(source) -> str:
|
def _customer_id_from_source(source) -> str:
|
||||||
snap = source.snapshot or {}
|
snap = source.snapshot or {}
|
||||||
cid = snap.get('customerId') or snap.get('customer_id')
|
cid = snap.get('customerId') or snap.get('customer_id')
|
||||||
@@ -181,13 +224,16 @@ def _linked_transports(db, user, customer_id, purchase_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _diagnose_select_failure(db, user, source, *, vehicle_no: str, on_date: date, candidates):
|
def _diagnose_select_failure(
|
||||||
|
db, user, source, *, vehicle_no: str, on_date: date, candidates, manual_allocations=(),
|
||||||
|
):
|
||||||
"""选证失败时按证据优先给出单一可执行原因,避免笼统兜底句。"""
|
"""选证失败时按证据优先给出单一可执行原因,避免笼统兜底句。"""
|
||||||
place = '同市' if not source.requires_transport else '异地'
|
place = '同市' if not source.requires_transport else '异地'
|
||||||
plate = (vehicle_no or '').strip()
|
plate = (vehicle_no or '').strip()
|
||||||
need_qty = Decimal(source.quantity)
|
need_qty = Decimal(source.quantity)
|
||||||
customer_label = _source_customer_label(source)
|
customer_label = _source_customer_label(source)
|
||||||
product_label = _source_product_label(db, user, source)
|
product_label = _source_product_label(db, user, source)
|
||||||
|
manuals = manual_allocations or ()
|
||||||
|
|
||||||
if not candidates:
|
if not candidates:
|
||||||
product_part = f'产品「{product_label}」' if product_label else '该产品'
|
product_part = f'产品「{product_label}」' if product_label else '该产品'
|
||||||
@@ -227,7 +273,10 @@ def _diagnose_select_failure(db, user, source, *, vehicle_no: str, on_date: date
|
|||||||
consumed_buy.append(purchase)
|
consumed_buy.append(purchase)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
need = _permit_qty_for(need_qty, source.unit, purchase.quantity_unit)
|
need = _need_permit_qty(
|
||||||
|
need_qty, source.unit, purchase.quantity_unit,
|
||||||
|
manuals, 'purchase', source.source_id, purchase.id,
|
||||||
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
unit_buy.append(purchase)
|
unit_buy.append(purchase)
|
||||||
continue
|
continue
|
||||||
@@ -263,7 +312,10 @@ def _diagnose_select_failure(db, user, source, *, vehicle_no: str, on_date: date
|
|||||||
vehicle_miss.append(transport)
|
vehicle_miss.append(transport)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
t_need = _permit_qty_for(need_qty, source.unit, transport.quantity_unit)
|
t_need = _need_permit_qty(
|
||||||
|
need_qty, source.unit, transport.quantity_unit,
|
||||||
|
manuals, 'transport', source.source_id, transport.id,
|
||||||
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
unit_move.append(transport)
|
unit_move.append(transport)
|
||||||
continue
|
continue
|
||||||
@@ -374,10 +426,13 @@ def _diagnose_select_failure(db, user, source, *, vehicle_no: str, on_date: date
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def select_whole_cover_certs(db, user, sources, *, vehicle_no: str, on_date: date):
|
def select_whole_cover_certs(
|
||||||
|
db, user, sources, *, vehicle_no: str, on_date: date, manual_allocations=(),
|
||||||
|
):
|
||||||
"""返回 purchase_cert_ids, transport_cert_ids, manual_allocations。
|
"""返回 purchase_cert_ids, transport_cert_ids, manual_allocations。
|
||||||
|
|
||||||
每个来源必须由一张购买证整笔覆盖;异地再配一张关联且车号/余量够的运输证。
|
每个来源必须由一张购买证整笔覆盖;异地再配一张关联且车号/余量够的运输证。
|
||||||
|
单位无法自动换算时,使用计划段次用证已保存的人工换算量。
|
||||||
"""
|
"""
|
||||||
if not sources:
|
if not sources:
|
||||||
return [], [], []
|
return [], [], []
|
||||||
@@ -385,6 +440,7 @@ def select_whole_cover_certs(db, user, sources, *, vehicle_no: str, on_date: dat
|
|||||||
purchase_ids = []
|
purchase_ids = []
|
||||||
transport_ids = []
|
transport_ids = []
|
||||||
manuals = []
|
manuals = []
|
||||||
|
saved_manuals = manual_allocations or ()
|
||||||
|
|
||||||
for source in sources:
|
for source in sources:
|
||||||
customer_id = _customer_id_from_source(source)
|
customer_id = _customer_id_from_source(source)
|
||||||
@@ -414,7 +470,10 @@ def select_whole_cover_certs(db, user, sources, *, vehicle_no: str, on_date: dat
|
|||||||
if is_single_use_consumed(db, user, 'purchase', purchase):
|
if is_single_use_consumed(db, user, 'purchase', purchase):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
need = _permit_qty_for(Decimal(source.quantity), source.unit, purchase.quantity_unit)
|
need = _need_permit_qty(
|
||||||
|
source.quantity, source.unit, purchase.quantity_unit,
|
||||||
|
saved_manuals, 'purchase', source.source_id, purchase.id,
|
||||||
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
continue
|
continue
|
||||||
if _available(purchase) < need:
|
if _available(purchase) < need:
|
||||||
@@ -441,7 +500,10 @@ def select_whole_cover_certs(db, user, sources, *, vehicle_no: str, on_date: dat
|
|||||||
if not _vehicle_ok(transport, vehicle_no):
|
if not _vehicle_ok(transport, vehicle_no):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
t_need = _permit_qty_for(Decimal(source.quantity), source.unit, transport.quantity_unit)
|
t_need = _need_permit_qty(
|
||||||
|
source.quantity, source.unit, transport.quantity_unit,
|
||||||
|
saved_manuals, 'transport', source.source_id, transport.id,
|
||||||
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
continue
|
continue
|
||||||
if _available(transport) < t_need:
|
if _available(transport) < t_need:
|
||||||
@@ -458,7 +520,8 @@ def select_whole_cover_certs(db, user, sources, *, vehicle_no: str, on_date: dat
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
409,
|
409,
|
||||||
_diagnose_select_failure(
|
_diagnose_select_failure(
|
||||||
db, user, source, vehicle_no=vehicle_no, on_date=on_date, candidates=candidates,
|
db, user, source, vehicle_no=vehicle_no, on_date=on_date,
|
||||||
|
candidates=candidates, manual_allocations=saved_manuals,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ def build_usage_request(db, user, plan_id, operation, action, *, lock=False, che
|
|||||||
on_date = max(date.today(), plan.plan_date or date.today())
|
on_date = max(date.today(), plan.plan_date or date.today())
|
||||||
purchase_ids, transport_ids, manuals = select_whole_cover_certs(
|
purchase_ids, transport_ids, manuals = select_whole_cover_certs(
|
||||||
db, user, sources, vehicle_no=vehicle, on_date=on_date,
|
db, user, sources, vehicle_no=vehicle, on_date=on_date,
|
||||||
|
manual_allocations=config.manual_allocations,
|
||||||
)
|
)
|
||||||
config = config.model_copy(update={
|
config = config.model_copy(update={
|
||||||
'purchase_cert_ids': purchase_ids,
|
'purchase_cert_ids': purchase_ids,
|
||||||
|
|||||||
@@ -1154,3 +1154,144 @@ def test_archive_cover_block_message_customer_rule_wording(monkeypatch):
|
|||||||
)
|
)
|
||||||
assert '客户名下' in message
|
assert '客户名下' in message
|
||||||
assert '合同' not 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')
|
||||||
Reference in new issue
Block a user