176 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			176 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| from flask import request, jsonify
 | |
| from datetime import datetime
 | |
| from decimal import Decimal
 | |
| from app.delivery_data import delivery_data
 | |
| from app import db
 | |
| from app.classes.customer import Customer_Customer, Customer_Description
 | |
| from app.classes.delivery import Delivery_Delivery
 | |
| from app.classes.employee import Employee_Employee
 | |
| from app.classes.cards import Card_Card
 | |
| from app.classes.stats_employee import Stats_Employee_Oil
 | |
| from app.classes.auto import Auto_Delivery
 | |
| from app.classes.stats_customer import Stats_Customer
 | |
| 
 | |
| @delivery_data.route("/finalize/<int:delivery_id>", methods=["PUT"])
 | |
| def office_finalize_delivery(delivery_id):
 | |
|     """
 | |
|     This will make a delivery finalized from the driver
 | |
|     """
 | |
| 
 | |
|     """
 | |
|     Finalizes a delivery from office
 | |
|     """
 | |
|     now = datetime.utcnow()
 | |
|     get_delivery = db.session \
 | |
|         .query(Delivery_Delivery) \
 | |
|         .filter(Delivery_Delivery.id == delivery_id) \
 | |
|         .first()
 | |
| 
 | |
|     get_customer = db.session \
 | |
|         .query(Customer_Customer) \
 | |
|         .filter(Customer_Customer.id == get_delivery.customer_id) \
 | |
|         .first()
 | |
|     
 | |
|     get_customer_description = db.session \
 | |
|         .query(Customer_Description) \
 | |
|         .filter(Customer_Description.customer_id == get_delivery.customer_id) \
 | |
|         .first()
 | |
|     if get_customer_description is None:
 | |
|         new_customer_desc = Customer_Description(
 | |
|             customer_id = get_customer.id,
 | |
|             account_number =get_customer.account_number,
 | |
|             company_id = get_customer.company_id,
 | |
|             fill_location = None,
 | |
|             description = None,
 | |
|         )
 | |
|         db.session.add(new_customer_desc)
 | |
|         db.session.flush()
 | |
|         get_customer_description = db.session \
 | |
|             .query(Customer_Description) \
 | |
|             .filter(Customer_Description.customer_id == get_delivery.customer_id) \
 | |
|             .first()
 | |
|     delivery_driver_id = request.json["driver_employee_id"]
 | |
|     if delivery_driver_id is None:
 | |
|         delivery_driver_id = 2
 | |
|     if delivery_driver_id == 0:
 | |
|         delivery_driver_id = 2
 | |
|    
 | |
|     get_driver = (db.session
 | |
|                     .query(Employee_Employee)
 | |
|                     .filter(Employee_Employee.id == delivery_driver_id)
 | |
|                     .first())
 | |
|     
 | |
|     get_stats_employee = (db.session
 | |
|                  .query(Stats_Employee_Oil)
 | |
|                  .filter(Stats_Employee_Oil.employee_id == get_delivery.driver_employee_id)
 | |
|                  .first())
 | |
|     
 | |
|     get_stats_customer = (db.session
 | |
|                 .query(Stats_Customer)
 | |
|                 .filter(Stats_Customer.customer_id == get_customer.id)
 | |
|                 .first())
 | |
|     if get_stats_customer is None:
 | |
|         create_stats_customer = Stats_Customer(
 | |
|             customer_id = get_customer.id,
 | |
|             total_calls = 1,
 | |
|             service_calls_total = 0,
 | |
|             service_calls_total_spent = 0,
 | |
|             service_calls_total_profit = 0,
 | |
|             oil_deliveries = 1,
 | |
|             oil_total_gallons = 0,
 | |
|             oil_total_spent = 0,
 | |
|             oil_total_profit = 0,
 | |
|     
 | |
|         )
 | |
|         db.session.add(create_stats_customer)
 | |
|         db.session.flush()
 | |
|         get_stats_customer = (db.session
 | |
|                     .query(Stats_Customer)
 | |
|                     .filter(Stats_Customer.customer_id == get_customer.id)
 | |
|                     .first())
 | |
|         
 | |
|     if get_stats_employee is None:
 | |
|         create_stats = Stats_Employee_Oil(
 | |
|             employee_id = get_delivery.driver_employee_id,
 | |
|             total_deliveries = 0,
 | |
|             total_gallons_delivered = 0,
 | |
|             total_primes = 0,
 | |
|             oil_total_profit_delivered = 0,
 | |
|         )
 | |
|         db.session.add(create_stats)
 | |
|         db.session.flush()
 | |
| 
 | |
|         get_stats_employee = (db.session
 | |
|                  .query(Stats_Employee_Oil)
 | |
|                  .filter(Stats_Employee_Oil.employee_id == get_delivery.driver_employee_id)
 | |
|                  .first())
 | |
|         
 | |
| 
 | |
|  
 | |
|     customer_filled = request.json["customer_filled"]
 | |
|     if customer_filled is True:
 | |
|         customer_filled = 1
 | |
|     else:
 | |
|         customer_filled = 0
 | |
| 
 | |
|         
 | |
| 
 | |
|     if request.json["cash_recieved"]:
 | |
|         cash_amount = request.json["cash_recieved"]
 | |
|     else:
 | |
|         cash_amount = None
 | |
| 
 | |
| 
 | |
| 
 | |
|     gallons_delivered = request.json["gallons_delivered"]
 | |
|     check_number = request.json["check_number"]
 | |
|     fill_location = request.json["fill_location"]
 | |
| 
 | |
| 
 | |
|     # update driver 
 | |
|     get_delivery.driver_last_name = get_driver.employee_last_name
 | |
|     get_delivery.driver_first_name = get_driver.employee_first_name
 | |
|     get_delivery.driver_employee_id = get_driver.id
 | |
| 
 | |
|     # update delivery
 | |
|     get_delivery.when_delivered = now
 | |
|     get_delivery.gallons_delivered = gallons_delivered
 | |
|     get_delivery.cash_recieved = cash_amount
 | |
|     get_delivery.check_number = check_number
 | |
|     get_delivery.delivery_status = 10
 | |
|     
 | |
|     # update stats employee
 | |
|     current_deliveres = get_stats_employee.total_deliveries + 1
 | |
|     get_stats_employee.total_deliveries = current_deliveres
 | |
|     
 | |
|     current_gallons_delivered = Decimal(get_stats_employee.total_gallons_delivered) + Decimal(gallons_delivered)
 | |
|     get_stats_employee.total_gallons_delivered = current_gallons_delivered
 | |
| 
 | |
|     # update stats customer
 | |
|     current_deliveres = int(get_stats_customer.oil_deliveries) + 1
 | |
|     get_stats_customer.oil_deliveries = current_deliveres
 | |
| 
 | |
|     new_gallons = Decimal(get_stats_customer.oil_total_gallons) + Decimal(gallons_delivered)
 | |
|     get_stats_customer.oil_total_gallons = new_gallons
 | |
| 
 | |
| 
 | |
|     # update fill location
 | |
|     get_customer_description.fill_location = fill_location
 | |
| 
 | |
|     db.session.add(get_customer_description)
 | |
|     db.session.add(get_stats_customer)
 | |
|     db.session.add(get_stats_employee)
 | |
|     db.session.add(get_delivery)
 | |
| 
 | |
|     db.session.commit()
 | |
| 
 | |
| 
 | |
|     return jsonify({
 | |
|         "ok": True,
 | |
|         'delivery': {
 | |
|             'id': get_delivery.id,
 | |
|         },
 | |
|     }), 200
 | |
| 
 | |
| 
 |