123 lines
5.0 KiB
PHP
123 lines
5.0 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'templates/header.php';
|
|
|
|
$customer_id = $plan_id = $amount = $status = "";
|
|
$errors = [];
|
|
|
|
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"]);
|
|
|
|
if (empty($customer_id)) {
|
|
$errors[] = "Customer is required.";
|
|
}
|
|
if (empty($plan_id)) {
|
|
$errors[] = "Plan is required.";
|
|
}
|
|
if (empty($amount) || !is_numeric($amount)) {
|
|
$errors[] = "A valid amount is required.";
|
|
}
|
|
if (empty($status)) {
|
|
$errors[] = "Status is required.";
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$sql = "INSERT INTO invoices (customer_id, plan_id, amount, status, created_at) VALUES (?, ?, ?, ?, NOW())";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$customer_id, $plan_id, $amount, $status]);
|
|
header("Location: invoices.php");
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch customers and plans for dropdowns
|
|
try {
|
|
$pdo = db();
|
|
$customer_stmt = $pdo->query("SELECT id, name FROM customers ORDER BY name");
|
|
$customers = $customer_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
$plan_stmt = $pdo->query("SELECT id, name FROM plans ORDER BY name");
|
|
$plans = $plan_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$customers = [];
|
|
$plans = [];
|
|
$errors[] = "Could not fetch data for form.";
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container-fluid px-4">
|
|
<h1 class="mt-4">Create New Invoice</h1>
|
|
<ol class="breadcrumb mb-4">
|
|
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
|
|
<li class="breadcrumb-item"><a href="invoices.php">Invoices</a></li>
|
|
<li class="breadcrumb-item active">Create</li>
|
|
</ol>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<i class="fas fa-file-invoice-dollar me-1"></i>
|
|
New Invoice Details
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<ul>
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?php echo htmlspecialchars($error); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="create_invoice.php" method="post">
|
|
<div class="mb-3">
|
|
<label for="customer_id" class="form-label">Customer</label>
|
|
<select class="form-select" id="customer_id" name="customer_id" required>
|
|
<option value="">Select a customer</option>
|
|
<?php foreach ($customers as $customer): ?>
|
|
<option value="<?php echo htmlspecialchars($customer['id']); ?>" <?php echo ($customer_id == $customer['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($customer['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="plan_id" class="form-label">Plan</label>
|
|
<select class="form-select" id="plan_id" name="plan_id" required>
|
|
<option value="">Select a plan</option>
|
|
<?php foreach ($plans as $plan): ?>
|
|
<option value="<?php echo htmlspecialchars($plan['id']); ?>" <?php echo ($plan_id == $plan['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($plan['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="amount" class="form-label">Amount ($)</label>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" value="<?php echo htmlspecialchars($amount); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="status" class="form-label">Status</label>
|
|
<select class="form-select" id="status" name="status" required>
|
|
<option value="paid" <?php echo ($status == 'paid') ? 'selected' : ''; ?>>Paid</option>
|
|
<option value="pending" <?php echo ($status == 'pending') ? 'selected' : ''; ?>>Pending</option>
|
|
<option value="overdue" <?php echo ($status == 'overdue') ? 'selected' : ''; ?>>Overdue</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Create Invoice</button>
|
|
<a href="invoices.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'templates/footer.php'; ?>
|