ifixkart-admin/Backend/app/services/EmailTemplateService.py
Adithiyan bbd3a3e959 feat: initial commit — full platform snapshot
- Backend: FastAPI app with all models, services, schemas, routes
- Admin Panel: Next.js dashboard with all modules
  (CRM, POS, repairs, warranties, billing, notifications, etc.)
- Ecommerce: storefront (submodule) + product catalog
- AI Agents: .agents/ skill library for Antigravity IDE
- Docs: architecture, billing, notification gateway markdown
- Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
2026-10-13 12:59:47 +05:30

404 lines
24 KiB
Python

from typing import Dict, Any, List
class EmailTemplateService:
@staticmethod
def quotation_email(data: Dict[str, Any]) -> str:
quote_number = data.get("quote_number", "SQ-2026-000")
customer_name = data.get("customer_name", "Valued Customer")
customer_phone = data.get("customer_phone", "N/A")
customer_email = data.get("customer_email", "N/A")
quote_title = data.get("quote_title", "CRM Service & Repair Quotation")
items: List[Dict[str, Any]] = data.get("items", [])
subtotal = float(data.get("subtotal", 0.0))
tax_amount = float(data.get("tax_amount", 0.0))
grand_total = float(data.get("grand_total", 0.0))
terms = data.get("terms", "6-Month IMEI Warranty included. Valid for 15 days from issue date.")
terms_html = terms.replace('\n', '<br>') if isinstance(terms, str) else str(terms)
items_rows_html = ""
for idx, item in enumerate(items, 1):
desc = item.get("description", "Item")
sku = item.get("sku", "PART/SVC")
qty = int(item.get("quantity", item.get("qty", 1)))
unit_price = float(item.get("unit_price", 0.0))
line_total = qty * unit_price
items_rows_html += f"""
<tr style="border-bottom: 1px solid #e2e8f0;">
<td style="padding: 12px 16px; font-weight: 500; color: #1e293b;">{idx}. {desc}<br><small style="color: #64748b;">SKU: {sku}</small></td>
<td style="padding: 12px 16px; text-align: center; color: #334155;">{qty}</td>
<td style="padding: 12px 16px; text-align: right; color: #334155;">₹{unit_price:,.2f}</td>
<td style="padding: 12px 16px; text-align: right; font-weight: 600; color: #0f172a;">₹{line_total:,.2f}</td>
</tr>
"""
html_content = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quotation #{quote_number} - iFixKart</title>
</head>
<body style="margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background-color: #f8fafc; color: #334155;">
<div style="max-width: 680px; margin: 30px auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">
<!-- Brand Header -->
<div style="background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); padding: 32px 40px; color: #ffffff;">
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<h1 style="margin: 0; font-size: 26px; font-weight: 800; letter-spacing: -0.5px; color: #38bdf8;">iFixKart</h1>
<p style="margin: 4px 0 0 0; font-size: 13px; color: #94a3b8;">Unified Enterprise Commerce & Repair Platform</p>
</div>
<div style="text-align: right;">
<span style="background: #0284c7; color: #ffffff; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 700; text-transform: uppercase;">OFFICIAL QUOTATION</span>
</div>
</div>
</div>
<!-- Quotation Details Header -->
<div style="padding: 24px 40px; background: #f1f5f9; border-bottom: 1px solid #e2e8f0;">
<div style="display: table; width: 100%;">
<div style="display: table-cell; width: 50%; vertical-align: top;">
<p style="margin: 0 0 4px 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: #64748b; font-weight: 700;">PREPARED FOR</p>
<p style="margin: 0; font-size: 16px; font-weight: 700; color: #0f172a;">{customer_name}</p>
<p style="margin: 2px 0 0 0; font-size: 13px; color: #475569;">📞 {customer_phone}</p>
{f'<p style="margin: 2px 0 0 0; font-size: 13px; color: #475569;">✉️ {customer_email}</p>' if customer_email and customer_email != 'N/A' else ''}
</div>
<div style="display: table-cell; width: 50%; vertical-align: top; text-align: right;">
<p style="margin: 0 0 4px 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: #64748b; font-weight: 700;">QUOTE REFERENCE</p>
<p style="margin: 0; font-size: 18px; font-weight: 800; color: #0284c7;">{quote_number}</p>
<p style="margin: 2px 0 0 0; font-size: 13px; color: #64748b;">Subject: {quote_title}</p>
</div>
</div>
</div>
<!-- Line Items Table -->
<div style="padding: 32px 40px;">
<table style="width: 100%; border-collapse: collapse; font-size: 14px;">
<thead>
<tr style="background: #f8fafc; border-bottom: 2px solid #cbd5e1; text-align: left;">
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-transform: uppercase; font-size: 11px;">Description / Product / Service</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-transform: uppercase; font-size: 11px; text-align: center;">Qty</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-transform: uppercase; font-size: 11px; text-align: right;">Unit Price</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-transform: uppercase; font-size: 11px; text-align: right;">Amount</th>
</tr>
</thead>
<tbody>
{items_rows_html}
</tbody>
</table>
<!-- Calculation Totals -->
<div style="margin-top: 24px; display: flex; justify-content: flex-end;">
<table style="width: 260px; font-size: 14px; border-collapse: collapse;">
<tr>
<td style="padding: 6px 0; color: #64748b;">Subtotal:</td>
<td style="padding: 6px 0; text-align: right; font-weight: 600; color: #1e293b;">₹{subtotal:,.2f}</td>
</tr>
<tr>
<td style="padding: 6px 0; color: #64748b;">GST Tax (18%):</td>
<td style="padding: 6px 0; text-align: right; font-weight: 600; color: #1e293b;">₹{tax_amount:,.2f}</td>
</tr>
<tr style="border-top: 2px solid #0f172a;">
<td style="padding: 12px 0 0 0; font-size: 16px; font-weight: 800; color: #0f172a;">Grand Total:</td>
<td style="padding: 12px 0 0 0; text-align: right; font-size: 18px; font-weight: 800; color: #0284c7;">₹{grand_total:,.2f}</td>
</tr>
</table>
</div>
<!-- Terms & Conditions Box -->
<div style="margin-top: 32px; background: #f0fdf4; border-left: 4px solid #16a34a; padding: 16px 20px; border-radius: 4px;">
<h4 style="margin: 0 0 6px 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px; color: #15803d; font-weight: 700;">WARRANTY & GUARANTEE TERMS</h4>
<p style="margin: 0; font-size: 13px; color: #166534; line-height: 1.5;">{terms_html}</p>
</div>
</div>
<!-- Footer -->
<div style="background: #f8fafc; padding: 24px 40px; border-top: 1px solid #e2e8f0; text-align: center; font-size: 12px; color: #94a3b8;">
<p style="margin: 0 0 4px 0;">iFixKart Unified Commerce & Enterprise Service Platform</p>
<p style="margin: 0;">For queries regarding this quotation, reply to this email or call our support desk.</p>
</div>
</div>
</body>
</html>"""
return html_content
@staticmethod
def invoice_email(data: Dict[str, Any]) -> str:
invoice_number = data.get("invoice_number", "INV-2026-000")
customer_name = data.get("customer_name", "Valued Customer")
grand_total = float(data.get("grand_total", 0.0))
payment_status = data.get("payment_status", "PAID")
return f"""
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; background-color: #f8fafc; padding: 20px;">
<div style="max-width: 600px; margin: 0 auto; background: #ffffff; padding: 30px; border-radius: 8px; border: 1px solid #e2e8f0;">
<h2 style="color: #0f172a; margin-top: 0;">iFixKart Tax Invoice - #{invoice_number}</h2>
<p>Hello <strong>{customer_name}</strong>,</p>
<p>Thank you for choosing iFixKart! Your GST invoice has been generated.</p>
<div style="background: #f1f5f9; padding: 15px; border-radius: 6px; margin: 20px 0;">
<p style="margin: 5px 0;"><strong>Invoice Number:</strong> {invoice_number}</p>
<p style="margin: 5px 0;"><strong>Amount Paid:</strong> ₹{grand_total:,.2f}</p>
<p style="margin: 5px 0;"><strong>Payment Status:</strong> <span style="color: #16a34a; font-weight: bold;">{payment_status}</span></p>
</div>
<p style="font-size: 12px; color: #64748b;">All repairs come with standard iFixKart warranty coverage.</p>
</div>
</body>
</html>
"""
@staticmethod
def repair_ticket_email(data: Dict[str, Any]) -> str:
ticket_number = data.get("ticket_number", "REP-2026-000")
customer_name = data.get("customer_name", "Valued Customer")
device = data.get("device", "Mobile Device")
issue = data.get("issue", "Repair Inspection")
status = data.get("status", "IN_DIAGNOSIS")
return f"""
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; background-color: #f8fafc; padding: 20px;">
<div style="max-width: 600px; margin: 0 auto; background: #ffffff; padding: 30px; border-radius: 8px; border: 1px solid #e2e8f0;">
<h2 style="color: #0284c7; margin-top: 0;">iFixKart Repair Ticket #{ticket_number}</h2>
<p>Hello <strong>{customer_name}</strong>,</p>
<p>Your repair booking status has been updated.</p>
<ul>
<li><strong>Device:</strong> {device}</li>
<li><strong>Reported Issue:</strong> {issue}</li>
<li><strong>Current Status:</strong> <span style="color: #0284c7; font-weight: bold;">{status}</span></li>
</ul>
<p style="font-size: 12px; color: #64748b;">You can track real-time progress on your iFixKart account portal.</p>
</div>
</body>
</html>
"""
@staticmethod
def lead_qualification_email(data: Dict[str, Any]) -> str:
customer_name = data.get("customer_name", "Valued Client")
lead_title = data.get("lead_title", "Enterprise Requirement")
assigned_agent = data.get("assigned_agent", "Support Desk")
return f"""
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; background-color: #f8fafc; padding: 20px;">
<div style="max-width: 600px; margin: 0 auto; background: #ffffff; padding: 30px; border-radius: 8px; border: 1px solid #e2e8f0;">
<h2 style="color: #0f172a; margin-top: 0;">Requirement Qualified - iFixKart CRM</h2>
<p>Hello <strong>{customer_name}</strong>,</p>
<p>Your requirement (<strong>{lead_title}</strong>) has been qualified by our technical team.</p>
<p>Assigned Account Representative: <strong>{assigned_agent}</strong></p>
<p style="font-size: 12px; color: #64748b;">Our representative will reach out to send a customized formal quotation.</p>
</div>
</body>
</html>
"""
@staticmethod
def simple_notification_email(data: Dict[str, Any]) -> str:
title = data.get("title", "Notification from iFixKart")
customer_name = data.get("customer_name", "Valued Customer")
message = data.get("message", "")
return f"""
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; background-color: #f8fafc; padding: 20px;">
<div style="max-width: 600px; margin: 0 auto; background: #ffffff; padding: 30px; border-radius: 8px; border: 1px solid #e2e8f0;">
<h2 style="color: #0284c7; margin-top: 0;">{title}</h2>
<p>Hello <strong>{customer_name}</strong>,</p>
<p>{message}</p>
<p style="font-size: 12px; color: #64748b; margin-top: 30px;">Thank you for choosing iFixKart!</p>
</div>
</body>
</html>
"""
@staticmethod
def order_invoice_email(data: Dict[str, Any]) -> str:
order_no = data.get("order_no", "ORD-000")
invoice_no = data.get("invoice_no", "INV-000")
customer_name = data.get("customer_name", "Valued Customer")
items: List[Dict[str, Any]] = data.get("items", [])
subtotal = float(data.get("subtotal", 0.0))
tax_amount = float(data.get("tax_amount", 0.0))
final_amount = float(data.get("final_amount", 0.0))
payment_status = data.get("payment_status", "PAID")
pdf_url = data.get("pdf_url", "#")
store_logo_url = data.get("store_logo_url", "https://admin.trionixsolution.com/images/logo/ifixkart-logo.webp")
items_rows_html = ""
for idx, item in enumerate(items, 1):
name = item.get("product_name", "Product")
sku = item.get("sku", "")
qty = int(item.get("quantity", item.get("qty", 1)))
unit_price = float(item.get("unit_price", 0.0))
line_total = qty * unit_price
items_rows_html += f"""
<tr style="border-bottom: 1px solid #e2e8f0;">
<td style="padding: 12px 16px; font-weight: 500; color: #1e293b;">{idx}. {name}<br><small style="color: #64748b;">SKU: {sku}</small></td>
<td style="padding: 12px 16px; text-align: center; color: #334155;">{qty}</td>
<td style="padding: 12px 16px; text-align: right; color: #334155;">₹{unit_price:,.2f}</td>
<td style="padding: 12px 16px; text-align: right; font-weight: 600; color: #0f172a;">₹{line_total:,.2f}</td>
</tr>
"""
return EmailTemplateService.order_invoice_email_wrapper(
body_content=f"""<p style="margin: 0; font-size: 16px; font-weight: 700; color: #0f172a;">Dear {customer_name},</p>
<p style="margin: 4px 0 0 0; font-size: 14px; color: #475569;">Thank you for your order! Your Tax Invoice <strong>#{invoice_no}</strong> (Order Ref: <strong>{order_no}</strong>) is attached as a PDF to this email.</p>""",
data=data,
items_rows_html=items_rows_html
)
@staticmethod
def order_invoice_email_wrapper(body_content: str, data: Dict[str, Any], items_rows_html: str = "") -> str:
order_no = data.get("order_no", "ORD-000")
invoice_no = data.get("invoice_no", "INV-000")
subtotal = float(data.get("subtotal", 0.0))
tax_amount = float(data.get("tax_amount", 0.0))
final_amount = float(data.get("final_amount", 0.0))
payment_status = data.get("payment_status", "PAID")
pdf_url = data.get("pdf_url", "#")
store_logo_url = data.get("store_logo_url", "https://admin.trionixsolution.com/images/logo/ifixkart-logo.webp")
if not items_rows_html:
items = data.get("items", [])
for idx, item in enumerate(items, 1):
name = item.get("product_name", "Product")
sku = item.get("sku", "")
qty = int(item.get("quantity", item.get("qty", 1)))
unit_price = float(item.get("unit_price", 0.0))
line_total = qty * unit_price
items_rows_html += f"""
<tr style="border-bottom: 1px solid #e2e8f0;">
<td style="padding: 12px 16px; font-weight: 500; color: #1e293b;">{idx}. {name}<br><small style="color: #64748b;">SKU: {sku}</small></td>
<td style="padding: 12px 16px; text-align: center; color: #334155;">{qty}</td>
<td style="padding: 12px 16px; text-align: right; color: #334155;">₹{unit_price:,.2f}</td>
<td style="padding: 12px 16px; text-align: right; font-weight: 600; color: #0f172a;">₹{line_total:,.2f}</td>
</tr>
"""
return f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Order Invoice #{invoice_no} - iFixKart</title>
</head>
<body style="margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background-color: #f8fafc; color: #334155;">
<div style="max-width: 680px; margin: 30px auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); border: 1px solid #e2e8f0;">
<div style="background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); padding: 32px 40px; color: #ffffff;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" valign="middle">
<img src="{store_logo_url}" alt="iFixKart" height="40" style="display: block; border: 0;" />
</td>
<td align="right" valign="middle">
<span style="background: #16a34a; color: #ffffff; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 700; display: inline-block;">ORDER CONFIRMED</span>
</td>
</tr>
</table>
</div>
<div style="padding: 24px 40px; background: #f1f5f9; border-bottom: 1px solid #e2e8f0;">
{body_content}
</div>
<div style="padding: 32px 40px;">
<table style="width: 100%; border-collapse: collapse; font-size: 14px;">
<thead>
<tr style="background: #f8fafc; border-bottom: 2px solid #cbd5e1; text-align: left;">
<th style="padding: 12px 16px; font-weight: 700; color: #475569;">Product</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-align: center;">Qty</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-align: right;">Unit Price</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-align: right;">Total</th>
</tr>
</thead>
<tbody>
{items_rows_html}
</tbody>
</table>
<div style="margin-top: 24px; text-align: right;">
<p style="margin: 4px 0; font-size: 14px; color: #64748b;">Subtotal: <strong>₹{subtotal:,.2f}</strong></p>
<p style="margin: 4px 0; font-size: 14px; color: #64748b;">GST Tax (18%): <strong>₹{tax_amount:,.2f}</strong></p>
<p style="margin: 8px 0 0 0; font-size: 18px; font-weight: 800; color: #0f172a;">Grand Total: <span style="color: #0284c7;">₹{final_amount:,.2f}</span></p>
</div>
{f'<div style="margin-top: 24px; text-align: center;"><a href="{pdf_url}" style="background: #0284c7; color: #ffffff; padding: 10px 20px; border-radius: 6px; text-decoration: none; font-weight: 700;">Download PDF Invoice</a></div>' if pdf_url != '#' else ''}
</div>
</div>
</body>
</html>
"""
@staticmethod
def service_invoice_email(data: dict) -> str:
job_no = data.get("job_no", "JOB-XXXX")
invoice_no = data.get("invoice_no", "INV-XXXX")
customer_name = data.get("customer_name", "Valued Customer")
service_name = data.get("service_name", "Repair Service")
subtotal = data.get("subtotal", 0.0)
paid_amount = data.get("paid_amount", 0.0)
balance = data.get("balance", 0.0)
pdf_url = data.get("pdf_url", "#")
store_logo_url = data.get("store_logo_url", "https://admin.trionixsolution.com/images/logo/ifixkart-logo.webp")
# Determine tag based on balance
if balance <= 0:
status_tag = '<span style="background: #16a34a; color: #ffffff; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 700; display: inline-block;">SERVICE READY</span>'
else:
status_tag = '<span style="background: #f59e0b; color: #ffffff; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 700; display: inline-block;">SERVICE BOOKED</span>'
return f'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Service Invoice #{invoice_no} - iFixKart</title>
</head>
<body style="margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background-color: #f8fafc; color: #334155;">
<div style="max-width: 680px; margin: 30px auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); border: 1px solid #e2e8f0;">
<div style="background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); padding: 32px 40px; color: #ffffff;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" valign="middle">
<img src="{store_logo_url}" alt="iFixKart" height="40" style="display: block; border: 0; max-width: 150px; object-fit: contain;" />
</td>
<td align="right" valign="middle">
{status_tag}
</td>
</tr>
</table>
</div>
<div style="padding: 24px 40px; background: #f1f5f9; border-bottom: 1px solid #e2e8f0;">
<p style="margin: 0; font-size: 16px; font-weight: 700; color: #0f172a;">Dear {customer_name},</p>
<p style="margin: 4px 0 0 0; font-size: 14px; color: #475569;">Your service job update has been processed successfully. Your tax invoice <strong>#{invoice_no}</strong> (Job Ref: <strong>{job_no}</strong>) is attached as a PDF to this email.</p>
</div>
<div style="padding: 32px 40px;">
<table style="width: 100%; border-collapse: collapse; font-size: 14px;">
<thead>
<tr style="background: #f8fafc; border-bottom: 2px solid #cbd5e1; text-align: left;">
<th style="padding: 12px 16px; font-weight: 700; color: #475569;">Service Details</th>
<th style="padding: 12px 16px; font-weight: 700; color: #475569; text-align: right;">Total Amount</th>
</tr>
</thead>
<tbody>
<tr style="border-bottom: 1px solid #e2e8f0;">
<td style="padding: 12px 16px; font-weight: 500; color: #1e293b;">{service_name}</td>
<td style="padding: 12px 16px; text-align: right; font-weight: 600; color: #0f172a;">₹{subtotal:,.2f}</td>
</tr>
</tbody>
</table>
<div style="margin-top: 24px; text-align: right;">
<p style="margin: 4px 0; font-size: 14px; color: #64748b;">Total Amount: <strong>₹{subtotal:,.2f}</strong></p>
<p style="margin: 4px 0; font-size: 14px; color: #16a34a;">Total Paid: <strong>₹{paid_amount:,.2f}</strong></p>
<p style="margin: 8px 0 0 0; font-size: 18px; font-weight: 800; color: #0f172a;">Balance Due: <span style="color: #ef4444;">₹{balance:,.2f}</span></p>
</div>
{f'<div style="margin-top: 24px; text-align: center;"><a href="{pdf_url}" style="background: #0284c7; color: #ffffff; padding: 10px 20px; border-radius: 6px; text-decoration: none; font-weight: 700;">Download PDF Invoice</a></div>' if pdf_url != '#' else ''}
</div>
</div>
</body>
</html>
'''