Skip to content

Commit

Permalink
fix: handle inconsistency in state naming in Unicommerce
Browse files Browse the repository at this point in the history
India compliance expected slightly different names, this PR converts UC
codes to IC codes.
  • Loading branch information
ankush committed Feb 13, 2023
1 parent f466088 commit 50d3f1a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
43 changes: 43 additions & 0 deletions ecommerce_integrations/unicommerce/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
13 changes: 11 additions & 2 deletions ecommerce_integrations/unicommerce/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CUSTOMER_CODE_FIELD,
SETTINGS_DOCTYPE,
UNICOMMERCE_COUNTRY_MAPPING,
UNICOMMERCE_INDIAN_STATES_MAPPING,
)


Expand Down Expand Up @@ -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"),
Expand Down
7 changes: 4 additions & 3 deletions ecommerce_integrations/unicommerce/tests/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 50d3f1a

Please sign in to comment.