PDO::ERRMODE_EXCEPTION]
);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle status update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
$quote_id = $_POST['quote_id'];
$new_status = $_POST['new_status'];
$stmt = $pdo->prepare("UPDATE quotes SET status = ?, updated_at = NOW() WHERE id = ?");
$stmt->execute([$new_status, $quote_id]);
header('Location: index.php?updated=1');
exit;
}
// Get statistics
$stats = getStats($pdo);
// Get quotes with filtering
$status_filter = $_GET['status'] ?? '';
$search = $_GET['search'] ?? '';
$quotes = getQuotes($pdo, $status_filter, $search);
// Get single quote details if requested
$quote_detail = null;
if (isset($_GET['id'])) {
$quote_detail = getQuoteDetail($pdo, $_GET['id']);
}
// Helper functions
function getStats($pdo) {
$stats = [];
// Total quotes
$stmt = $pdo->query("SELECT COUNT(*) FROM quotes");
$stats['total'] = $stmt->fetchColumn();
// By status
$stmt = $pdo->query("SELECT status, COUNT(*) as count FROM quotes GROUP BY status");
$stats['by_status'] = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// Total monthly value (submitted quotes only)
$stmt = $pdo->query("SELECT COALESCE(SUM(monthly_total), 0) FROM quotes WHERE status = 'submitted'");
$stats['total_monthly'] = $stmt->fetchColumn();
// This month
$stmt = $pdo->query("SELECT COUNT(*) FROM quotes WHERE created_at >= DATE_FORMAT(NOW(), '%Y-%m-01')");
$stats['this_month'] = $stmt->fetchColumn();
return $stats;
}
function getQuotes($pdo, $status_filter = '', $search = '') {
$sql = "SELECT q.*,
(SELECT COUNT(*) FROM quote_items WHERE quote_id = q.id) as item_count
FROM quotes q WHERE 1=1";
$params = [];
if ($status_filter) {
$sql .= " AND q.status = ?";
$params[] = $status_filter;
}
if ($search) {
$sql .= " AND (q.company_name LIKE ? OR q.contact_name LIKE ? OR q.contact_email LIKE ?)";
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
}
$sql .= " ORDER BY q.created_at DESC LIMIT 100";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getQuoteDetail($pdo, $id) {
// Get quote
$stmt = $pdo->prepare("SELECT * FROM quotes WHERE id = ?");
$stmt->execute([$id]);
$quote = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$quote) return null;
// Get items
$stmt = $pdo->prepare("SELECT * FROM quote_items WHERE quote_id = ? ORDER BY category, created_at");
$stmt->execute([$id]);
$quote['items'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get activity
$stmt = $pdo->prepare("SELECT * FROM quote_activity WHERE quote_id = ? ORDER BY created_at DESC");
$stmt->execute([$id]);
$quote['activities'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $quote;
}
function showLoginPage($error = null) {
?>
Quote Admin - Login
Quote Admin
= htmlspecialchars($error) ?>
'bg-gray-500',
'submitted' => 'bg-blue-500',
'viewed' => 'bg-purple-500',
'followed_up' => 'bg-yellow-500',
'converted' => 'bg-green-500',
'expired' => 'bg-red-500',
];
$color = $colors[$status] ?? 'bg-gray-500';
return "" . ucfirst(str_replace('_', ' ', $status)) . "";
}
?>
Quote Admin Dashboard
Quote status updated successfully.
Total Quotes
= $stats['total'] ?>
Submitted
= $stats['by_status']['submitted'] ?? 0 ?>
Converted
= $stats['by_status']['converted'] ?? 0 ?>
Monthly Value
= formatMoney($stats['total_monthly']) ?>
Contact Information
Company: = htmlspecialchars($quote_detail['company_name'] ?: '(not provided)') ?>
Contact: = htmlspecialchars($quote_detail['contact_name'] ?: '(not provided)') ?>
Email: = htmlspecialchars($quote_detail['contact_email'] ?: '(not provided)') ?>
Phone: = htmlspecialchars($quote_detail['contact_phone'] ?: '(not provided)') ?>
Employees: = $quote_detail['employee_count'] ?>
Quote Summary
Status: = statusBadge($quote_detail['status']) ?>
Monthly Total: = formatMoney($quote_detail['monthly_total']) ?>
Setup Total: = formatMoney($quote_detail['setup_total']) ?>
Created: = $quote_detail['created_at'] ?>
Submitted: = $quote_detail['submitted_at'] ?>
Line Items
| Category |
Product |
Qty |
Unit Price |
Monthly |
| = ucfirst(str_replace('_', ' ', $item['category'])) ?> |
= htmlspecialchars($item['product_name']) ?> |
= $item['quantity'] ?> |
= formatMoney($item['unit_price']) ?> |
= formatMoney($item['monthly_amount']) ?> |
Activity Log
= $activity['created_at'] ?>
|
= ucfirst($activity['action']) ?>
(= $activity['step_name'] ?>)
| Date |
Company |
Contact |
Status |
Items |
Monthly |
|
| No quotes found |
| = date('M j, Y', strtotime($quote['created_at'])) ?> |
= htmlspecialchars($quote['company_name'] ?: '(not provided)') ?> |
= htmlspecialchars($quote['contact_name'] ?: '-') ?>
= htmlspecialchars($quote['contact_email'] ?: '') ?>
|
= statusBadge($quote['status']) ?> |
= $quote['item_count'] ?> |
= formatMoney($quote['monthly_total']) ?> |
View
|