Completed
Push — master ( 2d4ee1...dfe00f )
by Daniel
04:00
created

Bonuses::setDynamicValues()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 6
nc 4
nop 4
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2016 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
 */
35
trait Bonuses
36
{
37
38
    use InputValidation;
39
40
    /**
41
     * Tichete de alimente
42
     * */
43
    protected function setFoodTicketsValue($dtR, $lngDate, $aryStngMealTickets) {
44
        $valueMealTicket   = 0;
45
        $indexArrayValues  = 0;
46
        $crtUpperLimitDate = (int) $dtR['maximum']->format('Ymd');
47
        while (($valueMealTicket === 0)) {
48
            $crtVal            = $aryStngMealTickets[$indexArrayValues];
49
            $crtLLDate         = \DateTime::createFromFormat('Y-n-j', $crtVal['Year'].'-'.$crtVal['Month'].'-1');
50
            $crtLowerLimitDate = (int) $crtLLDate->format('Ymd');
51
            $valueMealTicket = $this->setDynamicValues($lngDate, $crtVal, $crtLowerLimitDate, $crtUpperLimitDate);
52
            $crtUpperLimitDate = $crtLowerLimitDate;
53
            $indexArrayValues++;
54
        }
55
        return $valueMealTicket;
56
    }
57
    
58
    protected function setDynamicValues($lngDate, $crtVal, $crtLowerLimitDate, $crtUpperLimitDate) {
59
        if (($lngDate <= $crtUpperLimitDate) && ($lngDate >= $crtLowerLimitDate)) {
60
            if (array_key_exists('Value', $crtVal)) {
61
                return $crtVal['Value'];
62
            } elseif (array_key_exists('Multiplier', $crtVal) && array_key_exists('Denominator', $crtVal)) {
63
                return ($crtVal['Multiplier'] / $crtVal['Denominator']);
64
            }
65
        }
66
    }
67
68
    /**
69
     * Deducere personala
70
     * */
71
    protected function setPersonalDeduction($lngDate, $lngBrutto, $sPersons, $cValues) {
72
        $yrDate  = substr($lngDate, 0, 4);
73
        $nReturn = 0;
74
        if ($yrDate >= 2005) {
75
            $nReturn = $this->setPersonalDeductionComplex2016($sPersons, $lngBrutto, $yrDate, $cValues);
76
        } elseif ($yrDate >= 2001) {
77
            $valuesYearly = [
78
                2001 => $this->setPersonalDeductionSimple2001($lngDate),
79
                2002 => 1600000,
80
                2003 => 1800000,
81
                2004 => 2000000,
82
            ];
83
            $nReturn      = $valuesYearly[$yrDate];
84
        }
85
        return (($lngDate >= 20060701) ? round($nReturn, -4) : $nReturn);
86
    }
87
88
    private function setPersonalDeductionComplex($sPersons, $lngBrutto, $inRule) {
89
        $nDeduction = $inRule['Limit maximum amount'];
90
        if ($sPersons <= $inRule['Limit persons']) {
91
            $nDeduction = $inRule['Limit basic amount'] + ($sPersons * $inRule['Limit /person amount']);
92
        }
93
        $nReturn = $nDeduction;
94
        if ($lngBrutto >= $inRule['Limit zero deduction']) {
95
            $nReturn = 0;
96
        } else {
97
            $reducedDed = $inRule['Reduced deduction'];
98
            if ($lngBrutto > $reducedDed) {
99
                $nReturn = $nDeduction * (1 - ($lngBrutto - $reducedDed) / $reducedDed);
100
            }
101
        }
102
        return $nReturn;
103
    }
104
105
    private function setPersonalDeductionComplex2016($sPersons, $lngBrutto, $yrDate, $cValues) {
106
        $inRule = $cValues[2005];
107
        if ($yrDate >= 2018) {
108
            $inRule = $cValues[2018];
109
        } elseif (in_array($yrDate, [2016, 2017])) {
110
            $inRule = $cValues[2016];
111
        }
112
        return $this->setPersonalDeductionComplex($sPersons, $lngBrutto, $inRule);
113
    }
114
115
    private function setPersonalDeductionSimple2001($lngDate) {
116
        $nReturn = 1300000;
117
        $mnDate  = substr($lngDate, 4, 2);
118
        if ($mnDate <= 6) {
119
            $nReturn = 1099000;
120
        } elseif ($mnDate <= 9) {
121
            $nReturn = 1273000;
122
        }
123
        return $nReturn;
124
    }
125
126
}
127
128