Link Accounting Mirror metadata and Tally paths to Client FY

This commit is contained in:
A R R R Associates
2026-09-20 14:59:14 +05:30
parent 827231a135
commit 0e12ccfe4e
8 changed files with 318 additions and 171 deletions
@@ -790,6 +790,73 @@ def get_registered_mirror(db, tenant_id: int, client_id: int, financial_year: st
return get_current_accounting_mirror(db, tenant_id, client_id, financial_year)
def registered_mirror_payload(
db,
*,
tenant_id: int,
client_id: int,
financial_year: str,
fallback: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Return the standard Local-Agent payload using the current registered mirror.
Existing callers may pass the old deterministic payload as ``fallback``. When a
current Client/FY mirror is registered, its stored directory/file metadata wins.
This keeps all Accounting tools on one source of truth without breaking clients
that have not yet been backfilled into the registry.
"""
payload = dict(fallback or {})
payload.setdefault("client_id", int(client_id))
payload["financial_year"] = str(financial_year or payload.get("financial_year") or "").strip()
row = get_current_accounting_mirror(db, int(tenant_id), int(client_id), payload["financial_year"])
if not row:
payload["mirror_registered"] = False
return payload
payload.update({
"accounting_relative_dir": str(row.accounting_relative_dir or "").strip(),
"mirror_file_name": str(row.mirror_file_name or "").strip(),
"mirror_local_path": str(getattr(row, "mirror_local_path", "") or "").strip(),
"tally_data_relative_path": str(getattr(row, "tally_data_relative_path", "") or "").strip(),
"tally_data_local_path": str(getattr(row, "tally_data_local_path", "") or "").strip(),
"mirror_registry_id": int(row.id),
"mirror_version_no": int(row.version_no or 1),
"mirror_registered": True,
})
return payload
def update_accounting_mirror_paths(
db,
*,
tenant_id: int,
client_id: int,
financial_year: str,
mirror_local_path: str | None = None,
tally_data_relative_path: str | None = None,
tally_data_local_path: str | None = None,
) -> AccountingMirrorRegistry | None:
"""Idempotently enrich the current Client/FY mirror with workstation paths."""
row = get_current_accounting_mirror(db, tenant_id, client_id, financial_year)
if not row:
return None
changed = False
for attr, value in (
("mirror_local_path", mirror_local_path),
("tally_data_relative_path", tally_data_relative_path),
("tally_data_local_path", tally_data_local_path),
):
if value is None:
continue
value = str(value or "").strip()
if value and str(getattr(row, attr, "") or "").strip() != value:
setattr(row, attr, value)
changed = True
if changed:
row.updated_at_utc = _mirror_utcnow()
db.flush()
return row
def list_accounting_mirror_versions(db, tenant_id: int, client_id: int, financial_year: str) -> list[AccountingMirrorRegistry]:
rows = db.execute(
select(AccountingMirrorRegistry).where(
@@ -903,6 +970,15 @@ def register_accounting_mirror_version(
row.storage_node_id = int(storage_node_id) if storage_node_id else None
row.accounting_relative_dir = str(accounting_relative_dir or row.accounting_relative_dir or "").strip()
row.mirror_file_name = Path(str(job.get("accounting_db_path") or row.mirror_file_name or f"client_{int(client_id):08d}.act")).name
mirror_local_path = str(job.get("accounting_db_path") or mirror.get("path") or "").strip()
if mirror_local_path:
row.mirror_local_path = mirror_local_path
tally_relative = str(job.get("tally_data_relative_path") or mirror.get("tally_data_relative_path") or "").strip()
tally_local = str(job.get("tally_data_local_path") or mirror.get("tally_data_local_path") or "").strip()
if tally_relative:
row.tally_data_relative_path = tally_relative
if tally_local:
row.tally_data_local_path = tally_local
row.company_name = str(job.get("company_name") or company.get("company_name") or row.company_name or "").strip()
row.company_guid = str(job.get("tally_guid") or company.get("company_guid") or row.company_guid or "").strip()
row.voucher_from_date = _parse_mirror_date(voucher_period.get("from_date")) or row.voucher_from_date
@@ -964,6 +1040,9 @@ def upsert_registered_mirror(
current.company_name = str(((mirror or {}).get("company") or {}).get("company_name") or current.company_name or "").strip()
current.company_guid = str(((mirror or {}).get("company") or {}).get("company_guid") or current.company_guid or "").strip()
current.file_size_bytes = int((mirror or {}).get("size_bytes") or current.file_size_bytes or 0)
discovered_path = str((job or {}).get("accounting_db_path") or (mirror or {}).get("path") or "").strip()
if discovered_path:
current.mirror_local_path = discovered_path
current.updated_at_utc = _mirror_utcnow()
db.flush()
return current