Completed
Push — master ( 5dd1b0...143927 )
by Daniel
03:21
created

Taxation::determineUnemploymentTax()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 9
Ratio 64.29 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 14
rs 9.2
cc 4
eloc 11
nc 3
nop 3
1
<?php
2
3
//
4
5
/**
6
 *
7
 * The MIT License (MIT)
8
 *
9
 * Copyright (c) 2017 Daniel Popiniuc
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in all
19
 * copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
 * SOFTWARE.
28
 *
29
 */
30
31
namespace danielgp\salariu;
32
33
/**
34
 * Description of Taxation
35
 *
36
 * @author E303778
37
 */
38
trait Taxation
39
{
40
41
    private $txLvl;
42
43
    private function determineUnemploymentTax($yearMonth, $inMny, $dtR) {
44
        $intValue   = 0;
45
        $maxCounter = count($inMny);
46 View Code Duplication
        for ($counter = 0; $counter < $maxCounter; $counter++) {
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...
47
            $crtVal         = $inMny[$counter];
48
            $crtDV          = \DateTime::createFromFormat('Y-n-j', $crtVal['Year'].'-'.$crtVal['Month'].'-01');
49
            $crtDateOfValue = (int) $crtDV->format('Ymd');
50
            if (($yearMonth <= $dtR['maximumInt']) && ($yearMonth >= $crtDateOfValue)) {
51
                $intValue = $crtVal['Percentage'];
52
                $counter  = $maxCounter;
53
            }
54
        }
55
        return $intValue;
56
    }
57
58
    private function getIncomeTaxBaseAdjustments(\Symfony\Component\HttpFoundation\Request $tCSG, $rest, $inMny) {
59
        $restFinal    = $rest + round($inMny['Food Tickets Value'], -4);
60
        $val          = $tCSG->get('gbns');
61
        $taxMinAmount = 0;
62
        if ($inMny['inDate'] >= 20160101) {
63
            $taxMinAmount = 150 * ($val || 0);
64
        }
65
        if ($inMny['inDate'] >= 20101001) {
66
            $restFinal += round(min($val, $val - $taxMinAmount) * pow(10, 4), -4);
67
        }
68
        return $restFinal;
69
    }
70
71
    private function getIncomeTaxValue(\Symfony\Component\HttpFoundation\Request $tCSG, $inMny) {
72
        $rest   = 0;
73
        $dinish = array_sum($inMny['Deductions']) + round($tCSG->get('afet') * pow(10, 4), -4);
74
        if ($inMny['lngBase'] > $dinish) {
75
            $rest = $inMny['lngBase'] - array_sum($inMny['Deductions']) + round($tCSG->get('afet') * pow(10, 4), -4);
76
        }
77
        if ($inMny['inDate'] >= 20100701) {
78
            $rest = $this->getIncomeTaxBaseAdjustments($tCSG, $rest, $inMny);
79
        }
80
        return $this->setIncomeTax($inMny['inDate'], $rest, $inMny['Income Tax']);
81
    }
82
83
    /**
84
     * CAS
85
     *
86
     * */
87
    private function setHealthFundTax($lngDate, $lngBrutto, $nPercentages, $nValues) {
88
        $this->txLvl['casP']      = $this->setValuesFromJson($lngDate, $nPercentages);
89
        $this->txLvl['casP_base'] = $this->setHealthFndTxBs($lngDate, $lngBrutto, $nValues);
90
        $nReturn                  = $this->txLvl['casP_base'] * $this->txLvl['casP'] / 100;
91
        if ($lngDate > 20060701) {
92
            $nReturn = ceil($nReturn / pow(10, 4)) * pow(10, 4);
93
        }
94
        $this->txLvl['cas'] = round($nReturn, 0);
95
    }
96
97
    /**
98
     * baza CAS
99
     *
100
     * http://www.lapensie.com/forum/salariul-mediu-brut.php
101
     * */
102
    private function setHealthFndTxBs($lngDate, $lngBrutto, $nValues) {
103
        $crtValues = $nValues[substr($lngDate, 0, 4)];
104
        $base      = min($lngBrutto, $crtValues['Multiplier'] * $crtValues['Monthly Average Salary']);
105
        if ($lngDate >= 20170201) {
106
            $base = $lngBrutto;
107
        }
108
        if (array_key_exists('Month Secondary Value', $crtValues)) {
109
            if (substr($lngDate, 4, 2) >= $crtValues['Month Secondary Value']) {
110
                $base = min($lngBrutto, $crtValues['Multiplier'] * $crtValues['Monthly Average Salary Secondary']);
111
            }
112
        }
113
        return $base;
114
    }
115
116
    /**
117
     * Sanatate
118
     * */
119
    protected function setHealthTax($lngDate, $lngBrutto, $nPercentages, $nValues) {
120
        $this->txLvl['sntP'] = $this->setValuesFromJson($lngDate, $nPercentages);
121
        $nReturn             = round($lngBrutto * $this->txLvl['sntP'] / 100, 0);
122
        if ($lngDate >= 20170101) {
123
            $this->txLvl['sntP_base'] = $this->setHealthFndTxBs($lngDate, $lngBrutto, $nValues);
124
            $nReturn                  = round($this->txLvl['sntP_base'] * $this->txLvl['sntP'] / 100, 0);
125
        }
126
        $this->txLvl['snt'] = (($lngDate > 20060701) ? round($nReturn, -4) : $nReturn);
127
    }
128
129
    /**
130
     * Impozit pe salariu
131
     * */
132
    protected function setIncomeTax($lngDate, $lngTaxBase, $nValues) {
133
        $yrDate = (int) substr($lngDate, 0, 4);
134
        if ($yrDate == 2001) {
135
            $nReturn = $this->setIncomeTax2001($lngDate, $lngTaxBase, $nValues);
136
        } else {
137
            $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues, $yrDate);
138
        }
139
        return (($lngDate >= 20060701) ? round($nReturn, -4) : $nReturn);
140
    }
141
142
    /**
143
     * Impozit pe salariu
144
     * */
145
    private function setIncomeTax2001($lngDate, $lngTaxBase, $nValues) {
146
        $nReturn = 0;
147
        $mnth    = substr($lngDate, 4, 2);
148
        if ($mnth <= 6) {
149
            $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues["2001-06"]);
0 ignored issues
show
Bug introduced by
The call to setIncomeTaxFromJson() misses a required argument $yrDate.

This check looks for function calls that miss required arguments.

Loading history...
150
        } elseif ($mnth <= 9) {
151
            $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues["2001-09"]);
0 ignored issues
show
Bug introduced by
The call to setIncomeTaxFromJson() misses a required argument $yrDate.

This check looks for function calls that miss required arguments.

Loading history...
152
        } elseif ($mnth > 9) {
153
            $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues["2001-12"]);
0 ignored issues
show
Bug introduced by
The call to setIncomeTaxFromJson() misses a required argument $yrDate.

This check looks for function calls that miss required arguments.

Loading history...
154
        }
155
        return $nReturn;
156
    }
157
158
    private function setIncomeTaxFromJson($lngTaxBase, $nValues, $yrDate) {
159
        $nReturn = 0;
160
        $howMany = count($nValues);
161
        if (array_key_exists('Percentage', $nValues[$yrDate])) {
162
            $this->txLvl['inTaxP']      = $nValues[$yrDate]['Percentage'];
163
            $this->txLvl['inTaxP_base'] = $lngTaxBase;
164
            $nReturn                    = $lngTaxBase * $this->txLvl['inTaxP'] / 100;
165
        } else {
166
            for ($counter = 0; $counter <= $howMany; $counter++) {
167
                if (($lngTaxBase <= $nValues[$counter]['Upper Limit Value'])) {
168
                    $sLbl                  = [
169
                        'BDP' => $nValues[$counter]['Base Deducted Percentage'],
170
                        'BDV' => $nValues[$counter]['Base Deduction Value'],
171
                        'TFV' => $nValues[$counter]['Tax Free Value'],
172
                    ];
173
                    $nReturn               = $sLbl['TFV'] + ($lngTaxBase - $sLbl['BDV']) * $sLbl['BDP'] / 100;
174
                    $this->txLvl['inTaxP'] = 'fx+'.$sLbl['BDP'];
175
                    $counter               = $howMany;
176
                } else {
177
                    $nReturn = $lngTaxBase * $this->txLvl['inTaxP'];
178
                }
179
            }
180
        }
181
        return $nReturn;
182
    }
183
184
    /**
185
     * Somaj
186
     * */
187
    protected function setUnemploymentTax($lngDate, $lngBase, $yearMonth, $aStngs, $dtR) {
188
        $this->txLvl['smjP'] = $this->determineUnemploymentTax($yearMonth, $aStngs, $dtR);
189
        $nReturn             = round($lngBase * $this->txLvl['smjP'] / 100, 0);
190
        $this->txLvl['smj']  = (($lngDate >= 20060701) ? round($nReturn, -4) : $nReturn);
191
    }
192
193
    /**
194
     * Media zilelor lucratoare (pt. calcularea suplimentarelor)
195
     * astfel incat acestea sa nu fie mai valoroase sau nu functie
196
     * de numarul zilelor din luna respectiva
197
     *
198
     * @param string $lngDate
199
     * @param array $stdAvgWrkngHrs
200
     * @param boolean $bCEaster
201
     * @return int
202
     */
203
    protected function setMonthlyAverageWorkingHours($lngDate, $stdAvgWrkngHrs, $bCEaster = false) {
204
        $nReturn = $stdAvgWrkngHrs[substr($lngDate, 0, 4)];
205
        if ($bCEaster) {
206
            $nReturn = ($nReturn * 12 - 8) / 12;
207
        }
208
        return $nReturn;
209
    }
210
211
    private function setValuesFromJson($lngDate, $nValues) {
212
        $crtValues = $nValues[substr($lngDate, 0, 4)];
213
        $nReturn   = $crtValues['Value'];
214
        if (array_key_exists('Month Secondary Value', $crtValues)) {
215
            if (date('n', $lngDate) >= $crtValues['Month Secondary Value']) {
216
                $nReturn = $crtValues['Secondary Value'];
217
            }
218
        }
219
        return $nReturn;
220
    }
221
222
}
223