120 lines
4.9 KiB
PHP
120 lines
4.9 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$projectId = $_GET['id'] ?? null;
|
|
if (!$projectId) {
|
|
header("Location: projects.php");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$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 billing 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 billingMonthly WHERE projectId = :projectId AND month = :month");
|
|
$stmt->execute([':projectId' => $projectId, ':month' => $monthStr]);
|
|
$count = $stmt->fetchColumn();
|
|
|
|
if ($count == 0) {
|
|
$insertStmt = $pdo->prepare("INSERT INTO billingMonthly (projectId, month, amount) VALUES (:projectId, :month, 0)");
|
|
$insertStmt->execute([':projectId' => $projectId, ':month' => $monthStr]);
|
|
}
|
|
|
|
$currentMonth->modify('+1 month');
|
|
}
|
|
|
|
// Fetch billing data
|
|
$stmt = $pdo->prepare("SELECT * FROM billingMonthly WHERE projectId = :projectId ORDER BY month");
|
|
$stmt->execute([':projectId' => $projectId]);
|
|
$billingData = $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>Billing - <?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">Billing - <?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>
|
|
<button id="save-billing" class="btn btn-primary">Save</button>
|
|
<a href="export_billing.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;">Billing</th>
|
|
<?php
|
|
$currentMonth = clone $startDate;
|
|
while ($currentMonth <= $endDate) {
|
|
echo '<th>' . $currentMonth->format('M Y') . '</th>';
|
|
$currentMonth->modify('+1 month');
|
|
}
|
|
?>
|
|
<th style="width: 150px;">Total</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>Billing</td>
|
|
<?php
|
|
$totalBilling = 0;
|
|
foreach ($billingData as $billingMonth) {
|
|
$totalBilling += $billingMonth['amount'];
|
|
echo '<td><input type="text" class="form-control billing-amount text-end" data-month="' . $billingMonth['month'] . '" value="' . htmlspecialchars(number_format($billingMonth['amount'], 2, ',', '.')) . '"></td>';
|
|
}
|
|
?>
|
|
<td id="total-billing" class="fw-bold text-end"><?php echo number_format($totalBilling, 2, ',', '.'); ?></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/billing.js?v=<?php echo time(); ?>"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const projectId = <?php echo json_encode($projectId); ?>;
|
|
initBillingPage(projectId);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|