fix ксе tariffs
Deploy / deploy (push) Successful in 57s

This commit is contained in:
Раис Юсупалиев
2026-06-27 17:34:06 +03:00
parent e4d9b581a6
commit 4262b8a200
8 changed files with 207 additions and 34 deletions
+24 -6
View File
@@ -18,11 +18,14 @@ def map_cse_calc_response(
*,
delivery_type: str = "",
delivery_type_label: str = "",
service_guid: str | None = None,
) -> list[DeliveryPrice]:
"""Map a parsed ``Calc`` ``return`` Element into unified delivery prices."""
prices: list[DeliveryPrice] = []
for tariff in _iter_tariffs(root):
if service_guid is not None and tariff.value != service_guid:
continue
price = _map_tariff(tariff, delivery_type, delivery_type_label)
if price is not None:
prices.append(price)
@@ -35,11 +38,11 @@ def map_cse_calc_response_for_tariff_code(
tariff_code: str,
delivery_type_label: str = "",
) -> DeliveryPrice | None:
"""Return the tariff matching ``tariff_code`` (``"<DeliveryType>|<Urgency>"``)."""
"""Return tariff matching ``"<DeliveryType>|<ServiceGuid>|<Urgency>"``."""
delivery_type, _, urgency = tariff_code.partition("|")
delivery_type, tariff_guid, urgency = _split_tariff_code(tariff_code)
for tariff in _iter_tariffs(root):
if _tariff_urgency(tariff) == urgency:
if _tariff_matches(tariff, tariff_guid, urgency):
price = _map_tariff(tariff, delivery_type, delivery_type_label)
if price is not None:
return price
@@ -52,6 +55,19 @@ def _tariff_urgency(tariff: Element) -> str | None:
return tariff.field_value("Urgency") or tariff.value
def _tariff_matches(tariff: Element, tariff_guid: str, urgency: str) -> bool:
if _tariff_urgency(tariff) != urgency:
return False
return tariff.value == tariff_guid
def _split_tariff_code(tariff_code: str) -> tuple[str, str, str]:
parts = tariff_code.split("|")
if len(parts) != 3 or not parts[1] or not parts[2]:
raise CSEMappingError("CSE tariff_code has invalid format.")
return parts[0], parts[1], parts[2]
def _iter_tariffs(root: Element):
for destination in root.items:
for tariff in destination.items:
@@ -70,9 +86,11 @@ def _map_tariff(
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}"
if not tariff.value:
return None
# Unified tariff_code carries the delivery scheme, CSE service GUID and
# urgency so payment validation can recalculate the exact selected service.
tariff_code = f"{delivery_type}|{tariff.value}|{urgency}"
raw_total = tariff.field_value("Total")
if raw_total is None: