Completed
Push — master ( 9ba612...ba7d13 )
by Daniel
02:01
created

Salariu::setFormInputBottom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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