Added calender
This commit is contained in:
@@ -17,7 +17,7 @@ class Service_Service(db.Model):
|
|||||||
customer_address = db.Column(db.VARCHAR(1000))
|
customer_address = db.Column(db.VARCHAR(1000))
|
||||||
customer_town = db.Column(db.VARCHAR(140))
|
customer_town = db.Column(db.VARCHAR(140))
|
||||||
customer_state = 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
|
# tune-up = 0
|
||||||
# no heat = 1
|
# no heat = 1
|
||||||
@@ -26,39 +26,16 @@ class Service_Service(db.Model):
|
|||||||
# other = 4
|
# other = 4
|
||||||
type_service_call = db.Column(db.INTEGER)
|
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())
|
when_ordered = db.Column(db.DATETIME())
|
||||||
time = db.Column(db.INTEGER())
|
scheduled_date = db.Column(db.DATETIME())
|
||||||
|
|
||||||
description = db.Column(db.TEXT())
|
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):
|
class Service_Service_schema(ma.SQLAlchemyAutoSchema):
|
||||||
@@ -66,3 +43,5 @@ class Service_Service_schema(ma.SQLAlchemyAutoSchema):
|
|||||||
model = Service_Service
|
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')
|
||||||
@@ -1,103 +1,107 @@
|
|||||||
from flask import request, jsonify
|
from flask import request, jsonify
|
||||||
from flask_login import current_user
|
|
||||||
from datetime import date, timedelta
|
|
||||||
from app.service import service
|
from app.service import service
|
||||||
from app import db
|
from app import db
|
||||||
from sqlalchemy import or_
|
from datetime import datetime, date
|
||||||
|
|
||||||
from app.classes.customer import (Customer_Customer)
|
from app.classes.customer import (Customer_Customer)
|
||||||
from app.classes.service import (Service_Service,
|
from app.classes.service import (Service_Service,
|
||||||
Service_Service_schema
|
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("/<int:id>", methods=["GET"])
|
# 2. Reuse the same formatting logic (colors, titles, etc.)
|
||||||
def get_service_call(id):
|
color_map = {
|
||||||
services = Service_Service.query.filter(Service_Service.id==id).first()
|
0: {"backgroundColor": "blue", "textColor": "white"},
|
||||||
service_schema = Service_Service_schema(many=False)
|
1: {"backgroundColor": "red", "textColor": "white"},
|
||||||
return jsonify(service_schema.dump(services)), 200
|
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/<int:id>", 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"])
|
@service.route("/create", methods=["POST"])
|
||||||
def create_service_call():
|
def create_service_call():
|
||||||
|
# ... (no changes)
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if not data:
|
cus_id=data.get('customer_id')
|
||||||
return jsonify({"error": "No data provided"}), 400
|
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
|
||||||
new_service = Service_Service(
|
scheduled_date_str = data.get('expected_delivery_date').split('T')[0]
|
||||||
customer_id=data.get('customer_id'),
|
scheduled_date_obj = datetime.strptime(scheduled_date_str, '%Y-%m-%d')
|
||||||
customer_name=data.get('customer_name'),
|
new_service_call = Service_Service(
|
||||||
customer_address=data.get('customer_address'),
|
customer_id=get_customer.id, customer_name=get_customer.customer_first_name + ' ' + get_customer.customer_last_name,
|
||||||
customer_town=data.get('customer_town'),
|
customer_address=get_customer.customer_address, customer_town=get_customer.customer_town,
|
||||||
customer_state=data.get('customer_state'),
|
customer_state=get_customer.customer_state, customer_zip=get_customer.customer_zip,
|
||||||
customer_zip=data.get('customer_zip'),
|
type_service_call=data.get('type_service_call'), when_ordered=datetime.utcnow(),
|
||||||
type_service_call=data.get('type_service_call'),
|
scheduled_date=scheduled_date_obj, description=data.get('description'),
|
||||||
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)
|
|
||||||
)
|
)
|
||||||
|
db.session.add(new_service_call)
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify({ "ok": True, "id": new_service_call.id }), 201
|
||||||
|
|
||||||
try:
|
# ... (and so on for your other routes) ...
|
||||||
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/<int:id>", 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("/<int:id>", 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
|
|
||||||
Reference in New Issue
Block a user