diff --git a/app/modules/documents/agent_package.py b/app/modules/documents/agent_package.py index d183230..03b8fad 100644 --- a/app/modules/documents/agent_package.py +++ b/app/modules/documents/agent_package.py @@ -4,7 +4,7 @@ import io from pathlib import Path import zipfile -ERP_LOCAL_AGENT_VERSION = "1.26.2" +ERP_LOCAL_AGENT_VERSION = "1.26.3" ERP_LOCAL_AGENT_NAME = "ERP Local Agent" RUNTIME_ROOT = Path(__file__).resolve().parent / "local_agent_runtime" _DETERMINISTIC_ZIP_TIMESTAMP = (2026, 1, 1, 0, 0, 0) diff --git a/app/modules/documents/local_agent_runtime/erp_local_agent/__init__.py b/app/modules/documents/local_agent_runtime/erp_local_agent/__init__.py index 759f64f..1a14052 100644 --- a/app/modules/documents/local_agent_runtime/erp_local_agent/__init__.py +++ b/app/modules/documents/local_agent_runtime/erp_local_agent/__init__.py @@ -1,2 +1,2 @@ -__version__ = "1.26.2" +__version__ = "1.26.3" AGENT_NAME = "ERP Local Agent" diff --git a/app/modules/documents/local_agent_runtime/erp_local_agent/accounting_store.py b/app/modules/documents/local_agent_runtime/erp_local_agent/accounting_store.py index a3fb826..e8dc800 100644 --- a/app/modules/documents/local_agent_runtime/erp_local_agent/accounting_store.py +++ b/app/modules/documents/local_agent_runtime/erp_local_agent/accounting_store.py @@ -634,9 +634,13 @@ class LocalAccountingStore: raise ValueError("Accounting storage is not initialized for this client.") tally_guid = str(mapping.get("tally_guid") or "").strip() company_name = str(mapping.get("company_name") or "").strip() - mapping_id = int(mapping.get("id")) + mapping_id = ( + int(mapping.get("id")) + if mapping.get("id") not in (None, "") + else None + ) if not tally_guid or not company_name: - raise ValueError("Active Tally mapping is incomplete.") + raise ValueError("Accounting Mirror company identity is incomplete.") started = _utc_now_iso() counts: dict[str, int] = {} run_id = None @@ -688,12 +692,16 @@ class LocalAccountingStore: raise ValueError("Accounting storage is not initialized for this client.") tally_guid = str(mapping.get("tally_guid") or "").strip() company_name = str(mapping.get("company_name") or "").strip() - mapping_id = int(mapping.get("id")) + mapping_id = ( + int(mapping.get("id")) + if mapping.get("id") not in (None, "") + else None + ) date_from = str(transactions.get("date_from") or "").strip() date_to = str(transactions.get("date_to") or "").strip() vouchers = transactions.get("vouchers") or [] if not tally_guid or not company_name: - raise ValueError("Active Tally mapping is incomplete.") + raise ValueError("Accounting Mirror company identity is incomplete.") if not date_from or not date_to: raise ValueError("Transaction sync date range is missing.") @@ -1047,7 +1055,42 @@ class LocalAccountingStore: def depreciation_preview(self, client_id: int, *, tally_guid: str, fy_start: str, fy_end: str) -> dict: if not self.exists(client_id): raise ValueError("Accounting storage is not initialized for this client.") - mapping = self.get_active_mapping_by_guid(client_id, tally_guid) + try: + mapping = self.get_active_mapping_by_guid(client_id, tally_guid) + except Exception: + mapping = {} + with self.connect(client_id) as db: + mapped = db.execute( + """SELECT * + FROM tally_company_mapping + WHERE client_id=? AND is_active=1 + ORDER BY id DESC LIMIT 1""", + (int(client_id),), + ).fetchone() + if mapped: + mapping = dict(mapped) + else: + identity = db.execute( + """SELECT company_name + FROM tally_ledgers + WHERE tally_guid=? AND trim(COALESCE(company_name,''))<>'' + ORDER BY id LIMIT 1""", + (str(tally_guid or "").strip(),), + ).fetchone() + mapping = { + "id": None, + "client_id": int(client_id), + "tally_guid": str(tally_guid or "").strip(), + "company_name": str(identity["company_name"] or "").strip() if identity else "", + "is_active": 1, + "mirror_only_identity": True, + } + if not str(mapping.get("company_name") or "").strip(): + raise ValueError( + "Accounting Mirror is ready, but its company identity could not be resolved " + "for depreciation analysis. Refresh the mirror once and retry." + ) + start = date.fromisoformat(str(fy_start)); end = date.fromisoformat(str(fy_end)) if start > end: raise ValueError("Financial-year start cannot be after end date.") with self.connect(client_id) as db: