Completed
Push — master ( 4aa071...e9302d )
by Daniel
02:00
created

Salariu::getIncomeTaxValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 5
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2017 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
class Salariu
32
{
33
34
    use \danielgp\bank_holidays\Romanian,
35
        \danielgp\salariu\FormattingSalariu,
36
        \danielgp\salariu\Bonuses,
37
        \danielgp\salariu\ForeignCurrency,
38
        \danielgp\salariu\Taxation;
39
40
    public function __construct()
41
    {
42
        $configPath     = 'Salariu' . DIRECTORY_SEPARATOR . 'config';
43
        $inElmnts       = $this->readTypeFromJsonFileUniversal($configPath, 'interfaceElements');
44
        $this->appFlags = [
45
            'FI'   => $inElmnts['Form Input'],
46
            'TCAS' => $inElmnts['Table Cell Applied Style'],
47
            'TCSD' => $inElmnts['Table Cell Style Definitions'],
48
        ];
49
        $this->initializeSprGlbAndSession();
50
        $this->handleLocalizationSalariu($inElmnts['Application']);
51
        echo $this->setHeaderHtml();
52
        $this->establishLocalizationToDisplay();
53
        $dtR            = $this->dateRangesInScope();
54
        $ymValues       = $this->buildYMvalues($dtR);
55
        $arySts         = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute');
56
        echo $this->setFormInput($dtR, $ymValues, $arySts['Minimum Wage'], $inElmnts['Values Filter Rules']);
57
        echo $this->setFormOutput($dtR, $arySts, $inElmnts);
58
        echo $this->setFooterHtml($inElmnts['Application']);
59
    }
60
61
    private function getOvertimes($aryStngs)
62
    {
63
        $pcToBoolean = [0 => true, 1 => false];
64
        $pcBoolean   = $pcToBoolean[$this->tCmnSuperGlobals->request->get('pc')];
65
        $ymVal       = (string) $this->tCmnSuperGlobals->request->get('ym');
66
        $snVal       = $this->tCmnSuperGlobals->request->get('sn');
67
        $mnth        = $this->setMonthlyAverageWorkingHours($ymVal, $aryStngs, $pcBoolean);
68
        return [
69
            'os175' => ceil($this->tCmnSuperGlobals->request->get('os175') * 1.75 * $snVal / $mnth),
70
            'os200' => ceil($this->tCmnSuperGlobals->request->get('os200') * 2 * $snVal / $mnth),
71
        ];
72
    }
73
74
    private function getValues($dtR, $lngBase, $aStngs, $shLabels)
75
    {
76
        $inDate             = $this->tCmnSuperGlobals->request->get('ym');
77
        $aReturn            = $this->getValuesPrimary($dtR, $inDate, $lngBase, $aStngs, $shLabels);
78
        $pdV                = [
79
            ($lngBase + $aReturn['ba']),
80
            $this->tCmnSuperGlobals->request->get('pi'),
81
        ];
82
        $aReturn['pd']      = $this->setPersonalDeduction($inDate, $pdV[0], $pdV[1], $aStngs['Personal Deduction']);
83
        $aryDeductions      = [
84
            $this->txLvl['cas'],
85
            $this->txLvl['snt'],
86
            $this->txLvl['smj'],
87
            $aReturn['pd'],
88
        ];
89
        $aReturn['impozit'] = $this->getIncomeTaxValue($inDate, $lngBase, $aReturn['ba'], $aryDeductions, $aStngs);
90
        return $aReturn;
91
    }
92
93
    private function getValuesPrimary($dtR, $inDate, $lngBase, $aStngs, $shLbl)
94
    {
95
        $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]);
96
        $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']], $aStngs[$shLbl['HFUL']]);
97
        $nMealDays        = $this->tCmnSuperGlobals->request->get('nDays');
98
        $unemploymentBase = $lngBase;
99
        if ($this->tCmnSuperGlobals->request->get('ym') < 20080101) {
100
            $unemploymentBase = $this->tCmnSuperGlobals->request->get('sn');
101
        }
102
        $this->setUnemploymentTax($inDate, $unemploymentBase);
103
        return [
104
            'b1'   => $this->setFoodTicketsValue($dtR, $inDate, $aStngs[$shLbl['MTV']]),
105
            'ba'   => $this->setFoodTicketsValue($dtR, $inDate, $aStngs[$shLbl['MTV']]) * $nMealDays,
106
            'gbns' => $this->tCmnSuperGlobals->request->get('gbns') * pow(10, 4),
107
        ];
108
    }
109
110
    private function getWorkingDays()
111
    {
112
        $components = [
113
            \DateTime::createFromFormat('Ymd', $this->tCmnSuperGlobals->request->get('ym')),
114
            $this->tCmnSuperGlobals->request->get('pc'),
115
        ];
116
        $this->tCmnSuperGlobals->request->set('wkDays', $this->setWorkingDaysInMonth($components[0], $components[1]));
0 ignored issues
show
Security Bug introduced by
It seems like $components[0] can also be of type false; however, danielgp\bank_holidays\R...setWorkingDaysInMonth() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
117
        $vDays      = $this->tCmnSuperGlobals->request->get('wkDays') - $this->tCmnSuperGlobals->request->get('zfb');
118
        $this->tCmnSuperGlobals->request->set('nDays', max($vDays, 0));
119
    }
120
121
    private function setFormInput($dtR, $ymValues, $inMW, $inVFR)
122
    {
123
        $this->applyYMvalidations($this->tCmnSuperGlobals, $ymValues, $dtR);
124
        $minWage   = $this->determineCrtMinWage($this->tCmnSuperGlobals, [
125
            'EMW'      => $inMW,
126
            'YM range' => $dtR
127
        ]);
128
        $this->processFormInputDefaults($this->tCmnSuperGlobals, [
129
            'VFR'               => $inVFR,
130
            'Year Month Values' => $ymValues,
131
            'MW'                => $minWage,
132
            'YM range'          => $dtR,
133
        ]);
134
        $sReturn   = $this->setFormInputElements($ymValues, $minWage);
135
        $sReturn[] = $this->setFormInputBottom();
136
        $frm       = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [
137
            'method' => 'get',
138
            'action' => $this->tCmnSuperGlobals->getScriptName()
139
        ]);
140
        return $this->setFormInputIntoFieldSet($frm);
141
    }
142
143
    private function setFormInputBottom()
144
    {
145
        $btn = $this->setStringIntoShortTag('input', [
146
            'type'  => 'submit',
147
            'id'    => 'submit',
148
            'value' => $this->tApp->gettext('i18n_Form_Button_Recalculate')
149
        ]);
150
        return $this->setFormRow($this->setLabel('fd'), $btn, 1) . '</tbody>';
151
    }
152
153
    private function setFormInputElements($ymValues, $crtMinWage)
154
    {
155
        $sReturn   = [];
156
        $sReturn[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_InputElements')
157
            . '</th><th>' . $this->tApp->gettext('i18n_Form_Label_InputValues') . '</th></tr></thead><tbody>';
158
        $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM($ymValues), 1);
159
        $sReturn[] = $this->setFormRow($this->setLabel('sm'), $this->setFormInputText('sm', 10, 'RON', $crtMinWage), 1);
160
        $sReturn[] = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1);
161
        $sReturn[] = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 7, '%'), 1);
162
        $sReturn[] = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1);
163
        $sReturn[] = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1);
164
        $sReturn[] = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, 'ore'), 1);
165
        $sReturn[] = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, 'ore'), 1);
166
        $sReturn[] = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1);
167
        $sReturn[] = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1);
168
        $sReturn[] = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 10, 'RON'), 1);
169
        $sReturn[] = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, 'zile'), 1);
170
        $sReturn[] = $this->setFormRow($this->setLabel('zfs'), $this->setFormInputText('zfs', 2, 'zile'), 1);
171
        $sReturn[] = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 10, 'RON'), 1);
172
        $sReturn[] = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 10, 'RON'), 1);
173
        return $sReturn;
174
    }
175
176
    private function setFormInputIntoFieldSet($frm)
177
    {
178
        $aryFieldSet = [
179
            $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'),
180
            $frm
181
        ];
182
        return $this->setStringIntoTag(implode('', $aryFieldSet), 'fieldset', ['style' => 'float: left;']);
183
    }
184
185
    private function setFormInputText($inName, $inSize, $inAfterLabel, $crtMinWage = 0)
186
    {
187
        $inputParameters = [
188
            'type'      => 'text',
189
            'name'      => $inName,
190
            'value'     => $this->tCmnSuperGlobals->request->get($inName),
191
            'size'      => $inSize,
192
            'maxlength' => $inSize,
193
        ];
194
        if ($inName == 'sm') {
195
            $inputParameters['readonly'] = 'readonly';
196
            $inputParameters['value']    = $crtMinWage;
197
            $this->tCmnSuperGlobals->request->set('sm', $crtMinWage);
198
        }
199
        return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel;
200
    }
201
202
    private function setFormOutput($dtR, $aryStngs, $inElements)
203
    {
204
        $sReturn     = [];
205
        $this->setExchangeRateValues($inElements['Application'], $inElements['Relevant Currencies']);
206
        $sReturn[]   = $this->setFormOutputHeader();
207
        $ovTimeVal   = $this->getOvertimes($aryStngs['Monthly Average Working Hours']);
208
        $additions   = $this->tCmnSuperGlobals->request->get('pb') + $ovTimeVal['os175'] + $ovTimeVal['os200'];
209
        $this->getWorkingDays();
210
        $bComponents = [
211
            'sc'   => $this->tCmnSuperGlobals->request->get('sc'),
212
            'sn'   => $this->tCmnSuperGlobals->request->get('sn'),
213
            'zile' => $this->tCmnSuperGlobals->request->get('wkDays'),
214
        ];
215
        $xDate       = '<span style="font-size:smaller;">' . date('d.m.Y', $this->currencyDetails['CXD']) . '</span>';
216
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('xrate@Date'), $xDate, pow(10, 7));
217
        $snValue     = $this->tCmnSuperGlobals->request->get('sn') * pow(10, 4);
218
        $amntLAA     = round(($this->tCmnSuperGlobals->request->get('zfs') / $bComponents['zile']) * $snValue, -4);
219
        $sReturn[]   = $this->setFormOutputBonuses($snValue, $bComponents['zile'], $amntLAA, $ovTimeVal);
220
        $brut        = ($bComponents['sn'] * (1 + $bComponents['sc'] / 100) + $additions) * pow(10, 4) - $amntLAA;
221
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('sb'), '&nbsp;', $brut);
222
        $brut2       = $brut + $this->tCmnSuperGlobals->request->get('afet') * pow(10, 4);
223
        $amnt        = $this->getValues($dtR, $brut2, $aryStngs, $inElements['Short Labels']);
224
        $sReturn[]   = $this->setFormOutputTaxations($brut2, $amnt);
225
        $pnValue     = $this->tCmnSuperGlobals->request->get('pn') * pow(10, 4);
226
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('pn'), '&nbsp;', $pnValue);
227
        $retineri    = $this->txLvl['cas'] + $this->txLvl['smj'] + $this->txLvl['snt'] + $amnt['impozit'];
228
        $net         = $brut2 - $retineri + $this->tCmnSuperGlobals->request->get('pn') * pow(10, 4);
229
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('ns'), '&nbsp;', $net);
230
        $szamntValue = $this->tCmnSuperGlobals->request->get('szamnt') * pow(10, 4);
231
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('szamnt'), '&nbsp;', $szamntValue);
232
        $nsc         = $net - $this->tCmnSuperGlobals->request->get('szamnt') * pow(10, 4);
233
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('nsc'), '&nbsp;', $nsc);
234
        $fBonus      = [
235
            'main'   => $this->setLabel('gb'),
236
            'value'  => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue'),
237
            'mtDays' => $this->tCmnSuperGlobals->request->get('nDays') . '&nbsp;/&nbsp;' . $bComponents['zile']
238
        ];
239
        $fBonusTxt   = sprintf($fBonus['main'], $fBonus['value']);
240
        $sReturn[]   = $this->setFrmRowTwoLbls($fBonusTxt, $fBonus['mtDays'], $amnt['ba']);
241
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('gbns'), '&nbsp;', $amnt['gbns']);
242
        $total       = ($net + $amnt['ba'] + $amnt['gbns'] - $this->tCmnSuperGlobals->request->get('szamnt') * 10000);
243
        $sReturn[]   = $this->setFrmRowTwoLbls($this->setLabel('total'), '&nbsp;', $total);
244
        $sReturn[]   = '</tbody>';
245
        $tDate       = \DateTime::createFromFormat('Ymd', $this->tCmnSuperGlobals->request->get('ym'));
246
        $legentText  = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results')
247
            . '', strftime('%B', mktime(0, 0, 0, $tDate->format('n'), 1, $tDate->format('Y'))), $tDate->format('Y'));
248
        $fieldsetC   = $this->setStringIntoTag($legentText, 'legend')
249
            . $this->setStringIntoTag(implode('', $sReturn), 'table');
250
        return $this->setStringIntoTag($fieldsetC, 'fieldset', [
251
                'style' => 'float: left;'
252
        ]);
253
    }
254
255
    private function setFormOutputBonuses($snValue, $wkDays, $amntLAA, $ovTimeVal)
256
    {
257
        $sRt      = [];
258
        $sMin     = $this->tCmnSuperGlobals->request->get('sm') * pow(10, 4);
259
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('sm'), '&nbsp;', $sMin);
260
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('sn'), '&nbsp;', $snValue);
261
        $scValue  = $this->tCmnSuperGlobals->request->get('sc');
262
        $prima    = $this->tCmnSuperGlobals->request->get('sn') * $scValue * 100;
263
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('sc'), $scValue . '%', $prima);
264
        $pbValue  = $this->tCmnSuperGlobals->request->get('pb') * pow(10, 4);
265
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('pb'), '&nbsp;', $pbValue);
266
        $ovTime   = [
267
            'o1' => $this->tCmnSuperGlobals->request->get('os175'),
268
            'o2' => $this->tCmnSuperGlobals->request->get('os200'),
269
        ];
270
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('ovAmount1'), '<span style="font-size:smaller;">'
271
            . $ovTime['o1'] . 'h&nbsp;x&nbsp;175%</span>', $ovTimeVal['os175'] * pow(10, 4));
272
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('ovAmount2'), '<span style="font-size:smaller;">'
273
            . $ovTime['o2'] . 'h&nbsp;x&nbsp;200%</span>', $ovTimeVal['os200'] * pow(10, 4));
274
        $fLeaveAA = $this->tCmnSuperGlobals->request->get('zfs') . '&nbsp;/&nbsp;' . $wkDays;
275
        $sRt[]    = $this->setFrmRowTwoLbls($this->setLabel('zfsa'), $fLeaveAA, $amntLAA);
276
        return implode('', $sRt);
277
    }
278
279
    private function setFormOutputHeader()
280
    {
281
        $sReturn   = [];
282
        $sReturn[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_ResultedElements') . '</th>';
283
        $sReturn[] = '<th><i class="fa fa-map-signs floatRight" style="font-size:2em;">&nbsp;</i></th>';
284
        foreach ($this->currencyDetails['CX'] as $value) {
285
            $sReturn[] = '<th style="text-align:center;"><span class="flag-icon flag-icon-' . $value['country']
286
                . '" style="font-size:2em;" title="'
287
                . $this->tApp->gettext('i18n_CountryName_' . strtoupper($value['country'])) . '">&nbsp;</span></th>';
288
        }
289
        return implode('', $sReturn) . '</tr></thead></tbody>';
290
    }
291
292
    private function setFormOutputTaxations($brut, $amnt)
293
    {
294
        $sRn              = [];
295
        $limitDisplayBase = false;
296 View Code Duplication
        if ($brut > $this->txLvl['casP_base']) {
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...
297
            $limitDisplayBase = true;
298
            $sRn[]            = $this->setFrmRowTwoLbls($this->setLabel('cas_base'), '', $this->txLvl['casP_base']);
299
        }
300
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('cas'), $this->txLvl['casP'] . '%', $this->txLvl['cas']);
301
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('somaj'), $this->txLvl['smjP'] . '%', $this->txLvl['smj']);
302 View Code Duplication
        if ($limitDisplayBase && array_key_exists('sntP_base', $this->txLvl)) {
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...
303
            $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('sntP_base'), '', $this->txLvl['sntP_base']);
304
        }
305
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('sanatate'), $this->txLvl['sntP'] . '%', $this->txLvl['snt']);
306
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('pd'), '&nbsp;', $amnt['pd']);
307
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('impozit_base'), '&nbsp;', $this->txLvl['inTaxP_base']);
308
        $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('impozit'), $this->txLvl['inTaxP'] . '%', $amnt['impozit']);
309
        return implode('', $sRn);
310
    }
311
312
    private function setFormRow($text, $value, $type = 'amount')
313
    {
314
        $defaultCellStyle = $this->setFormatRow($text, $value);
315
        switch ($type) {
316
            case 'amount':
317
                $value2show = $this->setFormRowAmount(($value / pow(10, 4)), $defaultCellStyle);
318
                break;
319
            case 'value':
320
                $value2show = $this->setStringIntoTag($value, 'td', array_merge($defaultCellStyle, [
321
                    'colspan' => count($this->currencyDetails['CX'])
322
                ]));
323
                break;
324
            default:
325
                $value2show = $this->setStringIntoTag($value, 'td');
326
                break;
327
        }
328
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
329
    }
330
331
    private function setFormRowAmount($value, $defaultCellStyle)
332
    {
333
        $cellValue                 = [];
334
        $defaultCellStyle['style'] .= 'text-align:right;';
335
        foreach ($this->currencyDetails['CX'] as $key2 => $value2) {
336
            $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
337
            $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
338
            $finalValue  = $fmt->formatCurrency($value / $this->currencyDetails['CXV'][$key2], $key2);
339
            $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle);
340
        }
341
        return implode('', $cellValue);
342
    }
343
344
    private function setFrmRowTwoLbls($text1, $text2, $value)
345
    {
346
        return str_replace(':</td>', ':</td><td class="labelS" style="text-align:right;">'
347
            . $text2 . '</td>', $this->setFormRow($text1, $value, 'amount'));
348
    }
349
}
350