279 lines
13 KiB
PHP
279 lines
13 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
|
|
$pdo = db();
|
|
require_permission('customers_view');
|
|
|
|
$message = '';
|
|
|
|
// Handle Add/Edit Customer
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
$action = $_POST['action'];
|
|
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$phone = trim($_POST['phone']);
|
|
$address = trim($_POST['address'] ?? '');
|
|
|
|
if (empty($name)) {
|
|
$message = '<div class="alert alert-danger">Customer name is required.</div>';
|
|
} else {
|
|
try {
|
|
if ($action === 'edit_customer' && $id) {
|
|
if (!has_permission('customers_edit') && !has_permission('customers_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to edit customers.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE customers SET name = ?, email = ?, phone = ?, address = ? WHERE id = ?");
|
|
$stmt->execute([$name, $email, $phone, $address, $id]);
|
|
$message = '<div class="alert alert-success">Customer updated successfully!</div>';
|
|
}
|
|
} elseif ($action === 'add_customer') {
|
|
if (!has_permission('customers_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to add customers.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO customers (name, email, phone, address) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$name, $email, $phone, $address]);
|
|
|
|
// --- Send Welcome WhatsApp Message via Wablas ---
|
|
if (!empty($phone)) {
|
|
try {
|
|
require_once __DIR__ . '/../includes/WablasService.php';
|
|
$wablas = new WablasService($pdo);
|
|
$companyStmt = $pdo->query("SELECT company_name FROM company_settings LIMIT 1");
|
|
$companyName = $companyStmt->fetchColumn() ?: 'Our Restaurant';
|
|
|
|
$settingsStmt = $pdo->query("SELECT points_for_free_meal FROM loyalty_settings WHERE id = 1");
|
|
$settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
|
|
$threshold = $settings ? intval($settings['points_for_free_meal']) : 70;
|
|
|
|
$welcomeMsg = "Welcome *{$name}* to *{$companyName}*! 🎉\n\nThank you for registering. You can now earn loyalty points with every order!\n\nYou currently have 0 points. Collect {$threshold} points to earn a free meal!";
|
|
$wablas->sendMessage($phone, $welcomeMsg);
|
|
} catch (Exception $w) {
|
|
error_log("Wablas Admin Welcome Exception: " . $w->getMessage());
|
|
}
|
|
}
|
|
|
|
$message = '<div class="alert alert-success">Customer created successfully!</div>';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Delete
|
|
if (isset($_GET['delete'])) {
|
|
if (!has_permission('customers_del')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to delete customers.</div>';
|
|
} else {
|
|
try {
|
|
$id = $_GET['delete'];
|
|
$pdo->prepare("DELETE FROM customers WHERE id = ?")->execute([$id]);
|
|
header("Location: customers.php?deleted=1");
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() == '23000') {
|
|
$message = '<div class="alert alert-danger">Cannot delete this customer because they are linked to other records (e.g., orders).</div>';
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Error deleting customer: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($_GET['deleted'])) {
|
|
$message = '<div class="alert alert-success">Customer deleted successfully!</div>';
|
|
}
|
|
|
|
$search = $_GET['search'] ?? '';
|
|
$params = [];
|
|
$query = "SELECT * FROM customers";
|
|
|
|
if ($search) {
|
|
$query .= " WHERE name LIKE ? OR phone LIKE ? OR email LIKE ?";
|
|
$params = ["%$search%", "%$search%", "%$search%"];
|
|
}
|
|
|
|
$query .= " ORDER BY id DESC";
|
|
|
|
$customers_pagination = paginate_query($pdo, $query, $params);
|
|
$customers = $customers_pagination['data'];
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<div>
|
|
<h2 class="fw-bold mb-1">Customer Relationship</h2>
|
|
<p class="text-muted mb-0">Manage your customer database and contact info</p>
|
|
</div>
|
|
<?php if (has_permission('customers_add')):
|
|
?>
|
|
<button class="btn btn-primary btn-lg shadow-sm" data-bs-toggle="modal" data-bs-target="#customerModal" onclick="prepareAddForm()" style="border-radius: 12px;">
|
|
<i class="bi bi-person-plus me-1"></i> Add Customer
|
|
</button>
|
|
<?php endif;
|
|
?>
|
|
</div>
|
|
|
|
<?= $message ?>
|
|
|
|
<div class="card border-0 shadow-sm mb-4 rounded-4">
|
|
<div class="card-body p-4">
|
|
<form method="GET" class="row g-3 align-items-center">
|
|
<div class="col-md-9">
|
|
<div class="input-group">
|
|
<span class="input-group-text bg-light border-0 text-muted"><i class="bi bi-search"></i></span>
|
|
<input type="text" name="search" class="form-control border-0 bg-light rounded-3" placeholder="Search by name, phone or email..." value="<?= htmlspecialchars($search) ?>" style="border-radius: 0 10px 10px 0;">
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<button type="submit" class="btn btn-primary px-4 w-100 rounded-pill fw-bold shadow-sm">Search Records</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (empty($customers)):
|
|
?>
|
|
<div class="text-center py-5 bg-white rounded-4 shadow-sm">
|
|
<i class="bi bi-people display-1 text-muted opacity-25 mb-3 d-block"></i>
|
|
<h4 class="text-dark">No customers found</h4>
|
|
<p class="text-muted">No results matching your search criteria.</p>
|
|
</div>
|
|
<?php else:
|
|
?>
|
|
<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-4">Customer</th>
|
|
<th>Contact Info</th>
|
|
<th>Address</th>
|
|
<th>Points</th>
|
|
<th class="text-end pe-4">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($customers as $customer):
|
|
?>
|
|
<tr>
|
|
<td class="ps-4">
|
|
<div class="d-flex align-items-center py-2">
|
|
<div class="bg-primary-subtle text-primary rounded-circle d-flex align-items-center justify-content-center fw-bold me-3" style="width: 42px; height: 42px;">
|
|
<?= strtoupper(substr($customer['name'], 0, 1)) ?>
|
|
</div>
|
|
<div class="fw-bold text-dark fs-6"><?= htmlspecialchars($customer['name']) ?></div>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="small fw-bold text-dark mb-1"><i class="bi bi-phone me-1 text-muted"></i><?= htmlspecialchars($customer['phone'] ?: '-') ?></div>
|
|
<div class="small text-muted"><i class="bi bi-envelope me-1"></i><?= htmlspecialchars($customer['email'] ?: '-') ?></div>
|
|
</td>
|
|
<td>
|
|
<div class="small text-muted text-truncate" style="max-width: 200px;"><?= htmlspecialchars($customer['address'] ?: '-') ?></div>
|
|
</td>
|
|
<td>
|
|
<span class="badge bg-info-subtle text-info border border-info rounded-pill px-3"><?= number_format($customer['points'] ?? 0) ?> pts</span>
|
|
</td>
|
|
<td class="text-end pe-4">
|
|
<div class="d-inline-flex gap-2">
|
|
<?php if (has_permission('customers_edit') || has_permission('customers_add')):
|
|
?>
|
|
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill px-3"
|
|
data-bs-toggle="modal" data-bs-target="#customerModal"
|
|
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($customer), ENT_QUOTES, "UTF-8") ?>)'>Edit</button>
|
|
<?php endif;
|
|
?>
|
|
|
|
<?php if (has_permission('customers_del')):
|
|
?>
|
|
<a href="?delete=<?= $customer['id'] ?>" class="btn btn-sm btn-outline-danger rounded-pill px-3" onclick="return confirm('Delete customer? This will remove their loyalty history.')">Delete</a>
|
|
<?php endif;
|
|
?>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach;
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="p-3 border-top bg-light">
|
|
<?php render_pagination_controls($customers_pagination); ?>
|
|
</div>
|
|
</div>
|
|
<?php endif;
|
|
?>
|
|
|
|
<!-- Customer Modal -->
|
|
<?php if (has_permission('customers_add') || has_permission('customers_edit')):
|
|
?>
|
|
<div class="modal fade" id="customerModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content border-0 shadow-lg rounded-4">
|
|
<div class="modal-header bg-primary text-white border-0 py-3">
|
|
<h5 class="modal-title fw-bold" id="customerModalTitle">Add New Customer</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form method="POST" id="customerForm">
|
|
<div class="modal-body p-4">
|
|
<input type="hidden" name="action" id="customerAction" value="add_customer">
|
|
<input type="hidden" name="id" id="customerId">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold text-muted">FULL NAME <span class="text-danger">*</span></label>
|
|
<input type="text" name="name" id="customerName" class="form-control rounded-3 border-0 bg-light" required>
|
|
</div>
|
|
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label small fw-bold text-muted">PHONE NUMBER</label>
|
|
<input type="text" name="phone" id="customerPhone" class="form-control rounded-3 border-0 bg-light">
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label small fw-bold text-muted">EMAIL ADDRESS</label>
|
|
<input type="email" name="email" id="customerEmail" class="form-control rounded-3 border-0 bg-light">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-0">
|
|
<label class="form-label small fw-bold text-muted">ADDRESS</label>
|
|
<textarea name="address" id="customerAddress" class="form-control rounded-3 border-0 bg-light" rows="3" placeholder="Street, City, State..."></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0 p-4 pt-0">
|
|
<button type="button" class="btn btn-light rounded-pill px-4" data-bs-modal="modal" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary rounded-pill px-4 fw-bold shadow-sm">Save Customer Profile</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function prepareAddForm() {
|
|
document.getElementById('customerModalTitle').innerText = 'Add New Customer';
|
|
document.getElementById('customerAction').value = 'add_customer';
|
|
document.getElementById('customerForm').reset();
|
|
document.getElementById('customerId').value = '';
|
|
}
|
|
|
|
function prepareEditForm(customer) {
|
|
if (!customer) return;
|
|
document.getElementById('customerModalTitle').innerText = 'Edit Customer Profile';
|
|
document.getElementById('customerAction').value = 'edit_customer';
|
|
document.getElementById('customerId').value = customer.id;
|
|
document.getElementById('customerName').value = customer.name || '';
|
|
document.getElementById('customerPhone').value = customer.phone || '';
|
|
document.getElementById('customerEmail').value = customer.email || '';
|
|
document.getElementById('customerAddress').value = customer.address || '';
|
|
}
|
|
</script>
|
|
<?php endif;
|
|
?>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|