Files
comelifacciamo/public/userarea/customer_save.php
T

61 lines
1.9 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
include('include/headscript.php');
include('include/shop_context.php');
$pdo = DBHandlerSelect::getInstance()->getConnection();
if (!isset($iduserlogin) || empty($active_shop_id)) {
header("Location: login.php");
exit;
}
function setFlash(string $type, string $text): void
{
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
}
$id = (int)($_POST['customer_id'] ?? 0);
$first = trim($_POST['first_name'] ?? '');
$last = trim($_POST['last_name'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$email = trim($_POST['email'] ?? '');
$notes = trim($_POST['notes'] ?? '');
if ($first === '') {
setFlash('danger', 'Il nome è obbligatorio.');
header("Location: customers.php");
exit;
}
if ($id > 0) {
// MODIFICA: verifica che il cliente appartenga al salone attivo
$chk = $pdo->prepare("SELECT id FROM customers WHERE id = ? AND shop_id = ? LIMIT 1");
$chk->execute([$id, $active_shop_id]);
if (!$chk->fetch()) {
setFlash('danger', 'Cliente non valido.');
header("Location: customers.php");
exit;
}
$upd = $pdo->prepare("
UPDATE customers
SET first_name = ?, last_name = ?, phone = ?, email = ?, notes = ?, updated_at = NOW()
WHERE id = ? AND shop_id = ?
");
$upd->execute([$first, $last, $phone, $email, $notes, $id, $active_shop_id]);
setFlash('success', 'Cliente aggiornato.');
} else {
// CREA: cliente manuale (user_id NULL) nel salone attivo
$ins = $pdo->prepare("
INSERT INTO customers (shop_id, user_id, first_name, last_name, phone, email, notes, created_at)
VALUES (?, NULL, ?, ?, ?, ?, ?, NOW())
");
$ins->execute([$active_shop_id, $first, $last, $phone, $email, $notes]);
setFlash('success', 'Cliente aggiunto.');
}
header("Location: customers.php");
exit;