Files
roaauto/backend/app/clients/router.py
Marius Mutu 9db4e746e3 feat: add clients nomenclator, order edit/delete/devalidate, invoice types, dashboard redesign
- New clients table with PF/PJ support, fiscal data (CUI, IBAN, eFactura fields)
- Full CRUD API for clients with search, sync integration
- Order lifecycle: edit header (DRAFT), devalidate (VALIDAT→DRAFT), delete order/invoice
- Invoice types: FACTURA (B2B) vs BON_FISCAL (B2C) with different nr formats
- OrderCreateView redesigned as multi-step flow (client→vehicle→details)
- Autocomplete from catalog_norme/catalog_preturi in OrderLineForm
- Dashboard now combines stats + full orders table with filter tabs and search
- ClientPicker and VehiclePicker with inline creation capability
- Frontend schema aligned with backend (missing columns causing sync errors)
- Mobile responsive fixes for OrderDetailView buttons

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 00:36:40 +02:00

164 lines
4.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select, or_
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.base import uuid7
from app.db.models.client import Client
from app.db.session import get_db
from app.deps import get_tenant_id
from app.clients import schemas
router = APIRouter()
@router.get("")
async def list_clients(
search: str | None = None,
tenant_id: str = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
query = select(Client).where(Client.tenant_id == tenant_id)
if search:
pattern = f"%{search}%"
query = query.where(
or_(
Client.denumire.ilike(pattern),
Client.nume.ilike(pattern),
Client.prenume.ilike(pattern),
Client.cod_fiscal.ilike(pattern),
Client.telefon.ilike(pattern),
Client.email.ilike(pattern),
)
)
r = await db.execute(query)
clients = r.scalars().all()
return [
{
"id": c.id,
"tip_persoana": c.tip_persoana,
"denumire": c.denumire,
"nume": c.nume,
"prenume": c.prenume,
"cod_fiscal": c.cod_fiscal,
"reg_com": c.reg_com,
"telefon": c.telefon,
"email": c.email,
"adresa": c.adresa,
"judet": c.judet,
"oras": c.oras,
"cod_postal": c.cod_postal,
"tara": c.tara,
"cont_iban": c.cont_iban,
"banca": c.banca,
"activ": c.activ,
}
for c in clients
]
@router.post("")
async def create_client(
data: schemas.ClientCreate,
tenant_id: str = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
client = Client(
id=data.id or uuid7(),
tenant_id=tenant_id,
tip_persoana=data.tip_persoana,
denumire=data.denumire,
nume=data.nume,
prenume=data.prenume,
cod_fiscal=data.cod_fiscal,
reg_com=data.reg_com,
telefon=data.telefon,
email=data.email,
adresa=data.adresa,
judet=data.judet,
oras=data.oras,
cod_postal=data.cod_postal,
tara=data.tara,
cont_iban=data.cont_iban,
banca=data.banca,
activ=data.activ,
)
db.add(client)
await db.commit()
return {"id": client.id}
@router.get("/{client_id}")
async def get_client(
client_id: str,
tenant_id: str = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
r = await db.execute(
select(Client).where(
Client.id == client_id, Client.tenant_id == tenant_id
)
)
c = r.scalar_one_or_none()
if not c:
raise HTTPException(status_code=404, detail="Client not found")
return {
"id": c.id,
"tip_persoana": c.tip_persoana,
"denumire": c.denumire,
"nume": c.nume,
"prenume": c.prenume,
"cod_fiscal": c.cod_fiscal,
"reg_com": c.reg_com,
"telefon": c.telefon,
"email": c.email,
"adresa": c.adresa,
"judet": c.judet,
"oras": c.oras,
"cod_postal": c.cod_postal,
"tara": c.tara,
"cont_iban": c.cont_iban,
"banca": c.banca,
"activ": c.activ,
}
@router.put("/{client_id}")
async def update_client(
client_id: str,
data: schemas.ClientUpdate,
tenant_id: str = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
r = await db.execute(
select(Client).where(
Client.id == client_id, Client.tenant_id == tenant_id
)
)
c = r.scalar_one_or_none()
if not c:
raise HTTPException(status_code=404, detail="Client not found")
update_data = data.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(c, key, value)
await db.commit()
return {"ok": True}
@router.delete("/{client_id}")
async def delete_client(
client_id: str,
tenant_id: str = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
r = await db.execute(
select(Client).where(
Client.id == client_id, Client.tenant_id == tenant_id
)
)
c = r.scalar_one_or_none()
if not c:
raise HTTPException(status_code=404, detail="Client not found")
await db.delete(c)
await db.commit()
return {"ok": True}