-
Notifications
You must be signed in to change notification settings - Fork 0
/
payrollGeneration.php
232 lines (163 loc) · 9.21 KB
/
payrollGeneration.php
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
<?php
ob_start();
include_once("includes/Database.php");
include_once("includes/functions.php");
try{
if (isset($_POST['payrolltype'])){
$payrollType = $_POST['payrolltype'];
$region = $_POST['regionselect'];
$month = $_POST['month'];
$year = $_POST['year'];
$emailIntimation = $_POST['sendemail'];
$operator = $_POST['operator'];
$lastMonth = "";
$lastYear = "";
if ($month=="1"){
$lastMonth = "12";
$lastYear=$year-1;
}else{
$lastMonth = $month-1;
$lastYear=$year;
}
if ($lastMonth < 10){
$lastMonth="0".$lastMonth;
}
if ($month < 10){
$month="0".$month;
}
$daysLastMonth=cal_days_in_month(CAL_GREGORIAN,$lastMonth,$lastYear);
$daysCurrentMonth=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$startLastDate = $year."-".$month."-01";
$endLastDate = $year."-".$month."-".$daysCurrentMonth;
if ($payrollType == "all"){
if ($region == "all"){
//for all regions / all employess
}else{
//for employees of only one region
}
echo json_encode("success");
}else if ($payrollType == "single"){
$employee = $_POST['employeeSelect'];
if ($employee != "" && $employee != null && $employee != "null"){
//generate payroll of one selected employee
$employeeSalaryDetails = getEmployeeSalaryDetailsForPayroll($employee);
//iterate through above object as [0]['basic_salary']
if (isset($employeeSalaryDetails[0]['emp_id'])){
$empId = $employeeSalaryDetails[0]['emp_id'];
$accountNo = $employeeSalaryDetails[0]['account_no'];
$ibanNo = $employeeSalaryDetails[0]['iban_no'];
// $projectAllowance = $employeeSalaryDetails[0]['project_allowance'];
// $housingAllowance = $employeeSalaryDetails[0]['housing_allowance'];
// $conveyanceAllowance = $employeeSalaryDetails[0]['conveyance_allowance'];
// $otherAllowance = $employeeSalaryDetails[0]['other_allowance'];
// $basicSalary = $employeeSalaryDetails[0]['basic_salary'];
// $wpsSalary = $employeeSalaryDetails[0]['wps_salary'];
$wpsSalary = 0;
$absence = getAttendanceRecordsForMonthYear($employee,$region,'Absence',$month,$year);
if ($absence == null || $absence == ""){
$absence = 0;
}
$unpaidLeave = getAttendanceRecordsForMonthYear($employee,$region,'UnpaidLeave',$month,$year);
if ($unpaidLeave == null || $unpaidLeave == ""){
$unpaidLeave = 0;
}
$Job = getAttendanceRecordsForMonthYear($employee,$region,'Job',$month,$year);
if ($Job == null || $Job == ""){
$Job = 0;
}
// echo json_encode($absence);exit;
// A = basic_salary x ((total days - (absence + unpaid leave)) / total days)
$basicSalary = round( $employeeSalaryDetails[0]['basic_salary'] * (($daysCurrentMonth - ($absence + $unpaidLeave)) / $daysCurrentMonth));
// B = housing_allowance x ((total days - (absence + unpaid leave)) / total days)
$housingAllowance = round( $employeeSalaryDetails[0]['housing_allowance'] * (($daysCurrentMonth - ($absence + $unpaidLeave)) / $daysCurrentMonth));
// C = conveyance_allowance x ((total days - (absence + unpaid leave)) / total days)
$conveyanceAllowance = round( $employeeSalaryDetails[0]['conveyance_allowance'] * (($daysCurrentMonth - ($absence + $unpaidLeave)) / $daysCurrentMonth));
// D = other_allowance x ((total days - (absence + unpaid leave)) / total days)
$otherAllowance = round( $employeeSalaryDetails[0]['other_allowance'] * (($daysCurrentMonth - ($absence + $unpaidLeave)) / $daysCurrentMonth));
// E = project_allowance x ((total days - (absence + unpaid leave)) / total days)
$projectAllowance = round($employeeSalaryDetails[0]['project_allowance'] * ($Job));
$netSalary = $projectAllowance+$housingAllowance+$conveyanceAllowance+$otherAllowance+$basicSalary+$wpsSalary;
//arrears of last month = salary of last month - payments of last month
//test case ###########
//$employeearrear = getEmployeeSalary($employeeDetailsData['employee_internal_id'],$lastmonth,$lastyear);
// $arrearA = round( $empSalary['gross_salary']);
// End here ###########
if ($netSalary==null || $netSalary == ""){
$netSalary=0;
}
$lastMonthPayments = getLastMonthPayments($employee, $lastMonth, $lastYear);
$totalPreviousPayments = $lastMonthPayments[0]['totalamount'];
if ($totalPreviousPayments == null || $totalPreviousPayments == ""){
$totalPreviousPayments = 0;
}
// echo json_encode($totalPreviousPayments);exit;
$lastMonthSalary = getLastMonthSalary($empId, $lastMonth, $lastYear);
if (isset($lastMonthSalary[0]['totalsalary'])){
$lastSalary = $lastMonthSalary[0]['totalsalary'];
if ($lastSalary == "" || $lastSalary == null){
$lastSalary = 0;
}
}else{
$lastSalary = 0;
}
$arrears = $lastSalary - $totalPreviousPayments;
//$arrears = $lastSalary - $lastMonthSalary;
$grossSalary = $netSalary + $arrears;
// total payments this month
$currentMonthPayments = getLastMonthPayments($employee, $month, $year);
$totalCurrentMonthPayments = $currentMonthPayments[0]['totalamount'];
if ($totalCurrentMonthPayments == null || $totalCurrentMonthPayments == ""){
$totalCurrentMonthPayments = 0;
}
$deductions = $totalCurrentMonthPayments;
//attemp to fix
// $netsalary2 = $projectAllowance + $housingAllowance + $conveyanceAllowance + $otherAllowance + $basicSalary -$totalPreviousPayments + $arrears;
// $netSalary2 = $grossSalary - $totalCurrentMonthPayments;
$insertStatus = InsertPayrollData($empId, $accountNo, $ibanNo, $projectAllowance, $housingAllowance, $conveyanceAllowance, $otherAllowance, $basicSalary, $wpsSalary, $deductions, $arrears, $grossSalary, $month, $year,$operator);
// $insertStatus = true;
if ($insertStatus==true){
if ($emailIntimation == "notsend"){
echo json_encode("success");
}else if ($emailIntimation == "send"){
$empEmailRow = GetEmployeeEmailForPayroll($empId);
// $mailto = $empEmailRow[0]['employee_email'];
$mailto = "adnan@npm.works";
$empName = getemployeename($empId);
$salaryDetailsEmail = "<b>Account No:</b> $accountNo<br><b>Food Allowance:</b> $projectAllowance<br><b>Housing Allowance: </b>$housingAllowance<br><b>Conveyance Allowance: </b>$conveyanceAllowance<br><b>Other Allowance: </b>$otherAllowance<br><b>Basic Salary: </b>$basicSalary<br><b>Arrears: </b>$arrears<br><b>Payments Made: </b>$totalPreviousPayments<br><b>Gross Salary: </b>$grossSalary";
//The header can include their name, ID and the month,year for payroll.
$headEmail = "Salary Information | Employee ID: $employee | Name: $empName | Month: $month | Year: $year";
$subject = $headEmail;
$headers = "From: NPM\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$messageCommon = "Dear Employee,<br><br>";
$messageCommon .= "Please find below the details of your salary for $month-$year,<br/><br/>";
$messageCommon .= $salaryDetailsEmail."<br/><br/>"; //sending table via email
$messageCommon .= "Regards,<br>";
$messageCommon .= "NPM";
if (@mail ($mailto, $subject, $messageCommon, $headers)){
// echo $EMAILINTIMATION='SENT';
}else{
// echo $EMAILINTIMATION='NOT SENT';
}
//email sending code goes here...
echo json_encode("success");
}
}else{
echo json_encode("failure");
}
}else{
echo json_encode("failure");
}
}else{
echo json_encode("error");
}
}else{
echo json_encode("error");
}
}else{
echo json_encode("error");
}
}catch(Exception $e){
echo json_encode("error");
}
?>