Completed
Push — master ( 76756b...bf4509 )
by Daniel
02:36
created

Salariu::setFormOutputHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 9
nc 2
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
        $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[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, $wkDay, $lngBase, $aStngs, $shLbl)
107
    {
108
        $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]);
109
        $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']]);
110
        $nMealDays        = ($wkDay - $this->tCmnSuperGlobals->get('zfb'));
111
        $unemploymentBase = $lngBase;
112
        if ($this->tCmnSuperGlobals->get('ym') < mktime(0, 0, 0, 1, 1, 2008)) {
113
            $unemploymentBase = $this->tCmnSuperGlobals->get('sn');
114
        }
115
        $this->setUnemploymentTax($inDate, $unemploymentBase);
116
        return [
117
            'ba'   => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays,
118
            'gbns' => $this->tCmnSuperGlobals->get('gbns') * pow(10, 4),
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->tApp->gettext('i18n_Form_Button_Recalculate')
130
        ]);
131
        $sReturn[]   = $this->setFormRow($this->setLabel('fd'), $btn, 1) . '</tbody>';
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[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_InputElements')
147
            . '</th><th>' . $this->tApp->gettext('i18n_Form_Label_InputValues') . '</th></tr></thead><tbody>';
148
        $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM(), 1);
149
        $sReturn[] = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1);
150
        $sReturn[] = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 2, '%'), 1);
151
        $sReturn[] = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1);
152
        $sReturn[] = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1);
153
        $sReturn[] = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, 'ore'), 1);
154
        $sReturn[] = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, 'ore'), 1);
155
        $sReturn[] = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1);
156
        $sReturn[] = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1);
157
        $sReturn[] = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 10, 'RON'), 1);
158
        $sReturn[] = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, 'zile'), 1);
159
        $sReturn[] = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 10, 'RON'), 1);
160
        $sReturn[] = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 10, 'RON'), 1);
161
        return $sReturn;
162
    }
163
164
    private function setFormInputText($inName, $inSize, $inAfterLabel)
165
    {
166
        $inputParameters = [
167
            'type'      => 'text',
168
            'name'      => $inName,
169
            'value'     => $this->tCmnSuperGlobals->get($inName),
170
            'size'      => $inSize,
171
            'maxlength' => $inSize,
172
        ];
173
        return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel;
174
    }
175
176
    private function setFormOutput($configPath, $shLabels)
177
    {
178
        $aryStngs    = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute');
179
        $sReturn     = [];
180
        $sReturn[]   = $this->setFormOutputHeader();
181
        $ovTimeVal   = $this->getOvertimes($aryStngs['Monthly Average Working Hours']);
182
        $additions   = $this->tCmnSuperGlobals->get('pb') + $ovTimeVal['os175'] + $ovTimeVal['os200'];
183
        $bComponents = [
184
            'sc' => $this->tCmnSuperGlobals->get('sc'),
185
            'sn' => $this->tCmnSuperGlobals->get('sn'),
186
        ];
187
        $brut        = ($bComponents['sn'] * (1 + $bComponents['sc'] / 100) + $additions) * pow(10, 4);
188
        $xDate       = '<span style="font-size:smaller;">' . date('d.m.Y', $this->currencyDetails['CXD']) . '</span>';
189
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('xrate@Date'), $xDate, 10000000);
190
        $snValue     = $this->tCmnSuperGlobals->get('sn') * 10000;
191
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('sn'), '&nbsp;', $snValue);
192
        $scValue     = $this->tCmnSuperGlobals->get('sc');
193
        $prima       = $this->tCmnSuperGlobals->get('sn') * $scValue * 100;
194
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('sc'), $scValue . '%', $prima);
195
        $pbValue     = $this->tCmnSuperGlobals->get('pb') * 10000;
196
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('pb'), '&nbsp;', $pbValue);
197
        $ovTime      = [
198
            'm' => $this->setLabel('ovAmount'),
199
            1   => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
200
            2   => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
201
            11  => $ovTimeVal['os175'] * pow(10, 4),
202
            22  => $ovTimeVal['os200'] * pow(10, 4),
203
        ];
204
        $sReturn[]   = $this->setFormRowTwoLabels(sprintf($ovTime['m'], $ovTime[1]), '175%', $ovTime[11]);
205
        $sReturn[]   = $this->setFormRowTwoLabels(sprintf($ovTime['m'], $ovTime[2]), '200%', $ovTime[22]);
206
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('sb'), '&nbsp;', $brut);
207
        $brut        += $this->tCmnSuperGlobals->get('afet') * pow(10, 4);
208
        $amount      = $this->getValues($brut, $aryStngs, $shLabels);
209
        $casValue    = $this->txLvl['cas'];
210
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('cas'), $this->txLvl['casP'] . '%', $casValue);
211
        $smjValue    = $this->txLvl['smj'];
212
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('somaj'), $this->txLvl['smjP'] . '%', $this->txLvl['smj']);
213
        $sntValue    = $this->txLvl['snt'];
214
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('sanatate'), $this->txLvl['sntP'] . '%', $sntValue);
215
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('pd'), '&nbsp;', $amount['pd']);
216
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('impozit'), '&nbsp;', $amount['impozit']);
217
        $pnValue     = $this->tCmnSuperGlobals->get('pn') * 10000;
218
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('pn'), '&nbsp;', $pnValue);
219
        $retineri    = $casValue + $smjValue + $sntValue + $amount['impozit'];
220
        $net         = $brut - $retineri + $this->tCmnSuperGlobals->get('pn') * 10000;
221
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('ns'), '&nbsp;', $net);
222
        $szamntValue = $this->tCmnSuperGlobals->get('szamnt') * 10000;
223
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('szamnt'), '&nbsp;', $szamntValue);
224
        $nsc         = $net - $this->tCmnSuperGlobals->get('szamnt') * 10000;
225
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('nsc'), '&nbsp;', $nsc);
226
        $fBonus      = [
227
            'main'   => $this->setLabel('gb'),
228
            'value'  => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue'),
229
            'mtDays' => ($amount['zile'] - $this->tCmnSuperGlobals->get('zfb')) . '/' . $amount['zile']
230
        ];
231
        $fBonusTxt   = sprintf($fBonus['main'], $fBonus['value']);
232
        $sReturn[]   = $this->setFormRowTwoLabels($fBonusTxt, $fBonus['mtDays'], $amount['ba']);
233
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('gbns'), '&nbsp;', $amount['gbns']);
234
        $total       = ($net + $amount['ba'] + $amount['gbns'] - $this->tCmnSuperGlobals->get('szamnt') * 10000);
235
        $sReturn[]   = $this->setFormRowTwoLabels($this->setLabel('total'), '&nbsp;', $total);
236
        $sReturn[]   = '</tbody>';
237
        setlocale(LC_TIME, explode('_', $this->tCmnSession->get('lang'))[0]);
238
        $crtMonth    = strftime('%B', $this->tCmnSuperGlobals->get('ym'));
239
        $legentText  = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results')
240
            . '', $crtMonth, date('Y', $this->tCmnSuperGlobals->get('ym')));
241
        $legend      = $this->setStringIntoTag($legentText, 'legend');
242
        return $this->setStringIntoTag($legend . $this->setStringIntoTag(implode('', $sReturn), 'table'), 'fieldset', [
243
                'style' => 'float: left;'
244
        ]);
245
    }
246
247
    private function setFormOutputHeader()
248
    {
249
        $sReturn   = [];
250
        $sReturn[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_ResultedElements') . '</th>';
251
        $sReturn[] = '<th><i class="fa fa-map-signs floatRight" style="font-size:2em;">&nbsp;</i></th>';
252
        foreach ($this->currencyDetails['CX'] as $value) {
253
            $sReturn[] = '<th style="text-align:center;"><span class="flag-icon flag-icon-' . $value['country']
254
                . '" style="font-size:2em;" title="'
255
                . $this->tApp->gettext('i18n_CountryName_' . strtoupper($value['country'])) . '">&nbsp;</span></th>';
256
        }
257
        return implode('', $sReturn) . '</tr></thead></tbody>';
258
    }
259
260
    private function setFormRow($text, $value, $type = 'amount')
261
    {
262
        $defaultCellStyle = $this->setFormatRow($text, $value);
263
        switch ($type) {
264
            case 'amount':
265
                $value2show = $this->setFormRowAmount(($value / pow(10, 4)), $defaultCellStyle);
266
                break;
267
            case 'value':
268
                $value2show = $this->setStringIntoTag($value, 'td', array_merge($defaultCellStyle, [
269
                    'colspan' => count($this->currencyDetails['CX'])
270
                ]));
271
                break;
272
            default:
273
                $value2show = $this->setStringIntoTag($value, 'td');
274
                break;
275
        }
276
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
277
    }
278
279
    private function setFormRowAmount($value, $defaultCellStyle)
280
    {
281
        $cellValue                 = [];
282
        $defaultCellStyle['style'] .= 'text-align:right;';
283
        foreach ($this->currencyDetails['CX'] as $key2 => $value2) {
284
            $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
285
            $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
286
            $finalValue  = $fmt->formatCurrency($value / $this->currencyDetails['CXV'][$key2], $key2);
287
            $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle);
288
        }
289
        return implode('', $cellValue);
290
    }
291
292
    private function setFormRowTwoLabels($text1, $text2, $value)
293
    {
294
        return str_replace(':</td>', ':</td><td class="labelS" style="text-align:right;">'
295
            . $text2 . '</td>', $this->setFormRow($text1, $value, 'amount'));
296
    }
297
}
298