275 lines
13 KiB
PHP
275 lines
13 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$tenant_id = 1;
|
|
|
|
// Handle Add Expense
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_expense'])) {
|
|
$project_id = (int)($_POST['project_id'] ?? 0);
|
|
$supplier_id = (int)($_POST['supplier_id'] ?? 0);
|
|
$expense_type_id = (int)($_POST['expense_type_id'] ?? 0);
|
|
$amount = (float)($_POST['amount'] ?? 0);
|
|
$allocation = (float)($_POST['allocation_percent'] ?? 100);
|
|
$entry_date = $_POST['entry_date'] ?? date('Y-m-d');
|
|
$notes = $_POST['notes'] ?? '';
|
|
|
|
if ($project_id && $supplier_id && $amount > 0) {
|
|
$stmt = db()->prepare("INSERT INTO expenses (tenant_id, project_id, supplier_id, expense_type_id, amount, allocation_percent, entry_date, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$tenant_id, $project_id, $supplier_id, $expense_type_id, $amount, $allocation, $entry_date, $notes]);
|
|
$expense_id = (int)db()->lastInsertId();
|
|
|
|
// Handle File Uploads
|
|
if (!empty($_FILES['attachments']['name'][0])) {
|
|
foreach ($_FILES['attachments']['tmp_name'] as $key => $tmp_name) {
|
|
if ($_FILES['attachments']['error'][$key] === UPLOAD_ERR_OK) {
|
|
$file_name = $_FILES['attachments']['name'][$key];
|
|
$file_size = $_FILES['attachments']['size'][$key];
|
|
$mime_type = $_FILES['attachments']['type'][$key];
|
|
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
|
|
$new_file_name = uniqid() . '.' . $file_ext;
|
|
$file_path = 'uploads/' . $new_file_name;
|
|
|
|
if (!is_dir('uploads')) mkdir('uploads', 0775, true);
|
|
if (move_uploaded_file($tmp_name, $file_path)) {
|
|
$stmt = db()->prepare("INSERT INTO attachments (tenant_id, entity_type, entity_id, file_name, file_path, file_size, mime_type, uploaded_by) VALUES (?, 'expense', ?, ?, ?, ?, ?, 'John Manager')");
|
|
$stmt->execute([$tenant_id, $expense_id, $file_name, $file_path, $file_size, $mime_type]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$stmt = db()->prepare("INSERT INTO activity_log (tenant_id, action, details) VALUES (?, ?, ?)");
|
|
$stmt->execute([$tenant_id, 'Expense Logged', "Logged \$" . number_format($amount, 2) . " expense for project ID $project_id"]);
|
|
|
|
header("Location: expenses.php?success=1");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch Data
|
|
$filter_project = (int)($_GET['project_id'] ?? 0);
|
|
$filter_supplier = (int)($_GET['supplier_id'] ?? 0);
|
|
$filter_start = $_GET['start_date'] ?? '';
|
|
$filter_end = $_GET['end_date'] ?? '';
|
|
|
|
$where = ["e.tenant_id = ?"];
|
|
$params = [$tenant_id];
|
|
|
|
if ($filter_project) {
|
|
$where[] = "e.project_id = ?";
|
|
$params[] = $filter_project;
|
|
}
|
|
if ($filter_supplier) {
|
|
$where[] = "e.supplier_id = ?";
|
|
$params[] = $filter_supplier;
|
|
}
|
|
if ($filter_start) {
|
|
$where[] = "e.entry_date >= ?";
|
|
$params[] = $filter_start;
|
|
}
|
|
if ($filter_end) {
|
|
$where[] = "e.entry_date <= ?";
|
|
$params[] = $filter_end;
|
|
}
|
|
|
|
$where_clause = implode(" AND ", $where);
|
|
|
|
$expenseEntries = db()->prepare("
|
|
SELECT e.*, p.name as project_name, s.name as supplier_name, et.name as expense_type
|
|
FROM expenses e
|
|
JOIN projects p ON e.project_id = p.id
|
|
JOIN suppliers s ON e.supplier_id = s.id
|
|
LEFT JOIN expense_types et ON e.expense_type_id = et.id
|
|
WHERE $where_clause
|
|
ORDER BY e.entry_date DESC, e.created_at DESC
|
|
");
|
|
$expenseEntries->execute($params);
|
|
$expenseList = $expenseEntries->fetchAll();
|
|
|
|
$projects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? AND is_archived = 0 ORDER BY name");
|
|
$projects->execute([$tenant_id]);
|
|
$projectList = $projects->fetchAll();
|
|
|
|
$suppliers = db()->prepare("SELECT * FROM suppliers WHERE tenant_id = ? ORDER BY name");
|
|
$suppliers->execute([$tenant_id]);
|
|
$supplierList = $suppliers->fetchAll();
|
|
|
|
$expenseTypes = db()->prepare("SELECT * FROM expense_types WHERE tenant_id = ? ORDER BY name");
|
|
$expenseTypes->execute([$tenant_id]);
|
|
$expenseTypeList = $expenseTypes->fetchAll();
|
|
|
|
$pageTitle = "SR&ED Manager - Expenses";
|
|
include __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid py-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Expenses</h2>
|
|
<div>
|
|
<a href="api/export_expenses.php?<?= http_build_query($_GET) ?>" class="btn btn-primary me-2"><i class="bi bi-file-earmark-excel me-1"></i> Export to Excel</a>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addExpenseModal">+ Add Expense</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm mb-4">
|
|
<div class="card-body">
|
|
<form method="GET" class="row g-2 align-items-end">
|
|
<div class="col-md-3">
|
|
<label class="form-label small fw-bold">Project</label>
|
|
<select name="project_id" class="form-select form-select-sm">
|
|
<option value="">All Projects</option>
|
|
<?php
|
|
$allProjects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? ORDER BY name");
|
|
$allProjects->execute([$tenant_id]);
|
|
foreach ($allProjects->fetchAll() as $p): ?>
|
|
<option value="<?= $p['id'] ?>" <?= $filter_project == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label small fw-bold">Supplier</label>
|
|
<select name="supplier_id" class="form-select form-select-sm">
|
|
<option value="">All Suppliers</option>
|
|
<?php foreach ($supplierList as $s): ?>
|
|
<option value="<?= $s['id'] ?>" <?= $filter_supplier == $s['id'] ? 'selected' : '' ?>><?= htmlspecialchars($s['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">From</label>
|
|
<input type="date" name="start_date" class="form-control form-control-sm" value="<?= htmlspecialchars($filter_start) ?>">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">To</label>
|
|
<input type="date" name="end_date" class="form-control form-control-sm" value="<?= htmlspecialchars($filter_end) ?>">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<div class="d-flex gap-2">
|
|
<button type="submit" class="btn btn-sm btn-primary w-100">Filter</button>
|
|
<a href="expenses.php" class="btn btn-sm btn-outline-secondary w-100">Reset</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (isset($_GET['success'])): ?>
|
|
<div class="alert alert-success alert-dismissible fade show border-0 shadow-sm mb-4" role="alert">
|
|
Expense successfully logged.
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="table-responsive">
|
|
<table class="table align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Supplier</th>
|
|
<th>Project</th>
|
|
<th>Amount</th>
|
|
<th>Allocation</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($expenseList)): ?>
|
|
<tr><td colspan="6" class="text-center py-5 text-muted">No expenses found.</td></tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($expenseList as $ex): ?>
|
|
<tr>
|
|
<td class="text-muted"><?= $ex['entry_date'] ?></td>
|
|
<td>
|
|
<strong><?= htmlspecialchars($ex['supplier_name']) ?></strong><br>
|
|
<small class="text-muted"><?= htmlspecialchars($ex['expense_type'] ?? '') ?></small>
|
|
</td>
|
|
<td><?= htmlspecialchars($ex['project_name']) ?></td>
|
|
<td><span class="fw-bold">$<?= number_format((float)$ex['amount'], 2) ?></span></td>
|
|
<td>
|
|
<div class="progress mb-1" style="height: 6px; width: 100px;">
|
|
<div class="progress-bar bg-info" role="progressbar" style="width: <?= $ex['allocation_percent'] ?>%"></div>
|
|
</div>
|
|
<small class="extra-small text-muted"><?= (float)$ex['allocation_percent'] ?>% SR&ED</small>
|
|
</td>
|
|
<td class="text-end">
|
|
<button class="btn btn-sm btn-outline-secondary">Details</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="addExpenseModal" tabindex="-1">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title fw-bold">Add Expense</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="modal-body">
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label small fw-bold">Project</label>
|
|
<select name="project_id" class="form-select" required>
|
|
<option value="">Select Project...</option>
|
|
<?php foreach ($projectList as $p): ?>
|
|
<option value="<?= $p['id'] ?>"><?= htmlspecialchars($p['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Supplier</label>
|
|
<select name="supplier_id" class="form-select" required>
|
|
<option value="">Select Supplier...</option>
|
|
<?php foreach ($supplierList as $s): ?>
|
|
<option value="<?= $s['id'] ?>"><?= htmlspecialchars($s['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Cost Type</label>
|
|
<select name="expense_type_id" class="form-select">
|
|
<?php foreach ($expenseTypeList as $et): ?>
|
|
<option value="<?= $et['id'] ?>"><?= htmlspecialchars($et['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label small fw-bold">Amount ($)</label>
|
|
<input type="number" name="amount" class="form-control" step="0.01" required>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label small fw-bold">Allocation (%)</label>
|
|
<input type="number" name="allocation_percent" class="form-control" value="100">
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label small fw-bold">Date</label>
|
|
<input type="date" name="entry_date" class="form-control" value="<?= date('Y-m-d') ?>">
|
|
</div>
|
|
<div class="col-12 mb-3">
|
|
<label class="form-label small fw-bold">Receipts</label>
|
|
<input type="file" name="attachments[]" class="form-control" multiple>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label small fw-bold">Notes</label>
|
|
<textarea name="notes" class="form-control" rows="2"></textarea>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0">
|
|
<button type="submit" name="add_expense" class="btn btn-primary px-4">Log Expense</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|