-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPayrollController.php
More file actions
216 lines (177 loc) · 6.52 KB
/
Copy pathPayrollController.php
File metadata and controls
216 lines (177 loc) · 6.52 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
<?php
/*
=======================================================================
PHPPayroll — Open Source HR & Payroll Management Software
Version: 3.0
License: MIT Open Source License
Developed & Maintained By: PHPPayroll (https://www.phppayroll.com)
Description:
PHPPayroll is a Payroll and Human Resource Management platform
available in two deployment models: PHPPayroll Open-Source, a free,
self-hosted edition for essential HR and payroll operations, and
PHPPayroll Cloud, a fully managed, enterprise-grade payroll system.
PHPPayroll is designed to help organizations manage Employees,
Attendance, Leaves, Payroll, and core HR workflows with simplicity,
security, and speed.
Core Modules Included (Open-Source Edition):
- Employee Management
- Attendance Tracking
- Leave Management
- Payroll Management
- HR Dashboard
- Secure Login
This software is open for modification and extension.
You are free to customize, improve, and commercially use the
Open-Source edition without licensing fees, provided this
copyright notice remains preserved.
Website:
https://www.phppayroll.com
Live Demo:
https://www.phppayroll.com/demo/
Download:
https://www.phppayroll.com/download/
Community Contribution:
Developers are welcome to contribute improvements, bug fixes,
and enhancements to the Open-Source edition. Please visit our
website for documentation, updates, and community support.
Last Update: 14-12-2025
=======================================================================
*/
require_once __DIR__ . '/../core/Controller.php';
require_once __DIR__ . '/../models/Payroll.php';
require_once __DIR__ . '/../models/Employee.php';
class PayrollController extends Controller
{
protected $payroll;
protected $employee;
public function __construct($conn)
{
parent::__construct($conn);
$this->payroll = new Payroll($conn);
$this->employee = new Employee($conn);
if (empty($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
}
// =====================
// LIST
// =====================
public function index()
{
$records = $this->payroll->all();
$successMsg = $_SESSION['payroll_success'] ?? '';
$errorMsg = $_SESSION['payroll_error'] ?? '';
unset($_SESSION['payroll_success'], $_SESSION['payroll_error']);
$this->view('payroll/index', [
'records' => $records,
'successMsg' => $successMsg,
'errorMsg' => $errorMsg
]);
}
// =====================
// CREATE FORM (EMP DROPDOWN)
// =====================
public function create()
{
$errorMsg = $_SESSION['add_payroll_error'] ?? '';
unset($_SESSION['add_payroll_error']);
// fetch active employees
$employees = $this->employee->allActive();
$this->view('payroll/create', [
'errorMsg' => $errorMsg,
'employees' => $employees
]);
}
// =====================
// STORE
// =====================
public function store()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return $this->redirect('payroll.php?a=create');
}
$basic = (float)$_POST['basic_salary'];
$allowances = (float)($_POST['allowances'] ?? 0);
$deductions = (float)($_POST['deductions'] ?? 0);
$data = [
'employee_id' => (int)$_POST['employee_id'],
'salary_month' => $_POST['salary_month'],
'basic_salary' => $basic,
'allowances' => $allowances,
'deductions' => $deductions,
'net_salary' => $basic + $allowances - $deductions
];
if (!$data['employee_id'] || !$data['salary_month']) {
$_SESSION['add_payroll_error'] = 'Employee and Salary Month required.';
return $this->redirect('payroll.php?a=create');
}
if ($this->payroll->create($data)) {
$_SESSION['payroll_success'] = 'Payroll generated successfully.';
return $this->redirect('payroll.php?a=index');
}
$_SESSION['payroll_error'] = 'Error generating payroll.';
return $this->redirect('payroll.php?a=index');
}
// =====================
// EDIT FORM (EMP DROPDOWN)
// =====================
public function edit()
{
$id = $_GET['id'] ?? 0;
if (!$id) return $this->redirect('payroll.php?a=index');
$payroll = $this->payroll->find($id);
if (!$payroll) return $this->redirect('payroll.php?a=index');
$employees = $this->employee->allActive();
$errorMsg = $_SESSION['edit_payroll_error'] ?? '';
$successMsg = $_SESSION['edit_payroll_success'] ?? '';
unset($_SESSION['edit_payroll_error'], $_SESSION['edit_payroll_success']);
$this->view('payroll/edit', [
'payroll' => $payroll,
'employees' => $employees,
'errorMsg' => $errorMsg,
'successMsg' => $successMsg
]);
}
// =====================
// UPDATE
// =====================
public function update()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return $this->redirect('payroll.php?a=index');
}
$id = $_POST['id'] ?? 0;
if (!$id) return $this->redirect('payroll.php?a=index');
$basic = (float)$_POST['basic_salary'];
$allowances = (float)($_POST['allowances'] ?? 0);
$deductions = (float)($_POST['deductions'] ?? 0);
$data = [
'employee_id' => (int)$_POST['employee_id'],
'salary_month' => $_POST['salary_month'],
'basic_salary' => $basic,
'allowances' => $allowances,
'deductions' => $deductions,
'net_salary' => $basic + $allowances - $deductions
];
if ($this->payroll->update($id, $data)) {
$_SESSION['edit_payroll_success'] = 'Payroll updated successfully.';
} else {
$_SESSION['edit_payroll_error'] = 'Error updating payroll.';
}
return $this->redirect('payroll.php?a=edit&id=' . $id);
}
// =====================
// DELETE
// =====================
public function delete()
{
$id = $_GET['id'] ?? 0;
if ($id && $this->payroll->delete($id)) {
$_SESSION['payroll_success'] = 'Payroll deleted successfully.';
} else {
$_SESSION['payroll_error'] = 'Error deleting payroll.';
}
return $this->redirect('payroll.php?a=index');
}
}