Fix opening balance scope and add async cash ledger loading
This commit is contained in:
@@ -252,6 +252,7 @@ def cash_payment_review(
|
||||
|
||||
@router.post("/ledgers")
|
||||
async def cash_payment_ledgers(request: Request):
|
||||
"""Return cached ledgers immediately or start a non-blocking Tally master job."""
|
||||
form = await request.form()
|
||||
validate_csrf(request, str(form.get("csrf_token") or ""))
|
||||
db = CommonSessionLocal()
|
||||
@@ -269,6 +270,7 @@ async def cash_payment_ledgers(request: Request):
|
||||
return JSONResponse({"ok": False, "error": "Select a valid client."}, status_code=400)
|
||||
tally_guid = str(form.get("tally_guid") or "").strip()
|
||||
date_from = str(form.get("date_from") or date.today().isoformat()).strip()
|
||||
force_refresh = str(form.get("force_refresh") or "").strip().lower() in {"1","true","yes","on"}
|
||||
if not tally_guid:
|
||||
return JSONResponse({"ok": False, "error": "Select a mapped Tally company."}, status_code=400)
|
||||
node = get_active_storage_node_for_branch(db, scope.tenant_id, scope.branch_id)
|
||||
@@ -278,14 +280,52 @@ async def cash_payment_ledgers(request: Request):
|
||||
fy = _financial_year_for_date(date.fromisoformat(date_from))
|
||||
result = request_agent_command(
|
||||
node.node_code,
|
||||
"accounting_cash_payment_ledgers",
|
||||
{**_accounting_storage_payload(selected_client, fy), "tally_guid": tally_guid},
|
||||
timeout_seconds=45,
|
||||
"accounting_cash_payment_ledgers_start",
|
||||
{
|
||||
**_accounting_storage_payload(selected_client, fy),
|
||||
"tally_guid": tally_guid,
|
||||
"force_refresh": force_refresh,
|
||||
},
|
||||
timeout_seconds=20,
|
||||
)
|
||||
except Exception as exc:
|
||||
return JSONResponse({"ok": False, "error": str(exc)}, status_code=502)
|
||||
if not result.get("ok"):
|
||||
return JSONResponse({"ok": False, "error": str(result.get("error") or "Could not read Tally ledgers.")}, status_code=409)
|
||||
return JSONResponse({"ok": False, "error": str(result.get("error") or "Could not start Tally ledger sync.")}, status_code=409)
|
||||
return JSONResponse({"ok": True, **(result.get("result") or {})})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/ledgers/progress")
|
||||
def cash_payment_ledgers_progress(request: Request, client_id: int, job_id: str, date_from: str):
|
||||
db = CommonSessionLocal()
|
||||
try:
|
||||
user, response = _require_partner(request, db, "accounting.tally.view")
|
||||
if response:
|
||||
return JSONResponse({"ok": False, "error": "Access denied."}, status_code=403)
|
||||
clients, scope = _visible_clients(db, request, user)
|
||||
selected_client = next((row for row in clients if int(row.id) == int(client_id)), None)
|
||||
if not selected_client:
|
||||
return JSONResponse({"ok": False, "error": "Client not found."}, status_code=404)
|
||||
node = get_active_storage_node_for_branch(db, scope.tenant_id, scope.branch_id)
|
||||
if not node or not _node_online(node):
|
||||
return JSONResponse({"ok": False, "error": "ERP Local Agent is offline."}, status_code=409)
|
||||
try:
|
||||
fy = _financial_year_for_date(date.fromisoformat(date_from))
|
||||
result = request_agent_command(
|
||||
node.node_code,
|
||||
"accounting_cash_payment_ledgers_status",
|
||||
{
|
||||
**_accounting_storage_payload(selected_client, fy),
|
||||
"job_id": str(job_id),
|
||||
},
|
||||
timeout_seconds=15,
|
||||
)
|
||||
except Exception as exc:
|
||||
return JSONResponse({"ok": False, "error": str(exc)}, status_code=502)
|
||||
if not result.get("ok"):
|
||||
return JSONResponse({"ok": False, "error": str(result.get("error") or "Could not read ledger-sync progress.")}, status_code=409)
|
||||
return JSONResponse({"ok": True, **(result.get("result") or {})})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user