-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget.php
More file actions
261 lines (232 loc) · 10.4 KB
/
Copy pathbudget.php
File metadata and controls
261 lines (232 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
$page_title = "Smart Budget";
require_once __DIR__ . '/autoload.php';
use App\Core\Bootstrap;
use App\Helpers\Layout;
Bootstrap::init();
Layout::header();
Layout::sidebar();
$user_id = $_SESSION['user_id'];
$month = date('n');
$year = date('Y');
// 1. Get Total Income for this month
$stmt = $pdo->prepare("SELECT SUM(amount) FROM income WHERE tenant_id = ? AND MONTH(income_date) = ? AND YEAR(income_date) = ?");
$stmt->execute([$_SESSION['tenant_id'], $month, $year]);
$total_income = $stmt->fetchColumn() ?: 0;
// 2. Get Expenses grouped by Category
$stmt = $pdo->prepare("SELECT category, SUM(amount) as total FROM expenses WHERE tenant_id = ? AND MONTH(expense_date) = ? AND YEAR(expense_date) = ? GROUP BY category");
$stmt->execute([$_SESSION['tenant_id'], $month, $year]);
$expenses = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// 3. Define Buckets
$needs_cats = ['Grocery', 'Medical', 'Utilities', 'Transport', 'Education'];
$wants_cats = ['Food', 'Shopping', 'Entertainment', 'Travel', 'Other'];
$total_needs = 0;
$total_wants = 0;
foreach ($expenses as $cat => $amount) {
if (in_array($cat, $needs_cats)) {
$total_needs += $amount;
} else {
$total_wants += $amount; // Default to wants if unknown
}
}
// 4. Calculate Savings (Remaining)
$total_spent = $total_needs + $total_wants;
$total_savings = $total_income - $total_spent;
// Avoid division by zero
$income_base = $total_income > 0 ? $total_income : 1;
$needs_pct = ($total_needs / $income_base) * 100;
$wants_pct = ($total_wants / $income_base) * 100;
$savings_pct = ($total_savings / $income_base) * 100;
// Status Logic
function getStatusColor($pct, $target, $is_savings = false)
{
if ($is_savings) {
return $pct >= $target ? "success" : "danger";
}
return $pct <= $target ? "success" : "danger";
}
$needs_color = getStatusColor($needs_pct, 50);
$wants_color = getStatusColor($wants_pct, 30);
$savings_color = getStatusColor($savings_pct, 20, true);
?>
<div class="mb-4">
<h1 class="h3 fw-bold mb-1">Smart Budget <span class="badge bg-light text-dark border ms-2">50/30/20 Rule</span>
</h1>
<p class="text-muted">Analysis for
<?php echo date('F Y'); ?>
</p>
</div>
<?php if ($total_income == 0): ?>
<div class="alert alert-warning">
<i class="fa-solid fa-triangle-exclamation me-2"></i>
No income recorded for this month. <a href="add_income.php" class="alert-link">Add Income</a> to see your budget
analysis.
</div>
<?php endif; ?>
<div class="row g-4 mb-5">
<!-- Needs (50%) -->
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<div class="d-flex justify-content-between mb-3">
<span class="badge bg-<?php echo $needs_color; ?>-subtle text-<?php echo $needs_color; ?>">Target:
50%</span>
<i class="fa-solid fa-house-chimney text-<?php echo $needs_color; ?> fa-lg"></i>
</div>
<h4 class="fw-bold mb-0">Needs</h4>
<div class="display-6 fw-bold my-2">AED <span class="blur-sensitive">
<?php echo number_format($total_needs); ?>
</span></div>
<progress class="progress w-100" style="height: 10px;" value="<?php echo min($needs_pct, 100); ?>"
max="100"></progress>
<div class="mt-2 small text-muted">
You have used <strong>
<?php echo number_format($needs_pct, 1); ?>%
</strong> of your income.
</div>
<div class="mt-2 text-muted x-small">
Includes: Grocery, Rent, Bills, Transport
</div>
</div>
</div>
<!-- Wants (30%) -->
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<div class="d-flex justify-content-between mb-3">
<span class="badge bg-<?php echo $wants_color; ?>-subtle text-<?php echo $wants_color; ?>">Target:
30%</span>
<i class="fa-solid fa-gamepad text-<?php echo $wants_color; ?> fa-lg"></i>
</div>
<h4 class="fw-bold mb-0">Wants</h4>
<div class="display-6 fw-bold my-2">AED <span class="blur-sensitive">
<?php echo number_format($total_wants); ?>
</span></div>
<progress class="progress w-100" style="height: 10px;" value="<?php echo min($wants_pct, 100); ?>"
max="100"></progress>
<div class="mt-2 small text-muted">
You have used <strong>
<?php echo number_format($wants_pct, 1); ?>%
</strong> of your income.
</div>
<div class="mt-2 text-muted x-small">
Includes: Dining, Shopping, Travel, Entertainment
</div>
</div>
</div>
<!-- Savings (20%) -->
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<div class="d-flex justify-content-between mb-3">
<span class="badge bg-<?php echo $savings_color; ?>-subtle text-<?php echo $savings_color; ?>">Target:
20%</span>
<i class="fa-solid fa-piggy-bank text-<?php echo $savings_color; ?> fa-lg"></i>
</div>
<h4 class="fw-bold mb-0">Savings</h4>
<div class="display-6 fw-bold my-2">AED <span class="blur-sensitive">
<?php echo number_format($total_savings); ?>
</span></div>
<progress class="progress w-100" style="height: 10px;" value="<?php echo min(max($savings_pct, 0), 100); ?>"
max="100"></progress>
<div class="mt-2 small text-muted">
You have saved <strong>
<?php echo number_format($savings_pct, 1); ?>%
</strong> of your income.
</div>
<div class="mt-2 text-muted x-small">
Remaining Income (Income - Expenses)
</div>
</div>
</div>
</div>
<div class="glass-panel p-4 mb-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h5 class="fw-bold mb-0">Category Budgets vs Actuals</h5>
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<a href="manage_budgets.php" class="btn btn-outline-primary btn-sm rounded-pill px-3">Manage Targets</a>
<?php endif; ?>
</div>
<?php
// Fetch specifically defined budgets
$stmt = $pdo->prepare("SELECT category, amount FROM budgets WHERE tenant_id = ? AND month = ? AND year = ?");
$stmt->execute([$_SESSION['tenant_id'], $month, $year]);
$cat_budgets = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
?>
<?php if (empty($cat_budgets)): ?>
<div class="text-center py-4 bg-light rounded-4">
<p class="text-muted mb-0">No specific category budgets defined for this month.</p>
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<a href="manage_budgets.php" class="small text-primary">Set individual targets</a>
<?php endif; ?>
</div>
<?php else: ?>
<div class="row g-4">
<?php foreach ($cat_budgets as $cat => $limit):
$spent = $expenses[$cat] ?? 0;
$pct = ($spent / $limit) * 100;
$var = $limit - $spent;
$color = 'success';
if ($pct > 80) {
$color = 'warning';
}
if ($pct > 100) {
$color = 'danger';
}
?>
<div class="col-md-6 col-lg-4">
<div class="p-3 border rounded-4 bg-white hover-shadow transition-all">
<div class="d-flex justify-content-between align-items-start mb-2">
<div class="fw-bold"><?php echo $cat; ?></div>
<div class="badge bg-<?php echo $color; ?>-subtle text-<?php echo $color; ?>">
<?php echo number_format($pct, 0); ?>%
</div>
</div>
<progress class="progress w-100 mb-2" style="height: 8px;" value="<?php echo min($pct, 100); ?>"
max="100"></progress>
<div class="d-flex justify-content-between small text-muted">
<span>Spent: AED <?php echo number_format($spent); ?></span>
<span>Goal: <?php echo number_format($limit); ?></span>
</div>
<?php if ($var < 0): ?>
<div class="mt-2 text-danger x-small fw-bold">
<i class="fa-solid fa-arrow-up"></i> Over by AED <?php echo number_format(abs($var)); ?>
</div>
<?php else: ?>
<div class="mt-2 text-success x-small fw-bold">
<i class="fa-solid fa-arrow-down"></i> AED <?php echo number_format($var); ?> remaining
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="glass-panel p-4">
<h5 class="fw-bold mb-3">Budget Insights</h5>
<?php if ($savings_pct >= 20): ?>
<p class="text-success mb-0"><i class="fa-solid fa-circle-check me-2"></i> You are hitting your savings goal! Great
job.</p>
<?php else: ?>
<p class="text-danger mb-0"><i class="fa-solid fa-circle-exclamation me-2"></i> You are falling short of the 20%
savings target. Try reducing your 'Wants'.</p>
<?php endif; ?>
<?php if ($needs_pct > 50): ?>
<p class="text-warning mt-2 mb-0"><i class="fa-solid fa-triangle-exclamation me-2"></i> Your 'Needs' are high
(>50%). Consider reviewing recurring bills.</p>
<?php endif; ?>
<?php
$over_cats = [];
foreach ($cat_budgets as $cat => $limit) {
if (($expenses[$cat] ?? 0) > $limit) {
$over_cats[] = $cat;
}
}
if (!empty($over_cats)):
?>
<p class="text-danger mt-2 mb-0">
<i class="fa-solid fa-circle-xmark me-2"></i> You have exceeded your budget in:
<strong><?php echo implode(', ', $over_cats); ?></strong>
</p>
<?php endif; ?>
</div>
<?php Layout::footer(); ?>
// Structural Audit Complete