This commit is contained in:
@@ -13,12 +13,17 @@ from app.schemas.response import DeliveryPrice
|
||||
_TARIFF_KEY = "Tariff"
|
||||
|
||||
|
||||
def map_cse_calc_response(root: Element) -> list[DeliveryPrice]:
|
||||
def map_cse_calc_response(
|
||||
root: Element,
|
||||
*,
|
||||
delivery_type: str = "",
|
||||
delivery_type_label: str = "",
|
||||
) -> list[DeliveryPrice]:
|
||||
"""Map a parsed ``Calc`` ``return`` Element into unified delivery prices."""
|
||||
|
||||
prices: list[DeliveryPrice] = []
|
||||
for tariff in _iter_tariffs(root):
|
||||
price = _map_tariff(tariff)
|
||||
price = _map_tariff(tariff, delivery_type, delivery_type_label)
|
||||
if price is not None:
|
||||
prices.append(price)
|
||||
return prices
|
||||
@@ -28,15 +33,23 @@ def map_cse_calc_response_for_tariff_code(
|
||||
root: Element,
|
||||
*,
|
||||
tariff_code: str,
|
||||
delivery_type_label: str = "",
|
||||
) -> DeliveryPrice | None:
|
||||
"""Return the tariff matching the given GUID, or ``None`` when absent."""
|
||||
"""Return the tariff matching ``tariff_code`` (``"<DeliveryType>|<Urgency>"``)."""
|
||||
|
||||
delivery_type, _, urgency = tariff_code.partition("|")
|
||||
for tariff in _iter_tariffs(root):
|
||||
if tariff.value == tariff_code:
|
||||
return _map_tariff(tariff)
|
||||
if _tariff_urgency(tariff) == urgency:
|
||||
return _map_tariff(tariff, delivery_type, delivery_type_label)
|
||||
return None
|
||||
|
||||
|
||||
def _tariff_urgency(tariff: Element) -> str | None:
|
||||
"""Urgency GUID of a Calc tariff (fallback: the tariff record GUID)."""
|
||||
|
||||
return tariff.field_value("Urgency") or tariff.value
|
||||
|
||||
|
||||
def _iter_tariffs(root: Element):
|
||||
for destination in root.items:
|
||||
for tariff in destination.items:
|
||||
@@ -44,10 +57,17 @@ def _iter_tariffs(root: Element):
|
||||
yield tariff
|
||||
|
||||
|
||||
def _map_tariff(tariff: Element) -> DeliveryPrice | None:
|
||||
tariff_code = tariff.value
|
||||
if not tariff_code:
|
||||
def _map_tariff(
|
||||
tariff: Element,
|
||||
delivery_type: str,
|
||||
delivery_type_label: str,
|
||||
) -> DeliveryPrice | None:
|
||||
urgency = _tariff_urgency(tariff)
|
||||
if not urgency:
|
||||
return None
|
||||
# Unified tariff_code carries both the delivery scheme and the urgency so
|
||||
# registration (SaveDocuments) can set DeliveryOfCargo and Urgency.
|
||||
tariff_code = f"{delivery_type}|{urgency}"
|
||||
|
||||
raw_total = tariff.field_value("Total")
|
||||
if raw_total is None:
|
||||
@@ -60,9 +80,12 @@ def _map_tariff(tariff: Element) -> DeliveryPrice | None:
|
||||
if not price.is_finite() or price <= 0:
|
||||
return None
|
||||
|
||||
service_name = tariff.field_value("Service") or tariff.field_value("UrgencyName")
|
||||
if not service_name:
|
||||
service_name = tariff_code
|
||||
base_name = tariff.field_value("Service") or tariff.field_value("UrgencyName")
|
||||
if not base_name:
|
||||
base_name = urgency
|
||||
service_name = (
|
||||
f"{base_name} — {delivery_type_label}" if delivery_type_label else base_name
|
||||
)
|
||||
|
||||
currency = normalize_currency(tariff.field_value("CurrencyName"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user