egyedi jármű szerkesztés előtti mentés

This commit is contained in:
Roo
2026-06-12 07:56:15 +00:00
parent 0a3fd8de74
commit ef8df9608c
29 changed files with 3863 additions and 396 deletions

View File

@@ -3,7 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func
from app.api.deps import get_db, get_current_user
from app.models import Asset, AssetCost, SystemParameter
from app.models import Asset, AssetCost, OrganizationMember, SystemParameter
from app.schemas.asset_cost import AssetCostCreate
from datetime import datetime
@@ -53,13 +53,30 @@ async def create_expense(
detail=f"DRAFT_LIMIT_REACHED: Draft vehicles are limited to {limit} expenses. This asset already has {expense_count} expenses."
)
# Determine organization_id from asset (required by AssetCost model)
organization_id = asset.current_organization_id or asset.owner_org_id
# Determine organization_id: prefer explicit from payload, fallback to asset fields
organization_id = expense.organization_id or asset.current_organization_id or asset.owner_org_id
if not organization_id:
raise HTTPException(status_code=400, detail="Asset has no associated organization.")
# Map cost_type to cost_category (AssetCost uses cost_category)
cost_category = expense.cost_type
# B2C fallback: if the asset is owned by a person (not an org),
# try to find the current user's default organization
org_stmt = select(OrganizationMember).where(
OrganizationMember.user_id == current_user.id,
OrganizationMember.is_verified == True
).limit(1)
org_result = await db.execute(org_stmt)
org_member = org_result.scalar_one_or_none()
if org_member:
organization_id = org_member.organization_id
else:
# Last resort: any membership (even unverified)
org_stmt = select(OrganizationMember).where(
OrganizationMember.user_id == current_user.id
).limit(1)
org_result = await db.execute(org_stmt)
org_member = org_result.scalar_one_or_none()
if org_member:
organization_id = org_member.organization_id
else:
raise HTTPException(status_code=400, detail="Asset has no associated organization.")
# Prepare data JSON for extra fields (mileage_at_cost, description, etc.)
data = expense.data.copy() if expense.data else {}
@@ -72,7 +89,7 @@ async def create_expense(
new_cost = AssetCost(
asset_id=expense.asset_id,
organization_id=organization_id,
cost_category=cost_category,
category_id=expense.category_id,
amount_net=expense.amount_local,
currency=expense.currency_local,
date=expense.date,
@@ -88,7 +105,7 @@ async def create_expense(
"status": "success",
"id": new_cost.id,
"asset_id": new_cost.asset_id,
"cost_category": new_cost.cost_category,
"category_id": new_cost.category_id,
"amount_net": new_cost.amount_net,
"date": new_cost.date
}