TorbatYar/backend/services/accounting/app/api/v1/setup.py
Mortezakoohjani 12c8615615 Ship enterprise Accounting FE/API with CRUD parity and production wiring.
Adds accounting-service PATCH/archive, fiscal helpers, COA templates and setup status, plus SuperApp Accounting UI (DS, scoreboard, masters, vouchers, ledger, ops modules) with session refresh and HTTPS public API URLs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 15:26:43 +03:30

69 lines
2.4 KiB
Python

"""Setup progress API — computed from real tenant data (no mocks)."""
from __future__ import annotations
from uuid import UUID
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_db, require_tenant
from app.core.security import get_current_user
from app.repositories.foundation import (
AccountRepository,
ChartOfAccountsRepository,
CurrencyRepository,
FiscalPeriodRepository,
FiscalYearRepository,
)
from app.repositories.posting import VoucherRepository
from app.schemas.foundation import SetupStatusRead
from shared.security import CurrentUser
router = APIRouter()
@router.get("/status", response_model=SetupStatusRead)
async def setup_status(
tenant_id: UUID = Depends(require_tenant),
db: AsyncSession = Depends(get_db),
_user: CurrentUser = Depends(get_current_user),
):
charts = await ChartOfAccountsRepository(db).list_by_tenant(tenant_id, limit=1)
accounts = await AccountRepository(db).list_by_tenant(tenant_id, limit=1)
currencies = await CurrencyRepository(db).list_by_tenant(tenant_id, limit=100)
years = await FiscalYearRepository(db).list_by_tenant(tenant_id, limit=1)
periods = await FiscalPeriodRepository(db).list_by_tenant(tenant_id, limit=1)
has_cash = False
has_customer = False
try:
from app.api.v1.treasury import CashBoxRepo
from app.api.v1.receivable_payable import CustomerRepo
has_cash = bool(await CashBoxRepo(db).list_by_tenant(tenant_id, limit=1))
has_customer = bool(await CustomerRepo(db).list_by_tenant(tenant_id, limit=1))
except Exception:
pass
has_voucher = bool(await VoucherRepository(db).list_by_tenant(tenant_id, limit=1))
flags = {
"has_chart": bool(charts),
"has_accounts": bool(accounts),
"has_currency": bool(currencies),
"has_base_currency": any(c.is_base for c in currencies),
"has_fiscal_year": bool(years),
"has_fiscal_period": bool(periods),
"has_cash_box": has_cash,
"has_customer": has_customer,
"has_voucher": has_voucher,
}
completed = sum(1 for v in flags.values() if v)
total = len(flags)
return SetupStatusRead(
**flags,
completed_steps=completed,
total_steps=total,
percent=round(100 * completed / total) if total else 0,
)