from datetime import UTC, datetime from sqlalchemy import select, text from sqlalchemy.ext.asyncio import AsyncSession from app.db.base import uuid7 from app.db.models.order import Order from app.db.models.order_line import OrderLine TRANSITIONS = {"DRAFT": ["VALIDAT"], "VALIDAT": ["FACTURAT"]} async def create_order( db: AsyncSession, tenant_id: str, vehicle_id: str, tip_deviz_id: str | None = None, km_intrare: int | None = None, observatii: str | None = None, ) -> Order: now = datetime.now(UTC).isoformat() order = Order( id=uuid7(), tenant_id=tenant_id, vehicle_id=vehicle_id, tip_deviz_id=tip_deviz_id, status="DRAFT", data_comanda=now.split("T")[0], km_intrare=km_intrare, observatii=observatii, total_manopera=0, total_materiale=0, total_general=0, token_client=uuid7(), ) db.add(order) await db.commit() await db.refresh(order) return order async def add_line( db: AsyncSession, tenant_id: str, order_id: str, tip: str, descriere: str, ore: float = 0, pret_ora: float = 0, cantitate: float = 0, pret_unitar: float = 0, um: str | None = None, ) -> OrderLine: # Check order exists and belongs to tenant r = await db.execute( select(Order).where(Order.id == order_id, Order.tenant_id == tenant_id) ) order = r.scalar_one_or_none() if not order: raise ValueError("Order not found") if order.status != "DRAFT": raise ValueError("Cannot add lines to non-DRAFT order") if tip == "manopera": total = ore * pret_ora else: total = cantitate * pret_unitar line = OrderLine( id=uuid7(), tenant_id=tenant_id, order_id=order_id, tip=tip, descriere=descriere, ore=ore, pret_ora=pret_ora, cantitate=cantitate, pret_unitar=pret_unitar, um=um, total=total, ) db.add(line) await db.commit() await recalc_totals(db, order_id) return line async def recalc_totals(db: AsyncSession, order_id: str): lines = await db.execute( text( "SELECT tip, COALESCE(SUM(total), 0) as sub FROM order_lines " "WHERE order_id = :oid GROUP BY tip" ), {"oid": order_id}, ) totals = {r.tip: r.sub for r in lines} manopera = totals.get("manopera", 0) materiale = totals.get("material", 0) await db.execute( text( "UPDATE orders SET total_manopera=:m, total_materiale=:mat, " "total_general=:g, updated_at=:u WHERE id=:id" ), { "m": manopera, "mat": materiale, "g": manopera + materiale, "u": datetime.now(UTC).isoformat(), "id": order_id, }, ) await db.commit() async def validate_order( db: AsyncSession, tenant_id: str, order_id: str ) -> Order: r = await db.execute( select(Order).where(Order.id == order_id, Order.tenant_id == tenant_id) ) order = r.scalar_one_or_none() if not order: raise ValueError("Order not found") if "VALIDAT" not in TRANSITIONS.get(order.status, []): raise ValueError(f"Cannot transition from {order.status} to VALIDAT") order.status = "VALIDAT" order.updated_at = datetime.now(UTC).isoformat() await db.commit() await db.refresh(order) return order async def get_order( db: AsyncSession, tenant_id: str, order_id: str ) -> dict | None: r = await db.execute( select(Order).where(Order.id == order_id, Order.tenant_id == tenant_id) ) order = r.scalar_one_or_none() if not order: return None r = await db.execute( select(OrderLine).where(OrderLine.order_id == order_id) ) lines = r.scalars().all() return { "id": order.id, "vehicle_id": order.vehicle_id, "status": order.status, "data_comanda": order.data_comanda, "km_intrare": order.km_intrare, "observatii": order.observatii, "total_manopera": order.total_manopera, "total_materiale": order.total_materiale, "total_general": order.total_general, "token_client": order.token_client, "lines": [ { "id": l.id, "tip": l.tip, "descriere": l.descriere, "ore": l.ore, "pret_ora": l.pret_ora, "cantitate": l.cantitate, "pret_unitar": l.pret_unitar, "um": l.um, "total": l.total, } for l in lines ], } async def list_orders(db: AsyncSession, tenant_id: str) -> list: r = await db.execute( select(Order).where(Order.tenant_id == tenant_id) ) orders = r.scalars().all() return [ { "id": o.id, "status": o.status, "vehicle_id": o.vehicle_id, "total_general": o.total_general, } for o in orders ]