Completed
Push — master ( 2dbfbb...b82522 )
by Daniel
04:36
created

Salariu   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 18
Bugs 0 Features 2
Metric Value
wmc 17
c 18
b 0
f 2
lcom 1
cbo 8
dl 0
loc 236
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A getIncomeTaxValue() 0 12 3
A getOvertimes() 0 12 1
A getValues() 0 20 1
A getValuesPrimary() 0 16 2
A setFormInput() 0 19 1
A setFormInputElements() 0 18 1
A setFormInputText() 0 11 1
A setFormOutput() 0 60 1
A setFormRow() 0 18 3
A setFormRowAmount() 0 12 2
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\BasicSalariu,
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
        $interfaceElements = $this->readTypeFromJsonFileUniversal($configPath, 'interfaceElements');
44
        $this->appFlags    = [
45
            'FI'   => $interfaceElements['Form Input'],
46
            'TCAS' => $interfaceElements['Table Cell Applied Style'],
47
            'TCSD' => $interfaceElements['Table Cell Style Definitions'],
48
        ];
49
        $this->initializeSprGlbAndSession();
50
        $this->handleLocalizationSalariu($interfaceElements['Application']);
51
        echo $this->setHeaderHtml();
52
        $this->processFormInputDefaults($interfaceElements['Default Values']);
53
        echo $this->setFormInput();
54
        $this->setExchangeRateValues($interfaceElements['Application'], $interfaceElements['Relevant Currencies']);
55
        echo $this->setFormOutput($configPath, $interfaceElements['Short Labels']);
56
        echo $this->setFooterHtml($interfaceElements['Application']);
57
    }
58
59
    private function getIncomeTaxValue($inDate, $lngBase, $vBA, $aryDeductions, $arySettings)
60
    {
61
        $rest = $lngBase - array_sum($aryDeductions);
62
        if ($inDate >= mktime(0, 0, 0, 7, 1, 2010)) {
63
            $rest += round($vBA, -4);
64
            if ($inDate >= mktime(0, 0, 0, 10, 1, 2010)) {
65
                $rest += round($this->tCmnSuperGlobals->get('gbns') * pow(10, 4), -4);
66
            }
67
        }
68
        $rest += $this->tCmnSuperGlobals->get('afet') * pow(10, 4);
69
        return $this->setIncomeTax($inDate, $rest, $arySettings['Income Tax']);
70
    }
71
72
    private function getOvertimes($aryStngs)
73
    {
74
        $pcToBoolean = [0 => true, 1 => false];
75
        $pcBoolean   = $pcToBoolean[$this->tCmnSuperGlobals->get('pc')];
76
        $ymVal       = $this->tCmnSuperGlobals->get('ym');
77
        $snVal       = $this->tCmnSuperGlobals->get('sn');
78
        $mnth        = $this->setMonthlyAverageWorkingHours($ymVal, $aryStngs, $pcBoolean);
79
        return [
80
            'os175' => ceil($this->tCmnSuperGlobals->get('os175') * 1.75 * $snVal / $mnth),
81
            'os200' => ceil($this->tCmnSuperGlobals->get('os200') * 2 * $snVal / $mnth),
82
        ];
83
    }
84
85
    private function getValues($lngBase, $aStngs, $shLabels)
86
    {
87
        $inDate             = $this->tCmnSuperGlobals->get('ym');
88
        $inDT               = new \DateTime(date('Y/m/d', $inDate));
89
        $wkDay              = $this->setWorkingDaysInMonth($inDT, $this->tCmnSuperGlobals->get('pc'));
90
        $aReturn            = $this->getValuesPrimary($inDate, $wkDay, $lngBase, $aStngs, $shLabels);
91
        $pdV                = [
92
            ($lngBase + $aReturn['ba']),
93
            $this->tCmnSuperGlobals->get('pi'),
94
        ];
95
        $aReturn['pd']      = $this->setPersonalDeduction($inDate, $pdV[1], $pdV[2], $aStngs['Personal Deduction']);
96
        $aryDeductions      = [
97
            $aReturn['cas'],
98
            $aReturn['sanatate'],
99
            $aReturn['somaj'],
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, $wkDay, $lngBase, $aStngs, $shLbl)
107
    {
108
        $nMealDays        = ($wkDay - $this->tCmnSuperGlobals->get('zfb'));
109
        $unemploymentBase = $lngBase;
110
        if ($this->tCmnSuperGlobals->get('ym') < mktime(0, 0, 0, 1, 1, 2008)) {
111
            $unemploymentBase = $this->tCmnSuperGlobals->get('sn');
112
        }
113
        return [
114
            'ba'       => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays,
115
            'cas'      => $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]),
116
            'gbns'     => $this->tCmnSuperGlobals->get('gbns') * pow(10, 4),
117
            'sanatate' => $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']]),
118
            'somaj'    => $this->setUnemploymentTax($inDate, $unemploymentBase),
119
            'zile'     => $wkDay,
120
        ];
121
    }
122
123
    private function setFormInput()
124
    {
125
        $sReturn     = $this->setFormInputElements();
126
        $btn         = $this->setStringIntoShortTag('input', [
127
            'type'  => 'submit',
128
            'id'    => 'submit',
129
            'value' => $this->setLabel('bc')
130
        ]);
131
        $sReturn[]   = $this->setFormRow($this->setLabel('fd'), $btn, 1);
132
        $frm         = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [
133
            'method' => 'get',
134
            'action' => $this->tCmnSuperGlobals->getScriptName()
135
        ]);
136
        $aryFieldSet = [
137
            $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'),
138
            $frm
139
        ];
140
        return $this->setStringIntoTag(implode('', $aryFieldSet), 'fieldset', ['style' => 'float: left;']);
141
    }
142
143
    private function setFormInputElements()
144
    {
145
        $sReturn   = [];
146
        $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM(), 1);
147
        $sReturn[] = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1);
148
        $sReturn[] = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 2, '%'), 1);
149
        $sReturn[] = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1);
150
        $sReturn[] = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1);
151
        $sReturn[] = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, ''), 1);
152
        $sReturn[] = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, ''), 1);
153
        $sReturn[] = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1);
154
        $sReturn[] = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1);
155
        $sReturn[] = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 10, 'RON'), 1);
156
        $sReturn[] = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, ''), 1);
157
        $sReturn[] = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 10, 'RON'), 1);
158
        $sReturn[] = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 10, 'RON'), 1);
159
        return $sReturn;
160
    }
161
162
    private function setFormInputText($inName, $inSize, $inAfterLabel)
163
    {
164
        $inputParameters = [
165
            'type'      => 'text',
166
            'name'      => $inName,
167
            'value'     => $this->tCmnSuperGlobals->get($inName),
168
            'size'      => $inSize,
169
            'maxlength' => $inSize,
170
        ];
171
        return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel;
172
    }
173
174
    private function setFormOutput($configPath, $shLabels)
175
    {
176
        $aryStngs    = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute');
177
        $sReturn     = [];
178
        $ovTimeVal   = $this->getOvertimes($aryStngs['Monthly Average Working Hours']);
179
        $additions   = $this->tCmnSuperGlobals->get('pb') + $ovTimeVal['os175'] + $ovTimeVal['os200'];
180
        $bComponents = [
181
            'sc' => $this->tCmnSuperGlobals->get('sc'),
182
            'sn' => $this->tCmnSuperGlobals->get('sn'),
183
        ];
184
        $brut        = ($bComponents['sn'] * (1 + $bComponents['sc'] / 100) + $additions) * pow(10, 4);
185
        $xRate       = str_replace('%1', date('d.m.Y', $this->currencyDetails['CXD']), $this->setLabel('xrate@Date'));
186
        $sReturn[]   = $this->setFormRow($xRate, 10000000);
187
        $sReturn[]   = $this->setFormRow($this->setLabel('sn'), $this->tCmnSuperGlobals->get('sn') * 10000);
188
        $prima       = $this->tCmnSuperGlobals->get('sn') * $this->tCmnSuperGlobals->get('sc') * 100;
189
        $sReturn[]   = $this->setFormRow($this->setLabel('sc'), $prima);
190
        $sReturn[]   = $this->setFormRow($this->setLabel('pb'), $this->tCmnSuperGlobals->get('pb') * 10000);
191
        $ovTime      = [
192
            'm' => $this->setLabel('ovAmount'),
193
            1   => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
194
            2   => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
195
        ];
196
        $sReturn[]   = $this->setFormRow(sprintf($ovTime['m'], $ovTime[1], '175%'), ($ovTimeVal['os175'] * pow(10, 4)));
197
        $sReturn[]   = $this->setFormRow(sprintf($ovTime['m'], $ovTime[2], '200%'), ($ovTimeVal['os200'] * pow(10, 4)));
198
        $sReturn[]   = $this->setFormRow($this->setLabel('sb'), $brut);
199
        $brut        += $this->tCmnSuperGlobals->get('afet') * pow(10, 4);
200
        $amount      = $this->getValues($brut, $aryStngs, $shLabels);
201
        $sReturn[]   = $this->setFormRow($this->setLabel('cas'), $amount['cas']);
202
        $sReturn[]   = $this->setFormRow($this->setLabel('somaj'), $amount['somaj']);
203
        $sReturn[]   = $this->setFormRow($this->setLabel('sanatate'), $amount['sanatate']);
204
        $sReturn[]   = $this->setFormRow($this->setLabel('pd'), $amount['pd']);
205
        $sReturn[]   = $this->setFormRow($this->setLabel('impozit'), $amount['impozit']);
206
        $sReturn[]   = $this->setFormRow($this->setLabel('pn'), $this->tCmnSuperGlobals->get('pn') * 10000);
207
        $retineri    = $amount['cas'] + $amount['somaj'] + $amount['sanatate'] + $amount['impozit'];
208
        $net         = $brut - $retineri + $this->tCmnSuperGlobals->get('pn') * 10000;
209
        $sReturn[]   = $this->setFormRow($this->setLabel('ns'), $net);
210
        $sReturn[]   = $this->setFormRow($this->setLabel('szamnt'), $this->tCmnSuperGlobals->get('szamnt') * 10000);
211
        $nsc         = $net - $this->tCmnSuperGlobals->get('szamnt') * 10000;
212
        $sReturn[]   = $this->setFormRow($this->setLabel('nsc'), $nsc);
213
        $sReturn[]   = $this->setFormRow($this->setLabel('wkDays'), $amount['zile'], 'value');
214
        $fBonus      = [
215
            'main'   => $this->setLabel('gb'),
216
            'no'     => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceNo'),
217
            'value'  => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue'),
218
            'mtDays' => ($amount['zile'] - $this->tCmnSuperGlobals->get('zfb'))
219
        ];
220
        $fBonusTxt   = sprintf($fBonus['main'], $fBonus['value'], $fBonus['no'], $fBonus['mtDays']);
221
        $sReturn[]   = $this->setFormRow($fBonusTxt, $amount['ba']);
222
        $sReturn[]   = $this->setFormRow($this->setLabel('gbns'), $amount['gbns']);
223
        $total       = ($net + $amount['ba'] + $amount['gbns'] - $this->tCmnSuperGlobals->get('szamnt') * 10000);
224
        $sReturn[]   = $this->setFormRow($this->setLabel('total'), $total);
225
        setlocale(LC_TIME, explode('_', $this->tCmnSession->get('lang'))[0]);
226
        $crtMonth    = strftime('%B', $this->tCmnSuperGlobals->get('ym'));
227
        $legentText  = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results')
228
            . '', $crtMonth, date('Y', $this->tCmnSuperGlobals->get('ym')));
229
        $legend      = $this->setStringIntoTag($legentText, 'legend');
230
        return $this->setStringIntoTag($legend . $this->setStringIntoTag(implode('', $sReturn), 'table'), 'fieldset', [
231
                'style' => 'float: left;'
232
        ]);
233
    }
234
235
    private function setFormRow($text, $value, $type = 'amount')
236
    {
237
        $defaultCellStyle = $this->setFormatRow($text, $value);
238
        switch ($type) {
239
            case 'amount':
240
                $value2show = $this->setFormRowAmount(($value / pow(10, 4)), $defaultCellStyle);
241
                break;
242
            case 'value':
243
                $value2show = $this->setStringIntoTag($value, 'td', array_merge($defaultCellStyle, [
244
                    'colspan' => count($this->currencyDetails['CX'])
245
                ]));
246
                break;
247
            default:
248
                $value2show = $this->setStringIntoTag($value, 'td');
249
                break;
250
        }
251
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
252
    }
253
254
    private function setFormRowAmount($value, $defaultCellStyle)
255
    {
256
        $cellValue                 = [];
257
        $defaultCellStyle['style'] .= 'text-align:right;';
258
        foreach ($this->currencyDetails['CX'] as $key2 => $value2) {
259
            $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
260
            $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
261
            $finalValue  = $fmt->formatCurrency($value / $this->currencyDetails['CXV'][$key2], $key2);
262
            $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle);
263
        }
264
        return implode('', $cellValue);
265
    }
266
}
267