107 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| 
 | |
| from fastapi import APIRouter, Request
 | |
| from datetime import  date
 | |
| from database import session
 | |
| from pyowm import OWM
 | |
| from decimal import Decimal
 | |
| from app.models.auto import Auto_Delivery,  Tickets_Auto_Delivery
 | |
| from app.models.pricing import Pricing_Oil_Oil
 | |
| 
 | |
| 
 | |
| 
 | |
| from app.script.update_auto import calc_home_factor
 | |
| 
 | |
| router = APIRouter(
 | |
|     prefix="/confirm",
 | |
|     tags=["confirm"],
 | |
|     responses={404: {"description": "Not found"}},
 | |
| )
 | |
| 
 | |
| 
 | |
| @router.put("/auto/update/{autoid}")
 | |
| async def update_auto(autoid: int, request: Request):
 | |
| 
 | |
|     request_body = await request.json()
 | |
|     gallons_delivered = request_body['gallons_delivered']
 | |
|     gallons_delivered = Decimal(gallons_delivered)
 | |
| 
 | |
| 
 | |
|     get_auto_delivery = (session 
 | |
|         .query(Auto_Delivery) 
 | |
|         .filter(Auto_Delivery.id == autoid) 
 | |
|         .first())
 | |
|     
 | |
|     gallons_put_in_home = Decimal(gallons_delivered)
 | |
| 
 | |
|     customer_home_factor = get_auto_delivery.house_factor
 | |
| 
 | |
| 
 | |
|     new_home_factor = calc_home_factor(gallons_put_in_home = gallons_put_in_home,
 | |
|                                         current_house_factor=customer_home_factor)
 | |
|     
 | |
|     gallons_left_buffer = int(get_auto_delivery.tank_size) - 30
 | |
| 
 | |
| 
 | |
|     get_auto_delivery.house_factor = new_home_factor
 | |
|     get_auto_delivery.tank_height = 'Full'
 | |
|     get_auto_delivery.last_fill = date.today()
 | |
|     get_auto_delivery.estimated_gallons_left = gallons_left_buffer
 | |
|     get_auto_delivery.estimated_gallons_left_prev_day = gallons_left_buffer
 | |
|     get_auto_delivery.auto_status = 1
 | |
|     get_auto_delivery.days_since_last_fill = 0
 | |
| 
 | |
|     session.add(get_auto_delivery)
 | |
| 
 | |
|     session.commit()
 | |
|     
 | |
|     return ({"ok": True}), 200
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| @router.post("/auto/create/{autoid}")
 | |
| async def create_auto_ticket(autoid: int, request: Request):
 | |
|     
 | |
| 
 | |
|     request_body = await request.json()
 | |
|     gallons_delivered = request_body['gallons_delivered']
 | |
|     gallons_delivered = Decimal(gallons_delivered)
 | |
| 
 | |
| 
 | |
|     get_auto_delivery = (session 
 | |
|         .query(Auto_Delivery) 
 | |
|         .filter(Auto_Delivery.id == autoid) 
 | |
|         .first())
 | |
|     get_todays_price = (session.query(Pricing_Oil_Oil)
 | |
|                         .order_by(Pricing_Oil_Oil.id.desc())
 | |
|                         .first())
 | |
|     gallons_put_in_home = Decimal(gallons_delivered)
 | |
|     todays_price = Decimal(get_todays_price.price_for_customer)
 | |
|     total_amount = gallons_put_in_home * todays_price
 | |
| 
 | |
|     create_new_ticket = Tickets_Auto_Delivery(
 | |
|         customer_id =  get_auto_delivery.customer_id,
 | |
|         account_number = get_auto_delivery.account_number,
 | |
|         customer_town = get_auto_delivery.customer_town,
 | |
|         customer_state = get_auto_delivery.customer_state,
 | |
|         customer_address = get_auto_delivery.customer_address,
 | |
|         customer_zip =get_auto_delivery.customer_zip,
 | |
|         customer_full_name = get_auto_delivery.customer_full_name,
 | |
|         oil_prices_id = get_todays_price.id,
 | |
|         gallons_delivered = gallons_delivered,
 | |
|         price_per_gallon = get_todays_price.price_for_customer,
 | |
|         total_amount_customer = total_amount,
 | |
|         fill_date = date.today(),
 | |
|     )
 | |
| 
 | |
| 
 | |
|     session.add(create_new_ticket)
 | |
| 
 | |
|     session.commit()
 | |
|     
 | |
|     return ({
 | |
|             "ok": True,
 | |
|              "auto_ticket_id":create_new_ticket.id
 | |
|              }), 200
 | |
| 
 |