Completed
Push — master ( db914b...9ba612 )
by Daniel
01:53
created

Salariu::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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