first commit

This commit is contained in:
2024-07-15 18:00:33 -04:00
commit 6624cf541b
24 changed files with 371 additions and 0 deletions

17
Dockerfile Normal file
View File

@@ -0,0 +1,17 @@
FROM python:3.12-bullseye
ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=1
RUN mkdir -p /app
COPY requirements.txt /app
WORKDIR /app
RUN pip3 install -r requirements.txt
EXPOSE 8000
COPY . /app

17
Dockerfile.prod Normal file
View File

@@ -0,0 +1,17 @@
FROM python:3.12-bullseye
ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=1
RUN mkdir -p /app
COPY requirements.txt /app
WORKDIR /app
RUN pip3 install -r requirements.txt
EXPOSE 8001
COPY . /app

Binary file not shown.

Binary file not shown.

0
app/__init__.py Normal file
View File

Binary file not shown.

0
app/models/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

50
app/models/delivery.py Normal file
View File

@@ -0,0 +1,50 @@
from sqlalchemy import Column, Integer,\
DECIMAL, Text,\
VARCHAR, TIMESTAMP, Date
import datetime
from database import Base
class Delivery(Base):
__tablename__ = "delivery_delivery"
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
customer_id = Column(Integer)
customer_name = Column(VARCHAR(1000))
customer_address = Column(VARCHAR(1000))
customer_town = Column(VARCHAR(140))
customer_state = Column(VARCHAR(140))
customer_zip = Column(Integer)
gallons_ordered = Column(Integer)
customer_asked_for_fill = Column(Integer)
gallons_delivered = Column(DECIMAL(50, 2))
customer_filled = Column(Integer)
delivery_status = Column(Integer)
when_ordered = Column(TIMESTAMP(), default=datetime.datetime.utcnow())
when_delivered = Column(TIMESTAMP(), default=None)
expected_delivery_date = Column(Date(), default=None)
automatic = Column(Integer)
oil_id = Column(Integer)
supplier_price = Column(DECIMAL(50, 2))
customer_price = Column(DECIMAL(50, 2))
customer_temperature = Column(DECIMAL(50, 2))
dispatcher_notes = Column(Text())
prime = Column(Integer)
same_day = Column(Integer)
payment_type = Column(Integer)
payment_card_id = Column(Integer)
cash_recieved = Column(DECIMAL(50, 2))
driver_employee_id = Column(Integer)
driver_first_name = Column(VARCHAR(140))
driver_last_name = Column(VARCHAR(140))
pre_charge_amount = Column(DECIMAL(50, 2))
total_price = Column(DECIMAL(50, 2))
final_price = Column(DECIMAL(50, 2))

28
app/models/money.py Normal file
View File

@@ -0,0 +1,28 @@
from sqlalchemy import Column, Integer,\
DECIMAL, Text,\
VARCHAR, TIMESTAMP, Date
from datetime import datetime, timezone
from database import Base
class MoneyDelivery(Base):
__tablename__ = 'money_delivery'
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
delivery_id = Column(Integer)
time_added = Column(TIMESTAMP(), default=datetime.utcnow())
gallons_delivered = Column(DECIMAL(50, 2))
supplier_price = Column(DECIMAL(50, 2))
customer_price = Column(DECIMAL(50, 2))
total_amount_oil = Column(DECIMAL(50, 2))
total_amount_prime = Column(DECIMAL(50, 2))
total_amount_same_day = Column(DECIMAL(50, 2))
total_amount_fee = Column(DECIMAL(50, 2))
total_amount = Column(DECIMAL(50, 2))
taxes_paid = Column(DECIMAL(50, 2))
total_profit = Column(DECIMAL(50, 2))
total_profit_oil = Column(DECIMAL(50, 2))

34
app/models/pricing.py Normal file
View File

@@ -0,0 +1,34 @@
from sqlalchemy import (Column, Integer, DECIMAL, TIMESTAMP)
from datetime import datetime, timezone
from database import Base
class Pricing_Oil_Oil(Base):
__tablename__ = 'pricing_oil_oil'
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
price_from_supplier = Column(DECIMAL(50, 2))
price_for_customer = Column(DECIMAL(50, 2))
price_for_employee = Column(DECIMAL(50, 2))
price_same_day = Column(DECIMAL(50, 2))
price_prime = Column(DECIMAL(50, 2))
date = Column(TIMESTAMP(), default=datetime.utcnow())
class Pricing_Taxes(Base):
__tablename__ = 'taxes_pricing'
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
state_id = Column(Integer)
taxes_oil = Column(DECIMAL(50, 2))
taxes_other = Column(DECIMAL(50, 2))

0
app/routers/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

106
app/routers/delivery.py Normal file
View File

@@ -0,0 +1,106 @@
from typing import Annotated
from datetime import datetime, timezone
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from fastapi import APIRouter, Form, Body
from database import session
from app.schema.delivery import DeliverPricing
from app.models.delivery import Delivery
from app.models.money import MoneyDelivery
from app.models.pricing import Pricing_Oil_Oil, Pricing_Taxes
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())
print(get_money_delivery)
return JSONResponse(content=jsonable_encoder(get_money_delivery), status_code=200)
@router.post("/add/{delivery_id_order}", status_code=201)
async def add_delivery(delivery_id_order, deliverydata: DeliverPricing):
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_current_taxes = (session.query(Pricing_Taxes)
.filter(Pricing_Taxes.state_id == get_delivery.customer_state)
.order_by(Pricing_Taxes.id.desc())
.first())
# get total amount for just oil delivered
calc_total_oil = (float(deliverydata.gallons_delivered) * float(get_delivery.customer_price))
calc_total_oil_supplier = (float(deliverydata.gallons_delivered) * float(get_delivery.supplier_price))
# calculate prime
if deliverydata.prime:
prime_price = get_current_prices.price_prime
else:
prime_price = 0
# calculate same day
if deliverydata.same_day:
same_day_price = get_current_prices.price_same_day
else:
same_day_price = 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 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
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)
new_money = MoneyDelivery(
delivery_id = get_delivery.id,
time_added=now,
gallons_delivered=deliverydata.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=total_taxes,
total_profit_oil=profit_from_oil,
total_profit=profit_from_stop,
)
session.add(new_money)
session.commit()
return {"delivery_id": delivery_id_order}

Binary file not shown.

46
app/schema/delivery.py Normal file
View File

@@ -0,0 +1,46 @@
from pydantic import BaseModel, Field
from datetime import datetime, timezone
def datetime_now() -> datetime:
return datetime.now(timezone.utc)
class DeliverPricing(BaseModel):
prime: bool
same_day: bool
gallons_delivered: float
class SchemaDelivery(BaseModel):
customer_id: int
customer_name: str
customer_address: str
customer_town: str
customer_state: int
customer_zip: int
gallons_ordered: float
customer_asked_for_fill: int
gallons_delivered: float
customer_filled: int
delivery_status: int
when_ordered: datetime = Field(default_factory=datetime_now)
when_delivered: datetime = Field(default_factory=datetime_now)
expected_delivery_date: datetime = Field(default_factory=datetime_now)
automatic: int
oil_id: int
supplier_price: float
customer_price: float
customer_temperature: float
dispatcher_notes: str
prime: int
same_day: int
payment_type: int
payment_card_id: int
cash_recieved: float
driver_employee_id: int
driver_first_name: str
driver_last_name: str
pre_charge_amount: float
total_price: float
final_price: float

21
app/schema/money.py Normal file
View File

@@ -0,0 +1,21 @@
from pydantic import BaseModel, Field
from datetime import datetime, timezone
def datetime_now() -> datetime:
return datetime.now(timezone.utc)
class SchemaMoney(BaseModel):
delivery_id: int
time_added: datetime
gallons_delivered: float
supplier_price: float
customer_price: float
total_amount_oil: float
total_amount_prime: float
total_amount_same_day: float
total_amount_fee: float
total_amount: float
taxes_paid: float
total_profit: float

21
database.py Normal file
View File

@@ -0,0 +1,21 @@
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base
url = URL.create(
drivername="postgresql",
username="postgres",
password="password",
host="192.168.1.204",
database="eamco",
port=5432
)
engine = create_engine(url)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = Session()
Base = declarative_base()
Base.metadata.create_all(engine)

27
main.py Normal file
View File

@@ -0,0 +1,27 @@
from fastapi import FastAPI
from app.routers import delivery
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.include_router(delivery.router)
origins = [
"http://localhost:9000",
"https://localhost:5173",
"http://localhost",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"Status": "Money Service is online"}

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
fastapi
uvicorn[standard]
psycopg2-binary
sqlalchemy