Finance-Accounts/ar-aging-app/backend/app/core/regions.py

46 lines
1.6 KiB
Python

"""Map Amazon marketplace domains to the reporting-region labels used by the workbook."""
from __future__ import annotations
DOMAIN_TO_REGION = {
"amazon.com": "USA",
"amazon.ca": "Canada",
"amazon.co.uk": "UK",
"amazon.de": "Germany",
"amazon.fr": "France",
"amazon.it": "Italy",
"amazon.es": "Spain",
"amazon.nl": "Netherlands",
"amazon.se": "Sweden",
"amazon.pl": "Poland",
"amazon.com.be": "Belgium",
"amazon.ie": "Ireland",
"amazon.com.tr": "Turkey",
"amazon.com.au": "Australia",
}
# Fallback: pick a region label from the source filename if the marketplace column is absent.
_FILENAME_HINTS = {
"canada": "Canada", "uk": "UK", "germany": "Germany", "france": "France",
"italy": "Italy", "spain": "Spain", "netherlands": "Netherlands", "sweden": "Sweden",
"poland": "Poland", "belgium": "Belgium", "ireland": "Ireland", "turkey": "Turkey",
"australia": "Australia", "usa": "USA", "us ": "USA",
}
def region_for(marketplace_value: str | None, filename: str | None = None,
default: str = "USA") -> str:
if marketplace_value:
mv = str(marketplace_value).strip().lower()
if mv in DOMAIN_TO_REGION:
return DOMAIN_TO_REGION[mv]
# tolerate values like "Amazon.com" or "amazon.com "
for dom, region in DOMAIN_TO_REGION.items():
if mv == dom or mv.endswith(dom):
return region
if filename:
low = filename.lower()
for hint, region in _FILENAME_HINTS.items():
if hint in low:
return region
return default