64 lines
2.3 KiB
PHP
64 lines
2.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/whatsapp.php';
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$id = $input['id'] ?? null;
|
|
$status = $input['status'] ?? null;
|
|
|
|
if (!$id || !$status) {
|
|
echo json_encode(['success' => false, 'error' => 'Missing order ID or status']);
|
|
exit;
|
|
}
|
|
|
|
$valid_statuses = ['received', 'processing', 'ready', 'delivered', 'cancelled'];
|
|
if (!in_array($status, $valid_statuses)) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid status']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("UPDATE orders SET status = ? WHERE id = ?");
|
|
$stmt->execute([$status, $id]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
// WhatsApp Notification for Order Ready
|
|
try {
|
|
if ($status === 'ready') {
|
|
if (get_setting('whatsapp_enabled') === '1') {
|
|
$stmt_details = $pdo->prepare('SELECT o.order_number, c.name_ar, c.phone FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.id = ?');
|
|
$stmt_details->execute([$id]);
|
|
$order = $stmt_details->fetch();
|
|
|
|
if ($order && !empty($order['phone'])) {
|
|
$template = get_setting('msg_order_ready_ar');
|
|
if (!empty($template)) {
|
|
$message = str_replace(
|
|
['{customer_name}', '{order_number}'],
|
|
[$order['name_ar'], $order['order_number']],
|
|
$template
|
|
);
|
|
send_whatsapp_message($order['phone'], $message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
// Silently fail for notification
|
|
}
|
|
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => 'Order not found or status already same']);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
} |