Completed
Push — master ( 4082fd...26097a )
by Daniel
02:02
created

Salariu   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 310
Duplicated Lines 2.26 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 21
Bugs 0 Features 2
Metric Value
wmc 26
c 21
b 0
f 2
lcom 1
cbo 10
dl 7
loc 310
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormInputText() 0 11 1
A __construct() 0 19 1
A getIncomeTaxValue() 0 12 3
A getOvertimes() 0 12 1
A setFormOutputHeader() 0 12 2
A getValues() 0 18 1
A getValuesPrimary() 0 16 2
A getWorkingDays() 0 10 1
A setFormInput() 0 19 1
A setFormInputElements() 0 21 1
A setFormOutput() 0 53 1
B setFormOutputBonuses() 0 28 1
A setFormOutputTaxations() 7 18 4
A setFormRow() 0 18 3
A setFormRowAmount() 0 12 2
A setFrmRowTwoLbls() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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