Added service plan. Password change
This commit is contained in:
@@ -4,7 +4,8 @@ from app import db
|
||||
from datetime import datetime, date, timedelta
|
||||
from app.classes.customer import (Customer_Customer)
|
||||
from app.classes.service import (Service_Service,
|
||||
Service_Service_schema, Service_Parts, Service_Parts_schema
|
||||
Service_Service_schema, Service_Parts, Service_Parts_schema,
|
||||
Service_Plans, Service_Plans_schema
|
||||
)
|
||||
|
||||
|
||||
@@ -173,6 +174,120 @@ def update_service_call(id):
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
# Service Plans CRUD endpoints
|
||||
@service.route("/plans/active", methods=["GET"])
|
||||
def get_active_service_plans():
|
||||
"""
|
||||
Get all active service plans (contract_plan > 0)
|
||||
"""
|
||||
try:
|
||||
plans = Service_Plans.query.filter(Service_Plans.contract_plan > 0).all()
|
||||
plans_schema = Service_Plans_schema(many=True)
|
||||
result = plans_schema.dump(plans)
|
||||
|
||||
# Add customer info to each plan
|
||||
for plan in result:
|
||||
customer = Customer_Customer.query.get(plan['customer_id'])
|
||||
if customer:
|
||||
plan['customer_name'] = f"{customer.customer_first_name} {customer.customer_last_name}"
|
||||
plan['customer_address'] = customer.customer_address
|
||||
plan['customer_town'] = customer.customer_town
|
||||
|
||||
return jsonify(result), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@service.route("/plans/customer/<int:customer_id>", methods=["GET"])
|
||||
def get_customer_service_plan(customer_id):
|
||||
"""
|
||||
Get service plan for a specific customer
|
||||
"""
|
||||
try:
|
||||
plan = Service_Plans.query.filter_by(customer_id=customer_id).first()
|
||||
if plan:
|
||||
plan_schema = Service_Plans_schema()
|
||||
return jsonify(plan_schema.dump(plan)), 200
|
||||
else:
|
||||
return jsonify(None), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@service.route("/plans/create", methods=["POST"])
|
||||
def create_service_plan():
|
||||
"""
|
||||
Create a new service plan for a customer
|
||||
"""
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "No data provided"}), 400
|
||||
|
||||
try:
|
||||
new_plan = Service_Plans(
|
||||
customer_id=data['customer_id'],
|
||||
contract_plan=data['contract_plan'],
|
||||
contract_years=data['contract_years'],
|
||||
contract_start_date=datetime.fromisoformat(data['contract_start_date'])
|
||||
)
|
||||
db.session.add(new_plan)
|
||||
db.session.commit()
|
||||
|
||||
plan_schema = Service_Plans_schema()
|
||||
return jsonify({"ok": True, "plan": plan_schema.dump(new_plan)}), 201
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@service.route("/plans/update/<int:customer_id>", methods=["PUT"])
|
||||
def update_service_plan(customer_id):
|
||||
"""
|
||||
Update existing service plan for a customer
|
||||
"""
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "No data provided"}), 400
|
||||
|
||||
try:
|
||||
plan = Service_Plans.query.filter_by(customer_id=customer_id).first()
|
||||
if not plan:
|
||||
# Create new plan if it doesn't exist
|
||||
plan = Service_Plans(customer_id=customer_id)
|
||||
db.session.add(plan)
|
||||
|
||||
plan.contract_plan = data.get('contract_plan', plan.contract_plan)
|
||||
plan.contract_years = data.get('contract_years', plan.contract_years)
|
||||
if data.get('contract_start_date'):
|
||||
plan.contract_start_date = datetime.fromisoformat(data['contract_start_date'])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
plan_schema = Service_Plans_schema()
|
||||
return jsonify({"ok": True, "plan": plan_schema.dump(plan)}), 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@service.route("/plans/delete/<int:customer_id>", methods=["DELETE"])
|
||||
def delete_service_plan(customer_id):
|
||||
"""
|
||||
Delete service plan for a customer
|
||||
"""
|
||||
try:
|
||||
plan = Service_Plans.query.filter_by(customer_id=customer_id).first()
|
||||
if not plan:
|
||||
return jsonify({"error": "Service plan not found"}), 404
|
||||
|
||||
db.session.delete(plan)
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True, "message": "Service plan deleted successfully"}), 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)
|
||||
|
||||
Reference in New Issue
Block a user