Files
eamco_office_api/app/customer/views.py
2025-10-06 21:14:01 -04:00

627 lines
21 KiB
Python
Executable File

from flask import request, jsonify
from flask_login import login_required
from geopy.geocoders import Nominatim
from app.customer import customer
from app import db
from datetime import datetime
from app.classes.cards import Card_Card
from app.classes.customer import \
Customer_Customer, \
Customer_Customer_schema,\
Customer_Description, \
Customer_Description_schema,\
Customer_Tank_Inspection_schema,\
Customer_Tank_Inspection
from app.classes.service import Service_Parts
from app.classes.admin import Admin_Company
from app.classes.auto import Auto_Delivery
from app.classes.stats_customer import Stats_Customer
import string
import random
def generate_random_number_string(length):
# Ensure the length is at least 1
if length < 1:
raise ValueError("Length must be at least 1")
# Generate a random number with the specified length
random_number = ''.join(random.choices(string.digits, k=length))
return random_number
@customer.route("/all", methods=["GET"])
def all_customers_around():
customer_list = db.session \
.query(Customer_Customer) \
.all()
customer_schema = Customer_Customer_schema(many=True)
return jsonify(customer_schema.dump(customer_list))
@customer.route("/all/<int:page>", methods=["GET"])
def all_customers(page):
"""
pagination all customers
"""
per_page_amount = 100
if page is None:
offset_limit = 0
elif page == 1:
offset_limit = 0
else:
offset_limit = (per_page_amount * page) - per_page_amount
customer_list = db.session \
.query(Customer_Customer) \
.order_by(Customer_Customer.id.desc()) \
.limit(per_page_amount).offset(offset_limit)
customer_schema = Customer_Customer_schema(many=True)
return jsonify(customer_schema.dump(customer_list))
@customer.route("/<int:customer_id>", methods=["GET"])
def get_a_customer(customer_id):
"""
"""
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
customer_schema = Customer_Customer_schema(many=False)
return jsonify(customer_schema.dump(get_customer))
@customer.route("/description/<int:customer_id>", methods=["GET"])
def get_a_customer_description(customer_id):
"""
"""
get_customer_description = (db.session
.query(Customer_Description)
.filter(Customer_Description.customer_id == customer_id)
.first())
if get_customer_description is None:
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
new_description = Customer_Description(
customer_id = customer_id,
account_number = get_customer.account_number,
company_id = get_customer.company_id,
fill_location = None,
description = None,
)
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("/tank/<int:customer_id>", 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"])
def create_customer():
"""
"""
now = datetime.utcnow()
get_company = (db.session
.query(Admin_Company)
.filter(Admin_Company.id == 1)
.first())
get_company = (db.session
.query(Admin_Company)
.filter(Admin_Company.id == 1)
.first())
random_string = generate_random_number_string(6)
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
if see_if_exists is not None:
random_string = generate_random_number_string(10)
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
if see_if_exists is not None:
random_string = generate_random_number_string(10)
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
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"]
response_customer_state = request.json["customer_state"]
response_customer_zip = request.json["customer_zip"]
response_customer_email = request.json["customer_email"]
response_customer_home_type = request.json["customer_home_type"]
customer_phone_number = request.json["customer_phone_number"]
customer_address = request.json["customer_address"]
customer_apt = request.json["customer_apt"]
customer_description_msg = request.json["customer_description"]
int_customer_home_type = int(response_customer_home_type)
response_customer_zip = str(response_customer_zip)
response_customer_state = int(response_customer_state)
if response_customer_state == 0:
the_state = 'MA'
elif response_customer_state == 1:
the_state = 'RI'
elif response_customer_state == 2:
the_state = 'NH'
else:
the_state = 'MA'
if response_customer_town == 0:
the_town = 'Auburn'
elif response_customer_state == 1:
the_town = 'Charlton'
elif response_customer_state == 2:
the_town = 'Cherry Valley'
elif response_customer_state == 3:
the_town = 'Dudley'
elif response_customer_state == 4:
the_town = 'Grafton'
elif response_customer_state == 5:
the_town = 'Leicester'
elif response_customer_state == 6:
the_town = 'Millbury'
elif response_customer_state == 7:
the_town = 'N Oxford'
elif response_customer_state == 8:
the_town = 'Oxford'
elif response_customer_state == 9:
the_town = 'Rochdale'
elif response_customer_state == 10:
the_town = 'Shrewsbury'
elif response_customer_state == 11:
the_town = 'Southbridge'
elif response_customer_state == 12:
the_town = 'Spencer'
elif response_customer_state == 13:
the_town = 'Sturbridge'
elif response_customer_state == 14:
the_town = 'Webster'
elif response_customer_state == 15:
the_town = 'Worcester'
else:
the_town = 'NA'
geolocator = Nominatim(user_agent="auburnoil")
address_string = customer_address + ' ' + response_customer_town+ ' ' + the_state
try:
location = geolocator.geocode(address_string)
user_lat =location.latitude
user_long = location.longitude
cor_ad = True
except:
user_lat =None
user_long = None
cor_ad = False
new_customer = Customer_Customer(
account_number=made_account_number,
customer_last_name=response_customer_last_name,
customer_first_name=response_customer_first_name,
customer_town=response_customer_town,
customer_state=response_customer_state,
customer_zip=response_customer_zip,
customer_first_call=now,
customer_email=response_customer_email,
customer_automatic=0,
customer_home_type=int_customer_home_type,
customer_phone_number=customer_phone_number,
customer_address=customer_address,
customer_apt=customer_apt,
company_id=1,
customer_latitude=user_lat,
customer_longitude=user_long,
correct_address=cor_ad
)
db.session.add(new_customer)
db.session.flush()
create_stats_customer = Stats_Customer(
customer_id = new_customer.id,
total_calls = 0,
service_calls_total = 0,
service_calls_total_spent = 0,
service_calls_total_profit = 0,
oil_deliveries = 0,
oil_total_gallons = 0,
oil_total_spent = 0,
oil_total_profit = 0,
)
db.session.add(create_stats_customer)
new_description = Customer_Description(
customer_id = new_customer.id,
account_number = made_account_number,
description = customer_description_msg,
fill_location=None,
company_id=1,
)
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)
db.session.commit()
return jsonify({
"ok": True,
'user': {
'user_id': new_customer.id,
'user_name': new_customer.customer_last_name,
'user_email': new_customer.customer_email,
},
}), 200
@customer.route("/edit/<int:customer_id>", methods=["PUT"])
@login_required
def edit_customer(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())
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"]
response_customer_state = request.json["customer_state"]
response_customer_zip = request.json["customer_zip"]
response_customer_phone_number = request.json["customer_phone_number"]
response_customer_email = request.json["customer_email"]
response_customer_home_type = request.json["customer_home_type"]
response_customer_address = request.json["customer_address"]
response_customer_apt = request.json["customer_apt"]
response_customer_description = request.json["customer_description"]
response_customer_fill_location = request.json["customer_fill_location"]
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)
if response_customer_state == 0:
the_state = 'MA'
elif response_customer_state == 1:
the_state = 'RI'
elif response_customer_state == 1:
the_state = 'NH'
else:
the_state = 'MA'
geolocator = Nominatim(user_agent="auburnoil")
address_string = response_customer_address + ' ' + response_customer_town+ ' ' + the_state
try:
location = geolocator.geocode(address_string, timeout=10)
get_customer.customer_latitude = location.latitude
get_customer.customer_longitude = location.longitude
cor_ad = True
except:
get_customer.customer_latitude = None
get_customer.customer_longitude = None
cor_ad = False
get_customer.customer_address = response_customer_address
get_customer.customer_home_type = response_customer_home_type
get_customer.customer_phone_number = response_customer_phone_number
get_customer.customer_last_name = response_customer_last_name
get_customer.customer_first_name = response_customer_first_name
get_customer.customer_town = response_customer_town
get_customer.customer_state = response_customer_state
get_customer.customer_zip = response_customer_zip
get_customer.customer_email = response_customer_email
get_customer.customer_apt = response_customer_apt
get_customer.correct_address = cor_ad
db.session.add(get_customer)
db.session.commit()
return jsonify({
"ok": True,
'user': {
'user_name': get_customer.customer_last_name,
'user_email': get_customer.customer_email,
},
}), 200
@customer.route("/delete/<int:customer_id>", methods=["DELETE"])
@login_required
def delete_customer(customer_id):
"""
"""
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
get_cards = (db.session
.query(Card_Card)
.filter(Card_Card.user_id == get_customer.id)
.first())
if get_cards is not None:
db.session.delete(get_cards)
db.session.delete(get_customer)
db.session.commit()
return jsonify({
"ok": True,
'user': {
'user_name': get_customer.customer_last_name,
'user_email': get_customer.customer_email,
},
}), 200
@customer.route("/count", methods=["GET"])
@login_required
def customer_count():
"""
"""
get_customer = (db.session
.query(Customer_Customer)
.count())
return jsonify({
"ok": True,
'count': get_customer
}), 200
@customer.route("/automatic/status/<int:customer_id>", methods=["GET"])
@login_required
def customer_automatic_status(customer_id):
"""
"""
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
if get_customer.customer_automatic == 1:
status = 1
if get_customer.customer_automatic == 0:
status = 0
return jsonify({
"ok": True,
'status': status
}), 200
@customer.route("/automatic/deliveries", methods=["GET"])
@login_required
def get_all_automatic_deliveries():
"""
Get all automatic deliveries for the table.
"""
from app.classes.auto import Auto_Delivery, Auto_Delivery_schema
try:
deliveries = Auto_Delivery.query.all()
schema = Auto_Delivery_schema(many=True)
return jsonify(schema.dump(deliveries)), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@customer.route("/automatic/assign/<int:customer_id>", methods=["GET"])
@login_required
def customer_automatic_assignment(customer_id):
"""
"""
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
get_auto = (db.session
.query(Auto_Delivery)
.filter(Auto_Delivery.customer_id == customer_id)
.first())
get_main_credit_card = (db.session
.query(Card_Card)
.filter(Card_Card.user_id == customer_id)
.filter(Card_Card.main_card == True)
.first())
get_customer_tank = (db.session
.query(Customer_Tank_Inspection)
.filter(Customer_Tank_Inspection.customer_id == customer_id)
.first())
get_service_parts = (db.session
.query(Service_Parts)
.filter(Service_Parts.customer_id == customer_id)
.first())
if get_customer.customer_automatic == 1:
# customer becomes will call
get_customer.customer_automatic = 0
db.session.add(get_customer)
if get_auto is not None:
db.session.delete(get_auto)
status = 3
else:
if get_main_credit_card is None:
status = 2
return jsonify({
"ok": True,
'status': status
}), 200
# customer becomes an automatic
if get_auto is None:
hot_water_value = get_service_parts.hot_water_tank if get_service_parts and get_service_parts.hot_water_tank is not None else 0
create_auto = Auto_Delivery(customer_id=customer_id,
customer_full_name=get_customer.customer_first_name + ' ' + get_customer.customer_last_name,
account_number=get_customer.account_number,
customer_town=get_customer.customer_town,
customer_state=get_customer.customer_state,
customer_zip=get_customer.customer_zip,
customer_address=get_customer.customer_address,
last_fill=None,
last_updated=None,
estimated_gallons_left=0,
estimated_gallons_left_prev_day=0,
tank_height=0,
tank_size=get_customer_tank.tank_size,
house_factor=1,
auto_status=1,
days_since_last_fill=0,
hot_water_summer=hot_water_value
)
db.session.add(create_auto)
get_customer.customer_automatic = 1
db.session.add(get_customer)
status = 1
db.session.commit()
return jsonify({
"ok": True,
'status': status
}), 200
@customer.route("/edit/tank/<int:customer_id>", methods=["PUT"])
@login_required
def edit_customer_tank(customer_id):
"""
Safely edits or creates tank and description details for a customer.
"""
get_customer = db.session.query(Customer_Customer).filter(Customer_Customer.id == customer_id).one_or_none()
if not get_customer:
return jsonify({"ok": False, "error": "Customer not found"}), 404
get_customer_description = db.session.query(Customer_Description).filter(Customer_Description.customer_id == customer_id).first()
if not get_customer_description:
get_customer_description = Customer_Description(customer_id=customer_id)
db.session.add(get_customer_description)
get_customer_tank = db.session.query(Customer_Tank_Inspection).filter(Customer_Tank_Inspection.customer_id == customer_id).first()
if not get_customer_tank:
get_customer_tank = Customer_Tank_Inspection(customer_id=customer_id)
db.session.add(get_customer_tank)
data = request.get_json()
if 'tank_status' in data:
get_customer_tank.tank_status = data["tank_status"]
if 'outside_or_inside' in data:
get_customer_tank.outside_or_inside = data["outside_or_inside"]
response_last_tank_inspection = data.get("last_tank_inspection", None)
response_tank_size = data.get("tank_size", 0)
# --- FIX APPLIED HERE ---
# 1. Get the value from the request. Default to 0 if it's missing.
response_customer_fill_location = data.get("fill_location", 0)
# 2. Add a safety check: if the frontend sent an empty string, convert it to 0.
if response_customer_fill_location == "":
response_customer_fill_location = 0
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
if get_customer.customer_automatic == 1:
get_auto_info = db.session.query(Auto_Delivery).filter(Auto_Delivery.customer_id == customer_id).first()
if get_auto_info:
get_auto_info.tank_size = response_tank_size
db.session.add(get_auto_info)
db.session.commit()
return jsonify({"ok": True}), 200