|
|
|
@ -3,172 +3,58 @@
|
|
|
|
from sqlalchemy import Select, Delete, func
|
|
|
|
from sqlalchemy import Select, Delete, func
|
|
|
|
|
|
|
|
|
|
|
|
from src.database.service import search_filter_sort_paginate
|
|
|
|
from src.database.service import search_filter_sort_paginate
|
|
|
|
from src.overhaul_activity.model import OverhaulActivity
|
|
|
|
from .model import OverhaulJob
|
|
|
|
from src.scope_equipment.service import get_by_scope_name
|
|
|
|
|
|
|
|
from src.utils import time_now
|
|
|
|
|
|
|
|
from .model import OverhaulScope
|
|
|
|
|
|
|
|
from .schema import ScopeCreate, ScopeUpdate
|
|
|
|
|
|
|
|
from .utils import get_material_cost, get_service_cost
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from .schema import OverhaulJobCreate
|
|
|
|
|
|
|
|
|
|
|
|
from src.database.core import DbSession
|
|
|
|
from src.database.core import DbSession
|
|
|
|
from src.auth.service import CurrentUser
|
|
|
|
from src.auth.service import CurrentUser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get(*, db_session: DbSession, overhaul_session_id: str) -> Optional[OverhaulScope]:
|
|
|
|
|
|
|
|
"""Returns a document based on the given document id."""
|
|
|
|
|
|
|
|
query = Select(OverhaulScope).filter(
|
|
|
|
|
|
|
|
OverhaulScope.id == overhaul_session_id)
|
|
|
|
|
|
|
|
result = await db_session.execute(query)
|
|
|
|
|
|
|
|
return result.scalars().one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_all(*, common, overhaul_session_id: str):
|
|
|
|
async def get_all(*, common, scope_name: Optional[str] = None):
|
|
|
|
|
|
|
|
"""Returns all documents."""
|
|
|
|
"""Returns all documents."""
|
|
|
|
query = Select(OverhaulScope)
|
|
|
|
query = Select(OverhaulJob).where(OverhaulJob.overhaul_scope_id == overhaul_session_id)
|
|
|
|
|
|
|
|
|
|
|
|
if scope_name:
|
|
|
|
|
|
|
|
query = query.filter(OverhaulScope.type == scope_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = await search_filter_sort_paginate(model=query, **common)
|
|
|
|
results = await search_filter_sort_paginate(model=query, **common)
|
|
|
|
return results
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create(*, db_session: DbSession, scope_in: ScopeCreate):
|
|
|
|
async def create(*, db_session: DbSession, overhaul_session_id, scope_job_in: OverhaulJobCreate):
|
|
|
|
"""Creates a new document."""
|
|
|
|
scope_jobs = []
|
|
|
|
overhaul_session = OverhaulScope(**scope_in.model_dump())
|
|
|
|
|
|
|
|
db_session.add(overhaul_session)
|
|
|
|
|
|
|
|
# Need to flush to get the id
|
|
|
|
|
|
|
|
await db_session.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scope_name = scope_in.type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Fix the function call - parameters were in wrong order
|
|
|
|
|
|
|
|
equipments = await get_by_scope_name(
|
|
|
|
|
|
|
|
db_session=db_session,
|
|
|
|
|
|
|
|
scope_name=scope_name
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
material_cost = get_material_cost(scope=overhaul_session.type, total_equipment=len(equipments))
|
|
|
|
if not overhaul_session_id:
|
|
|
|
service_cost = get_service_cost(scope=overhaul_session.type, total_equipment=len(equipments))
|
|
|
|
raise ValueError("assetnum parameter is required")
|
|
|
|
|
|
|
|
|
|
|
|
scope_equipments = [
|
|
|
|
for job_id in scope_job_in.job_ids:
|
|
|
|
OverhaulActivity(
|
|
|
|
scope_equipment_job = OverhaulJob(
|
|
|
|
assetnum=equipment.assetnum,
|
|
|
|
overhaul_session_id=overhaul_session_id, job_id=job_id)
|
|
|
|
overhaul_scope_id=overhaul_session.id,
|
|
|
|
|
|
|
|
material_cost=material_cost,
|
|
|
|
scope_jobs.append(scope_equipment_job)
|
|
|
|
service_cost=service_cost,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
for equipment in equipments
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if scope_equipments: # Only add if there are items
|
|
|
|
|
|
|
|
db_session.add_all(scope_equipments)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db_session.add_all(scope_jobs)
|
|
|
|
await db_session.commit()
|
|
|
|
await db_session.commit()
|
|
|
|
return overhaul_session
|
|
|
|
return scope_job_in.job_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def update(*, db_session: DbSession, scope: OverhaulScope, scope_in: ScopeUpdate):
|
|
|
|
|
|
|
|
"""Updates a document."""
|
|
|
|
|
|
|
|
data = scope_in.model_dump()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update_data = scope_in.model_dump(exclude_defaults=True)
|
|
|
|
# async def update(*, db_session: DbSession, scope: OverhaulScope, scope_in: ScopeUpdate):
|
|
|
|
|
|
|
|
# """Updates a document."""
|
|
|
|
|
|
|
|
# data = scope_in.model_dump()
|
|
|
|
|
|
|
|
|
|
|
|
for field in data:
|
|
|
|
# update_data = scope_in.model_dump(exclude_defaults=True)
|
|
|
|
if field in update_data:
|
|
|
|
|
|
|
|
setattr(scope, field, update_data[field])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await db_session.commit()
|
|
|
|
# for field in data:
|
|
|
|
|
|
|
|
# if field in update_data:
|
|
|
|
|
|
|
|
# setattr(scope, field, update_data[field])
|
|
|
|
|
|
|
|
|
|
|
|
return scope
|
|
|
|
# await db_session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return scope
|
|
|
|
|
|
|
|
|
|
|
|
async def delete(*, db_session: DbSession, scope_id: str):
|
|
|
|
|
|
|
|
"""Deletes a document."""
|
|
|
|
|
|
|
|
query = Delete(OverhaulScope).where(OverhaulScope.id == scope_id)
|
|
|
|
|
|
|
|
await db_session.execute(query)
|
|
|
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# async def delete(*, db_session: DbSession, scope_id: str):
|
|
|
|
|
|
|
|
# """Deletes a document."""
|
|
|
|
|
|
|
|
# query = Delete(OverhaulScope).where(OverhaulScope.id == scope_id)
|
|
|
|
|
|
|
|
# await db_session.execute(query)
|
|
|
|
|
|
|
|
# await db_session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
async def get_overview_overhaul(*, db_session: DbSession):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_date = time_now().date()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# For ongoing overhaul with count
|
|
|
|
|
|
|
|
ongoing_query = Select(
|
|
|
|
|
|
|
|
OverhaulScope,
|
|
|
|
|
|
|
|
func.count(OverhaulActivity.id).label('equipment_count')
|
|
|
|
|
|
|
|
).outerjoin(
|
|
|
|
|
|
|
|
OverhaulScope.activity_equipments
|
|
|
|
|
|
|
|
).where(
|
|
|
|
|
|
|
|
OverhaulScope.start_date <= current_date,
|
|
|
|
|
|
|
|
OverhaulScope.end_date >= current_date,
|
|
|
|
|
|
|
|
).group_by(
|
|
|
|
|
|
|
|
OverhaulScope.id
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ongoing_result = await db_session.execute(ongoing_query)
|
|
|
|
|
|
|
|
# Use first() instead of scalar_one_or_none()
|
|
|
|
|
|
|
|
ongoing_result = ongoing_result.first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ongoing_result:
|
|
|
|
|
|
|
|
ongoing_overhaul, equipment_count = ongoing_result # Unpack the result tuple
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
"status": "Ongoing",
|
|
|
|
|
|
|
|
"overhaul": {
|
|
|
|
|
|
|
|
"id": ongoing_overhaul.id,
|
|
|
|
|
|
|
|
"type": ongoing_overhaul.type,
|
|
|
|
|
|
|
|
"start_date": ongoing_overhaul.start_date,
|
|
|
|
|
|
|
|
"end_date": ongoing_overhaul.end_date,
|
|
|
|
|
|
|
|
"duration_oh": ongoing_overhaul.duration_oh,
|
|
|
|
|
|
|
|
"crew_number": ongoing_overhaul.crew_number,
|
|
|
|
|
|
|
|
"remaining_days": (ongoing_overhaul.end_date - current_date).days,
|
|
|
|
|
|
|
|
"equipment_count": equipment_count
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# For upcoming overhaul with count
|
|
|
|
|
|
|
|
upcoming_query = Select(
|
|
|
|
|
|
|
|
OverhaulScope,
|
|
|
|
|
|
|
|
func.count(OverhaulActivity.id).label('equipment_count')
|
|
|
|
|
|
|
|
).outerjoin(
|
|
|
|
|
|
|
|
OverhaulScope.activity_equipments
|
|
|
|
|
|
|
|
).where(
|
|
|
|
|
|
|
|
OverhaulScope.start_date > current_date,
|
|
|
|
|
|
|
|
).group_by(
|
|
|
|
|
|
|
|
OverhaulScope.id
|
|
|
|
|
|
|
|
).order_by(
|
|
|
|
|
|
|
|
OverhaulScope.start_date
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upcoming_result = await db_session.execute(upcoming_query)
|
|
|
|
|
|
|
|
upcoming_result = upcoming_result.first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if upcoming_result:
|
|
|
|
|
|
|
|
upcoming_overhaul, equipment_count = upcoming_result # Unpack the result tuple
|
|
|
|
|
|
|
|
days_until = (upcoming_overhaul.start_date - current_date).days
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
"status": "Upcoming",
|
|
|
|
|
|
|
|
"overhaul": {
|
|
|
|
|
|
|
|
"id": upcoming_overhaul.id,
|
|
|
|
|
|
|
|
"type": upcoming_overhaul.type,
|
|
|
|
|
|
|
|
"start_date": upcoming_overhaul.start_date,
|
|
|
|
|
|
|
|
"end_date": upcoming_overhaul.end_date,
|
|
|
|
|
|
|
|
"duration_oh": upcoming_overhaul.duration_oh,
|
|
|
|
|
|
|
|
"crew_number": upcoming_overhaul.crew_number,
|
|
|
|
|
|
|
|
"remaining_days": days_until,
|
|
|
|
|
|
|
|
"equipment_count": equipment_count
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
"status": "no_overhaul",
|
|
|
|
|
|
|
|
"overhaul": None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|