from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from datetime import date, datetime from database import get_db from models import Delivery_Delivery, Customer_Customer, Pricing_Oil_Oil from schemas import DeliveryCreate from routes.auth.current_user import get_current_user from models import Account_User router = APIRouter() # State mapping for converting integer codes to string abbreviations state_mapping = {0: "MA", 1: "NH"} # Add more as needed @router.post("/") async def create_delivery( delivery_data: DeliveryCreate, current_user: Account_User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ): if not current_user.user_id: raise HTTPException(status_code=404, detail="Customer not found") # Get customer details customer = await db.execute( select(Customer_Customer).where(Customer_Customer.id == current_user.user_id) ) customer = customer.scalar_one_or_none() if not customer: raise HTTPException(status_code=404, detail="Customer not found") # Validate gallons_ordered >= 100 if delivery_data.gallons_ordered < 100: raise HTTPException(status_code=400, detail="Gallons ordered must be at least 100") # Validate expected_delivery_date >= today today = date.today() if delivery_data.expected_delivery_date < today: raise HTTPException(status_code=400, detail="Expected delivery date cannot be in the past") # Get latest oil pricing oil = await db.execute( select(Pricing_Oil_Oil).order_by(Pricing_Oil_Oil.id.desc()).limit(1) ) oil = oil.scalar_one_or_none() if not oil: raise HTTPException(status_code=500, detail="No oil pricing available") # Create delivery new_delivery = Delivery_Delivery( customer_id=customer.id, customer_name=f"{customer.customer_first_name} {customer.customer_last_name}", customer_address=customer.customer_address, customer_town=customer.customer_town, customer_state=str(customer.customer_state), customer_zip=int(customer.customer_zip), gallons_ordered=delivery_data.gallons_ordered, gallons_delivered=None, customer_filled=None, delivery_status=0, # waiting when_ordered=today, when_delivered=None, expected_delivery_date=delivery_data.expected_delivery_date, automatic=None, automatic_id=None, oil_id=oil.id, supplier_price=oil.price_for_customer, # assuming this is the price customer_price=oil.price_for_customer, dispatcher_notes=delivery_data.dispatcher_notes, driver_employee_id=None, driver_first_name=None, driver_last_name=None, promo_id=None, promo_money_discount=None, payment_type=delivery_data.payment_type, # Set other fields to None or defaults customer_asked_for_fill=0, customer_temperature=None, prime=delivery_data.prime, same_day=delivery_data.same_day, emergency=delivery_data.emergency, payment_card_id=None, cash_recieved=None, pre_charge_amount=delivery_data.pre_charge_amount, total_price=None, final_price=None, check_number=None, ) db.add(new_delivery) await db.commit() await db.refresh(new_delivery) return {"message": "Delivery created successfully", "delivery_id": new_delivery.id}