From 8d6f43b3dd30103bd3d0127750d44f6797b5f250 Mon Sep 17 00:00:00 2001 From: Lakshit Jain <108322669+ljain112@users.noreply.github.com> Date: Thu, 2 May 2024 19:44:13 +0530 Subject: [PATCH] fix: avoid duplicate address keys in gstin info based on key priority (#2094) Co-authored-by: Sagar Vora (cherry picked from commit 0875bfba00a37dde320613fd893421bb00a6b10b) --- .../gst_india/utils/gstin_info.py | 24 +++++++++++++++---- .../gst_india/utils/test_gstin_info.py | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) 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": "",