major update
This commit is contained in:
83
app/money/views.py
Normal file
83
app/money/views.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from flask import jsonify
|
||||
from app.money import money
|
||||
from app import db
|
||||
import datetime
|
||||
from datetime import date
|
||||
from app.classes.money import Money_delivery, Money_delivery_schema
|
||||
from app.classes.delivery import Delivery_Delivery, Delivery_Delivery_schema
|
||||
|
||||
def get_monday_date(date_object):
|
||||
"""Gets the date of the Monday for the given date."""
|
||||
|
||||
# Get the day of the week as an integer (0 = Monday, 6 = Sunday)
|
||||
day_of_week = date_object.weekday()
|
||||
|
||||
# Calculate the number of days to subtract to get to Monday
|
||||
days_to_monday = day_of_week - 0 # Monday is 0
|
||||
|
||||
# Subtract the days from the given date to get Monday's date
|
||||
monday_date = date_object - datetime.timedelta(days=days_to_monday)
|
||||
|
||||
return monday_date
|
||||
|
||||
|
||||
|
||||
|
||||
@money.route("/profit/week", methods=["GET"])
|
||||
def total_profit_week():
|
||||
# Get today's date
|
||||
total_profit = 0
|
||||
total_deliveries = 0
|
||||
today = datetime.date.today()
|
||||
|
||||
# Get the date of the Monday for today
|
||||
monday = get_monday_date(today)
|
||||
get_total = (db.session
|
||||
.query(Money_delivery)
|
||||
.filter(Money_delivery.time_added >= monday)
|
||||
.filter(Money_delivery.time_added <= today)
|
||||
.all())
|
||||
for f in get_total:
|
||||
|
||||
total_profit = total_profit + f.total_profit
|
||||
total_deliveries = total_deliveries + 1
|
||||
|
||||
|
||||
return jsonify({"ok": True,
|
||||
'total_profit': total_profit,
|
||||
'total_deliveries': total_deliveries
|
||||
}), 200
|
||||
|
||||
|
||||
|
||||
@money.route("/profit/year", methods=["GET"])
|
||||
def total_profit_year():
|
||||
# Get today's date
|
||||
total_profit = 0
|
||||
|
||||
first_of_year = date(date.today().year, 1, 1)
|
||||
last_of_year = date(date.today().year, 12, 31)
|
||||
# Get the date of the Monday for today
|
||||
get_total = (db.session
|
||||
.query(Money_delivery)
|
||||
.filter(Money_delivery.time_added >= first_of_year)
|
||||
.filter(Money_delivery.time_added <= last_of_year)
|
||||
.all())
|
||||
for f in get_total:
|
||||
total_profit = total_profit + f.total_profit
|
||||
return jsonify({"ok": True,
|
||||
'total': total_profit
|
||||
}), 200
|
||||
|
||||
|
||||
@money.route("/<int:delivery_id>", methods=["GET"])
|
||||
def get_money_delivery(delivery_id):
|
||||
"""
|
||||
"""
|
||||
profit = (db.session
|
||||
.query(Money_delivery)
|
||||
.filter(Money_delivery.delivery_id == delivery_id)
|
||||
.first())
|
||||
|
||||
money_schema = Money_delivery_schema(many=False)
|
||||
return jsonify(money_schema.dump(profit))
|
||||
Reference in New Issue
Block a user