From 5ee4da5971eba14245cb9f088246204ac54c2141 Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Mon, 5 Jan 2026 09:14:23 -0500 Subject: [PATCH] Added totals to delivery --- app/delivery_status/views.py | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/app/delivery_status/views.py b/app/delivery_status/views.py index f780df4..4e669d2 100755 --- a/app/delivery_status/views.py +++ b/app/delivery_status/views.py @@ -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