Fix live Purchase and Sales historical Tally sync

This commit is contained in:
A R R R Associates
2026-08-23 11:48:09 +05:30
parent 395f4eed1a
commit 6910b5b6b5
8 changed files with 293 additions and 68 deletions
+16 -29
View File
@@ -11,7 +11,6 @@ from app.core.db.common import CommonSessionLocal
from app.core.security.csrf import get_or_create_csrf_token, validate_csrf
from app.core.templating import templates
from app.modules.accounting.historical_learning_models import AccountingHistoricalLearningRun
from app.modules.accounting.ledger_learning_service import available_tally_guids
from app.modules.accounting.sales_learning_models import AccountingSalesHistoricalRun
from app.modules.accounting.sales_learning_service import (
historical_sales_rows,
@@ -19,7 +18,8 @@ from app.modules.accounting.sales_learning_service import (
learning_summary,
sales_mappings,
)
from app.modules.accounting.ui import _find_visible_client, _require_partner, _visible_clients
from app.modules.accounting.ui import _accounting_storage_payload, _find_visible_client, _require_partner, _visible_clients
from app.modules.accounting.historical_sync_support import eligible_tally_workstations, resolve_workstation_company, workstation_companies
from app.modules.core.rbac.deps import get_user_permissions, get_user_roles
from app.modules.documents.agent_jobs import enqueue_agent_job
from app.modules.documents.models import ERPWorkstationAgent
@@ -44,15 +44,6 @@ def _go(client_id=0, message="", error=""):
)
def _workstations(db, tenant_id: int):
return list(db.execute(
select(ERPWorkstationAgent).where(
ERPWorkstationAgent.tenant_id == int(tenant_id),
ERPWorkstationAgent.is_active.is_(True),
ERPWorkstationAgent.tally_connected.is_(True),
).order_by(ERPWorkstationAgent.machine_name)
).scalars().all())
@router.get("")
def page(
@@ -76,7 +67,6 @@ def page(
mappings = []
evidence = []
summary = {"mapping_count": 0, "historical_rows": 0, "review_confirmed": 0, "high_confidence": 0}
guids = []
workstations = []
runs = []
@@ -95,8 +85,7 @@ def page(
mappings = sales_mappings(db, tenant_id=scope.tenant_id, client_id=selected.id)
evidence = historical_sales_rows(db, tenant_id=scope.tenant_id, client_id=selected.id)[:200]
summary = learning_summary(db, tenant_id=scope.tenant_id, client_id=selected.id)
guids = available_tally_guids(db, scope.tenant_id, selected.id)
workstations = _workstations(db, scope.tenant_id)
workstations = eligible_tally_workstations(db, scope.tenant_id, scope.branch_id)
return templates.TemplateResponse(
"modules/accounting/templates/accounting/sales_learning.html",
@@ -112,8 +101,8 @@ def page(
"mappings": mappings,
"evidence": evidence,
"summary": summary,
"tally_guids": guids,
"workstations": workstations,
"workstation_companies": {w.id: workstation_companies(w) for w in workstations},
"runs": runs,
"message": message,
"error": error,
@@ -144,18 +133,13 @@ def collect(
if not client:
return _go(error="Client is not visible.")
workstation = db.get(ERPWorkstationAgent, int(workstation_id))
if (
not workstation
or int(workstation.tenant_id) != int(scope.tenant_id)
or not workstation.is_active
or not workstation.tally_connected
):
raise ValueError("Selected workstation is not available with Tally connected.")
guid_options = dict(available_tally_guids(db, scope.tenant_id, client.id))
if tally_guid not in guid_options:
raise ValueError("Selected Tally company is not mapped to this client.")
workstation, company = resolve_workstation_company(
db,
workstation_id=workstation_id,
tenant_id=scope.tenant_id,
branch_id=scope.branch_id,
tally_guid=tally_guid,
)
if not date_from:
date_from = (date.today() - timedelta(days=730)).isoformat()
@@ -163,12 +147,15 @@ def collect(
date_to = date.today().isoformat()
payload = {
**_accounting_storage_payload(client),
"tenant_id": scope.tenant_id,
"client_id": client.id,
"requested_by_user_id": user.id,
"tally_guid": tally_guid,
"company_name": str(company.get("name") or "").strip(),
"date_from": date_from,
"date_to": date_to,
"voucher_scope": "sales",
"refresh_transactions": True,
}
job = enqueue_agent_job(
db,
@@ -184,7 +171,7 @@ def collect(
tenant_id=scope.tenant_id,
client_id=client.id,
tally_guid=tally_guid,
company_name=guid_options.get(tally_guid, ""),
company_name=str(company.get("name") or ""),
workstation_agent_id=workstation.id,
agent_job_id=job.id,
date_from=date_from,