123 lines
4.9 KiB
PHP
123 lines
4.9 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$title = 'Edit Invoice - Billing';
|
|
$page = 'invoices';
|
|
require_once 'templates/header.php';
|
|
|
|
$invoice_id = $_GET['id'] ?? null;
|
|
$error_message = '';
|
|
$success_message = '';
|
|
$invoice = null;
|
|
|
|
if (!$invoice_id) {
|
|
header('Location: invoices.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$customer_id = trim($_POST['customer_id']);
|
|
$plan_id = trim($_POST['plan_id']);
|
|
$amount = trim($_POST['amount']);
|
|
$status = trim($_POST['status']);
|
|
$due_date = trim($_POST['due_date']);
|
|
|
|
if (empty($customer_id) || empty($plan_id) || empty($amount) || empty($status) || empty($due_date)) {
|
|
$error_message = 'Please fill in all required fields.';
|
|
} elseif (!is_numeric($amount)) {
|
|
$error_message = 'Amount must be a number.';
|
|
} else {
|
|
$stmt = $pdo->prepare('UPDATE invoices SET customer_id = ?, plan_id = ?, amount = ?, status = ?, due_date = ? WHERE id = ?');
|
|
$stmt->execute([$customer_id, $plan_id, $amount, $status, $due_date, $invoice_id]);
|
|
$success_message = 'Invoice updated successfully!';
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM invoices WHERE id = ?');
|
|
$stmt->execute([$invoice_id]);
|
|
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$invoice) {
|
|
header('Location: invoices.php');
|
|
exit;
|
|
}
|
|
|
|
$customers_stmt = $pdo->query('SELECT id, name FROM customers');
|
|
$customers = $customers_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$plans_stmt = $pdo->query('SELECT id, name FROM plans');
|
|
$plans = $plans_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
|
<h1 class="h2">Edit Invoice #<?php echo htmlspecialchars($invoice_id); ?></h1>
|
|
</div>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success_message): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($success_message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($invoice): ?>
|
|
<form action="edit_invoice.php?id=<?php echo htmlspecialchars($invoice_id); ?>" method="POST">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="customer_id" class="form-label">Customer</label>
|
|
<select class="form-select" id="customer_id" name="customer_id" required>
|
|
<?php foreach ($customers as $customer): ?>
|
|
<option value="<?php echo $customer['id']; ?>" <?php echo ($invoice['customer_id'] == $customer['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($customer['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="plan_id" class="form-label">Plan</label>
|
|
<select class="form-select" id="plan_id" name="plan_id" required>
|
|
<?php foreach ($plans as $plan): ?>
|
|
<option value="<?php echo $plan['id']; ?>" <?php echo ($invoice['plan_id'] == $plan['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($plan['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-4 mb-3">
|
|
<label for="amount" class="form-label">Amount</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text">$</span>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" value="<?php echo htmlspecialchars($invoice['amount']); ?>" required>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label for="status" class="form-label">Status</label>
|
|
<select class="form-select" id="status" name="status" required>
|
|
<option value="Pending" <?php echo ($invoice['status'] === 'Pending') ? 'selected' : ''; ?>>Pending</option>
|
|
<option value="Paid" <?php echo ($invoice['status'] === 'Paid') ? 'selected' : ''; ?>>Paid</option>
|
|
<option value="Overdue" <?php echo ($invoice['status'] === 'Overdue') ? 'selected' : ''; ?>>Overdue</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label for="due_date" class="form-label">Due Date</label>
|
|
<input type="date" class="form-control" id="due_date" name="due_date" value="<?php echo htmlspecialchars($invoice['due_date']); ?>" required>
|
|
</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
<a href="invoices.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
<?php else: ?>
|
|
<p>Invoice not found.</p>
|
|
<?php endif; ?>
|
|
|
|
<?php
|
|
require_once 'templates/footer.php';
|
|
?>
|