adding customer messages
This commit is contained in:
parent
d229f476df
commit
4f4b85539c
@ -34,6 +34,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|||||||
} else {
|
} else {
|
||||||
$stmt = $pdo->prepare("INSERT INTO customers (name, email, phone, address) VALUES (?, ?, ?, ?)");
|
$stmt = $pdo->prepare("INSERT INTO customers (name, email, phone, address) VALUES (?, ?, ?, ?)");
|
||||||
$stmt->execute([$name, $email, $phone, $address]);
|
$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>';
|
$message = '<div class="alert alert-success">Customer created successfully!</div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,11 +109,13 @@ include 'includes/header.php';
|
|||||||
<h2 class="fw-bold mb-1">Customer Relationship</h2>
|
<h2 class="fw-bold mb-1">Customer Relationship</h2>
|
||||||
<p class="text-muted mb-0">Manage your customer database and contact info</p>
|
<p class="text-muted mb-0">Manage your customer database and contact info</p>
|
||||||
</div>
|
</div>
|
||||||
<?php if (has_permission('customers_add')): ?>
|
<?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;">
|
<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
|
<i class="bi bi-person-plus me-1"></i> Add Customer
|
||||||
</button>
|
</button>
|
||||||
<?php endif; ?>
|
<?php endif;
|
||||||
|
?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?= $message ?>
|
<?= $message ?>
|
||||||
@ -114,13 +136,15 @@ include 'includes/header.php';
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php if (empty($customers)): ?>
|
<?php if (empty($customers)):
|
||||||
|
?>
|
||||||
<div class="text-center py-5 bg-white rounded-4 shadow-sm">
|
<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>
|
<i class="bi bi-people display-1 text-muted opacity-25 mb-3 d-block"></i>
|
||||||
<h4 class="text-dark">No customers found</h4>
|
<h4 class="text-dark">No customers found</h4>
|
||||||
<p class="text-muted">No results matching your search criteria.</p>
|
<p class="text-muted">No results matching your search criteria.</p>
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php else:
|
||||||
|
?>
|
||||||
<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
|
<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
<table class="table table-hover align-middle mb-0">
|
<table class="table table-hover align-middle mb-0">
|
||||||
@ -134,7 +158,8 @@ include 'includes/header.php';
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php foreach ($customers as $customer): ?>
|
<?php foreach ($customers as $customer):
|
||||||
|
?>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="ps-4">
|
<td class="ps-4">
|
||||||
<div class="d-flex align-items-center py-2">
|
<div class="d-flex align-items-center py-2">
|
||||||
@ -152,23 +177,28 @@ include 'includes/header.php';
|
|||||||
<div class="small text-muted text-truncate" style="max-width: 200px;"><?= htmlspecialchars($customer['address'] ?: '-') ?></div>
|
<div class="small text-muted text-truncate" style="max-width: 200px;"><?= htmlspecialchars($customer['address'] ?: '-') ?></div>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<span class="badge bg-info-subtle text-info border border-info rounded-pill px-3"><?= number_format($customer['loyalty_points'] ?? 0) ?> pts</span>
|
<span class="badge bg-info-subtle text-info border border-info rounded-pill px-3"><?= number_format($customer['points'] ?? 0) ?> pts</span>
|
||||||
</td>
|
</td>
|
||||||
<td class="text-end pe-4">
|
<td class="text-end pe-4">
|
||||||
<div class="d-inline-flex gap-2">
|
<div class="d-inline-flex gap-2">
|
||||||
<?php if (has_permission('customers_edit') || has_permission('customers_add')): ?>
|
<?php if (has_permission('customers_edit') || has_permission('customers_add')):
|
||||||
|
?>
|
||||||
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill px-3"
|
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill px-3"
|
||||||
data-bs-toggle="modal" data-bs-target="#customerModal"
|
data-bs-toggle="modal" data-bs-target="#customerModal"
|
||||||
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($customer), ENT_QUOTES, "UTF-8") ?>)'>Edit</button>
|
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($customer), ENT_QUOTES, "UTF-8") ?>)'>Edit</button>
|
||||||
<?php endif; ?>
|
<?php endif;
|
||||||
|
?>
|
||||||
|
|
||||||
<?php if (has_permission('customers_del')): ?>
|
<?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>
|
<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; ?>
|
<?php endif;
|
||||||
|
?>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php endforeach; ?>
|
<?php endforeach;
|
||||||
|
?>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
@ -176,10 +206,12 @@ include 'includes/header.php';
|
|||||||
<?php render_pagination_controls($customers_pagination); ?>
|
<?php render_pagination_controls($customers_pagination); ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif;
|
||||||
|
?>
|
||||||
|
|
||||||
<!-- Customer Modal -->
|
<!-- Customer Modal -->
|
||||||
<?php if (has_permission('customers_add') || has_permission('customers_edit')): ?>
|
<?php if (has_permission('customers_add') || has_permission('customers_edit')):
|
||||||
|
?>
|
||||||
<div class="modal fade" id="customerModal" tabindex="-1" aria-hidden="true">
|
<div class="modal fade" id="customerModal" tabindex="-1" aria-hidden="true">
|
||||||
<div class="modal-dialog">
|
<div class="modal-dialog">
|
||||||
<div class="modal-content border-0 shadow-lg rounded-4">
|
<div class="modal-content border-0 shadow-lg rounded-4">
|
||||||
@ -241,6 +273,7 @@ function prepareEditForm(customer) {
|
|||||||
document.getElementById('customerAddress').value = customer.address || '';
|
document.getElementById('customerAddress').value = customer.address || '';
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<?php endif; ?>
|
<?php endif;
|
||||||
|
?>
|
||||||
|
|
||||||
<?php include 'includes/footer.php'; ?>
|
<?php include 'includes/footer.php'; ?>
|
||||||
@ -38,7 +38,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
|
|
||||||
// Wablas
|
// Wablas
|
||||||
if ($provider === 'wablas') {
|
if ($provider === 'wablas') {
|
||||||
$keys = ['domain', 'token', 'secret_key', 'order_template', 'is_enabled'];
|
$keys = ['domain', 'token', 'secret_key', 'order_template', 'welcome_template', 'is_enabled'];
|
||||||
foreach ($keys as $k) {
|
foreach ($keys as $k) {
|
||||||
$val = $_POST[$k] ?? '0';
|
$val = $_POST[$k] ?? '0';
|
||||||
if ($k === 'is_enabled' && !isset($_POST[$k])) {
|
if ($k === 'is_enabled' && !isset($_POST[$k])) {
|
||||||
@ -110,6 +110,7 @@ $wablasDom = getSetting($allSettings, 'wablas', 'domain');
|
|||||||
$wablasTok = getSetting($allSettings, 'wablas', 'token');
|
$wablasTok = getSetting($allSettings, 'wablas', 'token');
|
||||||
$wablasSecKey = getSetting($allSettings, 'wablas', 'secret_key');
|
$wablasSecKey = getSetting($allSettings, 'wablas', 'secret_key');
|
||||||
$wablasTemplate = getSetting($allSettings, 'wablas', 'order_template');
|
$wablasTemplate = getSetting($allSettings, 'wablas', 'order_template');
|
||||||
|
$wablasWelcomeTemplate = getSetting($allSettings, 'wablas', 'welcome_template');
|
||||||
$wablasEnabled = getSetting($allSettings, 'wablas', 'is_enabled');
|
$wablasEnabled = getSetting($allSettings, 'wablas', 'is_enabled');
|
||||||
|
|
||||||
// SMTP Settings
|
// SMTP Settings
|
||||||
@ -136,6 +137,14 @@ You've earned *{points_earned} points* with this order.
|
|||||||
💰 *Current Balance: {new_balance} points*";
|
💰 *Current Balance: {new_balance} points*";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($wablasWelcomeTemplate)) {
|
||||||
|
$wablasWelcomeTemplate = "Welcome *{customer_name}* to *{company_name}*! 🎉
|
||||||
|
|
||||||
|
Thank you for registering. You can now earn loyalty points with every order!
|
||||||
|
|
||||||
|
You currently have 0 points. Collect {points_threshold} points to earn a free meal!";
|
||||||
|
}
|
||||||
|
|
||||||
require_once __DIR__ . '/includes/header.php';
|
require_once __DIR__ . '/includes/header.php';
|
||||||
?>
|
?>
|
||||||
|
|
||||||
@ -302,6 +311,15 @@ require_once __DIR__ . '/includes/header.php';
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label">Welcome Message Template</label>
|
||||||
|
<textarea class="form-control font-monospace" name="welcome_template" rows="6" <?= !has_permission('settings_add') ? 'readonly' : '' ?>><?= htmlspecialchars($wablasWelcomeTemplate) ?></textarea>
|
||||||
|
<div class="form-text mt-2">
|
||||||
|
<strong>Available Variables:</strong><br>
|
||||||
|
<code>{customer_name}</code>, <code>{company_name}</code>, <code>{points_threshold}</code>.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<?php if (has_permission('settings_add')): ?>
|
<?php if (has_permission('settings_add')): ?>
|
||||||
<div class="mb-3 border-top pt-3">
|
<div class="mb-3 border-top pt-3">
|
||||||
<label class="form-label text-muted small">Test Configuration</label>
|
<label class="form-label text-muted small">Test Configuration</label>
|
||||||
|
|||||||
@ -44,6 +44,34 @@ try {
|
|||||||
$settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
|
$settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
|
||||||
$threshold = $settings ? intval($settings['points_for_free_meal']) : 70;
|
$threshold = $settings ? intval($settings['points_for_free_meal']) : 70;
|
||||||
|
|
||||||
|
// --- Send Welcome WhatsApp Message via Wablas ---
|
||||||
|
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';
|
||||||
|
|
||||||
|
// Fetch welcome template
|
||||||
|
$templateStmt = $pdo->query("SELECT setting_value FROM integration_settings WHERE provider='wablas' AND setting_key='welcome_template'");
|
||||||
|
$welcomeTemplate = $templateStmt->fetchColumn();
|
||||||
|
|
||||||
|
if (empty($welcomeTemplate)) {
|
||||||
|
$welcomeTemplate = "Welcome *{customer_name}* to *{company_name}*! 🎉\n\nThank you for registering. You can now earn loyalty points with every order!\n\nYou currently have 0 points. Collect {points_threshold} points to earn a free meal!";
|
||||||
|
}
|
||||||
|
|
||||||
|
$welcomeMsg = str_replace(
|
||||||
|
['{customer_name}', '{company_name}', '{points_threshold}'],
|
||||||
|
[$name, $companyName, $threshold],
|
||||||
|
$welcomeTemplate
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$wablas->sendMessage($phone, $welcomeMsg);
|
||||||
|
} catch (Exception $w) {
|
||||||
|
error_log("Wablas Welcome Msg Exception: " . $w->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'customer' => [
|
'customer' => [
|
||||||
|
|||||||
@ -359,8 +359,8 @@ try {
|
|||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
|
|
||||||
// --- Post-Transaction Actions (WhatsApp) ---
|
// --- Post-Transaction Actions (WhatsApp) ---
|
||||||
// DISABLED TEMPORARILY AS PER USER REQUEST
|
|
||||||
/*
|
|
||||||
if ($customer_id && $customer_phone) {
|
if ($customer_id && $customer_phone) {
|
||||||
try {
|
try {
|
||||||
$final_points = $current_points - $points_deducted + ($loyalty_enabled ? $points_awarded : 0);
|
$final_points = $current_points - $points_deducted + ($loyalty_enabled ? $points_awarded : 0);
|
||||||
@ -423,7 +423,7 @@ You've earned *{points_earned} points* with this order.
|
|||||||
error_log("Wablas Exception: " . $w->getMessage());
|
error_log("Wablas Exception: " . $w->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
echo json_encode(['success' => true, 'order_id' => $order_id]);
|
echo json_encode(['success' => true, 'order_id' => $order_id]);
|
||||||
|
|
||||||
|
|||||||
@ -2,4 +2,4 @@
|
|||||||
# https://curl.se/docs/http-cookies.html
|
# https://curl.se/docs/http-cookies.html
|
||||||
# This file was generated by libcurl! Edit at your own risk.
|
# This file was generated by libcurl! Edit at your own risk.
|
||||||
|
|
||||||
#HttpOnly_127.0.0.1 FALSE / FALSE 1772904726 PHPSESSID p7okqi6joq82i0rfhlmbbj2pgd
|
#HttpOnly_127.0.0.1 FALSE / FALSE 1773424134 PHPSESSID p5u70l3ue978cjhaqb88q2fob2
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user