added promo
This commit is contained in:
@@ -1,50 +1,79 @@
|
|||||||
from sqlalchemy import Column, Integer,\
|
from sqlalchemy import Column, TEXT, DECIMAL, INTEGER, VARCHAR, DATE
|
||||||
DECIMAL, Text,\
|
|
||||||
VARCHAR, TIMESTAMP, Date
|
|
||||||
import datetime
|
|
||||||
from database import Base
|
from database import Base
|
||||||
|
|
||||||
|
|
||||||
class Delivery(Base):
|
class Delivery(Base):
|
||||||
__tablename__ = "delivery_delivery"
|
__tablename__ = "delivery_delivery"
|
||||||
|
|
||||||
id = Column(Integer,
|
id = Column(INTEGER,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
unique=False)
|
unique=False)
|
||||||
|
|
||||||
customer_id = Column(Integer)
|
customer_id = Column(INTEGER)
|
||||||
customer_name = Column(VARCHAR(1000))
|
customer_name = Column(VARCHAR(1000))
|
||||||
customer_address = Column(VARCHAR(1000))
|
customer_address = Column(VARCHAR(1000))
|
||||||
customer_town = Column(VARCHAR(140))
|
customer_town = Column(VARCHAR(140))
|
||||||
customer_state = Column(VARCHAR(140))
|
customer_state = Column(VARCHAR(140))
|
||||||
customer_zip = Column(Integer)
|
customer_zip = Column(INTEGER)
|
||||||
|
# how many gallons ordered
|
||||||
gallons_ordered = Column(Integer)
|
gallons_ordered = Column(INTEGER)
|
||||||
customer_asked_for_fill = Column(Integer)
|
# if customer asked for a fill
|
||||||
gallons_delivered = Column(DECIMAL(6, 2))
|
customer_asked_for_fill = Column(INTEGER)
|
||||||
customer_filled = Column(Integer)
|
# integer value if delivered, waiting, cancelled etc
|
||||||
|
gallons_delivered =Column(DECIMAL(6, 2))
|
||||||
delivery_status = Column(Integer)
|
# if customer has a full tank
|
||||||
when_ordered = Column(TIMESTAMP(), default=datetime.datetime.utcnow())
|
customer_filled = Column(INTEGER)
|
||||||
when_delivered = Column(TIMESTAMP(), default=None)
|
# integer value if delivered, waiting, cancelled etc
|
||||||
expected_delivery_date = Column(Date(), default=None)
|
# waiting = 0
|
||||||
automatic = Column(Integer)
|
# delivered = 1
|
||||||
oil_id = Column(Integer)
|
# out for delivery = 2
|
||||||
|
# cancelled = 3
|
||||||
|
# partial delivery = 4
|
||||||
|
# issue = 5
|
||||||
|
# finalized = 10
|
||||||
|
delivery_status = Column(INTEGER)
|
||||||
|
|
||||||
|
# when the call to order took place
|
||||||
|
when_ordered = Column(DATE(), default=None)
|
||||||
|
# when the delivery date happened
|
||||||
|
when_delivered = Column(DATE(), default=None)
|
||||||
|
# when the delivery is expected ie what day
|
||||||
|
expected_delivery_date = Column(DATE(), default=None)
|
||||||
|
# automatic delivery
|
||||||
|
automatic = Column(INTEGER)
|
||||||
|
automatic_id = Column(INTEGER)
|
||||||
|
# OIL info and id from table
|
||||||
|
oil_id = Column(INTEGER)
|
||||||
supplier_price = Column(DECIMAL(6, 2))
|
supplier_price = Column(DECIMAL(6, 2))
|
||||||
customer_price = Column(DECIMAL(6, 2))
|
customer_price = Column(DECIMAL(6, 2))
|
||||||
|
# weather
|
||||||
customer_temperature = Column(DECIMAL(6, 2))
|
customer_temperature = Column(DECIMAL(6, 2))
|
||||||
dispatcher_notes = Column(Text())
|
# services
|
||||||
prime = Column(Integer)
|
dispatcher_notes = Column(TEXT())
|
||||||
same_day = Column(Integer)
|
|
||||||
payment_type = Column(Integer)
|
prime = Column(INTEGER)
|
||||||
payment_card_id = Column(Integer)
|
same_day = Column(INTEGER)
|
||||||
|
emergency = Column(INTEGER)
|
||||||
|
|
||||||
|
# cash = 0
|
||||||
|
# credit = 1
|
||||||
|
# credit/cash = 2
|
||||||
|
# check = 3
|
||||||
|
# other = 4
|
||||||
|
payment_type = Column(INTEGER)
|
||||||
|
payment_card_id = Column(INTEGER)
|
||||||
cash_recieved = Column(DECIMAL(6, 2))
|
cash_recieved = Column(DECIMAL(6, 2))
|
||||||
|
|
||||||
driver_employee_id = Column(Integer)
|
driver_employee_id = Column(INTEGER)
|
||||||
driver_first_name = Column(VARCHAR(140))
|
driver_first_name = Column(VARCHAR(140))
|
||||||
driver_last_name = Column(VARCHAR(140))
|
driver_last_name = Column(VARCHAR(140))
|
||||||
|
|
||||||
pre_charge_amount = Column(DECIMAL(6, 2))
|
pre_charge_amount = Column(DECIMAL(6, 2))
|
||||||
total_price = Column(DECIMAL(6, 2))
|
total_price = Column(DECIMAL(6, 2))
|
||||||
final_price = Column(DECIMAL(6, 2))
|
final_price = Column(DECIMAL(6, 2))
|
||||||
|
check_number = Column(VARCHAR(20))
|
||||||
|
|
||||||
|
|
||||||
|
promo_id = Column(INTEGER)
|
||||||
|
promo_money_discount = Column(DECIMAL(6, 2))
|
||||||
|
|||||||
@@ -1,28 +1,30 @@
|
|||||||
from sqlalchemy import Column, Integer,\
|
from sqlalchemy import Column, Integer,\
|
||||||
DECIMAL, Text,\
|
DECIMAL, Text,\
|
||||||
VARCHAR, TIMESTAMP, Date
|
VARCHAR, TIMESTAMP, DATE
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from database import Base
|
from database import Base
|
||||||
|
|
||||||
|
|
||||||
class MoneyDelivery(Base):
|
class MoneyDelivery(Base):
|
||||||
__tablename__ = 'money_delivery'
|
__tablename__ = 'money_delivery'
|
||||||
|
|
||||||
id = Column(Integer,
|
id = Column(Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
unique=False)
|
unique=False)
|
||||||
|
|
||||||
delivery_id = Column(Integer)
|
delivery_id = Column(Integer)
|
||||||
time_added = Column(TIMESTAMP(), default=datetime.utcnow())
|
time_added = Column(DATE())
|
||||||
gallons_delivered = Column(DECIMAL(6, 2))
|
gallons_delivered = Column(DECIMAL(6, 2))
|
||||||
supplier_price = Column(DECIMAL(6, 2))
|
supplier_price = Column(DECIMAL(6, 2))
|
||||||
customer_price = Column(DECIMAL(6, 2))
|
customer_price = Column(DECIMAL(6, 2))
|
||||||
total_amount_oil = Column(DECIMAL(6, 2))
|
total_amount_oil = Column(DECIMAL(6, 2))
|
||||||
total_amount_prime = Column(DECIMAL(6, 2))
|
total_amount_prime = Column(DECIMAL(6, 2))
|
||||||
total_amount_same_day = Column(DECIMAL(6, 2))
|
total_amount_same_day = Column(DECIMAL(6, 2))
|
||||||
total_amount_fee = Column(DECIMAL(6, 2))
|
total_amount_fee = Column(DECIMAL(6, 2))
|
||||||
total_amount = Column(DECIMAL(6, 2))
|
total_amount = Column(DECIMAL(6, 2))
|
||||||
taxes_paid = Column(DECIMAL(6, 2))
|
total_discount_amount = Column(DECIMAL(6, 2))
|
||||||
total_profit = Column(DECIMAL(6, 2))
|
total_discount_total = Column(DECIMAL(6, 2))
|
||||||
total_profit_oil = Column(DECIMAL(6, 2))
|
taxes_paid = Column(DECIMAL(6, 2))
|
||||||
|
total_profit = Column(DECIMAL(6, 2))
|
||||||
|
total_profit_oil = Column(DECIMAL(6, 2))
|
||||||
22
app/models/promo.py
Normal file
22
app/models/promo.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from sqlalchemy import Column, INTEGER, DECIMAL, VARCHAR, DATE, BOOLEAN, TEXT
|
||||||
|
from database import Base
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Promo_Promo(Base):
|
||||||
|
__tablename__ = 'promo_Promo'
|
||||||
|
|
||||||
|
|
||||||
|
id = Column(INTEGER,
|
||||||
|
primary_key=True,
|
||||||
|
autoincrement=True,
|
||||||
|
unique=False)
|
||||||
|
active = Column(BOOLEAN())
|
||||||
|
name_of_promotion = Column(VARCHAR(1000))
|
||||||
|
money_off_delivery = Column(DECIMAL(6, 2))
|
||||||
|
description = Column(VARCHAR(1000))
|
||||||
|
date_created = Column(DATE())
|
||||||
|
|
||||||
|
|
||||||
@@ -2,15 +2,15 @@ from typing import Annotated
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from fastapi import APIRouter, Form, Body
|
from fastapi import APIRouter
|
||||||
from database import session
|
from database import session
|
||||||
|
|
||||||
from app.schema.delivery import DeliverPricing
|
|
||||||
|
|
||||||
|
|
||||||
from app.models.delivery import Delivery
|
from app.models.delivery import Delivery
|
||||||
from app.models.money import MoneyDelivery
|
from app.models.money import MoneyDelivery
|
||||||
from app.models.pricing import Pricing_Oil_Oil, Pricing_Taxes
|
from app.models.pricing import Pricing_Oil_Oil
|
||||||
|
from app.models.promo import Promo_Promo
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/delivery",
|
prefix="/delivery",
|
||||||
@@ -18,6 +18,7 @@ router = APIRouter(
|
|||||||
responses={404: {"description": "Not found"}},
|
responses={404: {"description": "Not found"}},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/order/money/{delivery_id_order}", status_code=201)
|
@router.get("/order/money/{delivery_id_order}", status_code=201)
|
||||||
async def add_delivery(delivery_id_order):
|
async def add_delivery(delivery_id_order):
|
||||||
get_delivery = (session.query(Delivery)
|
get_delivery = (session.query(Delivery)
|
||||||
@@ -26,14 +27,13 @@ async def add_delivery(delivery_id_order):
|
|||||||
get_money_delivery = (session.query(MoneyDelivery)
|
get_money_delivery = (session.query(MoneyDelivery)
|
||||||
.filter(MoneyDelivery.delivery_id == get_delivery.id)
|
.filter(MoneyDelivery.delivery_id == get_delivery.id)
|
||||||
.first())
|
.first())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return JSONResponse(content=jsonable_encoder(get_money_delivery), status_code=200)
|
return JSONResponse(content=jsonable_encoder(get_money_delivery), status_code=200)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/add/{delivery_id_order}", status_code=201)
|
@router.post("/add/{delivery_id_order}", status_code=201)
|
||||||
async def add_delivery(delivery_id_order, deliverydata: DeliverPricing):
|
async def calculate_delivery_final(delivery_id_order):
|
||||||
|
|
||||||
|
|
||||||
now = datetime.now(timezone.utc).replace(tzinfo=None)
|
now = datetime.now(timezone.utc).replace(tzinfo=None)
|
||||||
@@ -45,46 +45,54 @@ async def add_delivery(delivery_id_order, deliverydata: DeliverPricing):
|
|||||||
.order_by(Pricing_Oil_Oil.id.desc())
|
.order_by(Pricing_Oil_Oil.id.desc())
|
||||||
.first())
|
.first())
|
||||||
|
|
||||||
get_current_taxes = (session.query(Pricing_Taxes)
|
get_promo_data = (session
|
||||||
.filter(Pricing_Taxes.state_id == get_delivery.customer_state)
|
.query(Promo_Promo)
|
||||||
.order_by(Pricing_Taxes.id.desc())
|
.filter(Promo_Promo.id == get_delivery.promo_id)
|
||||||
.first())
|
.first())
|
||||||
|
|
||||||
# get total amount for just oil delivered
|
# get total amount for just oil delivered
|
||||||
calc_total_oil = (float(deliverydata.gallons_delivered) * float(get_delivery.customer_price))
|
calc_total_oil = (float(get_delivery.gallons_delivered) * float(get_delivery.customer_price))
|
||||||
calc_total_oil_supplier = (float(deliverydata.gallons_delivered) * float(get_delivery.supplier_price))
|
calc_total_oil_supplier = (float(get_delivery.gallons_delivered) * float(get_delivery.supplier_price))
|
||||||
|
|
||||||
# calculate prime
|
# calculate prime
|
||||||
if deliverydata.prime:
|
if get_delivery.prime:
|
||||||
prime_price = get_current_prices.price_prime
|
prime_price = get_current_prices.price_prime
|
||||||
else:
|
else:
|
||||||
prime_price = 0
|
prime_price = 0
|
||||||
|
|
||||||
# calculate same day
|
# calculate same day
|
||||||
if deliverydata.same_day:
|
if get_delivery.same_day:
|
||||||
same_day_price = get_current_prices.price_same_day
|
same_day_price = get_current_prices.price_same_day
|
||||||
else:
|
else:
|
||||||
same_day_price = 0
|
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))
|
||||||
|
else:
|
||||||
|
discount = 0
|
||||||
|
|
||||||
# calculate fees
|
# calculate fees
|
||||||
total_amount_fees_from_delivery = (float(same_day_price) + float(prime_price))
|
total_amount_fees_from_delivery = (float(same_day_price) + float(prime_price))
|
||||||
|
|
||||||
# total amount
|
# total amount
|
||||||
total_amount_delivery = (float(same_day_price) + float(prime_price) +float(calc_total_oil))
|
total_amount_delivery = (float(same_day_price) + float(prime_price) +float(calc_total_oil))
|
||||||
|
|
||||||
# calculate taxes
|
|
||||||
tax_oil = (float(get_current_taxes.taxes_oil) * float(deliverydata.gallons_delivered))
|
|
||||||
tax_other = (float(get_current_taxes.taxes_oil) * float(total_amount_fees_from_delivery))
|
|
||||||
total_taxes = (float(tax_oil) + float(tax_other))
|
|
||||||
|
|
||||||
# calculate profit
|
# calculate profit
|
||||||
profit_from_oil = float(calc_total_oil) - float(calc_total_oil_supplier)
|
profit_from_oil = float(calc_total_oil) - float(calc_total_oil_supplier)
|
||||||
profit_from_stop = (float(profit_from_oil) + float(total_amount_fees_from_delivery)) - float(total_taxes)
|
|
||||||
|
|
||||||
|
# add it all together
|
||||||
|
profit_from_stop = float(profit_from_oil) + float(total_amount_fees_from_delivery) - float(discount)
|
||||||
|
|
||||||
|
|
||||||
new_money = MoneyDelivery(
|
new_money = MoneyDelivery(
|
||||||
delivery_id = get_delivery.id,
|
delivery_id = get_delivery.id,
|
||||||
time_added=now,
|
time_added=now,
|
||||||
gallons_delivered=deliverydata.gallons_delivered,
|
gallons_delivered=get_delivery.gallons_delivered,
|
||||||
supplier_price=get_delivery.supplier_price,
|
supplier_price=get_delivery.supplier_price,
|
||||||
customer_price=get_delivery.customer_price,
|
customer_price=get_delivery.customer_price,
|
||||||
total_amount_oil=calc_total_oil,
|
total_amount_oil=calc_total_oil,
|
||||||
@@ -92,7 +100,9 @@ async def add_delivery(delivery_id_order, deliverydata: DeliverPricing):
|
|||||||
total_amount_same_day=same_day_price,
|
total_amount_same_day=same_day_price,
|
||||||
total_amount_fee= total_amount_fees_from_delivery,
|
total_amount_fee= total_amount_fees_from_delivery,
|
||||||
total_amount=total_amount_delivery,
|
total_amount=total_amount_delivery,
|
||||||
taxes_paid=total_taxes,
|
taxes_paid=None,
|
||||||
|
total_discount_amount=get_promo_data.money_off_delivery,
|
||||||
|
total_discount_total=discount,
|
||||||
total_profit_oil=profit_from_oil,
|
total_profit_oil=profit_from_oil,
|
||||||
total_profit=profit_from_stop,
|
total_profit=profit_from_stop,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user