feat(backend): sync endpoints + all models + seed + order workflow
- All business models: Vehicle, Order, OrderLine, Invoice, Appointment, CatalogMarca/Model/Ansamblu/Norma/Pret/TipDeviz/TipMotor, Mecanic - Sync endpoints: GET /sync/full, GET /sync/changes?since=, POST /sync/push with tenant isolation and last-write-wins conflict resolution - Order CRUD with state machine: DRAFT -> VALIDAT -> FACTURAT Auto-recalculates totals (manopera + materiale) - Vehicle CRUD: list, create, get, update - Seed data: 24 marci, 11 ansamble, 6 tipuri deviz, 5 tipuri motoare, 3 preturi - Alembic migration for all business models - 13 passing tests (auth + sync + orders) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
0
backend/app/orders/__init__.py
Normal file
0
backend/app/orders/__init__.py
Normal file
83
backend/app/orders/router.py
Normal file
83
backend/app/orders/router.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.session import get_db
|
||||
from app.deps import get_tenant_id
|
||||
from app.orders import schemas, service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_orders(
|
||||
tenant_id: str = Depends(get_tenant_id),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
return await service.list_orders(db, tenant_id)
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_order(
|
||||
data: schemas.CreateOrderRequest,
|
||||
tenant_id: str = Depends(get_tenant_id),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
order = await service.create_order(
|
||||
db,
|
||||
tenant_id,
|
||||
data.vehicle_id,
|
||||
data.tip_deviz_id,
|
||||
data.km_intrare,
|
||||
data.observatii,
|
||||
)
|
||||
return {"id": order.id}
|
||||
|
||||
|
||||
@router.get("/{order_id}")
|
||||
async def get_order(
|
||||
order_id: str,
|
||||
tenant_id: str = Depends(get_tenant_id),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await service.get_order(db, tenant_id, order_id)
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Order not found")
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/{order_id}/lines")
|
||||
async def add_line(
|
||||
order_id: str,
|
||||
data: schemas.AddLineRequest,
|
||||
tenant_id: str = Depends(get_tenant_id),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
line = await service.add_line(
|
||||
db,
|
||||
tenant_id,
|
||||
order_id,
|
||||
data.tip,
|
||||
data.descriere,
|
||||
data.ore,
|
||||
data.pret_ora,
|
||||
data.cantitate,
|
||||
data.pret_unitar,
|
||||
data.um,
|
||||
)
|
||||
return {"id": line.id}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/{order_id}/validate")
|
||||
async def validate_order(
|
||||
order_id: str,
|
||||
tenant_id: str = Depends(get_tenant_id),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
order = await service.validate_order(db, tenant_id, order_id)
|
||||
return {"status": order.status}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
18
backend/app/orders/schemas.py
Normal file
18
backend/app/orders/schemas.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class CreateOrderRequest(BaseModel):
|
||||
vehicle_id: str
|
||||
tip_deviz_id: str | None = None
|
||||
km_intrare: int | None = None
|
||||
observatii: str | None = None
|
||||
|
||||
|
||||
class AddLineRequest(BaseModel):
|
||||
tip: str # manopera | material
|
||||
descriere: str
|
||||
ore: float = 0
|
||||
pret_ora: float = 0
|
||||
cantitate: float = 0
|
||||
pret_unitar: float = 0
|
||||
um: str | None = None
|
||||
188
backend/app/orders/service.py
Normal file
188
backend/app/orders/service.py
Normal file
@@ -0,0 +1,188 @@
|
||||
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
|
||||
]
|
||||
Reference in New Issue
Block a user