1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CleaniqueCoders\OpenPayroll\Processors; |
4
|
|
|
|
5
|
|
|
use CleaniqueCoders\OpenPayroll\Contracts\CalculateContract; |
6
|
|
|
|
7
|
|
|
class PayslipProcessor implements CalculateContract |
8
|
|
|
{ |
9
|
|
|
public $payslip; |
10
|
|
|
|
11
|
|
|
public function __construct($identifier = null) |
12
|
|
|
{ |
13
|
|
|
if (! is_null($identifier)) { |
14
|
|
|
if (is_string($identifier) || is_int($identifier)) { |
15
|
|
|
$this->payslip = config('open-payroll.models.payslip')::query() |
16
|
|
|
->with('earnings', 'deductions', 'payroll', 'employee', 'employee.salary') |
17
|
|
|
->whereId($identifier) |
18
|
|
|
->orWhere('hashslug', $identifier) |
19
|
|
|
->firstOrFail(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (is_object($identifier)) { |
23
|
|
|
$this->payslip($identifier); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function make($identifier = null) |
29
|
|
|
{ |
30
|
|
|
return new self($identifier); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function payslip($payslip) |
34
|
|
|
{ |
35
|
|
|
$this->payslip = $payslip; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function calculate() |
41
|
|
|
{ |
42
|
|
|
if ($this->payslip) { |
43
|
|
|
$employee = $this->payslip->employee; |
44
|
|
|
$salary = $employee->salary; |
45
|
|
|
$payroll = $this->payslip->payroll; |
|
|
|
|
46
|
|
|
$earnings = $this->payslip->earnings; |
47
|
|
|
$deductions = $this->payslip->deductions; |
48
|
|
|
|
49
|
|
|
$this->payslip->basic_salary = $gross_salary = $net_salary = $salary->amount; |
|
|
|
|
50
|
|
|
foreach ($earnings as $earning) { |
51
|
|
|
$class = config('open-payroll.processors.earnings.' . studly_case($earning->type->name)); |
52
|
|
|
if (class_exists($class)) { |
53
|
|
|
$gross_salary += $class::make($earning)->calculate(); |
54
|
|
|
} else { |
55
|
|
|
$gross_salary += $earning->amount; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$deduction_amount = 0; |
60
|
|
|
foreach ($deductions as $deduction) { |
61
|
|
|
$class = config('open-payroll.processors.deductions.' . studly_case($earning->type->name)); |
|
|
|
|
62
|
|
|
if (class_exists($class)) { |
63
|
|
|
$deduction_amount += $class::make($deduction)->calculate(); |
64
|
|
|
} else { |
65
|
|
|
$deduction_amount += $earning->amount; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->payslip->gross_salary = $gross_salary; |
70
|
|
|
$this->payslip->net_salary = $gross_salary - $deduction_amount; |
71
|
|
|
|
72
|
|
|
$this->payslip->save(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|