Completed
Push — master ( c666f3...dae9af )
by Nasrul Hazim
02:13
created

PayslipProcessor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 25
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A calculate() 0 17 2
A payslip() 0 5 1
A make() 0 3 1
A __construct() 0 13 5
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 = $salary->amount;
50
            $this->payslip->gross_salary = $gross_salary = ($earnings->sum('amount') + $salary->amount);
51
            $this->payslip->net_salary   = $gross_salary - $deductions->sum('amount');
52
53
            $this->payslip->save();
54
        }
55
56
        return $this;
57
    }
58
}
59