36293-vm/expenses.php
2025-11-26 10:23:27 +00:00

121 lines
4.7 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$projectId = $_GET['id'] ?? null;
if (!$projectId) {
header("Location: projects.php");
exit();
}
try {
$pdo = db();
// I will execute the migration to create the expensesMonthly table
$migrationFile = __DIR__ . '/db/migrations/008_create_expenses_monthly_table.sql';
if (file_exists($migrationFile)) {
$sql = file_get_contents($migrationFile);
$pdo->exec($sql);
}
$stmt = $pdo->prepare("SELECT * FROM projects WHERE id = :id");
$stmt->execute([':id' => $projectId]);
$project = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$project) {
header("Location: projects.php");
exit();
}
// Initialize expenses records if they don't exist
$startDate = new DateTime($project['startDate']);
$endDate = new DateTime($project['endDate']);
$currentMonth = clone $startDate;
while ($currentMonth <= $endDate) {
$monthStr = $currentMonth->format('Y-m-01');
$stmt = $pdo->prepare("SELECT COUNT(*) FROM expensesMonthly WHERE projectId = :projectId AND month = :month");
$stmt->execute([':projectId' => $projectId, ':month' => $monthStr]);
$count = $stmt->fetchColumn();
if ($count == 0) {
$insertStmt = $pdo->prepare("INSERT INTO expensesMonthly (projectId, month, amount) VALUES (:projectId, :month, 0)");
$insertStmt->execute([':projectId' => $projectId, ':month' => $monthStr]);
}
$currentMonth->modify('+1 month');
}
// Fetch expenses data
$stmt = $pdo->prepare("SELECT * FROM expensesMonthly WHERE projectId = :projectId ORDER BY month");
$stmt->execute([':projectId' => $projectId]);
$expensesData = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$db_error = "Database error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Expenses - <?php echo htmlspecialchars($project['name']); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="main-wrapper">
<main class="content-wrapper">
<div class="page-header">
<h1 class="h2">Expenses - <?php echo htmlspecialchars($project['name']); ?></h1>
<div class="header-actions">
<a href="project_details.php?id=<?php echo $projectId; ?>" class="btn btn-secondary">Return</a>
<a href="export_expenses.php?id=<?php echo $projectId; ?>" class="btn btn-secondary">Export to Excel</a>
</div>
</div>
<?php if (isset($db_error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($db_error); ?></div>
<?php endif; ?>
<div class="card">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 150px;">Expenses</th>
<?php
$currentMonth = clone $startDate;
while ($currentMonth <= $endDate) {
echo '<th>' . $currentMonth->format('M Y') . '</th>';
$currentMonth->modify('+1 month');
}
?>
</tr>
</thead>
<tbody>
<tr>
<td>Expenses</td>
<?php
foreach ($expensesData as $expensesMonth) {
echo '<td><input type="number" class="form-control expenses-amount" data-month="' . $expensesMonth['month'] . '" value="' . htmlspecialchars($expensesMonth['amount']) . '"></td>';
}
?>
</tr>
</tbody>
</table>
</div>
</div>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/expenses.js?v=<?php echo time(); ?>"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const projectId = <?php echo json_encode($projectId); ?>;
initExpensesPage(projectId);
});
</script>
</body>
</html>