32 lines
1.2 KiB
PHP
32 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/app.php';
|
|
$user = require_permission('customers', 'add');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
header('Content-Type: application/json');
|
|
$name = trim($_POST['name'] ?? '');
|
|
$phoneInput = trim($_POST['phone'] ?? '');
|
|
$phone = $phoneInput === '' ? '' : normalize_oman_phone($phoneInput);
|
|
|
|
if (!$name) {
|
|
echo json_encode(['success' => false, 'error' => tr('الاسم مطلوب', 'Name is required')]);
|
|
exit;
|
|
}
|
|
if ($phoneInput !== '' && $phone === '') {
|
|
echo json_encode(['success' => false, 'error' => tr('رقم الهاتف يجب أن يكون عمانياً من 8 خانات.', 'Phone must be an 8-digit Oman number.')]);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('INSERT INTO customers (name, phone) VALUES (?, ?)');
|
|
$stmt->execute([$name, $phone]);
|
|
$id = $pdo->lastInsertId();
|
|
|
|
echo json_encode(['success' => true, 'customer' => ['id' => $id, 'name' => $name, 'phone' => $phone]]);
|
|
} catch (Throwable $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|