298 lines
15 KiB
PHP
298 lines
15 KiB
PHP
<?php
|
|
require_once 'includes/auth.php';
|
|
$active_page = 'invoices';
|
|
require_once 'db/config.php';
|
|
|
|
// Handle delete request
|
|
if (isset($_GET['delete_id'])) {
|
|
$delete_id = $_GET['delete_id'];
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("DELETE FROM invoices WHERE id = ?");
|
|
$stmt->execute([$delete_id]);
|
|
header("Location: invoices.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle Add Invoice
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_invoice'])) {
|
|
$customer_id = $_POST['customer_id'];
|
|
$invoice_date = $_POST['invoice_date'];
|
|
$due_date = $_POST['due_date'];
|
|
$total = $_POST['total'];
|
|
$status = $_POST['status'];
|
|
|
|
try {
|
|
$sql = "INSERT INTO invoices (customer_id, invoice_date, due_date, total, status) VALUES (:customer_id, :invoice_date, :due_date, :total, :status)";
|
|
$stmt = db()->prepare($sql);
|
|
$stmt->execute([
|
|
':customer_id' => $customer_id,
|
|
':invoice_date' => $invoice_date,
|
|
':due_date' => $due_date,
|
|
':total' => $total,
|
|
':status' => $status
|
|
]);
|
|
header("Location: invoices.php");
|
|
exit();
|
|
} catch (PDOException $e) {
|
|
$error_message = "Error adding invoice: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// Handle form submission for updating an invoice
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_invoice'])) {
|
|
$id = $_POST['invoice_id'];
|
|
$customer_id = $_POST['customer_id'];
|
|
$invoice_date = $_POST['invoice_date'];
|
|
$due_date = $_POST['due_date'];
|
|
$total = $_POST['total'];
|
|
$status = $_POST['status'];
|
|
|
|
if (!empty($id)) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("UPDATE invoices SET customer_id = ?, invoice_date = ?, due_date = ?, total = ?, status = ? WHERE id = ?");
|
|
$stmt->execute([$customer_id, $invoice_date, $due_date, $total, $status, $id]);
|
|
header("Location: invoices.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch invoices and customers
|
|
try {
|
|
$invoices_stmt = db()->query("SELECT i.*, c.companyName as customer_name FROM invoices i JOIN customers c ON i.customer_id = c.id ORDER BY i.invoice_date DESC");
|
|
$invoices = $invoices_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$customers_stmt = db()->query("SELECT id, companyName FROM customers ORDER BY companyName ASC");
|
|
$customers = $customers_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$error_message = "Error fetching data: " . $e->getMessage();
|
|
$invoices = [];
|
|
$customers = [];
|
|
}
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="d-flex" id="wrapper">
|
|
<?php include 'includes/sidebar.php'; ?>
|
|
<!-- Page content wrapper-->
|
|
<div id="page-content-wrapper">
|
|
<!-- Top navigation-->
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
|
|
<div class="container-fluid">
|
|
<button class="btn btn-primary" id="sidebarToggle"><i class="bi bi-list"></i></button>
|
|
<ul class="navbar-nav ms-auto mt-2 mt-lg-0">
|
|
<li class="nav-item dropdown">
|
|
<a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo htmlspecialchars($_SESSION["username"]); ?></a>
|
|
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
|
|
<a class="dropdown-item" href="#!">Profile</a>
|
|
<a class="dropdown-item" href="#!">Settings</a>
|
|
<div class="dropdown-divider"></div>
|
|
<a class="dropdown-item" href="logout.php">Logout</a>
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Page content-->
|
|
<main class="container-fluid p-4">
|
|
<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">Invoices</h1>
|
|
<div class="btn-toolbar mb-2 mb-md-0">
|
|
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addInvoiceModal">
|
|
<i class="bi bi-plus-circle"></i>
|
|
Add Invoice
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">#</th>
|
|
<th scope="col">Customer</th>
|
|
<th scope="col">Invoice Date</th>
|
|
<th scope="col">Due Date</th>
|
|
<th scope="col">Amount</th>
|
|
<th scope="col">Status</th>
|
|
<th scope="col">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($invoices)): ?>
|
|
<tr>
|
|
<td colspan="7" class="text-center">No invoices found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($invoices as $invoice): ?>
|
|
<tr>
|
|
<td>INV-<?php echo htmlspecialchars($invoice['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($invoice['customer_name']); ?></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'], 2)); ?></td>
|
|
<td>
|
|
<?php
|
|
$status_class = 'bg-secondary';
|
|
if ($invoice['status'] == 'Paid') {
|
|
$status_class = 'bg-success';
|
|
} elseif ($invoice['status'] == 'Pending') {
|
|
$status_class = 'bg-warning text-dark';
|
|
} elseif ($invoice['status'] == 'Overdue') {
|
|
$status_class = 'bg-danger';
|
|
}
|
|
?>
|
|
<span class="badge <?php echo $status_class; ?>"><?php echo htmlspecialchars($invoice['status']); ?></span>
|
|
</td>
|
|
<td>
|
|
<button class="btn btn-sm btn-outline-primary edit-invoice-btn"
|
|
data-id="<?php echo $invoice['id']; ?>"
|
|
data-customer-id="<?php echo $invoice['customer_id']; ?>"
|
|
data-invoice-date="<?php echo htmlspecialchars($invoice['invoice_date']); ?>"
|
|
data-due-date="<?php echo htmlspecialchars($invoice['due_date']); ?>"
|
|
data-total="<?php echo htmlspecialchars($invoice['total']); ?>"
|
|
data-status="<?php echo htmlspecialchars($invoice['status']); ?>"
|
|
data-bs-toggle="modal" data-bs-target="#editInvoiceModal">
|
|
<i class="bi bi-pencil-square"></i>
|
|
</button>
|
|
<a href="invoices.php?delete_id=<?php echo $invoice['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this invoice?');"><i class="bi bi-trash"></i></a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Invoice Modal -->
|
|
<div class="modal fade" id="addInvoiceModal" tabindex="-1" aria-labelledby="addInvoiceModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="addInvoiceModalLabel">Add New Invoice</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form action="invoices.php" method="post">
|
|
<input type="hidden" name="add_invoice" value="1">
|
|
<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 Customer</option>
|
|
<?php foreach ($customers as $customer): ?>
|
|
<option value="<?php echo $customer['id']; ?>"><?php echo htmlspecialchars($customer['companyName']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="invoice_date" class="form-label">Invoice Date</label>
|
|
<input type="date" class="form-control" id="invoice_date" name="invoice_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="due_date" class="form-label">Due Date</label>
|
|
<input type="date" class="form-control" id="due_date" name="due_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="total" class="form-label">Total Amount</label>
|
|
<input type="number" step="0.01" class="form-control" id="total" name="total" 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="Draft">Draft</option>
|
|
<option value="Pending">Pending</option>
|
|
<option value="Paid">Paid</option>
|
|
<option value="Overdue">Overdue</option>
|
|
</select>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Add Invoice</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Invoice Modal -->
|
|
<div class="modal fade" id="editInvoiceModal" tabindex="-1" aria-labelledby="editInvoiceModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="editInvoiceModalLabel">Edit Invoice</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form method="POST" action="invoices.php">
|
|
<input type="hidden" name="update_invoice" value="1">
|
|
<input type="hidden" name="invoice_id" id="edit_invoice_id">
|
|
<div class="mb-3">
|
|
<label for="edit_customer_id" class="form-label">Customer</label>
|
|
<select class="form-select" id="edit_customer_id" name="customer_id" required>
|
|
<?php foreach ($customers as $customer): ?>
|
|
<option value="<?php echo $customer['id']; ?>"><?php echo htmlspecialchars($customer['companyName']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_invoice_date" class="form-label">Invoice Date</label>
|
|
<input type="date" class="form-control" id="edit_invoice_date" name="invoice_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_due_date" class="form-label">Due Date</label>
|
|
<input type="date" class="form-control" id="edit_due_date" name="due_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_total" class="form-label">Total Amount</label>
|
|
<input type="number" step="0.01" class="form-control" id="edit_total" name="total" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_status" class="form-label">Status</label>
|
|
<select class="form-select" id="edit_status" name="status" required>
|
|
<option value="Draft">Draft</option>
|
|
<option value="Pending">Pending</option>
|
|
<option value="Paid">Paid</option>
|
|
<option value="Overdue">Overdue</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var editInvoiceModal = document.getElementById('editInvoiceModal');
|
|
editInvoiceModal.addEventListener('show.bs.modal', function (event) {
|
|
var button = event.relatedTarget;
|
|
var invoiceId = button.getAttribute('data-id');
|
|
var customerId = button.getAttribute('data-customer-id');
|
|
var invoiceDate = button.getAttribute('data-invoice-date');
|
|
var dueDate = button.getAttribute('data-due-date');
|
|
var total = button.getAttribute('data-total');
|
|
var status = button.getAttribute('data-status');
|
|
|
|
var modal = this;
|
|
modal.querySelector('#edit_invoice_id').value = invoiceId;
|
|
modal.querySelector('#edit_customer_id').value = customerId;
|
|
modal.querySelector('#edit_invoice_date').value = invoiceDate;
|
|
modal.querySelector('#edit_due_date').value = dueDate;
|
|
modal.querySelector('#edit_total').value = total;
|
|
modal.querySelector('#edit_status').value = status;
|
|
});
|
|
});
|
|
</script>
|