81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
|
|
from copy import deepcopy
|
||
|
|
|
||
|
|
|
||
|
|
SERVICE_PRICING_DEFAULTS = {
|
||
|
|
"pack_walk": {
|
||
|
|
"label": "Pack Walk",
|
||
|
|
"amount": 58.0,
|
||
|
|
"unit": "per walk",
|
||
|
|
},
|
||
|
|
"1_1_walk": {
|
||
|
|
"label": "1-1 Walk",
|
||
|
|
"amount": 45.0,
|
||
|
|
"unit": "per walk",
|
||
|
|
},
|
||
|
|
"puppy_visit": {
|
||
|
|
"label": "Puppy Visit",
|
||
|
|
"amount": 39.0,
|
||
|
|
"unit": "per visit",
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def default_service_pricing() -> dict[str, dict[str, float | str]]:
|
||
|
|
return deepcopy(SERVICE_PRICING_DEFAULTS)
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_service_pricing(data: object | None) -> dict[str, dict[str, float | str]]:
|
||
|
|
normalized = default_service_pricing()
|
||
|
|
source = data if isinstance(data, dict) else {}
|
||
|
|
|
||
|
|
for service_type, defaults in normalized.items():
|
||
|
|
candidate = source.get(service_type) if isinstance(source, dict) else None
|
||
|
|
if not isinstance(candidate, dict):
|
||
|
|
continue
|
||
|
|
|
||
|
|
amount = candidate.get("amount")
|
||
|
|
try:
|
||
|
|
parsed_amount = round(float(amount), 2)
|
||
|
|
except (TypeError, ValueError):
|
||
|
|
parsed_amount = defaults["amount"]
|
||
|
|
|
||
|
|
if parsed_amount < 0:
|
||
|
|
parsed_amount = defaults["amount"]
|
||
|
|
|
||
|
|
unit = candidate.get("unit")
|
||
|
|
label = candidate.get("label")
|
||
|
|
|
||
|
|
normalized[service_type] = {
|
||
|
|
"label": label.strip() if isinstance(label, str) and label.strip() else defaults["label"],
|
||
|
|
"amount": parsed_amount,
|
||
|
|
"unit": unit.strip() if isinstance(unit, str) and unit.strip() else defaults["unit"],
|
||
|
|
}
|
||
|
|
|
||
|
|
return normalized
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_service_pricing_overrides(data: object | None) -> dict[str, float]:
|
||
|
|
if not isinstance(data, dict):
|
||
|
|
return {}
|
||
|
|
|
||
|
|
normalized: dict[str, float] = {}
|
||
|
|
for service_type in SERVICE_PRICING_DEFAULTS:
|
||
|
|
if service_type not in data:
|
||
|
|
continue
|
||
|
|
|
||
|
|
value = data.get(service_type)
|
||
|
|
if value in (None, ""):
|
||
|
|
continue
|
||
|
|
|
||
|
|
try:
|
||
|
|
parsed = round(float(value), 2)
|
||
|
|
except (TypeError, ValueError):
|
||
|
|
continue
|
||
|
|
|
||
|
|
if parsed < 0:
|
||
|
|
continue
|
||
|
|
|
||
|
|
normalized[service_type] = parsed
|
||
|
|
|
||
|
|
return normalized
|