Completed
Push — master ( 8f89e2...deaea0 )
by Daniel
02:04
created

Bonuses::setPersonalDeduction()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 15

Duplication

Lines 3
Ratio 15 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 3
loc 20
rs 9.2
cc 4
eloc 15
nc 6
nop 3
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\salariu;
30
31
/**
32
 * Description of Bonuses
33
 *
34
 * @author E303778
35
 */
36
trait Bonuses
37
{
38
39
    /**
40
     * returns an array with non-standard holidays from a JSON file
41
     *
42
     * @param string $fileBaseName
43
     * @return mixed
44
     */
45
    private function readTypeFromJsonFile($fileBaseName)
46
    {
47
        $fName       = __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $fileBaseName . '.min.json';
48
        $fJson       = fopen($fName, 'r');
49
        $jSonContent = fread($fJson, filesize($fName));
50
        fclose($fJson);
51
        return json_decode($jSonContent, true);
52
    }
53
54
    /**
55
     * Tichete de alimente
56
     * */
57
    protected function setFoodTicketsValue($lngDate)
58
    {
59
        $arrayValues           = $this->readTypeFromJsonFile('static')['Meal Ticket Value'];
60
        $valueMealTicket       = 0;
61
        $indexArrayValues      = 0;
62
        $currentUpperLimitDate = mktime(0, 0, 0, date('n'), 1, date('Y'));
63
        while (($valueMealTicket === 0)) {
64
            $crtVal                = $arrayValues[$indexArrayValues];
65
            $currentLowerLimitDate = mktime(0, 0, 0, $crtVal['Month'], 1, $crtVal['Year']);
66
            if (($lngDate <= $currentUpperLimitDate) && ($lngDate >= $currentLowerLimitDate)) {
67
                $valueMealTicket = $crtVal['Value'];
68
            }
69
            $currentUpperLimitDate = $currentLowerLimitDate;
70
            $indexArrayValues++;
71
        }
72
        return $valueMealTicket;
73
    }
74
75
    /**
76
     * Deducere personala
77
     * */
78
    protected function setPersonalDeduction($lngDate, $lngBrutto, $sPersons)
79
    {
80
        $yrDate  = date('Y', $lngDate);
81
        $nReturn = 0;
82
        if ($yrDate >= 2005) {
83
            $nReturn = $this->setPersonalDeductionComplex($sPersons, $lngBrutto, $yrDate);
84
        } elseif ($yrDate >= 2001) {
85
            $valuesYearly = [
86
                2001 => $this->setPersonalDeductionSimple2001($lngDate),
87
                2002 => 1600000,
88
                2003 => 1800000,
89
                2004 => 2000000,
90
            ];
91
            $nReturn      = $valuesYearly[$yrDate];
92
        }
93 View Code Duplication
        if ($lngDate >= mktime(0, 0, 0, 7, 1, 2006)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $nReturn = round($nReturn, -4);
95
        }
96
        return $nReturn;
97
    }
98
99
    private function setPersonalDeductionComplex($sPersons, $lngBrutto, $inRule)
100
    {
101
        $nDeduction = $inRule['Limit maximum amount'];
102
        if ($sPersons <= $inRule['Limit persons']) {
103
            $nDeduction = $inRule['Limit basic amount'] + ($sPersons * $inRule['Limit /person amount']);
104
        }
105
        $nReturn = $nDeduction;
106
        if ($lngBrutto >= $inRule['Limit zero deduction']) {
107
            $nReturn = 0;
108
        } elseif ($lngBrutto > $inRule['Reduced deduction']) {
109
            $nReturn = $nDeduction * (1 - ($lngBrutto - $inRule['Reduced deduction']) / $inRule['Reduced deduction']);
110
        }
111
        return $nReturn;
112
    }
113
114
    private function setPersonalDeductionComplex2016($sPersons, $lngBrutto, $yrDate)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
115
    {
116
        $nValues = [
117
            2016 => [
118
                'Limit persons'        => 3,
119
                'Limit basic amount'   => 3500000,
120
                'Limit maximum amount' => 8000000,
121
                'Limit /person amount' => 1000000,
122
                'Limit zero deduction' => 30000000,
123
                'Reduced deduction'    => 15000000,
124
            ],
125
            2005 => [
126
                'Limit persons'        => 3,
127
                'Limit basic amount'   => 2500000,
128
                'Limit maximum amount' => 6500000,
129
                'Limit /person amount' => 1000000,
130
                'Limit zero deduction' => 30000000,
131
                'Reduced deduction'    => 10000000,
132
            ],
133
        ];
134
        $inRule  = $nValues[2005];
135
        if ($yrDate == 2016) {
136
            $inRule = $nValues[2016];
137
        }
138
        return $this->setPersonalDeductionComplex($sPersons, $lngBrutto, $inRule);
139
    }
140
141
    private function setPersonalDeductionSimple2001($lngDate)
142
    {
143
        $nReturn = 1300000;
144
        $mnDate  = date('n', $lngDate);
145
        if ($mnDate <= 6) {
146
            $nReturn = 1099000;
147
        } elseif ($mnDate <= 9) {
148
            $nReturn = 1273000;
149
        }
150
        return $nReturn;
151
    }
152
}
153