Fix opening balance FY mirror resolution
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from io import BytesIO
|
||||
from urllib.parse import urlencode
|
||||
from datetime import date, datetime
|
||||
@@ -107,6 +108,30 @@ def _page_rows(rows, page, per_page=50):
|
||||
return rows[start:start + per_page], total, page, pages
|
||||
|
||||
|
||||
def _previous_financial_year(financial_year: str) -> str:
|
||||
"""Return the immediately preceding Indian financial year."""
|
||||
value = str(financial_year or "").strip()
|
||||
match = re.fullmatch(r"(\d{4})-(\d{2})", value)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid financial year: {value}")
|
||||
start_year = int(match.group(1))
|
||||
previous_start = start_year - 1
|
||||
return f"{previous_start}-{str(start_year)[-2:]}"
|
||||
|
||||
|
||||
def _financial_year_options(reference_fy: str, years_back: int = 6) -> list[str]:
|
||||
"""Return the active FY and earlier FYs for the review selector."""
|
||||
value = str(reference_fy or "").strip()
|
||||
match = re.fullmatch(r"(\d{4})-(\d{2})", value)
|
||||
if not match:
|
||||
return [value] if value else []
|
||||
start_year = int(match.group(1))
|
||||
return [
|
||||
f"{year}-{str(year + 1)[-2:]}"
|
||||
for year in range(start_year, start_year - max(1, int(years_back)), -1)
|
||||
]
|
||||
|
||||
|
||||
@router.get("")
|
||||
def page(
|
||||
request: Request,
|
||||
@@ -120,6 +145,7 @@ def page(
|
||||
message: str = "",
|
||||
error: str = "",
|
||||
analysis_run_id: int = 0,
|
||||
financial_year: str = "",
|
||||
):
|
||||
db = CommonSessionLocal()
|
||||
try:
|
||||
@@ -139,36 +165,70 @@ def page(
|
||||
companies = []
|
||||
command_error = error or ""
|
||||
today = date.today()
|
||||
current_fy = _financial_year_for_date(today)
|
||||
current_start = int(current_fy.split("-", 1)[0])
|
||||
previous_fy = f"{current_start-1}-{str(current_start)[-2:]}"
|
||||
active_fy = _financial_year_for_date(today)
|
||||
fy_options = _financial_year_options(active_fy, 7)
|
||||
|
||||
requested_fy = str(financial_year or "").strip()
|
||||
if requested_fy and not re.fullmatch(r"\d{4}-\d{2}", requested_fy):
|
||||
command_error = command_error or "Invalid financial year selection."
|
||||
requested_fy = ""
|
||||
|
||||
current_fy = requested_fy or active_fy
|
||||
previous_fy = _previous_financial_year(current_fy)
|
||||
previous_mirror_status = None
|
||||
current_mirror_status = None
|
||||
|
||||
if selected and node and online:
|
||||
status_cache = {}
|
||||
|
||||
def mirror_status_for(fy_value: str):
|
||||
if fy_value in status_cache:
|
||||
return status_cache[fy_value]
|
||||
response = request_agent_command(
|
||||
node.node_code,
|
||||
"accounting_mirror_status",
|
||||
_accounting_storage_payload(selected, fy_value),
|
||||
timeout_seconds=20,
|
||||
)
|
||||
status = (response.get("result") or {}) if response.get("ok") else {}
|
||||
status_cache[fy_value] = (status, response)
|
||||
return status, response
|
||||
|
||||
try:
|
||||
prev_response = request_agent_command(
|
||||
node.node_code,
|
||||
"accounting_mirror_status",
|
||||
_accounting_storage_payload(selected, previous_fy),
|
||||
timeout_seconds=20,
|
||||
)
|
||||
curr_response = request_agent_command(
|
||||
node.node_code,
|
||||
"accounting_mirror_status",
|
||||
_accounting_storage_payload(selected, current_fy),
|
||||
timeout_seconds=20,
|
||||
)
|
||||
if prev_response.get("ok"):
|
||||
previous_mirror_status = prev_response.get("result") or {}
|
||||
if curr_response.get("ok"):
|
||||
current_mirror_status = curr_response.get("result") or {}
|
||||
if not prev_response.get("ok"):
|
||||
command_error = str(prev_response.get("error") or "Previous-year Accounting Mirror status failed.")
|
||||
elif not curr_response.get("ok"):
|
||||
command_error = str(curr_response.get("error") or "Current-year Accounting Mirror status failed.")
|
||||
# When the user has not selected a review FY explicitly, prefer
|
||||
# the active ERP FY only if its mirror exists. Otherwise use
|
||||
# the immediately preceding FY when that is the newest mirror
|
||||
# already available for the client. This prevents an existing
|
||||
# FY2025-26 mirror from being shown as "missing" merely because
|
||||
# the ERP header is now FY2026-27.
|
||||
if not requested_fy:
|
||||
active_status, active_response = mirror_status_for(active_fy)
|
||||
active_ready = bool((active_status.get("mirror") or {}).get("ready"))
|
||||
if active_ready:
|
||||
current_fy = active_fy
|
||||
else:
|
||||
prior_active_fy = _previous_financial_year(active_fy)
|
||||
prior_status, prior_response = mirror_status_for(prior_active_fy)
|
||||
prior_ready = bool((prior_status.get("mirror") or {}).get("ready"))
|
||||
if prior_ready:
|
||||
current_fy = prior_active_fy
|
||||
else:
|
||||
current_fy = active_fy
|
||||
|
||||
previous_fy = _previous_financial_year(current_fy)
|
||||
current_mirror_status, current_response = mirror_status_for(current_fy)
|
||||
previous_mirror_status, previous_response = mirror_status_for(previous_fy)
|
||||
|
||||
if not current_response.get("ok"):
|
||||
command_error = command_error or str(
|
||||
current_response.get("error") or "Current-year Accounting Mirror status failed."
|
||||
)
|
||||
elif not previous_response.get("ok"):
|
||||
command_error = command_error or str(
|
||||
previous_response.get("error") or "Previous-year Accounting Mirror status failed."
|
||||
)
|
||||
except Exception as exc:
|
||||
command_error = str(exc)
|
||||
command_error = command_error or str(exc)
|
||||
|
||||
runs = []
|
||||
selected_run = None
|
||||
@@ -276,6 +336,12 @@ def page(
|
||||
"node_online": online,
|
||||
"tally": tally,
|
||||
"companies": companies,
|
||||
"active_fy": active_fy,
|
||||
"fy_options": fy_options,
|
||||
"current_fy": current_fy,
|
||||
"previous_fy": previous_fy,
|
||||
"current_mirror_status": current_mirror_status,
|
||||
"previous_mirror_status": previous_mirror_status,
|
||||
"runs": runs,
|
||||
"selected_run": selected_run,
|
||||
"ledgers": ledgers,
|
||||
|
||||
Reference in New Issue
Block a user