-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoals.php
More file actions
271 lines (248 loc) · 13.5 KB
/
Copy pathgoals.php
File metadata and controls
271 lines (248 loc) · 13.5 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
262
263
264
265
266
267
268
269
270
271
<?php
$page_title = "Financial Goals";
require_once __DIR__ . '/autoload.php';
use App\Core\Bootstrap;
Bootstrap::init();
use App\Helpers\AuditHelper;
use App\Helpers\SecurityHelper;
use App\Helpers\Layout;
$user_id = $_SESSION['user_id'];
// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
SecurityHelper::verifyCsrfToken($_POST['csrf_token'] ?? '');
// Permission Check
if (($_SESSION['permission'] ?? 'edit') === 'read_only') {
header("Location: goals.php?error=Unauthorized: Read-only access");
exit();
}
if ($_POST['action'] == 'add_goal') {
$name = $_POST['name'];
$target = $_POST['target_amount'];
$saved = $_POST['current_saved'];
$date = $_POST['target_date'];
$category = $_POST['category'] ?? 'General';
$stmt = $pdo->prepare("INSERT INTO sinking_funds (user_id, tenant_id, name, target_amount, current_saved, target_date, category) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$_SESSION['user_id'], $_SESSION['tenant_id'], $name, $target, $saved, $date, $category]);
AuditHelper::log($pdo, 'add_goal', "Created Goal: $name (Target: $target AED)");
header("Location: goals.php?success=Goal+created");
exit;
} elseif ($_POST['action'] == 'add_funds') {
$id = $_POST['goal_id'];
$amount = $_POST['amount'];
$stmt = $pdo->prepare("UPDATE sinking_funds SET current_saved = current_saved + ? WHERE id = ? AND tenant_id = ? AND user_id = ?");
$stmt->execute([$amount, $id, $_SESSION['tenant_id'], $_SESSION['user_id']]);
AuditHelper::log($pdo, 'add_goal_funds', "Added $amount AED to Goal ID: $id");
header("Location: goals.php?success=Funds+added");
exit;
} elseif ($_POST['action'] == 'delete_goal') {
$id = $_POST['goal_id'];
$stmt = $pdo->prepare("DELETE FROM sinking_funds WHERE id = ? AND tenant_id = ? AND user_id = ?");
$stmt->execute([$id, $_SESSION['tenant_id'], $_SESSION['user_id']]);
AuditHelper::log($pdo, 'delete_goal', "Deleted Goal ID: $id");
header("Location: goals.php?success=Goal+deleted");
exit;
}
}
// Fetch Goals
$stmt = $pdo->prepare("SELECT * FROM sinking_funds WHERE tenant_id = ? ORDER BY target_date ASC");
$stmt->execute([$_SESSION['tenant_id']]);
$goals = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_saved = array_sum(array_column($goals, 'current_saved'));
$total_target = array_sum(array_column($goals, 'target_amount'));
Layout::header();
Layout::sidebar();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h1 class="h3 fw-bold mb-1">Financial Goals</h1>
<p class="text-muted mb-0">Sinking Funds & Big Purchases</p>
</div>
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<button class="btn btn-primary fw-bold" data-bs-toggle="modal" data-bs-target="#addGoalModal">
<i class="fa-solid fa-plus me-2"></i> New Goal
</button>
<?php endif; ?>
</div>
<!-- Summary Row -->
<div class="row g-4 mb-5">
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<h6 class="text-muted fw-bold text-uppercase small mb-2">Total Saved</h6>
<h3 class="fw-bold text-success mb-0">AED <span class="blur-sensitive">
<?php echo number_format($total_saved, 2); ?>
</span></h3>
</div>
</div>
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<h6 class="text-muted fw-bold text-uppercase small mb-2">Total Target</h6>
<h3 class="fw-bold text-primary mb-0">AED <span class="blur-sensitive">
<?php echo number_format($total_target, 2); ?>
</span></h3>
</div>
</div>
<div class="col-md-4">
<div class="glass-panel p-4 h-100">
<h6 class="text-muted fw-bold text-uppercase small mb-2">Active Goals</h6>
<h3 class="fw-bold text-dark mb-0">
<?php echo count($goals); ?>
</h3>
</div>
</div>
</div>
<!-- Goals Grid -->
<div class="row g-4">
<?php if (empty($goals)): ?>
<div class="col-12 text-center py-5">
<i class="fa-solid fa-bullseye empty-state-icon"></i>
<h5 class="fw-bold text-muted">No goals set yet</h5>
<p class="text-muted mb-4">Start saving for that dream car, house, or vacation!</p>
<button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#addGoalModal">Create
Goal</button>
</div>
<?php else: ?>
<?php foreach ($goals as $goal): ?>
<?php
$pct = ($goal['target_amount'] > 0) ? ($goal['current_saved'] / $goal['target_amount']) * 100 : 0;
$pct = min(100, $pct);
// Days left
$days_left = ceil((strtotime($goal['target_date']) - time()) / 86400);
$status_color = ($days_left < 30 && $pct < 100) ? 'text-danger' : 'text-muted';
// Monthly contribution needed
$needed = $goal['target_amount'] - $goal['current_saved'];
$months_left = max(1, ceil($days_left / 30));
$monthly_contrib = ($needed > 0) ? $needed / $months_left : 0;
?>
<div class="col-md-6 col-xl-4">
<div class="glass-panel p-4 h-100 position-relative">
<div class="d-flex justify-content-between align-items-start mb-3">
<div class="d-flex align-items-center gap-3">
<div class="rounded-circle bg-primary-subtle p-3 text-primary">
<i class="fa-solid <?php echo htmlspecialchars($goal['icon'] ?? 'fa-bullseye'); ?> fa-lg"></i>
</div>
<div>
<h5 class="fw-bold mb-0">
<?php echo htmlspecialchars($goal['name']); ?>
</h5>
<div class="small text-muted">
<span
class="badge bg-light text-dark border me-1"><?php echo htmlspecialchars($goal['category'] ?? 'General'); ?></span>
<?php echo $days_left > 0 ? $days_left . ' days left' : 'Due Passed'; ?>
</div>
</div>
</div>
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<div class="dropdown">
<button class="btn btn-link text-muted p-0" data-bs-toggle="dropdown">
<i class="fa-solid fa-ellipsis-vertical"></i>
</button>
<ul class="dropdown-menu dropdown-menu-end border-0 shadow">
<li>
<form method="POST"
onsubmit="return confirmSubmit(this, 'Delete goal: <?php echo addslashes(htmlspecialchars($goal['name'])); ?> (Target: AED <?php echo number_format($goal['target_amount'], 2); ?>)?');">
<input type="hidden" name="csrf_token"
value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="delete_goal">
<input type="hidden" name="goal_id" value="<?php echo $goal['id']; ?>">
<button type="submit" class="dropdown-item text-danger"><i
class="fa-solid fa-trash me-2"></i> Delete</button>
</form>
</li>
</ul>
</div>
<?php else: ?>
<i class="fa-solid fa-lock text-muted small" title="Read Only"></i>
<?php endif; ?>
</div>
<div class="mb-3">
<div class="d-flex justify-content-between small fw-bold mb-1">
<span class="text-primary">AED
<?php echo number_format($goal['current_saved'], 2); ?>
</span>
<span class="text-muted">of AED
<?php echo number_format($goal['target_amount'], 2); ?>
</span>
</div>
<div class="progress-wrapper">
<progress class="w-100" value="<?php echo $pct; ?>" max="100"></progress>
</div>
</div>
<?php if ($pct < 100): ?>
<div class="alert alert-light border-0 small mb-3 p-2 text-center text-muted">
<i class="fa-solid fa-piggy-bank me-1 text-success"></i> Save <b>AED
<?php echo number_format($monthly_contrib, 2); ?>/mo
</b> to hit goal
</div>
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<form method="POST" class="d-flex gap-2">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="add_funds">
<input type="hidden" name="goal_id" value="<?php echo $goal['id']; ?>">
<input type="number" name="amount" class="form-control form-control-sm" placeholder="Add Amount"
required>
<button type="submit" class="btn btn-sm btn-success px-3"><i class="fa-solid fa-plus"></i></button>
</form>
<?php endif; ?>
<?php else: ?>
<div class="alert alert-success border-0 small mb-0 text-center fw-bold">
<i class="fa-solid fa-check-circle me-1"></i> Goal Achieved!
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Add Goal Modal -->
<div class="modal fade" id="addGoalModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content glass-panel border-0">
<div class="modal-header border-0">
<h5 class="modal-title fw-bold">New Financial Goal</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="add_goal">
<div class="mb-3">
<label for="goalName" class="form-label">Goal Name</label>
<input type="text" name="name" id="goalName" class="form-control"
placeholder="e.g. New Car, Europe Trip..." required>
</div>
<div class="mb-3">
<label for="goalCategory" class="form-label">Category</label>
<select name="category" id="goalCategory" class="form-select">
<option value="General">General Savings</option>
<option value="Travel">Travel</option>
<option value="Automobile">Automobile</option>
<option value="Electronics">Electronics</option>
<option value="Emergency">Emergency Fund</option>
<option value="Investments">Investments</option>
<option value="Other">Other</option>
</select>
</div>
<div class="row g-3 mb-3">
<div class="col-6">
<label for="targetAmount" class="form-label">Target Amount</label>
<input type="number" name="target_amount" id="targetAmount" class="form-control"
placeholder="10000" required>
</div>
<div class="col-6">
<label for="currentSaved" class="form-label">Already Saved</label>
<input type="number" name="current_saved" id="currentSaved" class="form-control" value="0">
</div>
</div>
<div class="mb-3">
<label for="targetDate" class="form-label">Target Date</label>
<input type="date" name="target_date" id="targetDate" class="form-control" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary fw-bold py-2">Create Goal</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php Layout::footer(); ?>