PayslipProcessor::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $payroll is dead and can be removed.
Loading history...
46
            $earnings   = $this->payslip->earnings;
47
            $deductions = $this->payslip->deductions;
48
49
            $this->payslip->basic_salary = $gross_salary = $net_salary = $salary->amount;
0 ignored issues
show
Unused Code introduced by
The assignment to $net_salary is dead and can be removed.
Loading history...
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));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $earning seems to be defined by a foreach iteration on line 50. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
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