forked from Aryansabasana/bringcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpenses.html
More file actions
634 lines (572 loc) · 17.9 KB
/
expenses.html
File metadata and controls
634 lines (572 loc) · 17.9 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Admin Expenses - ExpenseEase</title>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap" rel="stylesheet" />
<style>
/* Reset & Base Styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Montserrat', sans-serif;
line-height: 1.6;
background-color: #f0f4f8;
color: #333;
padding: 20px;
}
/* Header */
.header {
background-color: #4CAF50;
padding: 15px 20px;
border-radius: 10px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 1.5em;
font-weight: 700;
color: #fff;
letter-spacing: 1px;
}
.user-info {
color: #fff;
font-weight: 500;
}
.logout-btn {
background: rgba(255,255,255,0.2);
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
cursor: pointer;
font-weight: 600;
transition: background 0.3s;
}
.logout-btn:hover {
background: rgba(255,255,255,0.3);
}
/* Page Title */
.page-title {
color: #4CAF50;
margin-bottom: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.page-title h2 {
font-size: 1.8em;
}
/* Stats Cards */
.stats-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.stat-card {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
text-align: center;
}
.stat-value {
font-size: 1.8em;
font-weight: 700;
margin: 10px 0;
}
.stat-label {
color: #666;
font-weight: 500;
}
.stat-total { border-top: 4px solid #4CAF50; }
.stat-pending { border-top: 4px solid #ffc107; }
.stat-approved { border-top: 4px solid #28a745; }
.stat-declined { border-top: 4px solid #dc3545; }
/* Controls */
.controls {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
margin-bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 15px;
align-items: end;
}
.control-group {
display: flex;
flex-direction: column;
min-width: 150px;
}
.control-group label {
font-weight: 600;
margin-bottom: 5px;
color: #555;
}
.control-group select, .control-group input {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-family: 'Montserrat', sans-serif;
}
.btn {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
font-weight: 600;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #45a049;
}
/* Tables */
.table-container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
margin-bottom: 20px;
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th, td {
border: 1px solid #ddd;
padding: 12px 15px;
text-align: left;
}
th {
background-color: #f8f9fa;
font-weight: 600;
color: #495057;
}
tr:nth-child(even) {
background-color: #f8f9fa;
}
.status-badge {
padding: 5px 10px;
border-radius: 20px;
font-size: 0.8em;
font-weight: 600;
}
.status-pending {
background-color: #fff3cd;
color: #856404;
}
.status-approved {
background-color: #d4edda;
color: #155724;
}
.status-declined {
background-color: #f8d7da;
color: #721c24;
}
/* Empty State */
.empty-state {
text-align: center;
padding: 40px 20px;
color: #6c757d;
}
.empty-state i {
font-size: 3em;
margin-bottom: 15px;
opacity: 0.5;
}
/* Access Denied */
.access-denied {
text-align: center;
padding: 60px 20px;
background: white;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.access-denied h2 {
color: #dc3545;
margin-bottom: 15px;
}
.access-denied p {
color: #666;
margin-bottom: 20px;
}
/* Responsive */
@media (max-width: 768px) {
.controls {
flex-direction: column;
align-items: stretch;
}
.control-group {
width: 100%;
}
.stats-container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<!-- Header -->
<div class="header">
<div class="logo">ExpenseEase - Admin Panel</div>
<div class="user-info">
<span id="userDisplay">Welcome, Admin</span>
<button class="logout-btn" onclick="logout()">Logout</button>
</div>
</div>
<!-- Page Title -->
<div class="page-title">
<h2>All Expenses Management</h2>
<div id="reportPeriod">Viewing all expenses</div>
</div>
<!-- Main Content -->
<div id="mainContent">
<!-- This will be populated based on user role -->
</div>
<script>
// Global variables
let currentUser = null;
let allExpenses = [];
let allRequests = [];
let allUsers = [];
// Initialize the page
document.addEventListener('DOMContentLoaded', function() {
// Check authentication and role
currentUser = JSON.parse(localStorage.getItem('loggedInUser'));
if (!currentUser) {
showAccessDenied('Please login first.');
return;
}
// Only allow admin access
if (currentUser.role !== 'admin') {
showAccessDenied('Access denied: Only administrators can view this page.');
return;
}
// Update UI based on user role
document.getElementById('userDisplay').textContent = `Welcome, ${currentUser.name || currentUser.username} (Admin)`;
// Load data and generate initial view
loadData();
initializeAdminView();
});
// Load data from localStorage
function loadData() {
allExpenses = JSON.parse(localStorage.getItem('expenses')) || [];
allRequests = JSON.parse(localStorage.getItem('expenseRequests')) || [];
allUsers = JSON.parse(localStorage.getItem('users')) || [];
}
// Initialize admin view
function initializeAdminView() {
const mainContent = document.getElementById('mainContent');
// Create stats cards
const statsHTML = `
<div class="stats-container">
<div class="stat-card stat-total">
<div class="stat-label">Total Expenses</div>
<div class="stat-value" id="totalExpenses">$0.00</div>
<div>All Time</div>
</div>
<div class="stat-card stat-approved">
<div class="stat-label">Approved Requests</div>
<div class="stat-value" id="approvedRequests">0</div>
<div>Fully processed</div>
</div>
<div class="stat-card stat-pending">
<div class="stat-label">Pending Requests</div>
<div class="stat-value" id="pendingRequests">0</div>
<div>Awaiting approval</div>
</div>
<div class="stat-card stat-declined">
<div class="stat-label">Declined Requests</div>
<div class="stat-value" id="declinedRequests">0</div>
<div>Not approved</div>
</div>
</div>
`;
// Create controls
const controlsHTML = `
<div class="controls">
<div class="control-group">
<label for="timeRange">Time Range</label>
<select id="timeRange">
<option value="all">All Time</option>
<option value="7">Last 7 Days</option>
<option value="30" selected>Last 30 Days</option>
<option value="90">Last 90 Days</option>
<option value="365">Last Year</option>
</select>
</div>
<div class="control-group">
<label for="categoryFilter">Category</label>
<select id="categoryFilter">
<option value="all">All Categories</option>
<option value="Travel">Travel</option>
<option value="Meals">Meals</option>
<option value="Equipment">Equipment</option>
<option value="Training">Training</option>
<option value="Other">Other</option>
</select>
</div>
<div class="control-group">
<label for="employeeFilter">Employee</label>
<select id="employeeFilter">
<option value="all">All Employees</option>
</select>
</div>
<button class="btn" onclick="generateReport()">Generate Report</button>
<button class="btn" onclick="exportExpenses()">Export to CSV</button>
</div>
`;
// Create tables section
const tablesHTML = `
<div class="table-container">
<h3>All Expenses</h3>
<table id="expensesTable">
<thead>
<tr>
<th>Date</th>
<th>Employee</th>
<th>Description</th>
<th>Category</th>
<th>Amount</th>
<th>Status</th>
</tr>
</thead>
<tbody id="expensesTableBody">
<!-- Expenses will be loaded here -->
</tbody>
</table>
<div id="noExpenses" class="empty-state" style="display:none;">
<div>No expenses found for the selected criteria</div>
</div>
</div>
<div class="table-container">
<h3>Expense Requests</h3>
<table id="requestsTable">
<thead>
<tr>
<th>Submitted</th>
<th>Employee</th>
<th>Description</th>
<th>Category</th>
<th>Amount</th>
<th>Status</th>
<th>Approved/Declined By</th>
<th>Decision Date</th>
</tr>
</thead>
<tbody id="requestsTableBody">
<!-- Requests will be loaded here -->
</tbody>
</table>
<div id="noRequests" class="empty-state" style="display:none;">
<div>No expense requests found for the selected criteria</div>
</div>
</div>
`;
mainContent.innerHTML = statsHTML + controlsHTML + tablesHTML;
// Load employee filter options
loadEmployeeFilter();
// Generate initial report
generateReport();
}
// Load employee filter options
function loadEmployeeFilter() {
const employeeFilter = document.getElementById('employeeFilter');
employeeFilter.innerHTML = '<option value="all">All Employees</option>';
// Show all employees for admin
allUsers.forEach(user => {
if (user.role === 'employee') {
employeeFilter.innerHTML += `<option value="${user.id}">${user.name || user.username}</option>`;
}
});
}
// Generate report based on filters
function generateReport() {
loadData();
// Get filter values
const timeRange = document.getElementById('timeRange').value;
const categoryFilter = document.getElementById('categoryFilter').value;
const employeeFilter = document.getElementById('employeeFilter').value;
let startDate, endDate;
if (timeRange === 'all') {
// No date filtering for "all time"
startDate = new Date(0); // Beginning of time
endDate = new Date(); // Current date
} else {
endDate = new Date();
startDate = new Date();
startDate.setDate(startDate.getDate() - parseInt(timeRange));
}
// Filter expenses and requests
const filteredExpenses = filterExpenses(startDate, endDate, categoryFilter, employeeFilter);
const filteredRequests = filterRequests(startDate, endDate, categoryFilter, employeeFilter);
// Update statistics
updateStatistics(filteredExpenses, filteredRequests);
// Update tables
updateExpensesTable(filteredExpenses);
updateRequestsTable(filteredRequests);
}
// Filter expenses based on criteria
function filterExpenses(startDate, endDate, category, employee) {
return allExpenses.filter(expense => {
const expenseDate = new Date(expense.date);
const categoryMatch = category === 'all' || expense.category === category;
const employeeMatch = employee === 'all' || expense.userId === employee;
const dateMatch = expenseDate >= startDate && expenseDate <= endDate;
return categoryMatch && employeeMatch && dateMatch;
});
}
// Filter requests based on criteria
function filterRequests(startDate, endDate, category, employee) {
return allRequests.filter(request => {
const requestDate = new Date(request.submittedDate);
const categoryMatch = category === 'all' || request.category === category;
const employeeMatch = employee === 'all' || request.employeeId === employee;
const dateMatch = requestDate >= startDate && requestDate <= endDate;
return categoryMatch && employeeMatch && dateMatch;
});
}
// Update statistics cards
function updateStatistics(expenses, requests) {
// Calculate totals
const totalExpenses = expenses.reduce((sum, expense) => sum + expense.amount, 0);
const approvedRequests = requests.filter(req => req.status === 'approved').length;
const pendingRequests = requests.filter(req => req.status === 'pending').length;
const declinedRequests = requests.filter(req => req.status === 'declined').length;
// Update UI
document.getElementById('totalExpenses').textContent = `$${totalExpenses.toFixed(2)}`;
document.getElementById('approvedRequests').textContent = approvedRequests;
document.getElementById('pendingRequests').textContent = pendingRequests;
document.getElementById('declinedRequests').textContent = declinedRequests;
}
// Update expenses table
function updateExpensesTable(expenses) {
const tbody = document.getElementById('expensesTableBody');
const noExpenses = document.getElementById('noExpenses');
if (expenses.length === 0) {
tbody.innerHTML = '';
noExpenses.style.display = 'block';
return;
}
noExpenses.style.display = 'none';
let html = '';
expenses.forEach(expense => {
const user = allUsers.find(u => u.id === expense.userId) || {};
html += `
<tr>
<td>${expense.date}</td>
<td>${user.name || user.username || 'Unknown'}</td>
<td>${expense.title}</td>
<td>${expense.category}</td>
<td>$${expense.amount.toFixed(2)}</td>
<td><span class="status-badge status-approved">Recorded</span></td>
</tr>
`;
});
tbody.innerHTML = html;
}
// Update requests table
function updateRequestsTable(requests) {
const tbody = document.getElementById('requestsTableBody');
const noRequests = document.getElementById('noRequests');
if (requests.length === 0) {
tbody.innerHTML = '';
noRequests.style.display = 'block';
return;
}
noRequests.style.display = 'none';
let html = '';
requests.forEach(request => {
let statusClass = 'status-pending';
if (request.status === 'approved') statusClass = 'status-approved';
if (request.status === 'declined') statusClass = 'status-declined';
const approvedBy = request.status === 'approved' ? request.approvedBy :
request.status === 'declined' ? request.declinedBy : 'N/A';
html += `
<tr>
<td>${request.submittedDate}</td>
<td>${request.employeeName}</td>
<td>${request.title}</td>
<td>${request.category}</td>
<td>$${request.amount.toFixed(2)}</td>
<td><span class="status-badge ${statusClass}">${request.status}</span></td>
<td>${approvedBy}</td>
<td>${request.decisionDate || 'N/A'}</td>
</tr>
`;
});
tbody.innerHTML = html;
}
// Export expenses to CSV
function exportExpenses() {
const expenses = JSON.parse(localStorage.getItem('expenses')) || [];
const requests = JSON.parse(localStorage.getItem('expenseRequests')) || [];
if (expenses.length === 0 && requests.length === 0) {
alert('No data to export.');
return;
}
// Create CSV content
let csvContent = "Expense Report\n\n";
// Add expenses
csvContent += "EXPENSES\n";
csvContent += "Date,Employee,Description,Category,Amount\n";
expenses.forEach(expense => {
const user = allUsers.find(u => u.id === expense.userId) || {};
csvContent += `"${expense.date}","${user.name || user.username}","${expense.title}","${expense.category}",${expense.amount}\n`;
});
// Add requests
csvContent += "\nEXPENSE REQUESTS\n";
csvContent += "Submitted Date,Employee,Description,Category,Amount,Status,Decision By,Decision Date\n";
requests.forEach(request => {
csvContent += `"${request.submittedDate}","${request.employeeName}","${request.title}","${request.category}",${request.amount},"${request.status}","${request.approvedBy || request.declinedBy || 'N/A'}","${request.decisionDate || 'N/A'}"\n`;
});
// Create and download file
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('hidden', '');
a.setAttribute('href', url);
a.setAttribute('download', 'expense_report.csv');
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
alert('Expense report exported successfully!');
}
// Show access denied message
function showAccessDenied(message) {
const mainContent = document.getElementById('mainContent');
mainContent.innerHTML = `
<div class="access-denied">
<h2>Access Denied</h2>
<p>${message}</p>
<button class="btn" onclick="goToLogin()">Go to Login</button>
</div>
`;
}
// Redirect to login
function goToLogin() {
window.location.href = 'login.html';
}
// Logout function
function logout() {
localStorage.removeItem('loggedInUser');
alert('Logged out successfully.');
window.location.href = 'index.html';
}
</script>
</body>
</html>