Major update spanning pricing, market data, and analytics: - Pricing: Replace single-price service fees with 5-tier pricing for same-day, prime, and emergency deliveries across create/edit/finalize - Market: Add Ticker_Price and CompanyPrice models with endpoints for live commodity prices (HO, CL, RB) and competitor price tracking - Stats: Add daily/weekly/monthly gallons endpoints with multi-year comparison and YoY totals for the stats dashboard - Delivery: Add map and history endpoints, fix finalize null-driver crash - Schema: Change fill_location from INTEGER to VARCHAR(250), add pre_load normalization for customer updates, fix admin auth check Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
174 lines
6.1 KiB
Python
Executable File
174 lines
6.1 KiB
Python
Executable File
import logging
|
|
from flask import request
|
|
from datetime import datetime
|
|
from decimal import Decimal
|
|
from app.delivery_data import delivery_data
|
|
from app import db
|
|
from app.common.responses import success_response, error_response
|
|
from app.classes.customer import Customer_Customer, Customer_Description
|
|
from app.classes.delivery import Delivery_Delivery
|
|
from app.classes.employee import Employee_Employee
|
|
from app.classes.cards import Card_Card
|
|
from app.classes.stats_employee import Stats_Employee_Oil
|
|
from app.classes.auto import Auto_Delivery
|
|
from app.classes.stats_customer import Stats_Customer
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@delivery_data.route("/finalize/<int:delivery_id>", methods=["PUT"])
|
|
def office_finalize_delivery(delivery_id):
|
|
"""
|
|
This will make a delivery finalized from the driver
|
|
"""
|
|
|
|
"""
|
|
Finalizes a delivery from office
|
|
"""
|
|
logger.info(f"PUT /deliverydata/finalize/{delivery_id} - Finalizing delivery from office")
|
|
now = datetime.utcnow()
|
|
get_delivery = db.session \
|
|
.query(Delivery_Delivery) \
|
|
.filter(Delivery_Delivery.id == delivery_id) \
|
|
.filter(Delivery_Delivery.id == delivery_id) \
|
|
.first()
|
|
|
|
if get_delivery is None:
|
|
return error_response("Delivery not found", 404)
|
|
|
|
get_customer = db.session \
|
|
.query(Customer_Customer) \
|
|
.filter(Customer_Customer.id == get_delivery.customer_id) \
|
|
.first()
|
|
|
|
get_customer_description = db.session \
|
|
.query(Customer_Description) \
|
|
.filter(Customer_Description.customer_id == get_delivery.customer_id) \
|
|
.first()
|
|
if get_customer_description is None:
|
|
new_customer_desc = Customer_Description(
|
|
customer_id = get_customer.id,
|
|
account_number =get_customer.account_number,
|
|
company_id = get_customer.company_id,
|
|
fill_location = None,
|
|
description = None,
|
|
)
|
|
db.session.add(new_customer_desc)
|
|
db.session.flush()
|
|
get_customer_description = db.session \
|
|
.query(Customer_Description) \
|
|
.filter(Customer_Description.customer_id == get_delivery.customer_id) \
|
|
.first()
|
|
|
|
get_driver = (db.session
|
|
.query(Employee_Employee)
|
|
.filter(Employee_Employee.id == get_delivery.driver_employee_id)
|
|
.first())
|
|
|
|
if get_driver is None:
|
|
logger.warning(f"Driver with ID {get_delivery.driver_employee_id} not found for delivery {delivery_id}. Proceeding without updating driver stats.")
|
|
|
|
get_stats_employee = None
|
|
if get_driver:
|
|
get_stats_employee = (db.session
|
|
.query(Stats_Employee_Oil)
|
|
.filter(Stats_Employee_Oil.employee_id == get_delivery.driver_employee_id)
|
|
.first())
|
|
|
|
get_stats_customer = (db.session
|
|
.query(Stats_Customer)
|
|
.filter(Stats_Customer.customer_id == get_customer.id)
|
|
.first())
|
|
if get_stats_customer is None:
|
|
create_stats_customer = Stats_Customer(
|
|
customer_id = get_customer.id,
|
|
total_calls = 1,
|
|
service_calls_total = 0,
|
|
service_calls_total_spent = 0,
|
|
service_calls_total_profit = 0,
|
|
oil_deliveries = 1,
|
|
oil_total_gallons = 0,
|
|
oil_total_spent = 0,
|
|
oil_total_profit = 0,
|
|
|
|
)
|
|
db.session.add(create_stats_customer)
|
|
db.session.flush()
|
|
get_stats_customer = (db.session
|
|
.query(Stats_Customer)
|
|
.filter(Stats_Customer.customer_id == get_customer.id)
|
|
.first())
|
|
|
|
if get_driver and get_stats_employee is None:
|
|
create_stats = Stats_Employee_Oil(
|
|
employee_id = get_delivery.driver_employee_id,
|
|
total_deliveries = 0,
|
|
total_gallons_delivered = 0,
|
|
total_primes = 0,
|
|
oil_total_profit_delivered = 0,
|
|
)
|
|
db.session.add(create_stats)
|
|
db.session.flush()
|
|
|
|
get_stats_employee = (db.session
|
|
.query(Stats_Employee_Oil)
|
|
.filter(Stats_Employee_Oil.employee_id == get_delivery.driver_employee_id)
|
|
.first())
|
|
|
|
if request.json["cash_recieved"]:
|
|
cash_amount = request.json["cash_recieved"]
|
|
else:
|
|
cash_amount = None
|
|
|
|
|
|
gallons_delivered = request.json["gallons_delivered"]
|
|
check_number = request.json["check_number"]
|
|
fill_location = request.json.get("fill_location")
|
|
|
|
|
|
# update driver if found
|
|
if get_driver:
|
|
get_delivery.driver_last_name = get_driver.employee_last_name
|
|
get_delivery.driver_first_name = get_driver.employee_first_name
|
|
get_delivery.driver_employee_id = get_driver.id
|
|
|
|
# update delivery
|
|
get_delivery.when_delivered = now
|
|
get_delivery.gallons_delivered = gallons_delivered
|
|
get_delivery.cash_recieved = cash_amount
|
|
get_delivery.check_number = check_number
|
|
get_delivery.delivery_status = 10
|
|
|
|
# update stats employee if found
|
|
if get_stats_employee:
|
|
current_deliveres = get_stats_employee.total_deliveries + 1
|
|
get_stats_employee.total_deliveries = current_deliveres
|
|
|
|
current_gallons_delivered = Decimal(get_stats_employee.total_gallons_delivered) + Decimal(gallons_delivered)
|
|
get_stats_employee.total_gallons_delivered = current_gallons_delivered
|
|
|
|
# update stats customer
|
|
current_deliveres = int(get_stats_customer.oil_deliveries) + 1
|
|
get_stats_customer.oil_deliveries = current_deliveres
|
|
|
|
new_gallons = Decimal(get_stats_customer.oil_total_gallons) + Decimal(gallons_delivered)
|
|
get_stats_customer.oil_total_gallons = new_gallons
|
|
|
|
|
|
# update fill location
|
|
get_customer_description.fill_location = fill_location
|
|
|
|
db.session.add(get_customer_description)
|
|
db.session.add(get_stats_customer)
|
|
if get_stats_employee:
|
|
db.session.add(get_stats_employee)
|
|
db.session.add(get_delivery)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
return success_response({
|
|
'delivery': {
|
|
'id': get_delivery.id,
|
|
},
|
|
})
|