201 előtti mentés
This commit is contained in:
@@ -30,22 +30,25 @@ class AssetService:
|
||||
|
||||
@staticmethod
|
||||
async def create_or_claim_vehicle(
|
||||
db: AsyncSession,
|
||||
user_id: int,
|
||||
org_id: int,
|
||||
vin: str,
|
||||
license_plate: str,
|
||||
catalog_id: int = None
|
||||
db: AsyncSession,
|
||||
user_id: int,
|
||||
org_id: int,
|
||||
vin: Optional[str] = None,
|
||||
license_plate: Optional[str] = None,
|
||||
catalog_id: int = None,
|
||||
draft: bool = False
|
||||
):
|
||||
"""
|
||||
Intelligens Jármű Rögzítés:
|
||||
Ha új: létrehozza.
|
||||
Intelligens Jármű Rögzítés:
|
||||
Ha új: létrehozza.
|
||||
Ha már létezik: Transzfer folyamatot indít.
|
||||
Ha draft=True vagy VIN hiányzik: draft státuszban hozza létre.
|
||||
"""
|
||||
try:
|
||||
vin_clean = vin.strip().upper()
|
||||
vin_clean = vin.strip().upper() if vin else None
|
||||
license_plate_clean = license_plate.strip().upper() if license_plate else None
|
||||
|
||||
# 1. ADMIN LIMIT ELLENŐRZÉS
|
||||
# 1. ADMIN LIMIT ELLENŐRZÉS (csak aktív járművek számítanak)
|
||||
user_stmt = select(User).where(User.id == user_id)
|
||||
user = (await db.execute(user_stmt)).scalar_one()
|
||||
|
||||
@@ -53,15 +56,21 @@ class AssetService:
|
||||
user_role = user.role.value if hasattr(user.role, 'value') else str(user.role)
|
||||
allowed_limit = limits.get(user_role, 1)
|
||||
|
||||
count_stmt = select(func.count(Asset.id)).where(Asset.current_organization_id == org_id)
|
||||
# Csak aktív járművek számítanak a limitbe (draft-ok nem)
|
||||
count_stmt = select(func.count(Asset.id)).where(
|
||||
Asset.current_organization_id == org_id,
|
||||
Asset.status == "active"
|
||||
)
|
||||
current_count = (await db.execute(count_stmt)).scalar()
|
||||
|
||||
if current_count >= allowed_limit:
|
||||
if current_count >= allowed_limit and not draft:
|
||||
raise ValueError(f"Limit túllépés! A csomagod {allowed_limit} autót engedélyez.")
|
||||
|
||||
# 2. LÉTEZIK-E MÁR A JÁRMŰ?
|
||||
stmt = select(Asset).where(Asset.vin == vin_clean)
|
||||
existing_asset = (await db.execute(stmt)).scalar_one_or_none()
|
||||
# 2. LÉTEZIK-E MÁR A JÁRMŰ? (csak ha van VIN)
|
||||
existing_asset = None
|
||||
if vin_clean:
|
||||
stmt = select(Asset).where(Asset.vin == vin_clean)
|
||||
existing_asset = (await db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
if existing_asset:
|
||||
# HA MÁR A JELENLEGI SZERVEZETNÉL VAN
|
||||
@@ -70,16 +79,17 @@ class AssetService:
|
||||
|
||||
# TRANSZFER FOLYAMAT INDÍTÁSA
|
||||
return await AssetService.initiate_ownership_transfer(
|
||||
db, existing_asset, user_id, org_id, license_plate
|
||||
db, existing_asset, user_id, org_id, license_plate_clean or ""
|
||||
)
|
||||
|
||||
# 3. ÚJ JÁRMŰ LÉTREHOZÁSA (Standard Flow)
|
||||
# 3. ÚJ JÁRMŰ LÉTREHOZÁSA (Standard vagy Draft Flow)
|
||||
status = "draft" if draft or not vin_clean else "active"
|
||||
new_asset = Asset(
|
||||
vin=vin_clean,
|
||||
license_plate=license_plate.strip().upper(),
|
||||
license_plate=license_plate_clean,
|
||||
catalog_id=catalog_id,
|
||||
current_organization_id=org_id,
|
||||
status="active",
|
||||
status=status,
|
||||
individual_equipment={},
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user