Files
eamco_money_api/app/routers/delivery.py
2024-11-20 18:00:52 -05:00

181 lines
5.6 KiB
Python

from typing import Annotated
from datetime import datetime, timezone
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from fastapi import APIRouter
from database import session
from app.models.delivery import Delivery
from app.models.money import MoneyDelivery
from app.models.pricing import Pricing_Oil_Oil
from app.models.promo import Promo_Promo
from app.models.auto import Tickets_Auto_Delivery
router = APIRouter(
prefix="/delivery",
tags=["delivery"],
responses={404: {"description": "Not found"}},
)
@router.get("/order/money/{delivery_id_order}", status_code=201)
async def add_delivery(delivery_id_order):
get_delivery = (session.query(Delivery)
.filter(Delivery.id == delivery_id_order)
.first())
get_money_delivery = (session.query(MoneyDelivery)
.filter(MoneyDelivery.delivery_id == get_delivery.id)
.first())
return JSONResponse(content=jsonable_encoder(get_money_delivery), status_code=200)
@router.post("/add/{delivery_id_order}", status_code=201)
async def calculate_delivery_final(delivery_id_order):
now = datetime.now(timezone.utc).replace(tzinfo=None)
get_delivery = (session.query(Delivery)
.filter(Delivery.id == delivery_id_order)
.first())
get_current_prices = (session.query(Pricing_Oil_Oil)
.order_by(Pricing_Oil_Oil.id.desc())
.first())
get_promo_data = (session
.query(Promo_Promo)
.filter(Promo_Promo.id == get_delivery.promo_id)
.first())
# get total amount for just oil delivered
calc_total_oil = (float(get_delivery.gallons_delivered) * float(get_delivery.customer_price))
calc_total_oil_supplier = (float(get_delivery.gallons_delivered) * float(get_delivery.supplier_price))
# calculate prime
if get_delivery.prime:
prime_price = get_current_prices.price_prime
else:
prime_price = 0
# calculate same day
if get_delivery.same_day:
same_day_price = get_current_prices.price_same_day
else:
same_day_price = 0
# promotion
if get_delivery.promo_id is not None:
get_promo_amount = (float(get_delivery.gallons_delivered)) *(float(get_promo_data.money_off_delivery))
discount = (float(calc_total_oil)) - (float(get_promo_amount))
discount_amount = get_promo_data.money_off_delivery
else:
discount = 0
discount_amount = 0
# calculate fees
total_amount_fees_from_delivery = (float(same_day_price) + float(prime_price))
# total amount
total_amount_delivery = (float(same_day_price) + float(prime_price) +float(calc_total_oil))
# calculate profit
profit_from_oil = float(calc_total_oil) - float(calc_total_oil_supplier)
# add it all together
profit_from_stop = float(profit_from_oil) + float(total_amount_fees_from_delivery) - float(discount)
new_money = MoneyDelivery(
delivery_id = get_delivery.id,
time_added=now,
gallons_delivered=get_delivery.gallons_delivered,
supplier_price=get_delivery.supplier_price,
customer_price=get_delivery.customer_price,
total_amount_oil=calc_total_oil,
total_amount_prime=prime_price,
total_amount_same_day=same_day_price,
total_amount_fee= total_amount_fees_from_delivery,
total_amount=total_amount_delivery,
taxes_paid=None,
total_discount_amount=discount_amount,
total_discount_total=discount,
total_profit_oil=profit_from_oil,
total_profit=profit_from_stop,
auto=False
)
session.add(new_money)
session.commit()
return {"delivery_id": delivery_id_order}
@router.post("/add/auto/{auto_ticket_id}", status_code=201)
async def calculate_delivery_auto_final(auto_ticket_id):
now = datetime.now(timezone.utc).replace(tzinfo=None)
auto_ticket_id = int(auto_ticket_id)
get_delivery = (session.query(Tickets_Auto_Delivery)
.filter(Tickets_Auto_Delivery.id == auto_ticket_id)
.first())
get_current_prices = (session.query(Pricing_Oil_Oil)
.order_by(Pricing_Oil_Oil.id.desc())
.first())
# get total amount for just oil delivered
calc_total_oil = (float(get_delivery.gallons_delivered) * float(get_current_prices.price_for_customer))
calc_total_oil_supplier = (float(get_delivery.gallons_delivered) * float(get_current_prices.price_from_supplier))
discount = 0
# total amount
total_amount_delivery =float(calc_total_oil)
# calculate profit
profit_from_oil = float(calc_total_oil) - float(calc_total_oil_supplier)
# add it all together
profit_from_stop = float(profit_from_oil)
new_money = MoneyDelivery(
delivery_id = get_delivery.id,
time_added=now,
gallons_delivered=get_delivery.gallons_delivered,
supplier_price=get_current_prices.price_from_supplier,
customer_price=get_current_prices.price_for_customer,
total_amount_oil=calc_total_oil,
total_amount_prime=0,
total_amount_same_day=0,
total_amount_fee= 0,
total_amount=total_amount_delivery,
taxes_paid=None,
total_discount_amount=0,
total_discount_total=discount,
total_profit_oil=profit_from_oil,
total_profit=profit_from_stop,
auto=True
)
session.add(new_money)
session.commit()
return {"delivery_id": auto_ticket_id}