diff --git a/public/userarea/quotations.php b/public/userarea/quotations.php index f2d9b43..067fe1c 100644 --- a/public/userarea/quotations.php +++ b/public/userarea/quotations.php @@ -1,15 +1,15 @@ getConnection(); -// Recupera l'ID dell'utente loggato $user_id = $iduserlogin ?? 1; -// Gestione creazione nuova quotation (crea record vuoto su conferma) -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create') { - $description = ''; - $customer = ''; +/** + * Helper redirect + */ +function redirectWithMessage($status, $message, $extraQuery = '') +{ + $url = 'quotations.php?status=' . urlencode($status) . '&message=' . urlencode($message); + + if ($extraQuery !== '') { + $url .= '&' . ltrim($extraQuery, '&'); + } + + header("Location: " . $url); + exit; +} + +/** + * CREATE quotation + * Ora description e customer vengono salvati subito dal modale. + */ +if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'create') { + $description = trim($_POST['description'] ?? ''); + $customer = trim($_POST['customer'] ?? ''); + + if ($description === '' || $customer === '') { + redirectWithMessage('error', 'Descrizione e Cliente sono obbligatori'); + } try { - $stmt = $pdo->prepare("INSERT INTO quotations (description, customer, iduser) VALUES (?, ?, ?)"); - $success = $stmt->execute([$description, $customer, $user_id]); + $stmt = $pdo->prepare(" + INSERT INTO quotations + (description, customer, iduser) + VALUES + (?, ?, ?) + "); + + $success = $stmt->execute([ + $description, + $customer, + $user_id + ]); + if ($success) { $newId = $pdo->lastInsertId(); - error_log("Creata nuova quotation ID: $newId"); - header("Location: quotations.php?edit_id=" . $newId . "&status=success&message=" . urlencode("Quotation creata con successo")); - } else { - error_log("Errore: Impossibile creare la quotation, nessun ID generato."); - header("Location: quotations.php?status=error&message=" . urlencode("Errore durante la creazione della quotation")); + error_log("Creata nuova quotation ID: " . $newId); + + redirectWithMessage('success', 'Quotation creata con successo'); } + + error_log("Errore: impossibile creare la quotation"); + redirectWithMessage('error', 'Errore durante la creazione della quotation'); } catch (PDOException $e) { error_log("Errore PDO durante la creazione della quotation: " . $e->getMessage()); - header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } - exit; } -// Gestione modifica quotation -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update' && isset($_POST['id'])) { +/** + * UPDATE quotation + * Gestisce sia form normale sia salvataggio inline AJAX. + */ +if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'update' && isset($_POST['id'])) { $id = intval($_POST['id']); - $description = $_POST['description'] ?? ''; - $customer = $_POST['customer'] ?? ''; + $description = trim($_POST['description'] ?? ''); + $customer = trim($_POST['customer'] ?? ''); + $isAjax = isset($_POST['ajax']) && $_POST['ajax'] === '1'; + + if ($description === '' || $customer === '') { + if ($isAjax) { + header('Content-Type: application/json'); + echo json_encode([ + 'success' => false, + 'message' => 'Descrizione e Cliente sono obbligatori' + ]); + exit; + } + + redirectWithMessage('error', 'Descrizione e Cliente sono obbligatori'); + } try { - $stmt = $pdo->prepare("UPDATE quotations SET description = ?, customer = ? WHERE id = ? AND iduser = ?"); - $stmt->execute([$description, $customer, $id, $user_id]); - error_log("Modificata quotation ID: $id"); - header("Location: quotations.php?status=success&message=" . urlencode("Quotation modificata con successo")); + $stmt = $pdo->prepare(" + UPDATE quotations + SET description = ?, customer = ? + WHERE id = ? AND iduser = ? + "); + + $stmt->execute([ + $description, + $customer, + $id, + $user_id + ]); + + error_log("Modificata quotation ID: " . $id); + + if ($isAjax) { + header('Content-Type: application/json'); + echo json_encode([ + 'success' => true, + 'message' => 'Quotation modificata con successo' + ]); + exit; + } + + redirectWithMessage('success', 'Quotation modificata con successo'); } catch (PDOException $e) { error_log("Errore PDO durante la modifica della quotation: " . $e->getMessage()); - header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + + if ($isAjax) { + header('Content-Type: application/json'); + echo json_encode([ + 'success' => false, + 'message' => 'Errore database: ' . $e->getMessage() + ]); + exit; + } + + redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } - exit; } -// Gestione cancellazione quotation -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['id'])) { +/** + * DELETE quotation + */ +if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delete' && isset($_POST['id'])) { $id = intval($_POST['id']); try { - $stmt = $pdo->prepare("DELETE FROM quotations WHERE id = ? AND iduser = ?"); - $stmt->execute([$id, $user_id]); - error_log("Cancellata quotation ID: $id"); - header("Location: quotations.php?status=success&message=" . urlencode("Quotation cancellata con successo")); + $stmt = $pdo->prepare(" + DELETE FROM quotations + WHERE id = ? AND iduser = ? + "); + + $stmt->execute([ + $id, + $user_id + ]); + + error_log("Cancellata quotation ID: " . $id); + + redirectWithMessage('success', 'Quotation cancellata con successo'); } catch (PDOException $e) { error_log("Errore PDO durante la cancellazione della quotation: " . $e->getMessage()); - header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } - exit; } -// Recupera tutte le quotations per l'utente +/** + * Recupera tutte le quotations dell'utente. + * Ultima creata in alto. + */ try { - $stmt = $pdo->prepare("SELECT * FROM quotations WHERE iduser = ? ORDER BY creation_date DESC"); - $stmt->execute([$user_id]); + $stmt = $pdo->prepare(" + SELECT * + FROM quotations + WHERE iduser = ? + ORDER BY creation_date DESC, id DESC + "); + + $stmt->execute([ + $user_id + ]); + $quotations = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { error_log("Errore PDO durante il recupero delle quotations: " . $e->getMessage()); $quotations = []; } -// Verifica se è richiesta la modifica di una quotation +/** + * Recupera quotation in modifica dettagliata + */ $editQuotation = null; + if (isset($_GET['edit_id'])) { $editId = intval($_GET['edit_id']); + try { - $stmt = $pdo->prepare("SELECT * FROM quotations WHERE id = ? AND iduser = ?"); - $stmt->execute([$editId, $user_id]); + $stmt = $pdo->prepare(" + SELECT * + FROM quotations + WHERE id = ? AND iduser = ? + "); + + $stmt->execute([ + $editId, + $user_id + ]); + $editQuotation = $stmt->fetch(PDO::FETCH_ASSOC); + if (!$editQuotation) { - error_log("Nessuna quotation trovata per id: $editId"); + error_log("Nessuna quotation trovata per id: " . $editId); } } catch (PDOException $e) { error_log("Errore PDO durante il recupero della quotation per modifica: " . $e->getMessage()); @@ -112,10 +227,14 @@ if (isset($_GET['edit_id'])) { + - + + + + + Gestione Quotations - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?> @@ -271,50 +444,100 @@ if (isset($_GET['edit_id'])) {
+
-
+
Gestione Quotations
+ + + +
+
+ -
- +
+
+ - -
Modifica Quotation ID:
-
+ +
+ Modifica Quotation ID: +
+ + - + +
- +
+
- +
- - Torna alla Lista + + + + + Torna alla Lista +
+
Azioni
- - + + + +
+ - -
- -
+
Quotations Esistenti
+ @@ -325,110 +548,246 @@ if (isset($_GET['edit_id'])) { + $row): ?> - - - + + + + + + +
Azioni
- + - + - - - - - + + + + + + + + + + +
+ +
- + + +
+ + + - - - \ No newline at end of file