diff --git a/app/__init__.py b/app/__init__.py index 19fe572..4cbbf1e 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -192,6 +192,9 @@ app.register_blueprint(query_blueprint, url_prefix='/query') from .payment import payment as payment_blueprint app.register_blueprint(payment_blueprint, url_prefix='/payment') +from .money import money as money_blueprint +app.register_blueprint(money_blueprint, url_prefix='/money') + from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') diff --git a/app/classes/customer.py b/app/classes/customer.py index a03ae95..1089044 100755 --- a/app/classes/customer.py +++ b/app/classes/customer.py @@ -98,3 +98,32 @@ class Customer_Description(db.Model): class Customer_Description_schema(ma.SQLAlchemyAutoSchema): class Meta: model = Customer_Description + + +class Customer_Property_schema(ma.SQLAlchemyAutoSchema): + class Meta: + model = Customer_Property + + +class Customer_Tank_Inspection(db.Model): + __tablename__ = 'customer_tank' + __table_args__ = {"schema": "public"} + + id = db.Column(db.Integer, + primary_key=True, + autoincrement=True, + unique=False) + + customer_id = db.Column(db.INTEGER) + last_tank_inspection = db.Column(db.DATE()) + tank_status = db.Column(db.BOOLEAN) + outside_or_inside = db.Column(db.BOOLEAN) + tank_size = db.Column(db.INTEGER) + + + +class Customer_Tank_Inspection_schema(ma.SQLAlchemyAutoSchema): + class Meta: + model = Customer_Tank_Inspection + + \ No newline at end of file diff --git a/app/customer/views.py b/app/customer/views.py index f9ebe46..31c6c57 100755 --- a/app/customer/views.py +++ b/app/customer/views.py @@ -8,7 +8,9 @@ from app.classes.customer import \ Customer_Customer, \ Customer_Customer_schema,\ Customer_Description, \ - Customer_Description_schema + Customer_Description_schema,\ + Customer_Tank_Inspection_schema,\ + Customer_Tank_Inspection from app.classes.admin import Admin_Company from app.classes.auto import Auto_Delivery from app.classes.stats_customer import Stats_Customer @@ -98,6 +100,38 @@ def get_a_customer_description(customer_id): customer_schema = Customer_Description_schema(many=False) return jsonify(customer_schema.dump(get_customer_description)) + + +@customer.route("/tank/", methods=["GET"]) +def get_a_customer_tank(customer_id): + """ + + """ + get_customer_tank = (db.session + .query(Customer_Tank_Inspection) + .filter(Customer_Tank_Inspection.customer_id == customer_id) + .first()) + if get_customer_tank is None: + + new_tank = Customer_Tank_Inspection( + customer_id = customer_id, + last_tank_inspection = None, + tank_status = False, + outside_or_inside = True, + tank_size = 275, + ) + db.session.add(new_tank) + db.session.commit() + get_customer_tank = (db.session + .query(Customer_Tank_Inspection) + .filter(Customer_Tank_Inspection.customer_id == customer_id) + .first()) + customer_schema = Customer_Tank_Inspection_schema(many=False) + return jsonify(customer_schema.dump(get_customer_tank)) + + + + @customer.route("/create", methods=["POST"]) @login_required def create_customer(): @@ -105,11 +139,22 @@ def create_customer(): """ now = datetime.utcnow() get_company = db.session.query(Admin_Company).filter(Admin_Company.id == 1).first() + last_customer = (db.session.query(Customer_Customer).order_by(Customer_Customer.id.desc()).first()) + new_customer_id = last_customer.id + 1 + if len(str(new_customer_id)) == 2: + new_customer_id = '000' + str(new_customer_id) + if len(str(new_customer_id)) == 3: + new_customer_id = '00' + str(new_customer_id) + if len(str(new_customer_id)) == 4: + new_customer_id = '0' + str(new_customer_id) + + + starter_digits = str(get_company.account_prefix) + '-' + str(new_customer_id) - starter_digits = str(get_company.account_prefix) + '-' + id_generator() made_account_number = starter_digits + response_customer_last_name = request.json["customer_last_name"] response_customer_first_name = request.json["customer_first_name"] response_customer_town = request.json["customer_town"] @@ -173,6 +218,15 @@ def create_customer(): ) db.session.add(new_description) + new_tank = Customer_Tank_Inspection( + customer_id = new_customer.id, + last_tank_inspection=None, + tank_status = False, + outside_or_inside = True, + tank_size=275, + ) + db.session.add(new_tank) + new_auto = Auto_Delivery( customer_id = new_customer.id, account_number = made_account_number, @@ -202,6 +256,63 @@ def create_customer(): }, }), 200 +@customer.route("/edit/tank/", methods=["PUT"]) +@login_required +def edit_customer_tank(customer_id): + """ + """ + get_customer = (db.session + .query(Customer_Customer) + .filter(Customer_Customer.id == customer_id) + .first()) + get_customer_description = (db.session + .query(Customer_Description) + .filter(Customer_Description.customer_id == customer_id) + .first()) + get_customer_tank = (db.session + .query(Customer_Tank_Inspection) + .filter(Customer_Tank_Inspection.customer_id == customer_id) + .first()) + + response_tank_status = request.json["tank_status"] + if response_tank_status == 'true': + get_customer_tank.tank_status = True + + elif response_tank_status == 'false': + get_customer_tank.outside_or_inside = False + else: + pass + + + response_outside_or_inside = request.json["outside_or_inside"] + if response_outside_or_inside == 'true': + get_customer_tank.outside_or_inside = True + + elif response_outside_or_inside == 'false': + get_customer_tank.outside_or_inside = False + else: + pass + + response_last_tank_inspection = request.json["last_tank_inspection"] + response_tank_size = request.json["tank_size"] + response_customer_fill_location = request.json["fill_location"] + + + get_customer_tank.last_tank_inspection = response_last_tank_inspection + get_customer_tank.tank_size = response_tank_size + + + get_customer_description.fill_location = response_customer_fill_location + db.session.add(get_customer_description) + + db.session.add(get_customer) + db.session.commit() + + return jsonify({ + "ok": True, + }), 200 + + @customer.route("/edit/", methods=["PUT"]) @login_required @@ -229,7 +340,7 @@ def edit_customer(customer_id): response_customer_description = request.json["customer_description"] - + response_customer_fill_location = request.json["customer_fill_location"] @@ -265,7 +376,7 @@ def edit_customer(customer_id): auto = 0 if get_customer_description is not None: get_customer_description.description = response_customer_description - + get_customer_description.fill_location = response_customer_fill_location db.session.add(get_customer_description) @@ -360,40 +471,6 @@ def customer_automatic_status(customer_id): }), 200 -@customer.route("/description/", methods=["GET"]) -@login_required -def customer_description(customer_id): - """ - """ - get_customer = (db.session - .query(Customer_Customer) - .filter(Customer_Customer.id == customer_id) - .first()) - get_customer_description = (db.session - .query(Customer_Description) - .filter(Customer_Description.customer_id == customer_id) - .first()) - - - if get_customer_description is None: - new_description = Customer_Description( - customer_id = get_customer.id, - account_number = get_customer.account_number, - description = None, - fill_location=None, - company_id=1, - ) - db.session.add(new_description) - db.session.commit() - - - get_customer_description = (db.session - .query(Customer_Description) - .filter(Customer_Description.customer_id == customer_id) - .first()) - - customer_schema = Customer_Description_schema(many=False) - return jsonify(customer_schema.dump(get_customer_description)) @customer.route("/automatic/assign/", methods=["GET"]) diff --git a/app/delivery/views.py b/app/delivery/views.py index 1e72022..4d07617 100755 --- a/app/delivery/views.py +++ b/app/delivery/views.py @@ -13,6 +13,8 @@ from app.classes.cards import Card_Card from app.classes.pricing import Pricing_Oil_Oil from app.classes.auth import Auth_User from app.classes.promo import Promo_Promo +from app.classes.stats_customer import Stats_Customer + @delivery.route("/", methods=["GET"]) def get_a_delivery(delivery_id): @@ -35,7 +37,7 @@ def get_a_delivery(delivery_id): 'delivery_prime': get_delivery.prime, 'delivery_emergency': get_delivery.emergency, 'delivery_same_day': get_delivery.same_day, - 'when_ordered': get_delivery.when_ordered, + 'when_ordered': str(get_delivery.when_ordered), 'customer_price': get_delivery.customer_price, 'delivery_status': get_delivery.delivery_status, 'payment_type': get_delivery.payment_type, @@ -418,7 +420,7 @@ def edit_a_delivery(delivery_id): gallons_ordered = request.json["gallons_ordered"] delivery_status = request.json["delivery_status"] when_to_deliver = request.json["expected_delivery_date"] - + when_ordered = request.json["created_delivery_date"] dispatcher_notes_taken = request.json["dispatcher_notes_taken"] customer_wants_fill = request.json["customer_asked_for_fill"] card_payment = request.json["credit"] @@ -504,6 +506,7 @@ def edit_a_delivery(delivery_id): get_delivery.gallons_ordered = gallons_ordered get_delivery.customer_asked_for_fill = customer_wants_fill get_delivery.expected_delivery_date = when_to_deliver + get_delivery.when_ordered = when_ordered get_delivery.dispatcher_notes = dispatcher_notes_taken get_delivery.prime = prime_info get_delivery.same_day = same_day_info @@ -715,6 +718,17 @@ def create_a_delivery(user_id): ) + get_stats_customer = (db.session + .query(Stats_Customer) + .filter(Stats_Customer.customer_id == get_customer.id) + .first()) + + new_calls = int(get_stats_customer.total_calls) + 1 + get_stats_customer.total_calls = new_calls + + + db.session.add(get_stats_customer) + db.session.add(new_delivery) db.session.commit() diff --git a/app/delivery_data/views.py b/app/delivery_data/views.py index 1b730f6..cf73b8e 100755 --- a/app/delivery_data/views.py +++ b/app/delivery_data/views.py @@ -217,8 +217,7 @@ def office_finalize_delivery(delivery_id): new_gallons = Decimal(get_stats_customer.oil_total_gallons) + Decimal(gallons_delivered) get_stats_customer.oil_total_gallons = new_gallons - new_calls = int(get_stats_customer.total_calls) + 1 - get_stats_customer.total_calls = new_calls + # update fill location diff --git a/app/main/views.py b/app/main/views.py index 1953d69..f46a8f4 100755 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,6 +1,9 @@ -from flask import jsonify, Response, request +from flask import jsonify, Response, request, url_for from app import app, WHITE +@app.route("/favicon.ico") +def favicon(): + return url_for('static', filename='data:,') @app.route('/robots.txt') diff --git a/app/money/__init__.py b/app/money/__init__.py new file mode 100644 index 0000000..d8a5a5f --- /dev/null +++ b/app/money/__init__.py @@ -0,0 +1,8 @@ +# coding=utf-8 +from flask import Blueprint + + +money = Blueprint('money', __name__) + + +from . import views \ No newline at end of file diff --git a/app/money/views.py b/app/money/views.py new file mode 100644 index 0000000..3991ce7 --- /dev/null +++ b/app/money/views.py @@ -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("/", 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)) \ No newline at end of file diff --git a/app/payment/views.py b/app/payment/views.py index c336368..9a77247 100755 --- a/app/payment/views.py +++ b/app/payment/views.py @@ -53,10 +53,10 @@ def get_user_cards(user_id): """ gets all cards of a user """ - get_u_cards = db.session \ - .query(Card_Card) \ - .filter(Card_Card.user_id == user_id) \ - .all() + get_u_cards = (db.session + .query(Card_Card) + .filter(Card_Card.user_id == user_id) + .all()) card_schema = Card_Card_schema(many=True) return jsonify(card_schema.dump(get_u_cards)) @@ -68,10 +68,10 @@ def get_user_cards_count(user_id): gets all cards of a user """ - get_u_cards = db.session \ - .query(Card_Card) \ - .filter(Card_Card.user_id == user_id) \ - .count() + get_u_cards = (db.session + .query(Card_Card) + .filter(Card_Card.user_id == user_id) + .count()) return jsonify({ "ok": True, @@ -85,10 +85,10 @@ def get_user_specific_card(card_id): gets a specific card of a user """ - get_user_card = db.session \ - .query(Card_Card) \ - .filter(Card_Card.id == card_id) \ - .first() + get_user_card = (db.session + .query(Card_Card) + .filter(Card_Card.id == card_id) + .first()) card_schema = Card_Card_schema(many=False) return jsonify(card_schema.dump(get_user_card)) @@ -100,10 +100,10 @@ def create_user_card(user_id): adds a card of a user """ - get_customer = db.session \ - .query(Customer_Customer) \ - .filter(Customer_Customer.id == user_id) \ - .first() + get_customer = (db.session + .query(Customer_Customer) + .filter(Customer_Customer.id == user_id) + .first()) name_on_card = request.json["card_name"] expiration_month = request.json["expiration_month"] @@ -148,17 +148,18 @@ def set_main_card(user_id, card_id): updates a card of a user """ - get_new_main_card = db.session \ - .query(Card_Card) \ - .filter(Card_Card.user_id == user_id) \ - .filter(Card_Card.id == card_id) \ - .first() + get_new_main_card = (db.session + .query(Card_Card) + .filter(Card_Card.user_id == user_id) + .filter(Card_Card.id == card_id) + .first()) - get_other_card = db.session \ - .query(Card_Card) \ - .filter(Card_Card.main_card == True) \ - .filter(Card_Card.user_id == user_id) \ - .first() + get_other_card = (db.session + .query(Card_Card) + .filter(Card_Card.main_card == True) + .filter(Card_Card.user_id == user_id) + .first()) + if get_other_card is not None: get_other_card.main_card = False db.session.add(get_other_card) @@ -176,15 +177,17 @@ def update_user_card(card_id): edits a card """ - get_card = db.session \ - .query(Card_Card) \ - .filter(Card_Card.id == card_id) \ - .first() + get_card = (db.session + .query(Card_Card) + .filter(Card_Card.id == card_id) + .first()) + + get_customer = (db.session + .query(Customer_Customer) + .filter(Customer_Customer.id == get_card.user_id) + .first()) + - get_customer = db.session \ - .query(Customer_Customer) \ - .filter(Customer_Customer.id == get_card.user_id) \ - .first() name_on_card = request.json["card_name"] expiration_month = request.json["expiration_month"] expiration_year = request.json["expiration_year"] @@ -219,10 +222,10 @@ def remove_user_card(card_id): removes a card """ - get_card = db.session \ - .query(Card_Card) \ - .filter(Card_Card.id == card_id) \ - .first() + get_card = (db.session + .query(Card_Card) + .filter(Card_Card.id == card_id) + .first()) db.session.delete(get_card) db.session.commit() diff --git a/app/search/views.py b/app/search/views.py index 66465b5..7315062 100755 --- a/app/search/views.py +++ b/app/search/views.py @@ -1,14 +1,8 @@ from flask import request, jsonify -from flask_login import current_user -from sqlalchemy.sql import func -from datetime import date, timedelta + from app.search import search from app import db -from datetime import datetime - -from app.classes.auth import Auth_User from app.classes.customer import Customer_Customer, Customer_Customer_schema -from app.classes.employee import Employee_Employee from app.classes.delivery import Delivery_Delivery, Delivery_Delivery_schema @@ -24,6 +18,7 @@ def search_customers(): search = search.replace("!", "") search = search.replace("#", "") search = search.replace("@", "") + search = search.replace("$", "") # search by last name if search_type == '@': search = search[1:] @@ -31,7 +26,7 @@ def search_customers(): .query(Customer_Customer) .filter(Customer_Customer.customer_last_name.ilike(search)) .all()) - + # Customer Address elif search_type == '!': search = search[::1] @@ -46,6 +41,15 @@ def search_customers(): .query(Customer_Customer) .filter(Customer_Customer.customer_phone_number.ilike(search)) .all()) + # Account Number + elif search_type == '$': + search = search[::1] + + customer_list = (db.session + .query(Customer_Customer) + .filter(Customer_Customer.account_number.ilike(search)) + .order_by(Customer_Customer.account_number.asc()) + .all()) else: customer_list = (db.session diff --git a/app/stats/views.py b/app/stats/views.py index 393631d..9e7e490 100755 --- a/app/stats/views.py +++ b/app/stats/views.py @@ -1,12 +1,30 @@ from flask import jsonify -from sqlalchemy import func, or_ from datetime import date from app.stats import stats +import datetime from app import db from app.classes.delivery import Delivery_Delivery from app.classes.stats_company import Stats_Company, Stats_Company_schema from app.classes.stats_customer import Stats_Customer, Stats_Customer_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 + + + + @stats.route("/calls/add", methods=["PUT"]) def total_calls_post(): total_calls_today = (db.session @@ -149,4 +167,26 @@ def get_user_last_delivery(user_id): date_delivered = "no deliveries on record" return jsonify({"ok": True, 'date': date_delivered, - }), 200 \ No newline at end of file + }), 200 + + +@stats.route("/gallons/week", methods=["GET"]) +def total_gallons_delivered_this_week(): + # Get today's date + total_gallons = 0 + + today = datetime.date.today() + + # Get the date of the Monday for today + monday = get_monday_date(today) + get_total = (db.session + .query(Delivery_Delivery) + .filter(Delivery_Delivery.when_delivered >= monday) + .filter(Delivery_Delivery.when_delivered <= today) + .all()) + for f in get_total: + total_gallons = total_gallons + f.gallons_delivered + return jsonify({"ok": True, + 'total': total_gallons, + }), 200 +