átlagos kiegészítséek jó sok
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
# backend/app/api/v1/endpoints/expenses.py
|
||||
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/expenses.py
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.models.asset import Asset, AssetCost # JAVÍTVA
|
||||
from app.models import Asset, AssetCost # JAVÍTVA
|
||||
from pydantic import BaseModel
|
||||
from datetime import date
|
||||
|
||||
@@ -18,15 +18,23 @@ class ExpenseCreate(BaseModel):
|
||||
@router.post("/add")
|
||||
async def add_expense(expense: ExpenseCreate, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
|
||||
stmt = select(Asset).where(Asset.id == expense.asset_id)
|
||||
if not (await db.execute(stmt)).scalar_one_or_none():
|
||||
result = await db.execute(stmt)
|
||||
asset = result.scalar_one_or_none()
|
||||
if not asset:
|
||||
raise HTTPException(status_code=404, detail="Jármű nem található.")
|
||||
|
||||
|
||||
# Determine organization_id from asset
|
||||
organization_id = asset.current_organization_id or asset.owner_org_id
|
||||
if not organization_id:
|
||||
raise HTTPException(status_code=400, detail="Az eszközhez nincs társított szervezet.")
|
||||
|
||||
new_cost = AssetCost(
|
||||
asset_id=expense.asset_id,
|
||||
cost_type=expense.category,
|
||||
amount_local=expense.amount,
|
||||
cost_category=expense.category,
|
||||
amount_net=expense.amount,
|
||||
currency="HUF",
|
||||
date=expense.date,
|
||||
currency_local="HUF"
|
||||
organization_id=organization_id
|
||||
)
|
||||
db.add(new_cost)
|
||||
await db.commit()
|
||||
|
||||
Reference in New Issue
Block a user