getConnection(); $user_id = $iduserlogin ?? 1; /** * 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 ]); if ($success) { $newId = $pdo->lastInsertId(); 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()); redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } } /** * 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 = 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); 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()); if ($isAjax) { header('Content-Type: application/json'); echo json_encode([ 'success' => false, 'message' => 'Errore database: ' . $e->getMessage() ]); exit; } redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } } /** * 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); redirectWithMessage('success', 'Quotation cancellata con successo'); } catch (PDOException $e) { error_log("Errore PDO durante la cancellazione della quotation: " . $e->getMessage()); redirectWithMessage('error', 'Errore database: ' . $e->getMessage()); } } /** * Recupera tutte le quotations dell'utente. * Ultima creata in alto. */ try { $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 = []; } /** * 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 ]); $editQuotation = $stmt->fetch(PDO::FETCH_ASSOC); if (!$editQuotation) { error_log("Nessuna quotation trovata per id: " . $editId); } } catch (PDOException $e) { error_log("Errore PDO durante il recupero della quotation per modifica: " . $e->getMessage()); $editQuotation = null; } } ?> Gestione Quotations - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?>
Gestione Quotations
Modifica Quotation ID:
Torna alla Lista
Azioni
Quotations Esistenti
$row): ?>
ID Data Creazione Descrizione Cliente Azioni