prepare( "SELECT ss.id, ss.serial_number, p.name as product_name FROM sold_serials ss JOIN products p ON ss.product_id = p.id WHERE ss.dealer_id = ? AND ss.is_activated = FALSE" ); $stmt->execute([$dealer_id]); $unactivated_serials = $stmt->fetchAll(); } catch (PDOException $e) { die("Database error: " . $e->getMessage()); } $success_message = ''; $error_message = ''; if ($_SERVER["REQUEST_METHOD"] == "POST") { $sold_serial_id = trim($_POST['sold_serial_id']); $end_customer_name = trim($_POST['end_customer_name']); $end_customer_address = trim($_POST['end_customer_address']); $dealer_invoice_date = trim($_POST['dealer_invoice_date']); $dealer_invoice_no = trim($_POST['dealer_invoice_no']); $installation_date = trim($_POST['installation_date']); if (empty($sold_serial_id) || empty($end_customer_name) || empty($end_customer_address) || empty($dealer_invoice_date) || empty($dealer_invoice_no) || empty($installation_date)) { $error_message = "All fields are required."; } else { $target_dir = "uploads/invoices/"; if (!is_dir($target_dir)) { mkdir($target_dir, 0777, true); } $target_file = $target_dir . basename($_FILES["dealer_invoice"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if file already exists if (file_exists($target_file)) { $error_message = "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["dealer_invoice"]["size"] > 500000) { $error_message = "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "pdf" ) { $error_message = "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed."; $uploadOk = 0; } if ($uploadOk == 0) { $error_message = "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["dealer_invoice"]["tmp_name"], $target_file)) { try { $pdo = db(); $pdo->beginTransaction(); // 1. Insert warranty registration $sql = "INSERT INTO warranty_registrations (sold_serial_id, end_customer_name, end_customer_address, dealer_invoice_date, dealer_invoice_no, dealer_invoice_path, installation_date, serial_number) SELECT ?, ?, ?, ?, ?, ?, ?, serial_number FROM sold_serials WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$sold_serial_id, $end_customer_name, $end_customer_address, $dealer_invoice_date, $dealer_invoice_no, $target_file, $installation_date, $sold_serial_id]); // 2. Mark serial as activated $sql_update = "UPDATE sold_serials SET is_activated = TRUE WHERE id = ?"; $stmt_update = $pdo->prepare($sql_update); $stmt_update->execute([$sold_serial_id]); $pdo->commit(); // Fetch serial number for display message $stmt_sn = $pdo->prepare("SELECT serial_number FROM sold_serials WHERE id = ?"); $stmt_sn->execute([$sold_serial_id]); $serial_number = $stmt_sn->fetchColumn(); $success_message = "Warranty for serial number " . htmlspecialchars($serial_number) . " registered successfully!"; } catch (PDOException $e) { $pdo->rollBack(); $error_message = "Database error: " . $e->getMessage(); } } else { $error_message = "Sorry, there was an error uploading your file."; } } } } require_once 'includes/header.php'; ?>
Register a device installation to activate the warranty. Please select a device serial number, enter the end customer's name, and the installation date.