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

105 lines
4.1 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
if (!isset($_GET['id'])) {
header('Location: invoices.php');
exit;
}
$invoice_id = $_GET['id'];
try {
$pdo = db();
$sql = "SELECT i.*, o.id as order_id FROM invoices i JOIN orders o ON i.order_id = o.id WHERE i.id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$invoice_id]);
$invoice = $stmt->fetch();
if (!$invoice) {
die('Invoice not found.');
}
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<h1 class="mb-4">Invoice Details #<?php echo htmlspecialchars($invoice['id']); ?></h1>
<div class="card">
<div class="card-header">Invoice Information</div>
<div class="card-body">
<p><strong>Invoice ID:</strong> <?php echo htmlspecialchars($invoice['id']); ?></p>
<p><strong>Order ID:</strong> <a href="order_details.php?order_id=<?php echo htmlspecialchars($invoice['order_id']); ?>"><?php echo htmlspecialchars($invoice['order_id']); ?></a></p>
<p><strong>Invoice Date:</strong> <?php echo htmlspecialchars($invoice['invoice_date']); ?></p>
<p><strong>Due Date:</strong> <?php echo htmlspecialchars($invoice['due_date']); ?></p>
<p><strong>Total Amount:</strong> $<?php echo htmlspecialchars(number_format($invoice['total_amount'], 2)); ?></p>
<p><strong>Status:</strong> <?php echo htmlspecialchars(ucfirst($invoice['status'])); ?></p>
</div>
</div>
<div class="card mt-4">
<div class="card-header">Payments</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Payment Date</th>
<th>Amount</th>
<th>Payment Method</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM payments WHERE invoice_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$invoice_id]);
$payments = $stmt->fetchAll();
$total_paid = 0;
foreach ($payments as $payment) {
$total_paid += $payment['amount'];
echo '<tr>';
echo '<td>' . htmlspecialchars($payment['payment_date']) . '</td>';
echo '<td>$' . htmlspecialchars(number_format($payment['amount'], 2)) . '</td>';
echo '<td>' . htmlspecialchars($payment['payment_method']) . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<p><strong>Total Paid:</strong> $<?php echo htmlspecialchars(number_format($total_paid, 2)); ?></p>
<p><strong>Amount Due:</strong> $<?php echo htmlspecialchars(number_format($invoice['total_amount'] - $total_paid, 2)); ?></p>
</div>
</div>
<?php if ($invoice['status'] !== 'paid'): ?>
<div class="card mt-4">
<div class="card-header">Post a Payment</div>
<div class="card-body">
<form action="post_payment.php" method="POST">
<input type="hidden" name="invoice_id" value="<?php echo $invoice_id; ?>">
<div class="mb-3">
<label for="amount" class="form-label">Amount</label>
<input type="number" class="form-control" id="amount" name="amount" step="0.01" min="0.01" max="<?php echo $invoice['total_amount'] - $total_paid; ?>" required>
</div>
<div class="mb-3">
<label for="payment_method" class="form-label">Payment Method</label>
<select class="form-select" id="payment_method" name="payment_method" required>
<option value="Credit Card">Credit Card</option>
<option value="Bank Transfer">Bank Transfer</option>
<option value="Cheque">Cheque</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Submit Payment</button>
</form>
</div>
</div>
<?php endif; ?>
<?php require_once 'includes/footer.php'; ?>