36766-vm/invoices.php
Flatlogic Bot 6c14b2436f 2.0
2025-12-18 09:40:37 +00:00

80 lines
2.7 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$is_admin = $_SESSION['is_admin'] ?? false;
try {
$pdo = db();
// Base query
$sql = "SELECT i.*, o.id as order_id FROM invoices i JOIN orders o ON i.order_id = o.id";
$params = [];
// If the user is not an admin, they can only see their own invoices
if (!$is_admin) {
$sql .= " WHERE o.user_id = ?";
$params[] = $user_id;
}
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$invoices = $stmt->fetchAll();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<h1 class="mb-4">Invoices</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Invoice ID</th>
<th>Order ID</th>
<th>Invoice Date</th>
<th>Due Date</th>
<th>Total Amount</th>
<th>Paid Amount</th>
<th>Balance</th>
<th>Age of Invoice</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($invoices as $invoice): ?>
<tr>
<td><?php echo htmlspecialchars($invoice['id']); ?></td>
<td><a href="order_details.php?order_id=<?php echo htmlspecialchars($invoice['order_id']); ?>"><?php echo htmlspecialchars($invoice['order_id']); ?></a></td>
<td><?php echo htmlspecialchars($invoice['invoice_date']); ?></td>
<td><?php echo htmlspecialchars($invoice['due_date']); ?></td>
<td>$<?php echo htmlspecialchars(number_format($invoice['total_amount'], 2)); ?></td>
<td>$<?php echo htmlspecialchars(number_format($invoice['paid_amount'], 2)); ?></td>
<td>$<?php
$balance = $invoice['total_amount'] - $invoice['paid_amount'];
echo htmlspecialchars(number_format($balance, 2));
?></td>
<td><?php
$invoice_date = new DateTime($invoice['invoice_date']);
$now = new DateTime();
$age = $now->diff($invoice_date)->days;
echo $age . ' days';
?></td>
<td><?php echo htmlspecialchars(ucfirst($invoice['status'])); ?></td>
<td>
<a href="invoice_details.php?id=<?php echo $invoice['id']; ?>" class="btn btn-primary btn-sm">View Details</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php require_once 'includes/footer.php'; ?>