|
|
|
|
@ -12,7 +12,7 @@ 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 datetime import datetime
|
|
|
|
|
|
|
|
|
|
async def get(
|
|
|
|
|
*, db_session: DbSession, overhaul_session_id: str
|
|
|
|
|
@ -35,12 +35,48 @@ async def get_all(*, common, scope_name: Optional[str] = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create(*, db_session: DbSession, scope_in: ScopeCreate):
|
|
|
|
|
"""Creates a new document."""
|
|
|
|
|
overhaul_session = OverhaulScope(**scope_in.model_dump())
|
|
|
|
|
# Ensure dates are datetime objects
|
|
|
|
|
if isinstance(scope_in.start_date, str):
|
|
|
|
|
try:
|
|
|
|
|
start_date = datetime.fromisoformat(scope_in.start_date.replace('Z', '+00:00'))
|
|
|
|
|
except ValueError:
|
|
|
|
|
start_date = datetime.strptime(scope_in.start_date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
else:
|
|
|
|
|
start_date = scope_in.start_date
|
|
|
|
|
|
|
|
|
|
# Handle end_date (which could be None)
|
|
|
|
|
end_date = None
|
|
|
|
|
if scope_in.end_date:
|
|
|
|
|
if isinstance(scope_in.end_date, str):
|
|
|
|
|
try:
|
|
|
|
|
end_date = datetime.fromisoformat(scope_in.end_date.replace('Z', '+00:00'))
|
|
|
|
|
except ValueError:
|
|
|
|
|
end_date = datetime.strptime(scope_in.end_date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
else:
|
|
|
|
|
end_date = scope_in.end_date
|
|
|
|
|
|
|
|
|
|
# Calculate duration in days if both dates are available
|
|
|
|
|
duration_days = None
|
|
|
|
|
if start_date and end_date:
|
|
|
|
|
duration_days = (end_date - start_date).days
|
|
|
|
|
|
|
|
|
|
# Create the OverhaulScope object with all hardcoded values
|
|
|
|
|
overhaul_session = OverhaulScope(
|
|
|
|
|
start_date=scope_in.start_date,
|
|
|
|
|
end_date=scope_in.end_date,
|
|
|
|
|
type=scope_in.type, # Hardcoded type
|
|
|
|
|
duration_oh=duration_days, # Hardcoded duration (30 days)
|
|
|
|
|
crew_number=scope_in.crew_number, # Hardcoded crew number
|
|
|
|
|
status=scope_in.status # Hardcoded status
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# raise Exception(overhaul_session.start_date)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|