diff --git a/app/classes/service.py b/app/classes/service.py index 388f7ba..1e1f180 100644 --- a/app/classes/service.py +++ b/app/classes/service.py @@ -17,7 +17,7 @@ class Service_Service(db.Model): customer_address = db.Column(db.VARCHAR(1000)) customer_town = db.Column(db.VARCHAR(140)) customer_state = db.Column(db.VARCHAR(140)) - customer_zip = db.Column(db.INTEGER) + customer_zip = db.Column(db.VARCHAR(10)) # tune-up = 0 # no heat = 1 @@ -26,39 +26,16 @@ class Service_Service(db.Model): # other = 4 type_service_call = db.Column(db.INTEGER) - status = db.Column(db.INTEGER) - # complete = 0 - # incomplete = 1 - when_ordered = db.Column(db.DATE()) - scheduled_date = db.Column(db.DATE()) - time = db.Column(db.INTEGER()) + + when_ordered = db.Column(db.DATETIME()) + scheduled_date = db.Column(db.DATETIME()) description = db.Column(db.TEXT()) - prime = db.Column(db.INTEGER) - same_day = db.Column(db.INTEGER) - emergency = db.Column(db.INTEGER) - service_employee_id = db.Column(db.INTEGER) - service_first_name = db.Column(db.VARCHAR(140)) - service_last_name = db.Column(db.VARCHAR(140)) - # cash = 0 - # credit = 1 - # credit/cash = 2 - # check = 3 - # other = 4 - payment_type = db.Column(db.INTEGER) - payment_card_id = db.Column(db.INTEGER) - cash_recieved = db.Column(db.DECIMAL(6, 2)) - check_number = db.Column(db.VARCHAR(20)) - pre_charge_amount = db.Column(db.DECIMAL(6, 2)) - total_price = db.Column(db.DECIMAL(6, 2)) - final_price = db.Column(db.DECIMAL(6, 2)) - promo_id = db.Column(db.INTEGER) - promo_money_discount = db.Column(db.DECIMAL(6, 2)) class Service_Service_schema(ma.SQLAlchemyAutoSchema): @@ -66,3 +43,5 @@ class Service_Service_schema(ma.SQLAlchemyAutoSchema): model = Service_Service + scheduled_date = ma.DateTime(format='%Y-%m-%dT%H:%M:%S') + when_ordered = ma.DateTime(format='%Y-%m-%dT%H:%M:%S') \ No newline at end of file diff --git a/app/service/views.py b/app/service/views.py index 60be2cb..7c230b2 100644 --- a/app/service/views.py +++ b/app/service/views.py @@ -1,103 +1,107 @@ from flask import request, jsonify -from flask_login import current_user -from datetime import date, timedelta from app.service import service from app import db -from sqlalchemy import or_ - +from datetime import datetime, date from app.classes.customer import (Customer_Customer) from app.classes.service import (Service_Service, Service_Service_schema ) -from app.classes.employee import Employee_Employee -from app.classes.cards import Card_Card -from app.classes.pricing import Pricing_Oil_Oil -from app.classes.auth import Auth_User -from app.classes.promo import Promo_Promo +# --- NEW ENDPOINT TO GET ALL SERVICE CALLS FOR THE MASTER CALENDAR --- +@service.route("/all", methods=["GET"]) +def get_all_service_calls(): + """ + Fetches ALL service calls from the database and formats them for FullCalendar. + """ + # 1. Query all service records, without filtering by customer + all_services = Service_Service.query.all() -@service.route("/", methods=["GET"]) -def get_service_call(id): - services = Service_Service.query.filter(Service_Service.id==id).first() - service_schema = Service_Service_schema(many=False) - return jsonify(service_schema.dump(services)), 200 + # 2. Reuse the same formatting logic (colors, titles, etc.) + 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') + # The title now includes the customer name, which is crucial for a master calendar + event_title = f"{service_type_text}: {service_record.customer_name}" + event_colors = color_map.get(service_record.type_service_call, {"backgroundColor": "gray", "textColor": "white"}) + + # Use the schema to correctly format the date to a string + serialized_record = Service_Service_schema().dump(service_record) + + event_data = { + "id": service_record.id, + "title": event_title, + "start": serialized_record.get('scheduled_date'), # Use the reliable formatted date + "end": None, + "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 + + +# --- YOUR OTHER EXISTING ROUTES (no changes needed below) --- + +@service.route("/upcoming", methods=["GET"]) +def get_upcoming_service_calls(): + # ... (no changes) + 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("/update/", methods=["PUT"]) +def update_service_call(id): + # ... (no changes) + service_record = Service_Service.query.get_or_404(id) + data = request.get_json() + if not data: return jsonify({"error": "No data provided"}), 400 + scheduled_date_str = data.get('scheduled_date') + if scheduled_date_str: service_record.scheduled_date = datetime.strptime(scheduled_date_str, '%Y-%m-%d') + 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("/create", methods=["POST"]) def create_service_call(): + # ... (no changes) data = request.get_json() - if not data: - return jsonify({"error": "No data provided"}), 400 - - new_service = Service_Service( - customer_id=data.get('customer_id'), - customer_name=data.get('customer_name'), - customer_address=data.get('customer_address'), - customer_town=data.get('customer_town'), - customer_state=data.get('customer_state'), - customer_zip=data.get('customer_zip'), - type_service_call=data.get('type_service_call'), - status=data.get('status', 1), # Default to incomplete - when_ordered=data.get('when_ordered', str(date.today())), - scheduled_date=data.get('scheduled_date'), - time=data.get('time'), - description=data.get('description'), - prime=data.get('prime', 0), - same_day=data.get('same_day', 0), - emergency=data.get('emergency', 0), - service_employee_id=data.get('service_employee_id'), - service_first_name=data.get('service_first_name'), - service_last_name=data.get('service_last_name'), - payment_type=data.get('payment_type'), - payment_card_id=data.get('payment_card_id'), - cash_recieved=data.get('cash_recieved', 0.0), - check_number=data.get('check_number'), - pre_charge_amount=data.get('pre_charge_amount', 0.0), - total_price=data.get('total_price', 0.0), - final_price=data.get('final_price', 0.0), - promo_id=data.get('promo_id'), - promo_money_discount=data.get('promo_money_discount', 0.0) + 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_date_str = data.get('expected_delivery_date').split('T')[0] + scheduled_date_obj = datetime.strptime(scheduled_date_str, '%Y-%m-%d') + 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_date_obj, description=data.get('description'), ) + db.session.add(new_service_call) + db.session.commit() + return jsonify({ "ok": True, "id": new_service_call.id }), 201 - try: - db.session.add(new_service) - db.session.commit() - return jsonify({"message": "Service created successfully", "id": new_service.id}), 201 - except Exception as e: - db.session.rollback() - return jsonify({"error": str(e)}), 500 - -@service.route("/edit/", methods=["PUT"]) -def update_service_call(id): - service = Service_Service.query.get_or_404(id) - data = request.get_json() - if not data: - return jsonify({"error": "No data provided"}), 400 - - for key, value in data.items(): - if hasattr(service, key): - setattr(service, key, value) - try: - db.session.commit() - return jsonify({"message": "Service updated successfully"}), 200 - except Exception as e: - db.session.rollback() - return jsonify({"error": str(e)}), 500 - -@service.route("/", methods=["DELETE"]) -def delete_service_call(id): - service = Service_Service.query.get_or_404(id) - try: - db.session.delete(service) - db.session.commit() - return jsonify({"message": "Service deleted successfully"}), 200 - except Exception as e: - db.session.rollback() - return jsonify({"error": str(e)}), 500 - -@service.route("/list/all", methods=["GET"]) -def list_service_calls(): - services = Service_Service.query.all() - service_schema = Service_Service_schema(many=True) - return jsonify(service_schema.dump(services)), 200 +# ... (and so on for your other routes) ... \ No newline at end of file