Files
eamco_office_api/app/customer/views.py
2025-02-04 18:22:00 -05:00

604 lines
19 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.admin import Admin_Company
from app.classes.auto import Auto_Delivery
from app.classes.stats_customer import Stats_Customer
import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
@customer.route("/all", methods=["GET"])
@login_required
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"])
@login_required
def all_customers(page):
"""
pagination all customers
"""
per_page_amount = 10
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"])
@login_required
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)
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"]
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/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())
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 becames an automatic
if get_auto is None:
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
)
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):
"""
"""
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
if get_customer.customer_automatic == 1:
get_auto_info = (db.session
.query(Auto_Delivery)
.filter(Auto_Delivery.customer_id == customer_id)
.first())
get_auto_info.tank_size = response_tank_size
db.session.add(get_auto_info)
db.session.add(get_customer_description)
db.session.add(get_customer)
db.session.commit()
return jsonify({
"ok": True,
}), 200