Added totals to delivery
This commit is contained in:
@@ -2,6 +2,7 @@ from flask import jsonify
|
||||
from datetime import date, timedelta
|
||||
from app.delivery_status import deliverystatus
|
||||
from app import db
|
||||
from sqlalchemy import func, case
|
||||
|
||||
from app.classes.delivery import (Delivery_Delivery,
|
||||
Delivery_Delivery_schema,
|
||||
@@ -65,3 +66,87 @@ def get_sidebar_counts():
|
||||
except Exception as e:
|
||||
# Basic error handling
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@deliverystatus.route("/tomorrow-totals", methods=["GET"])
|
||||
def get_tomorrow_totals():
|
||||
"""
|
||||
Get total gallons by town for tomorrow's deliveries, including grand total.
|
||||
"""
|
||||
try:
|
||||
deliveries = db.session.query(
|
||||
Delivery_Delivery.customer_town,
|
||||
func.sum(
|
||||
case(
|
||||
(Delivery_Delivery.customer_asked_for_fill == 1, 250),
|
||||
else_=Delivery_Delivery.gallons_ordered
|
||||
)
|
||||
).label('total_gallons')
|
||||
).filter(Delivery_Delivery.delivery_status == 3).group_by(Delivery_Delivery.customer_town).order_by(Delivery_Delivery.customer_town).all()
|
||||
|
||||
totals = [{'town': d.customer_town, 'gallons': int(d.total_gallons)} for d in deliveries]
|
||||
grand_total = sum(d.total_gallons for d in deliveries)
|
||||
result = {
|
||||
'totals': totals,
|
||||
'grand_total': int(grand_total)
|
||||
}
|
||||
return jsonify(result), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@deliverystatus.route("/today-totals", methods=["GET"])
|
||||
def get_today_totals():
|
||||
"""
|
||||
Get total gallons by town for today's deliveries, including grand total.
|
||||
"""
|
||||
try:
|
||||
deliveries = db.session.query(
|
||||
Delivery_Delivery.customer_town,
|
||||
func.sum(
|
||||
case(
|
||||
(Delivery_Delivery.customer_asked_for_fill == 1, 250),
|
||||
else_=Delivery_Delivery.gallons_ordered
|
||||
)
|
||||
).label('total_gallons')
|
||||
).filter(Delivery_Delivery.delivery_status == 2).group_by(Delivery_Delivery.customer_town).order_by(Delivery_Delivery.customer_town).all()
|
||||
|
||||
totals = [{'town': d.customer_town, 'gallons': int(d.total_gallons)} for d in deliveries]
|
||||
grand_total = sum(d.total_gallons for d in deliveries)
|
||||
result = {
|
||||
'totals': totals,
|
||||
'grand_total': int(grand_total)
|
||||
}
|
||||
return jsonify(result), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@deliverystatus.route("/waiting-totals", methods=["GET"])
|
||||
def get_waiting_totals():
|
||||
"""
|
||||
Get total gallons by town for waiting deliveries, including grand total.
|
||||
"""
|
||||
try:
|
||||
deliveries = db.session.query(
|
||||
Delivery_Delivery.customer_town,
|
||||
func.sum(
|
||||
case(
|
||||
(Delivery_Delivery.customer_asked_for_fill == 1, 250),
|
||||
else_=Delivery_Delivery.gallons_ordered
|
||||
)
|
||||
).label('total_gallons')
|
||||
).filter(Delivery_Delivery.delivery_status == 0).group_by(Delivery_Delivery.customer_town).order_by(Delivery_Delivery.customer_town).all()
|
||||
|
||||
totals = [{'town': d.customer_town, 'gallons': int(d.total_gallons)} for d in deliveries]
|
||||
grand_total = sum(d.total_gallons for d in deliveries)
|
||||
result = {
|
||||
'totals': totals,
|
||||
'grand_total': int(grand_total)
|
||||
}
|
||||
return jsonify(result), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user