diff --git a/india_compliance/gst_india/utils/gstin_info.py b/india_compliance/gst_india/utils/gstin_info.py index add8fe451..a46d03c0c 100644 --- a/india_compliance/gst_india/utils/gstin_info.py +++ b/india_compliance/gst_india/utils/gstin_info.py @@ -22,6 +22,11 @@ "URP": "Unregistered", } +# order of address keys is important +KEYS_TO_SANITIZE = ("dst", "stcd", "pncd", "bno", "flno", "bnm", "st", "loc", "city") +KEYS_TO_FILTER_DUPLICATES = frozenset(("dst", "bnm", "st", "loc", "city")) +CHARACTERS_TO_STRIP = f"{whitespace}," + @frappe.whitelist() def get_gstin_info(gstin, *, throw_error=True): @@ -121,12 +126,21 @@ def _get_address(address): def _extract_address_lines(address): """merge and divide address into exactly two lines""" - unique_values = set() - for key, value in address.items(): - value = value.strip(f"{whitespace},") - address[key] = value if value not in unique_values else None - unique_values.add(value) + + for key in KEYS_TO_SANITIZE: + value = address.get(key, "").strip(CHARACTERS_TO_STRIP) + + if key not in KEYS_TO_FILTER_DUPLICATES: + address[key] = value + continue + + if value not in unique_values: + address[key] = value + unique_values.add(value) + continue + + address[key] = "" address_line1 = ", ".join( titlecase(value) diff --git a/india_compliance/gst_india/utils/test_gstin_info.py b/india_compliance/gst_india/utils/test_gstin_info.py index 3471f2173..c8ad1a6a2 100644 --- a/india_compliance/gst_india/utils/test_gstin_info.py +++ b/india_compliance/gst_india/utils/test_gstin_info.py @@ -14,7 +14,7 @@ class TestGstinInfo(unittest.TestCase): "addr": { "bnm": "", "bno": "8-A", - "city": "", + "city": "Vadodara", "dst": "Vadodara", "flno": "Saimee society No.2", "lg": "",