Completed
Push — master ( 254490...158eb7 )
by Daniel
11:17
created

Taxation::setUnemploymentTax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 5
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
     * Impozit pe salariu
85
     * */
86
    protected function setIncomeTax($lngDate, $lngTaxBase, $nValues) {
87
        $yrDate  = (int) substr($lngDate, 0, 4);
88
        $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues, $yrDate);
89
        if ($yrDate == 2001) {
90
            $nReturn = $this->setIncomeTax2001($lngDate, $lngTaxBase, $nValues);
91
        }
92
        return (($lngDate >= 20060701) ? round($nReturn, -4) : $nReturn);
93
    }
94
95
    /**
96
     * Impozit pe salariu
97
     * */
98
    private function setIncomeTax2001($lngDate, $lngTaxBase, $nValues) {
99
        $mnth    = substr($lngDate, 4, 2);
100
        $nReturn = $this->setIncomeTaxFromJson($lngTaxBase, $nValues["2001-12"]); // for > 9
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...
101
        if ($mnth <= 6) {
102
            $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...
103
        } elseif ($mnth <= 9) {
104
            $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...
105
        }
106
        return $nReturn;
107
    }
108
109
    private function setIncomeTaxFromJson($lngTaxBase, $nValues, $yrDate) {
110
        $nReturn = 0;
111
        $howMany = count($nValues);
112
        if (array_key_exists('Percentage', $nValues[$yrDate])) {
113
            $this->txLvl['inTaxP']      = $nValues[$yrDate]['Percentage'];
114
            $this->txLvl['inTaxP_base'] = $lngTaxBase;
115
            $nReturn                    = $lngTaxBase * $this->txLvl['inTaxP'] / 100;
116
        } else {
117
            for ($counter = 0; $counter <= $howMany; $counter++) {
118
                $nReturn = $lngTaxBase * $this->txLvl['inTaxP'];
119
                if (($lngTaxBase <= $nValues[$counter]['Upper Limit Value'])) {
120
                    $sLbl                  = [
121
                        'BDP' => $nValues[$counter]['Base Deducted Percentage'],
122
                        'BDV' => $nValues[$counter]['Base Deduction Value'],
123
                        'TFV' => $nValues[$counter]['Tax Free Value'],
124
                    ];
125
                    $nReturn               = $sLbl['TFV'] + ($lngTaxBase - $sLbl['BDV']) * $sLbl['BDP'] / 100;
126
                    $this->txLvl['inTaxP'] = 'fx+' . $sLbl['BDP'];
127
                    $counter               = $howMany;
128
                }
129
            }
130
        }
131
        return $nReturn;
132
    }
133
134
    /**
135
     * Somaj
136
     * */
137
    protected function setUnemploymentTax($lngDate, $lngBase, $yearMonth, $aStngs, $dtR) {
138
        $this->txLvl['smjP'] = $this->determineUnemploymentTax($yearMonth, $aStngs, $dtR);
139
        $nReturn             = round($lngBase * $this->txLvl['smjP'] / 100, 0);
140
        $this->txLvl['smj']  = (($lngDate >= 20060701) ? round($nReturn, -4) : $nReturn);
141
    }
142
143
    /**
144
     * Media zilelor lucratoare (pt. calcularea suplimentarelor)
145
     * astfel incat acestea sa nu fie mai valoroase sau nu functie
146
     * de numarul zilelor din luna respectiva
147
     *
148
     * @param string $lngDate
149
     * @param array $stdAvgWrkngHrs
150
     * @param boolean $bCEaster
151
     * @return int
152
     */
153
    protected function setMonthlyAverageWorkingHours($lngDate, $stdAvgWrkngHrs, $bCEaster = false) {
154
        $nReturn = $stdAvgWrkngHrs[substr($lngDate, 0, 4)];
155
        if ($bCEaster) {
156
            $nReturn = ($nReturn * 12 - 8) / 12;
157
        }
158
        return $nReturn;
159
    }
160
161
    private function setValuesFromJson($lngDate, $nValues) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
162
        $crtValues = $nValues[substr($lngDate, 0, 4)];
163
        $nReturn   = $crtValues['Value'];
164
        if (array_key_exists('Month Secondary Value', $crtValues)) {
165
            if (date('n', $lngDate) >= $crtValues['Month Secondary Value']) {
166
                $nReturn = $crtValues['Secondary Value'];
167
            }
168
        }
169
        return $nReturn;
170
    }
171
172
}
173