Add Phase 4 Tally transaction synchronization

This commit is contained in:
A R R R Associates
2026-08-19 21:04:17 +05:30
parent 6551a20e05
commit 102520bb25
14 changed files with 567 additions and 19 deletions
+64 -3
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
from datetime import datetime, timezone
from datetime import date, datetime, timezone
from urllib.parse import quote
from fastapi import APIRouter, Form, Request
@@ -115,6 +115,7 @@ def tally_tool(
mapped: int = 0,
unmapped: int = 0,
synced: int = 0,
transactions_synced: int = 0,
error: str = "",
):
db = CommonSessionLocal()
@@ -149,7 +150,7 @@ def tally_tool(
try:
response_data = request_agent_command(
node.node_code,
"phase3_status",
"phase4_status",
payload,
timeout_seconds=20,
)
@@ -160,11 +161,16 @@ def tally_tool(
except Exception as exc:
command_error = str(exc)
today = date.today()
fy_start_year = today.year if today.month >= 4 else today.year - 1
transaction_date_from = date(fy_start_year, 4, 1).isoformat()
transaction_date_to = today.isoformat()
return _render(
request,
db,
user,
title="Tally Accounting Masters",
title="Tally Accounting",
clients=clients,
selected_client=selected_client,
registrations=registrations,
@@ -175,6 +181,9 @@ def tally_tool(
mapped=bool(mapped),
unmapped=bool(unmapped),
synced=bool(synced),
transactions_synced=bool(transactions_synced),
transaction_date_from=transaction_date_from,
transaction_date_to=transaction_date_to,
command_error=command_error,
)
finally:
@@ -408,3 +417,55 @@ def sync_tally_masters(
)
finally:
db.close()
@router.post("/sync-transactions")
def sync_tally_transactions(
request: Request,
client_id: int = Form(...),
tally_guid: str = Form(...),
date_from: str = Form(...),
date_to: str = Form(...),
csrf_token: str = Form(...),
):
validate_csrf(request, csrf_token)
db = CommonSessionLocal()
try:
user, response = _require_partner(request, db, "accounting.tally.sync_transactions")
if response:
return response
client, _clients, scope = _find_visible_client(db, request, user, client_id)
if not client:
return _denied()
try:
start = date.fromisoformat(str(date_from or ""))
end = date.fromisoformat(str(date_to or ""))
if start > end:
raise ValueError("From Date cannot be after To Date.")
if (end - start).days > 370:
raise ValueError("Transaction sync is limited to 371 days per run. Sync additional periods separately.")
except Exception as exc:
return RedirectResponse(url=f"/tools/tally?client_id={client.id}&error={quote(str(exc))}", status_code=303)
node = get_active_storage_node_for_branch(db, scope.tenant_id, scope.branch_id)
if not node or not _node_online(node):
return RedirectResponse(url=f"/tools/tally?client_id={client.id}&error={quote('ERP Local Agent is offline for the active branch.')}", status_code=303)
try:
result = request_agent_command(
node.node_code,
"accounting_sync_transactions",
{
"client_id": int(client.id),
"tally_guid": str(tally_guid or "").strip(),
"date_from": start.isoformat(),
"date_to": end.isoformat(),
"requested_by_user_id": int(user.id),
},
timeout_seconds=300,
)
if not result.get("ok"):
raise RuntimeError(str(result.get("error") or "Tally transaction synchronization failed."))
except Exception as exc:
return RedirectResponse(url=f"/tools/tally?client_id={client.id}&error={quote(str(exc))}", status_code=303)
return RedirectResponse(url=f"/tools/tally?client_id={client.id}&refresh=1&transactions_synced=1", status_code=303)
finally:
db.close()