autos working!

This commit is contained in:
2024-11-20 18:00:33 -05:00
parent 7c9b7f8998
commit 8569430468
8 changed files with 273 additions and 276 deletions

View File

@@ -1,12 +1,22 @@
from sqlalchemy import (Column, Integer, from sqlalchemy import (Column, Integer,
DECIMAL, TEXT, DECIMAL, TEXT,
VARCHAR, TIMESTAMP, DATE, INTEGER) VARCHAR, DATE, INTEGER)
from datetime import datetime from datetime import datetime
from database import Base from database import Base
class Auto_Update(Base):
__tablename__ = 'auto_update'
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
last_updated = Column(DATE())
class Auto_Temp(Base): class Auto_Temp(Base):
__tablename__ = 'auto_temp' __tablename__ = 'auto_temp'
@@ -16,7 +26,7 @@ class Auto_Temp(Base):
autoincrement=True, autoincrement=True,
unique=False) unique=False)
todays_date = Column(TIMESTAMP(), default=datetime.utcnow()) todays_date = Column(DATE)
temp = Column(DECIMAL(5, 2)) temp = Column(DECIMAL(5, 2))
temp_max = Column(DECIMAL(5, 2)) temp_max = Column(DECIMAL(5, 2))
temp_min = Column(DECIMAL(5, 2)) temp_min = Column(DECIMAL(5, 2))
@@ -40,11 +50,37 @@ class Auto_Delivery(Base):
customer_address = Column(VARCHAR(1000)) customer_address = Column(VARCHAR(1000))
customer_zip = Column(VARCHAR(25)) customer_zip = Column(VARCHAR(25))
customer_full_name = Column(VARCHAR(250)) customer_full_name = Column(VARCHAR(250))
last_fill = Column(TIMESTAMP()) last_fill = Column(DATE())
last_updated = Column(TIMESTAMP()) days_since_last_fill = Column(INTEGER())
estimated_gallons_left = Column(INTEGER()) last_updated = Column(DATE())
estimated_gallons_left_prev_day = Column(INTEGER()) estimated_gallons_left = Column(DECIMAL(6, 2))
estimated_gallons_left_prev_day = Column(DECIMAL(6, 2))
tank_height = Column(VARCHAR(25)) tank_height = Column(VARCHAR(25))
tank_size = Column(VARCHAR(25)) tank_size = Column(VARCHAR(25))
house_factor = Column(DECIMAL(5, 2)) house_factor = Column(DECIMAL(5, 2))
auto_status = Column(INTEGER()) auto_status = Column(INTEGER())
class Tickets_Auto_Delivery(Base):
__tablename__ = 'auto_tickets'
id = Column(Integer,
primary_key=True,
autoincrement=True,
unique=False)
customer_id = Column(INTEGER())
account_number = Column(VARCHAR(25))
customer_town = Column(VARCHAR(140))
customer_state = Column(Integer)
customer_address = Column(VARCHAR(1000))
customer_zip = Column(VARCHAR(25))
customer_full_name = Column(VARCHAR(250))
customer_zip = Column(VARCHAR(25))
oil_prices_id = Column(INTEGER())
gallons_delivered = Column(DECIMAL(6, 2))
price_per_gallon = Column(DECIMAL(6, 2))
total_amount_customer = Column(DECIMAL(6, 2))

View File

@@ -1,8 +1,8 @@
from sqlalchemy import (Column, Integer, from sqlalchemy import (Column, Integer,
DECIMAL, Text, BOOLEAN, DECIMAL, BOOLEAN, DATE,
VARCHAR, TIMESTAMP, VARCHAR,
Date, INTEGER) INTEGER)
from datetime import datetime, timezone from datetime import datetime, timezone
from database import Base from database import Base
@@ -16,7 +16,7 @@ class Card_Card(Base):
primary_key=True, primary_key=True,
autoincrement=True, autoincrement=True,
unique=False) unique=False)
date_added = Column(TIMESTAMP(), default=datetime.utcnow()) date_added = Column(DATE())
user_id = Column(INTEGER()) user_id = Column(INTEGER())
card_number = Column(VARCHAR(50)) card_number = Column(VARCHAR(50))
last_four_digits = Column(INTEGER()) last_four_digits = Column(INTEGER())

View File

@@ -1,7 +1,7 @@
from sqlalchemy import (Column, Integer, from sqlalchemy import (Column, Integer,
DECIMAL, Text, DECIMAL,
VARCHAR, TIMESTAMP, VARCHAR,
Date, INTEGER) DATE, INTEGER)
from datetime import datetime, timezone from datetime import datetime, timezone
from database import Base from database import Base
@@ -20,7 +20,7 @@ class Customer_Customer(Base):
customer_town = Column(VARCHAR(140)) customer_town = Column(VARCHAR(140))
customer_state = Column(INTEGER) customer_state = Column(INTEGER)
customer_zip = Column(VARCHAR(25)) customer_zip = Column(VARCHAR(25))
customer_first_call = Column(TIMESTAMP()) customer_first_call = Column(DATE())
customer_email = Column(VARCHAR(500)) customer_email = Column(VARCHAR(500))
customer_automatic = Column(INTEGER) customer_automatic = Column(INTEGER)
customer_phone_number = Column(VARCHAR(25)) customer_phone_number = Column(VARCHAR(25))

View File

@@ -26,8 +26,8 @@ class Delivery(Base):
customer_filled = Column(INTEGER) customer_filled = Column(INTEGER)
delivery_status = Column(INTEGER) delivery_status = Column(INTEGER)
when_ordered = Column(TIMESTAMP(), default=datetime.datetime.utcnow()) when_ordered = Column(DATE(), )
when_delivered = Column(TIMESTAMP(), default=None) when_delivered = Column(DATE())
expected_delivery_date = Column(DATE(), default=None) expected_delivery_date = Column(DATE(), default=None)
automatic = Column(INTEGER) automatic = Column(INTEGER)
automatic_id = Column(INTEGER) automatic_id = Column(INTEGER)

View File

@@ -1,11 +1,13 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request
from datetime import datetime from datetime import date
from database import session from database import session
from pyowm import OWM from pyowm import OWM
from decimal import Decimal from decimal import Decimal
from app.models.auto import Auto_Delivery from app.models.auto import Auto_Delivery, Tickets_Auto_Delivery
from app.models.delivery import Delivery from app.models.pricing import Pricing_Oil_Oil
from app.script.update_auto import calc_home_factor from app.script.update_auto import calc_home_factor
@@ -16,26 +18,17 @@ router = APIRouter(
) )
@router.put("/delivery") @router.put("/auto/update/{autoid}")
async def add_delivery(request: Request): async def update_auto(autoid: int, request: Request):
now = datetime.utcnow
request_body = await request.json() request_body = await request.json()
gallons_delivered = request_body['gallons'] gallons_delivered = request_body['gallons_delivered']
gallons_delivered = Decimal(gallons_delivered) gallons_delivered = Decimal(gallons_delivered)
delivery_id = request_body['delivery_id']
get_delivery = (session
.query(Delivery)
.filter(Delivery.id == delivery_id)
.first())
get_auto_delivery = (session get_auto_delivery = (session
.query(Auto_Delivery) .query(Auto_Delivery)
.filter(Auto_Delivery.customer_id == get_delivery.customer_id) .filter(Auto_Delivery.id == autoid)
.first()) .first())
gallons_put_in_home = Decimal(gallons_delivered) gallons_put_in_home = Decimal(gallons_delivered)
@@ -46,15 +39,67 @@ async def add_delivery(request: Request):
new_home_factor = calc_home_factor(gallons_put_in_home = gallons_put_in_home, new_home_factor = calc_home_factor(gallons_put_in_home = gallons_put_in_home,
current_house_factor=customer_home_factor) current_house_factor=customer_home_factor)
get_auto_delivery.house_factor = new_home_factor get_auto_delivery.house_factor = new_home_factor
get_auto_delivery.tank_height = 'Full' get_auto_delivery.tank_height = 'Full'
get_auto_delivery.last_fill = now get_auto_delivery.last_fill = date.today()
get_auto_delivery.estimated_gallons_left = 240 get_auto_delivery.estimated_gallons_left = 240
get_auto_delivery.estimated_gallons_left_prev_day = 240 get_auto_delivery.estimated_gallons_left_prev_day = 240
get_auto_delivery.auto_status = 0 get_auto_delivery.auto_status = 1
get_auto_delivery.days_since_last_fill = 0
session.add(get_auto_delivery) session.add(get_auto_delivery)
session.commit() session.commit()
return ({"ok": True}), 200 return ({"ok": True}), 200
@router.post("/auto/create/{autoid}")
async def create_auto_ticket(autoid: int, request: Request):
request_body = await request.json()
gallons_delivered = request_body['gallons_delivered']
gallons_delivered = Decimal(gallons_delivered)
get_auto_delivery = (session
.query(Auto_Delivery)
.filter(Auto_Delivery.id == autoid)
.first())
get_todays_price = (session.query(Pricing_Oil_Oil)
.order_by(Pricing_Oil_Oil.id.desc())
.first())
gallons_put_in_home = Decimal(gallons_delivered)
todays_price = Decimal(get_todays_price.price_for_customer)
total_amount = gallons_put_in_home * todays_price
create_new_ticket = Tickets_Auto_Delivery(
customer_id = get_auto_delivery.customer_id,
account_number = get_auto_delivery.account_number,
customer_town = get_auto_delivery.customer_town,
customer_state = get_auto_delivery.customer_state,
customer_address = get_auto_delivery.customer_address,
customer_zip =get_auto_delivery.customer_zip,
customer_full_name = get_auto_delivery.customer_full_name,
oil_prices_id = get_todays_price.id,
gallons_delivered = gallons_delivered,
price_per_gallon = get_todays_price.price_for_customer,
total_amount_customer = total_amount,
)
session.add(create_new_ticket)
session.commit()
return ({
"ok": True,
"auto_ticket_id":create_new_ticket.id
}), 200

View File

@@ -1,15 +1,10 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Depends
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from datetime import datetime
from database import session from database import session
from pyowm import OWM
from decimal import Decimal from app.models.auto import Auto_Delivery, Tickets_Auto_Delivery
from app.models.auto import Auto_Delivery
from app.models.delivery import Delivery from app.models.delivery import Delivery
from app.models.pricing import Pricing_Oil_Oil
from app.models.cards import Card_Card
from app.models.employee import Employee_Employee
@@ -19,6 +14,22 @@ router = APIRouter(
responses={404: {"description": "Not found"}}, responses={404: {"description": "Not found"}},
) )
@router.get("/all/customers", status_code=200)
def get_delivery_customers():
automatics = (
session.query(Auto_Delivery)
.filter(Auto_Delivery.auto_status == 1)
.order_by(Auto_Delivery.estimated_gallons_left.desc())
.all()
)
return JSONResponse(content=jsonable_encoder(automatics), status_code=200)
@router.get("/driver/{driver_employee_id}", status_code=200) @router.get("/driver/{driver_employee_id}", status_code=200)
def get_delivery_for_specific_driver(driver_employee_id: int): def get_delivery_for_specific_driver(driver_employee_id: int):
automatics = ( automatics = (
@@ -32,13 +43,12 @@ def get_delivery_for_specific_driver(driver_employee_id: int):
return JSONResponse(content=jsonable_encoder(automatics), status_code=200) return JSONResponse(content=jsonable_encoder(automatics), status_code=200)
@router.get("/{auto_id}", status_code=200)
@router.get("/{delivery_id_order}", status_code=200) def get_auto(auto_id):
def get_delivery(delivery_id_order):
get_delivery = ( get_delivery = (
session.query(Auto_Delivery) session.query(Auto_Delivery)
.filter(Auto_Delivery.id == delivery_id_order) .filter(Auto_Delivery.id == auto_id)
.first() .first()
) )
@@ -46,128 +56,15 @@ def get_delivery(delivery_id_order):
@router.get("/all/customers", status_code=200) @router.get("/delivery/{delivery_id_order}", status_code=200)
def get_delivery_customers(): def get_delivery(delivery_id_order):
# auto = ( get_delivery = (
# session.query(Auto_Delivery) session.query(Tickets_Auto_Delivery)
# .order_by(Auto_Delivery.estimated_gallons_left.desc()) .filter(Tickets_Auto_Delivery.id == delivery_id_order)
# .first()
# )
# auto.id = auto.id
# session.add(auto)
# session.flush()
automatics = (
session.query(Auto_Delivery)
.order_by(Auto_Delivery.estimated_gallons_left.desc())
.all()
)
return JSONResponse(content=jsonable_encoder(automatics), status_code=200)
@router.post("/create")
async def create_delivery_from_automatic(request: Request):
# client_host = await request.body()
# body_text = client_host.decode("utf-8")
# print(body_text)
list_added_tickets = []
now = datetime.utcnow()
request_body = await request.json()
for f in request_body['values']:
list_added_tickets.append(f)
driver_id = request_body['driver_employee_id']
get_today_price = session\
.query(Pricing_Oil_Oil)\
.order_by(Pricing_Oil_Oil.id.desc())\
.first() .first()
precharge_amount = (250 * get_today_price.price_for_customer)
get_driver = (session.query(Employee_Employee)
.filter(Employee_Employee.id == driver_id)
.first())
count = 0
for f in list_added_tickets:
count =+ 1
get_auto_delivery = (session.query(Auto_Delivery)
.filter(Auto_Delivery.id == f)
.first())
get_auto_delivery.auto_status = 1
session.add(get_auto_delivery)
get_main_credit_card = (session.query(Card_Card)
.filter(Card_Card.user_id == get_auto_delivery.customer_id)
.filter(Card_Card.main_card == True)
.first())
see_if_delivery_exists = (session.query(Delivery)
.filter(Delivery.driver_employee_id == get_driver.id)
.filter(Delivery.automatic_id == get_auto_delivery.id)
.filter(Delivery.delivery_status == 0)
.first())
if see_if_delivery_exists is None:
new_delivery = Delivery(
customer_id=get_auto_delivery.customer_id,
customer_address=get_auto_delivery.customer_address,
customer_name=get_auto_delivery.customer_full_name,
customer_town=get_auto_delivery.customer_town,
customer_state=get_auto_delivery.customer_state,
customer_zip=get_auto_delivery.customer_zip,
gallons_ordered=250,
customer_asked_for_fill=1,
gallons_delivered=0,
customer_filled=0,
delivery_status=0,
when_ordered=now,
when_delivered=None,
expected_delivery_date=None,
automatic=1,
automatic_id=get_auto_delivery.id,
oil_id=get_today_price.id,
supplier_price=get_today_price.price_from_supplier,
customer_price=get_today_price.price_for_customer,
customer_temperature=None,
dispatcher_notes=None,
prime=0,
same_day=0,
payment_type=1,
payment_card_id=get_main_credit_card.id,
pre_charge_amount=precharge_amount,
total_price=precharge_amount,
final_price=0,
driver_last_name=get_driver.employee_last_name,
driver_first_name=get_driver.employee_first_name,
driver_employee_id=get_driver.id,
) )
session.add(new_delivery) return JSONResponse(content=jsonable_encoder(get_delivery), status_code=200)
session.commit()
return ({
"ok": True,
'count': count,
})

View File

@@ -1,9 +1,9 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request
from datetime import datetime from datetime import datetime, date
from database import session from database import session
from pyowm import OWM from pyowm import OWM
from app.models.auto import Auto_Delivery, Auto_Temp from app.models.auto import Auto_Delivery, Auto_Temp, Auto_Update
from app.models.delivery import Delivery from app.models.delivery import Delivery
@@ -15,50 +15,91 @@ router = APIRouter(
responses={404: {"description": "Not found"}}, responses={404: {"description": "Not found"}},
) )
def Average(lst):
return sum(lst) / len(lst)
@router.get("/update", status_code=200) @router.get("/update", status_code=200)
def update_auto_customers(): def update_auto_customers():
try:
today_temp = session\ see_if_autos_updated = (session.query(Auto_Update)
.query(Auto_Temp)\ .filter(Auto_Update.last_updated == date.today())
.order_by(Auto_Temp.id.desc())\
.first() .first()
)
except:
session.rollback()
see_if_autos_updated = None
if see_if_autos_updated is not None:
return ({"ok": True}), 200
else:
create_new_update = Auto_Update(
last_updated = date.today()
)
today_temp = (session
.query(Auto_Temp)
.order_by(Auto_Temp.id.desc())
.first())
# get all automatic customers # get all automatic customers
auto_customers = session\ auto_customers = (session
.query(Auto_Delivery)\ .query(Auto_Delivery)
.order_by(Auto_Delivery.last_updated.desc())\ .order_by(Auto_Delivery.last_updated.desc())
.limit(25) .limit(25))
for f in auto_customers: for f in auto_customers:
# figure out days since last fill
d1 = date.today()
d0 = f.last_fill
delta = d1 - d0
f.days_since_last_fill = delta.days
# figure out how much temperature effects oil # figure out how much temperature effects oil
if today_temp >= 60: today_temptemp_avg = int(today_temp.temp_avg)
if today_temptemp_avg >= 65.01:
use_day = 1 use_day = 1
elif 40 >= today_temp >= 59: elif 50.01 <= today_temptemp_avg <= 65:
use_day = 2
elif 35.01 <= today_temptemp_avg <= 50:
use_day = 3 use_day = 3
elif 30 >= today_temp >= 39: elif 30.01 <= today_temptemp_avg <= 35:
use_day = 4
elif 25.01 <= today_temptemp_avg <= 30:
use_day = 5 use_day = 5
elif 0 >= today_temp >= 29: elif 20.01 <= today_temptemp_avg <= 25:
use_day = 7 use_day = 7
elif -20 >= today_temp >= -1: elif 15.01 <= today_temptemp_avg <= 20:
use_day = 8
elif 10.01 <= today_temptemp_avg <= 15:
use_day = 9
elif 5.01 <= today_temptemp_avg <= 10:
use_day = 10 use_day = 10
elif 0.01 <= today_temptemp_avg <= 5:
use_day =12
elif -20 <= today_temptemp_avg <= -1:
use_day = 15
else: else:
use_day = 0 use_day = 0
# times temp factory by house factor # times temp factory by house factor
gallon_use_today = f.house_factor * use_day gallon_use_today = f.house_factor * use_day
# get previous day gallons left
f.estimated_gallons_left_prev_day = f.estimated_gallons_left
# get estimated gallons left # get estimated gallons left
get_gallons_left = f.estimated_gallons_left_prev_day - gallon_use_today get_gallons_left = f.estimated_gallons_left_prev_day - gallon_use_today
# get previous day gallons left
f.estimated_gallons_left_prev_day = f.estimated_gallons_left + gallon_use_today
f.estimated_gallons_left = get_gallons_left f.estimated_gallons_left = get_gallons_left
session.add(create_new_update)
session.add(f) session.add(f)
session.commit() session.commit()
@@ -68,7 +109,15 @@ def update_auto_customers():
@router.get("/temp", status_code=200) @router.get("/temp", status_code=200)
def update_temp(): def update_temp():
try:
see_if_temp_exists = (session.query(Auto_Temp)
.filter(Auto_Temp.todays_date == date.today())
.first())
except:
see_if_temp_exists = None
if see_if_temp_exists is not None:
return ({"ok": True}), 200
else:
temps = [] temps = []
owm = OWM('21648d8c8d1a4ae495ace0b7810b4d36') owm = OWM('21648d8c8d1a4ae495ace0b7810b4d36')
@@ -98,7 +147,8 @@ def update_temp():
temp_max = temp_dict_fahrenheit['temp_max'], temp_max = temp_dict_fahrenheit['temp_max'],
temp_min = temp_dict_fahrenheit['temp_min'], temp_min = temp_dict_fahrenheit['temp_min'],
temp_avg = rounded_temp, temp_avg = rounded_temp,
degree_day = dday degree_day = dday,
todays_date = date.today()
) )
session.add(add_new_temp) session.add(add_new_temp)
@@ -107,7 +157,3 @@ def update_temp():
return ({"ok": True}), 200 return ({"ok": True}), 200
def Average(lst):
return sum(lst) / len(lst)

View File

@@ -1,40 +1,13 @@
from decimal import Decimal from decimal import Decimal
from datetime import datetime
from database import session from database import session
from app.models.auto import Auto_Delivery
def update_automatic(get_delivery, gallons_delivered ):
now = datetime.utcnow()
get_auto_delivery = (session
.query(Auto_Delivery)
.filter(Auto_Delivery.customer_id == get_delivery.customer_id)
.first())
gallons_put_in_home = Decimal(gallons_delivered)
customer_home_factor = get_auto_delivery.house_factor
new_home_factor = calc_home_factor(gallons_put_in_home = gallons_put_in_home,
current_house_factor=customer_home_factor)
get_auto_delivery.house_factor = new_home_factor
get_auto_delivery.tank_height = 'Full'
get_auto_delivery.last_fill = now
get_auto_delivery.estimated_gallons_left = 240
get_auto_delivery.estimated_gallons_left_prev_day = 240
get_auto_delivery.auto_status = 0
session.add(get_auto_delivery)
def calc_home_factor(gallons_put_in_home, current_house_factor): def calc_home_factor(gallons_put_in_home, current_house_factor):
if 200.01 >= gallons_put_in_home >= 500: if 200.01 <= gallons_put_in_home <= 500:
customer_home_factor = Decimal(current_house_factor) + Decimal(1) customer_home_factor = Decimal(current_house_factor) + Decimal(1)
elif 170.01 <= gallons_put_in_home <= 200: elif 170.01 <= gallons_put_in_home <= 200:
customer_home_factor = Decimal(current_house_factor) + Decimal(1.5) customer_home_factor = Decimal(current_house_factor) + Decimal(1.5)