423 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			423 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| from flask import request, jsonify
 | |
| from flask_login import login_required
 | |
| 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
 | |
| from app.classes.admin import Admin_Company
 | |
| from app.classes.auto import Auto_Delivery
 | |
| import string
 | |
| import random
 | |
| 
 | |
| @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 = 50
 | |
|     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):
 | |
|     """
 | |
|     Checks auth token to ensure user is authenticated
 | |
|     """
 | |
|     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))
 | |
| 
 | |
| def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
 | |
|     return ''.join(random.choice(chars) for _ in range(size))
 | |
| 
 | |
| @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()
 | |
| 
 | |
|     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"]
 | |
|     response_customer_state = request.json["customer_state"]
 | |
|     response_customer_zip = request.json["customer_zip"]
 | |
|     response_customer_email = request.json["customer_email"]
 | |
|     response_customer_automatic = request.json["customer_automatic"]
 | |
|     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"]
 | |
|     
 | |
|     if response_customer_automatic is True:
 | |
|         auto_customer = 1
 | |
|     else:
 | |
|         auto_customer = 0
 | |
| 
 | |
|     int_customer_home_type = int(response_customer_home_type)
 | |
|     response_customer_zip = str(response_customer_zip)
 | |
|     response_customer_state = int(response_customer_state)
 | |
| 
 | |
|     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=auto_customer,
 | |
|         customer_home_type=int_customer_home_type,
 | |
|         customer_phone_number=customer_phone_number,
 | |
|         customer_address=customer_address,
 | |
|         customer_apt=customer_apt,
 | |
|         company_id=1,
 | |
|     )
 | |
|     db.session.add(new_customer)
 | |
|     db.session.flush()
 | |
|         
 | |
|     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_auto = Auto_Delivery(
 | |
|         customer_id = new_customer.id,
 | |
|         account_number = made_account_number,
 | |
|         customer_full_name = new_customer.customer_last_name + " " + new_customer.customer_first_name,
 | |
|         last_fill = None,
 | |
|         last_updated = None,
 | |
|         estimated_gallons_left = None, 
 | |
|         estimated_gallons_left_prev_day = None, 
 | |
|         tank_height = None,
 | |
|         tank_size = None,
 | |
|         house_factor = 1,
 | |
|         customer_town = new_customer.customer_town,
 | |
|         customer_state = new_customer.customer_state,
 | |
|         customer_zip = new_customer.customer_zip,
 | |
|         auto_status=0
 | |
|     )
 | |
| 
 | |
|     db.session.add(new_auto)
 | |
|     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_automatic = request.json["customer_automatic"]
 | |
| 
 | |
|     response_customer_description = request.json["customer_description"]
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|     if response_customer_automatic is True:
 | |
|         get_auto = (db.session
 | |
|             .query(Auto_Delivery)
 | |
|             .filter(Auto_Delivery.customer_id == customer_id)
 | |
|             .first())
 | |
|             
 | |
|         auto = 1
 | |
|          # 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 = datetime.utcnow(),
 | |
|                                         last_updated = None,
 | |
|                                         estimated_gallons_left = 0,
 | |
|                                         estimated_gallons_left_prev_day = 0,
 | |
|                                         tank_height = 0,
 | |
|                                         tank_size =275,
 | |
|                                         house_factor = 1,
 | |
|                                         auto_status=0
 | |
|                                         )
 | |
|             db.session.add(create_auto)
 | |
| 
 | |
|     else:
 | |
|         auto = 0
 | |
|     if get_customer_description is not None:
 | |
|         get_customer_description.description = response_customer_description
 | |
| 
 | |
|         db.session.add(get_customer_description)
 | |
| 
 | |
| 
 | |
|     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_automatic = auto
 | |
| 
 | |
|     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("/description/<int:customer_id>", 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/<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())
 | |
| 
 | |
| 
 | |
|     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 = datetime.utcnow(),
 | |
|                                         last_updated = None,
 | |
|                                         estimated_gallons_left = 0,
 | |
|                                         estimated_gallons_left_prev_day = 0,
 | |
|                                         tank_height = 0,
 | |
|                                         tank_size =275,
 | |
|                                         house_factor = 1,
 | |
|                                         auto_status=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
 | |
|     
 |