- New models: WarrantyModel, WalletModel, SystemConfigModel - New schemas: WarrantySchema, RefundSchema, SystemConfig, CrmTaskCalendar, NotificationTemplate, NotificationGateway, RepairErp, ReviewModeration, InventoryPos, MultiDashboard, BillingFinance - New services: WarrantyService, RefundService, CrmService, CrmTaskCalendarService, DeliveryService, EmailTemplateService, InventoryPosService, MultiDashboardService, NotificationGatewayService, RepairErpService, ReviewModerationService, SystemConfigService, TemplateService, CreditService - Updated routers: AdminOrder, BillingFinance, Cart, Catalog, Checkout, Crm, InventoryPos, NotificationGateway, RolePermission, ServiceJob, Settings, Storefront, UserCreation - Updated models: Inventory, Notification, Order, Product, Service, Setting, SystemConfig, User - Updated core: database init, invoice generator, main app - Added WarrantyRouter with full CRUD and claim flow
133 lines
4.7 KiB
Python
133 lines
4.7 KiB
Python
from sqlalchemy.orm import Session
|
|
from sqlalchemy import select
|
|
from sqlalchemy.exc import IntegrityError
|
|
from datetime import datetime, timedelta, timezone
|
|
from fastapi import HTTPException
|
|
import ulid
|
|
|
|
from app.models.WarrantyModel import Warranty
|
|
from app.models.OrderModel import Order, OrderItem
|
|
from app.models.ProductModel import Product
|
|
from app.models.ServiceModel import ServiceJob
|
|
from app.schemas.WarrantySchema import WarrantyCreate
|
|
|
|
class WarrantyService:
|
|
|
|
@staticmethod
|
|
def generate_warranty_for_order(db: Session, order_id: str):
|
|
"""
|
|
Triggered when an order becomes DELIVERED.
|
|
Creates a warranty for each order item if the product has a warranty period.
|
|
"""
|
|
order = db.get(Order, order_id)
|
|
if not order:
|
|
return
|
|
|
|
items = db.execute(select(OrderItem).where(OrderItem.order_id == order_id)).scalars().all()
|
|
created_count = 0
|
|
|
|
for item in items:
|
|
# We assume product might have a 'warranty_months' or similar.
|
|
# For this MVP, we give 12 months for products if not specified.
|
|
warranty_months = 12
|
|
|
|
# Find serial number or IMEI from the item (assume it's stored in metadata if available)
|
|
serial_no = None
|
|
|
|
start_date = datetime.now(timezone.utc)
|
|
end_date = start_date + timedelta(days=30 * warranty_months)
|
|
|
|
warranty = Warranty(
|
|
warranty_id=str(ulid.ULID()),
|
|
tenant_id="tenant_default",
|
|
customer_id=order.customer_id or "anonymous",
|
|
reference_type="ORDER",
|
|
reference_id=order.order_id,
|
|
product_id=item.product_id,
|
|
order_item_id=item.item_id,
|
|
serial_no_imei=serial_no,
|
|
start_date=start_date,
|
|
end_date=end_date,
|
|
status="ACTIVE",
|
|
warranty_type="PRODUCT",
|
|
terms=f"{warranty_months}-month standard manufacturer warranty."
|
|
)
|
|
|
|
db.add(warranty)
|
|
try:
|
|
db.commit()
|
|
created_count += 1
|
|
except IntegrityError:
|
|
db.rollback() # Duplicate generation prevented by unique constraint
|
|
|
|
return created_count
|
|
|
|
@staticmethod
|
|
def generate_warranty_for_service(db: Session, job_id: str):
|
|
"""
|
|
Triggered when a service job becomes DELIVERED/COMPLETED.
|
|
"""
|
|
job = db.get(ServiceJob, job_id)
|
|
if not job:
|
|
return
|
|
|
|
start_date = datetime.now(timezone.utc)
|
|
# Default 90 days for service jobs
|
|
end_date = start_date + timedelta(days=90)
|
|
|
|
warranty = Warranty(
|
|
warranty_id=str(ulid.ULID()),
|
|
tenant_id=job.tenant_id,
|
|
customer_id=job.customer_id,
|
|
reference_type="SERVICE",
|
|
reference_id=job.job_id,
|
|
service_job_id=job.job_id,
|
|
serial_no_imei=job.serial_number_or_imei if hasattr(job, 'serial_number_or_imei') else None,
|
|
start_date=start_date,
|
|
end_date=end_date,
|
|
status="ACTIVE",
|
|
warranty_type="SERVICE",
|
|
terms="90-day service warranty on replaced parts and labor."
|
|
)
|
|
|
|
db.add(warranty)
|
|
try:
|
|
db.commit()
|
|
return 1
|
|
except IntegrityError:
|
|
db.rollback()
|
|
return 0
|
|
|
|
@staticmethod
|
|
def void_warranty(db: Session, warranty_id: str, void_reason: str):
|
|
warranty = db.get(Warranty, warranty_id)
|
|
if not warranty:
|
|
raise HTTPException(status_code=404, detail="Warranty not found")
|
|
|
|
if warranty.status != "ACTIVE":
|
|
raise HTTPException(status_code=400, detail=f"Cannot void warranty with status: {warranty.status}")
|
|
|
|
warranty.status = "VOID"
|
|
warranty.void_reason = void_reason
|
|
db.commit()
|
|
db.refresh(warranty)
|
|
return warranty
|
|
|
|
@staticmethod
|
|
def validate_claim(db: Session, warranty_id: str):
|
|
warranty = db.get(Warranty, warranty_id)
|
|
if not warranty:
|
|
raise HTTPException(status_code=404, detail="Warranty not found")
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
if warranty.status != "ACTIVE":
|
|
raise HTTPException(status_code=400, detail="Warranty is not ACTIVE")
|
|
|
|
if warranty.start_date > now:
|
|
raise HTTPException(status_code=400, detail="Warranty has not started yet")
|
|
|
|
if warranty.end_date < now:
|
|
raise HTTPException(status_code=400, detail="Warranty has expired")
|
|
|
|
return {"valid": True, "warranty": warranty}
|