Add client financial year Accounting Mirror registry

This commit is contained in:
A R R R Associates
2026-09-18 20:58:41 +05:30
parent 93f2e02c35
commit 8a0d5b212d
11 changed files with 630 additions and 71 deletions
+59 -1
View File
@@ -13,6 +13,12 @@ from app.core.db.common import CommonSessionLocal
from app.core.security.csrf import get_or_create_csrf_token
from app.core.templating import templates
from app.modules.accounting.accounting_mirror_service import AccountingMirrorError, sundry_creditors_aging
from app.modules.accounting.accounting_mirror_registry import (
get_registered_mirror,
list_registered_mirrors,
sync_discovered_mirrors,
)
from app.modules.accounting.agent_bridge import request_agent_command
from app.modules.accounting.ui import (
_accounting_storage_payload,
_active_financial_year,
@@ -43,6 +49,42 @@ def _resolve_fy(value: str, fallback: str) -> str:
return fallback
def _sync_client_mirror_registry(db, node, scope, user, client) -> None:
if not node or not _node_online(node) or not client:
return
try:
response = request_agent_command(
node.node_code,
"accounting_mirror_list",
{"client_id": int(client.id)},
timeout_seconds=20,
)
if response.get("ok"):
sync_discovered_mirrors(
db,
tenant_id=scope.tenant_id,
client_id=int(client.id),
storage_node_id=int(node.id),
discovered=(response.get("result") or {}).get("mirrors") or [],
requested_by_user_id=int(user.id),
)
except Exception:
pass
def _registered_follow_up_fys(db, tenant_id: int, client_id: int, selected_fy: str) -> list[str]:
base_start, _ = _financial_year_bounds(selected_fy)
values = []
for row in list_registered_mirrors(db, tenant_id, client_id):
try:
start, _ = _financial_year_bounds(row.financial_year)
except Exception:
continue
if start > base_start:
values.append(row.financial_year)
return sorted(set(values), key=lambda fy: _financial_year_bounds(fy)[0])
def _load_analysis(request: Request, db, user, client_id: int, financial_year: str, follow_up_fy: str):
client, clients, scope = _find_visible_client(db, request, user, client_id)
if not client:
@@ -51,6 +93,11 @@ def _load_analysis(request: Request, db, user, client_id: int, financial_year: s
if not node or not _node_online(node):
return None, clients, scope, "ERP Local Agent is offline for the active branch."
_sync_client_mirror_registry(db, node, scope, user, client)
base_registration = get_registered_mirror(db, scope.tenant_id, int(client.id), financial_year)
if not base_registration:
return None, clients, scope, f"No registered Accounting Mirror is available for FY {financial_year}. Create the mirror from Tally Accounting first."
fy_start, fy_end = _financial_year_bounds(financial_year)
follow_up_start = ""
follow_up_end = ""
@@ -59,6 +106,9 @@ def _load_analysis(request: Request, db, user, client_id: int, financial_year: s
follow_start, follow_end = _financial_year_bounds(follow_up_fy)
if follow_start <= fy_end:
return None, clients, scope, "Follow-up financial year must be after the analysis financial year."
follow_up_registration = get_registered_mirror(db, scope.tenant_id, int(client.id), follow_up_fy)
if not follow_up_registration:
return None, clients, scope, f"No registered Accounting Mirror is available for follow-up FY {follow_up_fy}."
follow_up_start = follow_start.isoformat()
follow_up_end = follow_end.isoformat()
follow_up_payload = _accounting_storage_payload(client, follow_up_fy)
@@ -103,6 +153,13 @@ def creditors_aging(
selected_fy = fallback_fy
selected_follow_up = str(follow_up_fy or "").strip()
selected_client = next((c for c in clients if client_id and int(c.id) == int(client_id)), None)
registered_fys: list[str] = []
follow_up_options: list[str] = []
if selected_client:
node = get_active_storage_node_for_branch(db, scope.tenant_id, scope.branch_id)
_sync_client_mirror_registry(db, node, scope, user, selected_client)
registered_fys = [row.financial_year for row in list_registered_mirrors(db, scope.tenant_id, int(selected_client.id))]
follow_up_options = _registered_follow_up_fys(db, scope.tenant_id, int(selected_client.id), selected_fy)
result = None
error = ""
if analyze and selected_client:
@@ -119,7 +176,8 @@ def creditors_aging(
"selected_client": selected_client,
"selected_fy": selected_fy,
"follow_up_fy": selected_follow_up,
"fy_options": _fy_options(selected_fy),
"fy_options": registered_fys or _fy_options(selected_fy),
"follow_up_options": follow_up_options,
"analysis": result,
"error": error,
}