-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsales.php
More file actions
604 lines (547 loc) · 24.2 KB
/
sales.php
File metadata and controls
604 lines (547 loc) · 24.2 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
<?php
session_start();
require_once 'connection.php';
// Check if the user is logged in and verified
if (!isset($_SESSION['user_id'])) {
// User is not logged in
header("Location: login.php");
exit();
}
// Check if company is selected
if (!isset($_SESSION['selected_company_id'])) {
header("Location: select_company_message.php");
exit();
}
// Fetch user details from DB
$user_id = $_SESSION['user_id'];
$stmt = $con->prepare("SELECT first_name, last_name FROM users WHERE user_id = ? AND is_verified = 1");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows !== 1) {
// Not found or not verified
header("Location: login.php");
exit();
}
$user = $result->fetch_assoc();
$full_name = $user['first_name'] . ' ' . $user['last_name'];
// Get current month and year
$current_month = isset($_GET['month']) ? intval($_GET['month']) : intval(date('m'));
$current_year = isset($_GET['year']) ? intval($_GET['year']) : intval(date('Y'));
// Get search term if any
$search = isset($_GET['search']) ? $_GET['search'] : '';
// Get sort column and direction
$sort_column = isset($_GET['sort']) ? $_GET['sort'] : 'date';
$sort_direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
// Fetch companies for the user
$companies_stmt = $con->prepare("
SELECT c.*
FROM companies c
JOIN user_companies uc ON c.company_id = uc.company_id
WHERE uc.user_id = ?
ORDER BY c.company_name
");
$companies_stmt->bind_param("i", $user_id);
$companies_stmt->execute();
$companies_result = $companies_stmt->get_result();
$stmt->close();
$con->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/x-icon" href="img/favicon.ico" />
<title>FinTrack | Sales</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
</head>
<body class="bg-gray-100 font-sans">
<div class="flex min-h-screen">
<!-- Sidebar -->
<aside id="sidebar" class="w-64 bg-white shadow-md fixed inset-y-0 left-0 transform -translate-x-full md:translate-x-0 transition-transform duration-200 z-50">
<div class="items-center border-b p-4">
<img class="w-96" src="img/eclick.png" alt="Logo" />
</div>
<nav class="p-4 mb-[6rem]">
<ul class="space-y-2 text-gray-700">
<li><a href="expenses.php" class="block py-2 px-3 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Expenses</a></li>
<li><a href="sales.php" class="block py-2 px-3 rounded bg-[#e4fbeaff] text-[#1bb34cff] font-semibold">Sales</a></li>
<li><a href="trial_balance.php" class="block py-2 px-3 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Trial Balance</a></li>
<li><a href="income_statements.php" class="block py-2 px-3 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Income Statements</a></li>
<li><a href="balance_sheet.php" class="block py-2 px-3 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Balance Sheet</a></li>
<li><a href="profile.php" class="block py-2 px-3 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Profile</a></li>
</ul>
</nav>
<a href="logout.php" class="block py-2 px-8 rounded hover:bg-[#e4fbeaff] hover:text-[#1bb34cff]">Logout</a>
</aside>
<!-- Overlay (only on mobile when sidebar is open) -->
<div id="overlay" class="fixed inset-0 bg-black bg-opacity-40 hidden z-40 md:hidden"></div>
<!-- Main Content -->
<main class="flex-1 p-6 md:ml-64">
<header class="flex justify-between items-center mb-6">
<div>
<h1 class="text-5xl font-semibold text-gray-800">Sales</h1>
<?php if (isset($_SESSION['selected_company_name'])): ?>
<p class="text-lg text-gray-600 mt-2">for <?php echo htmlspecialchars($_SESSION['selected_company_name']); ?></p>
<?php endif; ?>
</div>
<button id="menuBtn" class="md:hidden px-4 py-2 bg-blue-200 text-white rounded">
<div class="text-2xl font-bold text-blue-500">☰</div>
</button>
</header>
<!-- Controls Section -->
<div class="bg-white rounded-lg shadow-md p-6 mb-6">
<div class="flex flex-wrap gap-4 justify-between items-center">
<!-- Month/Year Selector -->
<div class="flex gap-4">
<select id="monthSelect" class="rounded border p-2">
<?php
$months = [
1 => 'January', 2 => 'February', 3 => 'March',
4 => 'April', 5 => 'May', 6 => 'June',
7 => 'July', 8 => 'August', 9 => 'September',
10 => 'October', 11 => 'November', 12 => 'December'
];
foreach ($months as $num => $name) {
$selected = $num === $current_month ? 'selected' : '';
echo "<option value='$num' $selected>$name</option>";
}
?>
</select>
<select id="yearSelect" class="rounded border p-2">
<?php
$start_year = 2020;
$end_year = date('Y') + 1;
for ($year = $start_year; $year <= $end_year; $year++) {
$selected = $year === $current_year ? 'selected' : '';
echo "<option value='$year' $selected>$year</option>";
}
?>
</select>
</div>
<!-- Search Box -->
<div class="flex-1 max-w-md mx-4">
<input type="text" id="searchBox" placeholder="Search Category"
class="w-full rounded border p-2" value="<?php echo htmlspecialchars($search); ?>">
</div>
<!-- Add New Sale Button -->
<div class="flex gap-4">
<button id="addSaleBtn" class="bg-[#1bb34cff] text-white px-4 py-2 rounded hover:bg-[#158f3cff] disabled:opacity-50 disabled:cursor-not-allowed">
Add New Sale
</button>
<button id="viewYearlySummaryBtn" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">
View Yearly Summary
</button>
</div>
</div>
<!-- No Categories Warning -->
<div id="noCategoriesWarning" class="hidden mt-4 p-4 bg-yellow-50 text-yellow-800 rounded-md">
<p>No sales categories found. Please add categories in your <a href="profile.php" class="underline">profile page</a> first.</p>
</div>
</div>
<!-- Summary Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
<div class="bg-white rounded-lg shadow-md p-4">
<h3 class="text-lg font-semibold text-gray-700">Total Sales</h3>
<p class="text-2xl font-bold text-[#1bb34cff]"><span id="totalSales">₱0.00</span></p>
</div>
<div class="bg-white rounded-lg shadow-md p-4">
<h3 class="text-lg font-semibold text-gray-700">Selected Account Total</h3>
<p class="text-2xl font-bold text-[#1bb34cff]"><span id="selectedAccountTotal">₱0.00</span></p>
</div>
</div>
<!-- Sales Table -->
<div class="p-4 bg-white rounded-lg shadow-md overflow-hidden p-4">
<div class="overflow-x-auto">
<table id="salesTable" class="min-w-full">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Particulars</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Account Title</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Amount</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<!-- Data will be populated by DataTables -->
</tbody>
</table>
</div>
</div>
<!-- Add/Edit Sale Modal -->
<div id="saleModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden">
<div class="flex items-center justify-center min-h-screen">
<div class="bg-white rounded-lg shadow-lg p-6 w-full max-w-md">
<h2 id="modalTitle" class="text-2xl font-bold mb-4">Add New Sale</h2>
<form id="saleForm">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="date">
Date
</label>
<input type="date" id="date" name="date" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="particulars">
Particulars
</label>
<input type="text" id="particulars" name="particulars" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="category">
Account Title
</label>
<select id="category" name="category" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
<option value="">Select a category...</option>
</select>
</div>
<div class="mb-6">
<label class="block text-gray-700 text-sm font-bold mb-2" for="amount">
Amount
</label>
<input type="number" id="amount" name="amount" step="0.01" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="flex items-center justify-between">
<button type="submit" class="bg-[#1bb34cff] text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hover:bg-[#158f3cff]">
Save Sale
</button>
<button type="button" onclick="closeModal()" class="bg-gray-500 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hover:bg-gray-600">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Yearly Summary Modal -->
<div id="yearlySummaryModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden">
<div class="flex items-center justify-end min-h-screen p-4">
<div class="bg-white rounded-lg shadow-lg p-6 w-full max-w-6xl max-h-[80vh] overflow-auto">
<div class="flex justify-between items-center mb-4">
<h2 class="text-2xl font-bold">Yearly Sales Summary (<span id="summaryYear"></span>)</h2>
<button onclick="closeYearlySummaryModal()" class="text-gray-500 hover:text-gray-700">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div id="yearlySummaryContent" class="overflow-x-auto">
<!-- Content will be loaded dynamically -->
</div>
</div>
</div>
</div>
</main>
</div>
<!-- JavaScript -->
<script>
// Initialize DataTable
$(document).ready(function() {
// Load categories and update UI
loadCategories();
const table = $('#salesTable').DataTable({
ajax: {
url: 'salesActions/get_sales.php',
data: function(d) {
d.month = $('#monthSelect').val();
d.year = $('#yearSelect').val();
d.category_search = $('#searchBox').val();
d.company_id = $('#companySelect').val();
},
dataSrc: function(json) {
// Calculate and update selected account total from the filtered data
const total = json.data.reduce((sum, row) => sum + parseFloat(row.amount), 0);
$('#selectedAccountTotal').text('₱' + total.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}));
return json.data;
}
},
columns: [
{ data: 'date' },
{ data: 'particulars' },
{ data: 'category' },
{
data: 'amount',
render: function(data) {
return '₱' + parseFloat(data).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
}
},
{
data: null,
render: function(data, type, row) {
return `
<button onclick="editSale(${row.id})" class="text-blue-600 hover:text-blue-800 mr-2">
Edit
</button>
<button onclick="deleteSale(${row.id})" class="text-red-600 hover:text-red-800">
Delete
</button>
`;
}
}
],
processing: true,
serverSide: true,
order: [[0, 'desc']],
pageLength: 200,
lengthMenu: [[50, 100, 200, -1], [50, 100, 200, "All"]],
responsive: true,
searching: false // We'll handle search manually
});
// Handle month/year change
$('#monthSelect, #yearSelect').change(function() {
updateSummaryCards();
table.ajax.reload();
});
// Handle category search
$('#searchBox').on('keyup', function() {
table.ajax.reload();
});
// Handle company change
$('#companySelect').change(function() {
const companyId = $(this).val();
// Update session via AJAX
$.post('company_selector.php', { company_id: companyId })
.done(function(response) {
if (response.success) {
// Reload the table with new company data
table.ajax.reload();
// Update summary cards
updateSummaryCards();
}
});
});
// Function to update summary cards
function updateSummaryCards() {
const month = $('#monthSelect').val();
const year = $('#yearSelect').val();
const search = $('#searchBox').val();
fetch(`salesActions/get_sales_summary.php?month=${month}&year=${year}&category_search=${search}`)
.then(response => response.json())
.then(data => {
$('#totalSales').text('₱' + parseFloat(data.total).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}));
});
}
// Initial update of summary cards
updateSummaryCards();
});
// Modal Functions
function resetForm() {
const form = document.getElementById('saleForm');
form.reset();
delete form.dataset.id;
document.getElementById('modalTitle').textContent = 'Add New Sale';
loadCategories(); // Reload categories when form is reset
}
function openModal() {
resetForm();
document.getElementById('saleModal').classList.remove('hidden');
}
function closeModal() {
document.getElementById('saleModal').classList.add('hidden');
resetForm();
}
// Load categories function
function loadCategories() {
fetch('salesActions/get_sales_categories.php')
.then(response => response.json())
.then(response => {
const categories = response.data || [];
const categorySelect = document.getElementById('category');
const addSaleBtn = document.getElementById('addSaleBtn');
const noCategoriesWarning = document.getElementById('noCategoriesWarning');
// Clear existing options
categorySelect.innerHTML = '<option value="">Select a category...</option>';
if (categories.length === 0) {
// Disable add sale button and show warning
addSaleBtn.disabled = true;
noCategoriesWarning.classList.remove('hidden');
} else {
// Enable add sale button and hide warning
addSaleBtn.disabled = false;
noCategoriesWarning.classList.add('hidden');
// Add new options
categories.forEach(category => {
const option = document.createElement('option');
option.value = category;
option.textContent = category;
categorySelect.appendChild(option);
});
}
})
.catch(error => {
console.error('Error loading categories:', error);
// Show error state
addSaleBtn.disabled = true;
noCategoriesWarning.classList.remove('hidden');
noCategoriesWarning.innerHTML = 'Error loading categories. Please try again later.';
});
}
// Add New Sale Button
document.getElementById('addSaleBtn').addEventListener('click', function() {
if (!this.disabled) {
openModal();
}
});
// Mobile Menu Toggle
const menuBtn = document.getElementById('menuBtn');
const sidebar = document.getElementById('sidebar');
const overlay = document.getElementById('overlay');
menuBtn.addEventListener('click', () => {
sidebar.classList.toggle('-translate-x-full');
overlay.classList.toggle('hidden');
});
overlay.addEventListener('click', () => {
sidebar.classList.add('-translate-x-full');
overlay.classList.add('hidden');
});
// CRUD Functions
function editSale(id) {
// Fetch sale details and open modal
fetch(`salesActions/get_sale.php?id=${id}`)
.then(response => response.json())
.then(data => {
document.getElementById('modalTitle').textContent = 'Edit Sale';
document.getElementById('date').value = data.date;
document.getElementById('particulars').value = data.particulars;
document.getElementById('category').value = data.category;
document.getElementById('amount').value = data.amount;
document.getElementById('saleForm').dataset.id = id;
document.getElementById('saleModal').classList.remove('hidden');
});
}
function deleteSale(id) {
if (confirm('Are you sure you want to delete this sale?')) {
fetch(`salesActions/delete_sale.php?id=${id}`, { method: 'DELETE' })
.then(response => response.json())
.then(data => {
if (data.success) {
$('#salesTable').DataTable().ajax.reload();
} else {
alert('Error deleting sale');
}
});
}
}
// Form Submission
document.getElementById('saleForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
const id = this.dataset.id;
fetch(id ? `salesActions/update_sale.php?id=${id}` : 'salesActions/add_sale.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
closeModal();
$('#salesTable').DataTable().ajax.reload();
this.reset();
delete this.dataset.id;
} else {
alert('Error saving sale');
}
});
});
function openYearlySummaryModal() {
const year = $('#yearSelect').val();
const companyName = <?php echo json_encode(isset($_SESSION['selected_company_name']) ? $_SESSION['selected_company_name'] : ''); ?>;
$('#summaryYear').text(year + (companyName ? ' - ' + companyName : ''));
fetch(`salesActions/get_yearly_sales.php?year=${year}`)
.then(response => response.json())
.then(data => {
const content = document.getElementById('yearlySummaryContent');
// Create table HTML
let tableHtml = `
<table class="min-w-full bg-white border border-gray-300">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 border-b text-left">Account Title</th>
<th class="px-6 py-3 border-b text-right">Jan</th>
<th class="px-6 py-3 border-b text-right">Feb</th>
<th class="px-6 py-3 border-b text-right">Mar</th>
<th class="px-6 py-3 border-b text-right">Apr</th>
<th class="px-6 py-3 border-b text-right">May</th>
<th class="px-6 py-3 border-b text-right">Jun</th>
<th class="px-6 py-3 border-b text-right">Jul</th>
<th class="px-6 py-3 border-b text-right">Aug</th>
<th class="px-6 py-3 border-b text-right">Sep</th>
<th class="px-6 py-3 border-b text-right">Oct</th>
<th class="px-6 py-3 border-b text-right">Nov</th>
<th class="px-6 py-3 border-b text-right">Dec</th>
<th class="px-6 py-3 border-b text-right">Total</th>
</tr>
</thead>
<tbody>`;
// Add rows for each account title
data.forEach(row => {
tableHtml += `
<tr class="hover:bg-gray-50">
<td class="px-6 py-3 border-b">${row.account_title}</td>
${row.monthly_totals.map(amount => `
<td class="px-6 py-3 border-b text-right">₱${parseFloat(amount).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})}</td>
`).join('')}
<td class="px-6 py-3 border-b text-right font-bold">₱${parseFloat(row.total).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})}</td>
</tr>`;
});
// Add total row
const totals = data.reduce((acc, row) => {
row.monthly_totals.forEach((amount, i) => {
acc[i] = (acc[i] || 0) + parseFloat(amount);
});
return acc;
}, []);
const grandTotal = totals.reduce((a, b) => a + b, 0);
tableHtml += `
<tr class="bg-gray-100 font-bold">
<td class="px-6 py-3 border-b">TOTAL</td>
${totals.map(total => `
<td class="px-6 py-3 border-b text-right">₱${total.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})}</td>
`).join('')}
<td class="px-6 py-3 border-b text-right">₱${grandTotal.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})}</td>
</tr>
</tbody>
</table>`;
content.innerHTML = tableHtml;
});
document.getElementById('yearlySummaryModal').classList.remove('hidden');
}
function closeYearlySummaryModal() {
document.getElementById('yearlySummaryModal').classList.add('hidden');
}
document.getElementById('viewYearlySummaryBtn').addEventListener('click', openYearlySummaryModal);
</script>
</body>
</html>