37253-vm/manage_lifecycle.php
2026-01-06 10:04:24 +00:00

105 lines
4.3 KiB
PHP

<?php
require_once 'db/config.php';
// Fetch all customers for the dropdown
try {
$pdoconn = db();
$stmt = $pdoconn->prepare('SELECT custcode, custname FROM customers ORDER BY custname ASC');
$stmt->execute();
$customers = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error fetching customers: " . $e->getMessage();
$customers = [];
}
$selected_custcode = $_GET['custcode'] ?? null;
$lifecycle_data = [];
if ($selected_custcode) {
try {
$stmt = $pdoconn->prepare('
SELECT l.id, l.product_id, p.prodname, DATE(l.first_ship_month) as first_ship_month, DATE(l.last_ship_month) as last_ship_month, l.store_count, l.runrate
FROM lifecycle l
JOIN products p ON l.product_id = p.product_id
WHERE l.custcode = :custcode
ORDER BY l.first_ship_month DESC
');
$stmt->execute(['custcode' => $selected_custcode]);
$lifecycle_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error fetching lifecycle data: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Lifecycle Data</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
</head>
<body>
<div class="container mt-5">
<h1>Manage Lifecycle Data</h1>
<p>Select a customer to view their product lifecycle information.</p>
<form method="GET" action="manage_lifecycle.php" class="form-inline mb-4">
<div class="form-group">
<label for="custcode" class="mr-2">Select Customer:</label>
<select name="custcode" id="custcode" class="form-control" onchange="this.form.submit()">
<option value="">-- Select a Customer --</option>
<?php foreach ($customers as $customer): ?>
<option value="<?= htmlspecialchars($customer['custcode']) ?>" <?= $selected_custcode == $customer['custcode'] ? 'selected' : '' ?>>
<?= htmlspecialchars($customer['custname']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
</form>
<?php if ($selected_custcode && !empty($lifecycle_data)): ?>
<h3>Lifecycle Records for <?= htmlspecialchars($customers[array_search($selected_custcode, array_column($customers, 'custcode'))]['custname']) ?></h3>
<table id="lifecycleTable" class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Product Name</th>
<th>First Ship Month</th>
<th>Last Ship Month</th>
<th>Store Count</th>
<th>Run Rate</th>
</tr>
</thead>
<tbody>
<?php foreach ($lifecycle_data as $row): ?>
<tr>
<td><?= htmlspecialchars($row['id']) ?></td>
<td><?= htmlspecialchars($row['prodname']) ?></td>
<td><?= htmlspecialchars($row['first_ship_month']) ?></td>
<td><?= htmlspecialchars($row['last_ship_month']) ?></td>
<td><?= htmlspecialchars($row['store_count']) ?></td>
<td><?= htmlspecialchars($row['runrate']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php elseif ($selected_custcode && empty($lifecycle_data)): ?>
<div class="alert alert-info">
No lifecycle records found for the selected customer.
</div>
<?php endif; ?>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#lifecycleTable').DataTable();
});
</script>
</body>
</html>