227 lines
11 KiB
PHP
227 lines
11 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$dealer_id = $_SESSION['dealer_id'];
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Fetch unactivated serial numbers for the dealer
|
|
$stmt = $pdo->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 <strong>" . htmlspecialchars($serial_number) . "</strong> 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';
|
|
?>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h1 class="h3 mb-0">Device Warranty Registration</h1>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="card-text">Register a device installation to activate the warranty. Please select a device serial number, enter the end customer's name, and the installation date.</p>
|
|
|
|
<?php if ($success_message): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
<?php echo $success_message; ?>
|
|
<script>
|
|
// Refresh the page after 3 seconds to update the dropdown
|
|
setTimeout(() => window.location.href = 'warranty_registration.php', 3000);
|
|
</script>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="warranty_registration.php" method="POST" enctype="multipart/form-data">
|
|
<div class="mb-3">
|
|
<label for="sold_serial_id" class="form-label">Device Serial Number</label>
|
|
<select class="form-select" id="sold_serial_id" name="sold_serial_id" required>
|
|
<option value="">-- Select a Serial Number --</option>
|
|
<?php foreach ($unactivated_serials as $serial): ?>
|
|
<option value="<?php echo htmlspecialchars($serial['id']); ?>">
|
|
<?php echo htmlspecialchars($serial['serial_number'] . ' (' . $serial['product_name'] . ')'); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php if (empty($unactivated_serials)): ?>
|
|
<div class="form-text">No devices are pending warranty activation.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div id="product-details" class="mb-3" style="display: none;">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5>Product Details</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<p><strong>Name:</strong> <span id="product-name"></span></p>
|
|
<p><strong>Model Number:</strong> <span id="product-model"></span></p>
|
|
<p><strong>Part Number:</strong> <span id="product-part"></span></p>
|
|
<p><strong>Description:</strong> <span id="product-description"></span></p>
|
|
<img id="product-image" src="" alt="Product Image" class="img-fluid" style="max-height: 200px;"/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="end_customer_name" class="form-label">End Customer's Name (Hospital/Lab)</label>
|
|
<input type="text" class="form-control" id="end_customer_name" name="end_customer_name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="end_customer_address" class="form-label">End Customer's Address</label>
|
|
<textarea class="form-control" id="end_customer_address" name="end_customer_address" rows="3" required></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="dealer_invoice_date" class="form-label">Dealer Invoice Date</label>
|
|
<input type="date" class="form-control" id="dealer_invoice_date" name="dealer_invoice_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="dealer_invoice_no" class="form-label">Dealer Invoice No</label>
|
|
<input type="text" class="form-control" id="dealer_invoice_no" name="dealer_invoice_no" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="dealer_invoice" class="form-label">Dealer Invoice</label>
|
|
<input class="form-control" type="file" id="dealer_invoice" name="dealer_invoice" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="installation_date" class="form-label">Installation Date</label>
|
|
<input type="date" class="form-control" id="installation_date" name="installation_date" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary" <?php echo empty($unactivated_serials) ? 'disabled' : ''; ?>><i class="bi bi-check-circle"></i> Register Warranty</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById('sold_serial_id').addEventListener('change', function() {
|
|
var soldSerialId = this.value;
|
|
if (soldSerialId) {
|
|
fetch('api/get_product_details.php?sold_serial_id=' + soldSerialId)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.error) {
|
|
alert(data.error);
|
|
} else {
|
|
document.getElementById('product-details').style.display = 'block';
|
|
document.getElementById('product-name').textContent = data.name;
|
|
document.getElementById('product-model').textContent = data.model_number;
|
|
document.getElementById('product-part').textContent = data.part_number;
|
|
document.getElementById('product-description').textContent = data.description;
|
|
document.getElementById('product-image').src = data.image_url;
|
|
}
|
|
})
|
|
.catch(error => console.error('Error:', error));
|
|
} else {
|
|
document.getElementById('product-details').style.display = 'none';
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<?php
|
|
require_once 'includes/footer.php';
|
|
?>
|