diff --git a/ecommerce_integrations/unicommerce/constants.py b/ecommerce_integrations/unicommerce/constants.py index 4307bf9f..261caf8e 100644 --- a/ecommerce_integrations/unicommerce/constants.py +++ b/ecommerce_integrations/unicommerce/constants.py @@ -303,3 +303,46 @@ "ZM": "Zambia", "ZW": "Zimbabwe", } + + +UNICOMMERCE_INDIAN_STATES_MAPPING = { + "AN": "Andaman and Nicobar Islands", + "AP": "Andhra Pradesh", + "AR": "Arunachal Pradesh", + "AS": "Assam", + "BR": "Bihar", + "CH": "Chandigarh", + "CT": "Chhattisgarh", + "DN": "Dadra and Nagar Haveli and Daman and Diu", + "DD": "Dadra and Nagar Haveli and Daman and Diu", + "DL": "Delhi", + "GA": "Goa", + "GJ": "Gujarat", + "HR": "Haryana", + "HP": "Himachal Pradesh", + "JK": "Jammu and Kashmir", + "KA": "Karnataka", + "KL": "Kerala", + "LD": "Lakshadweep Islands", + "MP": "Madhya Pradesh", + "MH": "Maharashtra", + "MN": "Manipur", + "ML": "Meghalaya", + "MZ": "Mizoram", + "NL": "Nagaland", + "OR": "Odisha", + "PY": "Pondicherry", + "PB": "Punjab", + "RJ": "Rajasthan", + "SK": "Sikkim", + "TN": "Tamil Nadu", + "TR": "Tripura", + "UP": "Uttar Pradesh", + "WB": "West Bengal", + "TL": "Telangana", + "AD": "Andhra Pradesh", + "UT": "Uttarakhand", + "UL": "Uttarakhand", + "JH": "Jharkhand", + "JR": "Jharkhand", +} diff --git a/ecommerce_integrations/unicommerce/customer.py b/ecommerce_integrations/unicommerce/customer.py index cf3da2bf..579d2732 100644 --- a/ecommerce_integrations/unicommerce/customer.py +++ b/ecommerce_integrations/unicommerce/customer.py @@ -10,6 +10,7 @@ CUSTOMER_CODE_FIELD, SETTINGS_DOCTYPE, UNICOMMERCE_COUNTRY_MAPPING, + UNICOMMERCE_INDIAN_STATES_MAPPING, ) @@ -92,19 +93,27 @@ def _create_customer_addresses(addresses: List[Dict[str, Any]], customer) -> Non def _create_customer_address(uni_address, address_type, customer, also_shipping=False): + + country_code = uni_address.get("country") + country = UNICOMMERCE_COUNTRY_MAPPING.get(country_code) + + state = uni_address.get("state") + if country_code == "IN" and state in UNICOMMERCE_COUNTRY_MAPPING: + state = UNICOMMERCE_INDIAN_STATES_MAPPING.get(state) + frappe.get_doc( { "address_line1": uni_address.get("addressLine1") or "Not provided", "address_line2": uni_address.get("addressLine2"), "address_type": address_type, "city": uni_address.get("city"), - "country": UNICOMMERCE_COUNTRY_MAPPING.get(uni_address.get("country")), + "country": country, "county": uni_address.get("district"), "doctype": "Address", "email_id": uni_address.get("email"), "phone": uni_address.get("phone"), "pincode": uni_address.get("pincode"), - "state": uni_address.get("state"), + "state": state, "links": [{"link_doctype": "Customer", "link_name": customer.name}], "is_primary_address": int(address_type == "Billing"), "is_shipping_address": int(also_shipping or address_type == "Shipping"), diff --git a/ecommerce_integrations/unicommerce/tests/test_customer.py b/ecommerce_integrations/unicommerce/tests/test_customer.py index 1d7002df..38206cda 100644 --- a/ecommerce_integrations/unicommerce/tests/test_customer.py +++ b/ecommerce_integrations/unicommerce/tests/test_customer.py @@ -27,14 +27,15 @@ def test_create_customer(self): _create_customer_addresses(order.get("addresses", []), customer) - new_addresses = frappe.get_all( - "Address", filters={"link_name": customer.name}, fields=["address_type"] - ) + new_addresses = frappe.get_all("Address", filters={"link_name": customer.name}, fields=["*"]) self.assertEqual(len(new_addresses), 2) addr_types = {d.address_type for d in new_addresses} self.assertEqual(addr_types, {"Shipping", "Billing"}) + states = {d.state for d in new_addresses} + self.assertEqual(states, {"Maharashtra"}) + def test_deduplication(self): """requirement: Literally same order should not create duplicates.""" order = self.load_fixture("order-SO5841")["saleOrderDTO"]