Enrich ERP and Tally GSTIN display
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, timezone
|
||||
from pathlib import Path
|
||||
import re
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Form, Request
|
||||
@@ -104,6 +105,59 @@ def _registration_payload(registration, registration_type) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _normalise_gstin(value: str) -> str:
|
||||
return re.sub(r"\s+", "", str(value or "").strip().upper())
|
||||
|
||||
|
||||
def _erp_client_gstins(registrations) -> list[str]:
|
||||
values: list[str] = []
|
||||
for registration, registration_type in registrations or []:
|
||||
if str(getattr(registration_type, "code", "") or "").strip().upper() != "GSTIN":
|
||||
continue
|
||||
number = _normalise_gstin(getattr(registration, "registration_number", ""))
|
||||
if number and number not in values:
|
||||
values.append(number)
|
||||
return values
|
||||
|
||||
|
||||
def _enrich_gstin_display(live_result: dict | None, registrations) -> None:
|
||||
"""Attach ERP/Tally GSTIN display metadata without mutating stored mappings."""
|
||||
if not isinstance(live_result, dict):
|
||||
return
|
||||
accounting = live_result.get("accounting") or {}
|
||||
tally = live_result.get("tally") or {}
|
||||
mappings = accounting.get("mappings") or []
|
||||
companies = tally.get("companies") or []
|
||||
client_gstins = _erp_client_gstins(registrations)
|
||||
company_by_guid = {str(row.get("guid") or "").strip(): row for row in companies if isinstance(row, dict)}
|
||||
|
||||
for mapping in mappings:
|
||||
if not isinstance(mapping, dict):
|
||||
continue
|
||||
registration_type = str(mapping.get("registration_type_code") or "").strip().upper()
|
||||
registration_number = _normalise_gstin(mapping.get("registration_number"))
|
||||
if mapping.get("registration_id") and registration_type == "GSTIN" and registration_number:
|
||||
erp_gstins = [registration_number]
|
||||
elif not mapping.get("registration_id"):
|
||||
erp_gstins = list(client_gstins)
|
||||
else:
|
||||
erp_gstins = []
|
||||
|
||||
live_company = company_by_guid.get(str(mapping.get("tally_guid") or "").strip()) or {}
|
||||
tally_gstin = _normalise_gstin(live_company.get("gstin") or mapping.get("gstin"))
|
||||
mapping["erp_gstins"] = erp_gstins
|
||||
mapping["tally_gstin_display"] = tally_gstin
|
||||
|
||||
if erp_gstins and tally_gstin:
|
||||
mapping["gstin_match_status"] = "matched" if tally_gstin in erp_gstins else "mismatch"
|
||||
elif erp_gstins and not tally_gstin:
|
||||
mapping["gstin_match_status"] = "tally_missing"
|
||||
elif tally_gstin and not erp_gstins:
|
||||
mapping["gstin_match_status"] = "erp_missing"
|
||||
else:
|
||||
mapping["gstin_match_status"] = "unavailable"
|
||||
|
||||
|
||||
def _node_online(node) -> bool:
|
||||
if not node or not node.last_seen_at_utc:
|
||||
return False
|
||||
@@ -175,6 +229,7 @@ def tally_tool(
|
||||
)
|
||||
if response_data.get("ok"):
|
||||
live_result = response_data.get("result") or {}
|
||||
_enrich_gstin_display(live_result, registrations)
|
||||
else:
|
||||
command_error = str(response_data.get("error") or "Local agent command failed.")
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user