158 lines
7.1 KiB
Python
158 lines
7.1 KiB
Python
from flask import request, jsonify
|
|
from app.service import service
|
|
from app import db
|
|
from datetime import datetime, date
|
|
from app.classes.customer import (Customer_Customer)
|
|
from app.classes.service import (Service_Service,
|
|
Service_Service_schema, Service_Parts, Service_Parts_schema
|
|
)
|
|
|
|
|
|
@service.route("/all", methods=["GET"])
|
|
def get_all_service_calls():
|
|
all_services = Service_Service.query.all()
|
|
color_map = {
|
|
0: {"backgroundColor": "blue", "textColor": "white"}, 1: {"backgroundColor": "red", "textColor": "white"},
|
|
2: {"backgroundColor": "green", "textColor": "white"}, 3: {"backgroundColor": "yellow", "textColor": "black"},
|
|
4: {"backgroundColor": "black", "textColor": "white"}
|
|
}
|
|
service_type_map = {0: 'Tune-up', 1: 'No Heat', 2: 'Fix', 3: 'Tank Install', 4: 'Other'}
|
|
calendar_events = []
|
|
for service_record in all_services:
|
|
service_type_text = service_type_map.get(service_record.type_service_call, 'Service')
|
|
event_title = f"{service_type_text}: {service_record.customer_name}"
|
|
event_colors = color_map.get(service_record.type_service_call, {"backgroundColor": "gray", "textColor": "white"})
|
|
serialized_record = Service_Service_schema().dump(service_record)
|
|
event_data = {
|
|
"id": service_record.id, "title": event_title, "start": serialized_record.get('scheduled_date'),
|
|
"end": None, "customer_id": service_record.customer_id,
|
|
"extendedProps": {
|
|
"description": service_record.description,
|
|
"type_service_call": service_record.type_service_call,
|
|
},
|
|
"backgroundColor": event_colors.get("backgroundColor"), "textColor": event_colors.get("textColor"),
|
|
"borderColor": event_colors.get("backgroundColor")
|
|
}
|
|
calendar_events.append(event_data)
|
|
return jsonify(calendar_events), 200
|
|
|
|
|
|
# --- THIS IS THE FIX ---
|
|
# The logic from /all has been copied here to ensure a consistent data structure.
|
|
@service.route("/upcoming", methods=["GET"])
|
|
def get_upcoming_service_calls():
|
|
"""
|
|
Fetches a list of all future service calls from today onwards.
|
|
"""
|
|
now = datetime.now()
|
|
upcoming_services = (
|
|
Service_Service.query
|
|
.filter(Service_Service.scheduled_date >= now)
|
|
.order_by(Service_Service.scheduled_date.asc())
|
|
.limit(100)
|
|
.all()
|
|
)
|
|
|
|
service_schema = Service_Service_schema(many=True)
|
|
result = service_schema.dump(upcoming_services)
|
|
|
|
|
|
return jsonify(result), 200
|
|
|
|
|
|
@service.route("/upcoming/count", methods=["GET"])
|
|
def get_upcoming_service_calls_count():
|
|
now = datetime.now()
|
|
try:
|
|
count = (db.session.query(Service_Service).filter(Service_Service.scheduled_date >= now).count())
|
|
return jsonify({"count": count}), 200
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@service.route("/for-customer/<int:customer_id>", methods=["GET"])
|
|
def get_service_calls_for_customer(customer_id):
|
|
service_records = (Service_Service.query.filter_by(customer_id=customer_id).order_by(Service_Service.scheduled_date.desc()).all())
|
|
service_schema = Service_Service_schema(many=True)
|
|
result = service_schema.dump(service_records)
|
|
return jsonify(result), 200
|
|
|
|
@service.route("/create", methods=["POST"])
|
|
def create_service_call():
|
|
data = request.get_json()
|
|
if not data: return jsonify({"error": "No data provided"}), 400
|
|
cus_id=data.get('customer_id')
|
|
get_customer = (db.session.query(Customer_Customer).filter(Customer_Customer.id == cus_id).first())
|
|
if not get_customer: return jsonify({"error": f"Customer with id {cus_id} not found."}), 404
|
|
scheduled_datetime_str = data.get('expected_delivery_date')
|
|
scheduled_datetime_obj = datetime.fromisoformat(scheduled_datetime_str)
|
|
new_service_call = Service_Service(
|
|
customer_id=get_customer.id, customer_name=get_customer.customer_first_name + ' ' + get_customer.customer_last_name,
|
|
customer_address=get_customer.customer_address, customer_town=get_customer.customer_town,
|
|
customer_state=get_customer.customer_state, customer_zip=get_customer.customer_zip,
|
|
type_service_call=data.get('type_service_call'), when_ordered=datetime.utcnow(),
|
|
scheduled_date=scheduled_datetime_obj, description=data.get('description'),
|
|
)
|
|
db.session.add(new_service_call)
|
|
db.session.commit()
|
|
return jsonify({ "ok": True, "id": new_service_call.id }), 201
|
|
|
|
@service.route("/update/<int:id>", methods=["PUT"])
|
|
def update_service_call(id):
|
|
service_record = Service_Service.query.get_or_404(id)
|
|
data = request.get_json()
|
|
if not data: return jsonify({"error": "No data provided"}), 400
|
|
scheduled_datetime_str = data.get('scheduled_date')
|
|
if scheduled_datetime_str:
|
|
service_record.scheduled_date = datetime.fromisoformat(scheduled_datetime_str)
|
|
service_record.type_service_call = data.get('type_service_call', service_record.type_service_call)
|
|
service_record.description = data.get('description', service_record.description)
|
|
try:
|
|
db.session.commit()
|
|
service_schema = Service_Service_schema(many=False)
|
|
return jsonify({"ok": True, "service": service_schema.dump(service_record)}), 200
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@service.route("/delete/<int:id>", methods=["DELETE"])
|
|
def delete_service_call(id):
|
|
service_record = Service_Service.query.get_or_404(id)
|
|
try:
|
|
db.session.delete(service_record)
|
|
db.session.commit()
|
|
return jsonify({"ok": True, "message": "Service deleted successfully"}), 200
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@service.route("/parts/customer/<int:customer_id>", methods=["GET"])
|
|
def get_service_parts(customer_id):
|
|
parts = Service_Parts.query.filter_by(customer_id=customer_id).first()
|
|
if parts:
|
|
parts_schema = Service_Parts_schema()
|
|
return jsonify(parts_schema.dump(parts)), 200
|
|
else:
|
|
return jsonify({
|
|
"customer_id": customer_id, "oil_filter": "", "oil_filter_2": "",
|
|
"oil_nozzle": "", "oil_nozzle_2": ""
|
|
}), 200
|
|
|
|
@service.route("/parts/update/<int:customer_id>", methods=["POST"])
|
|
def update_service_parts(customer_id):
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({"error": "No data provided"}), 400
|
|
parts = Service_Parts.query.filter_by(customer_id=customer_id).first()
|
|
if not parts:
|
|
parts = Service_Parts(customer_id=customer_id)
|
|
db.session.add(parts)
|
|
parts.oil_filter = data.get('oil_filter', parts.oil_filter)
|
|
parts.oil_filter_2 = data.get('oil_filter_2', parts.oil_filter_2)
|
|
parts.oil_nozzle = data.get('oil_nozzle', parts.oil_nozzle)
|
|
parts.oil_nozzle_2 = data.get('oil_nozzle_2', parts.oil_nozzle_2)
|
|
try:
|
|
db.session.commit()
|
|
return jsonify({"ok": True, "message": "Service parts updated successfully"}), 200
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({"error": str(e)}), 500 |