SalesLineHTMLMod::newTitles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2023 Joe Nilson <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Mod;
19
20
use FacturaScripts\Core\Contract\SalesLineModInterface;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Contract\SalesLineModInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use FacturaScripts\Core\Translator;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use FacturaScripts\Core\Tools;
23
use FacturaScripts\Core\Model\Base\SalesDocument;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\SalesDocument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use FacturaScripts\Core\Model\Base\SalesDocumentLine;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\SalesDocumentLine was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
26
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\ImpuestoAdicional;
27
28
class SalesLineHTMLMod implements SalesLineModInterface
29
{
30
31
    public function apply(SalesDocument &$model, array &$lines, array $formData): void
32
    {
33
        // TODO: Implement apply() method.
34
    }
35
36
    /**
37
     * @param array $formData
38
     * @param SalesDocumentLine $line
39
     * @param string $id
40
     * @return void
41
     */
42
    public function applyToLine(array $formData, SalesDocumentLine &$line, string $id): void
43
    {
44
        $line->rdtaxcodisc = $formData['rdtaxcodisc_' . $id] ?? $line->rdtaxcodisc;
45
        $line->rdtaxcodcdt = $formData['rdtaxcodcdt_' . $id] ?? $line->rdtaxcodcdt;
46
        $line->rdtaxcodlegaltip = $formData['rdtaxcodlegaltip_' . $id] ?? $line->rdtaxcodlegaltip;
47
        $line->rdtaxcodfirstplate = $formData['rdtaxcodfirstplate_' . $id] ?? $line->rdtaxcodfirstplate;
48
        $line->totalplustaxes = $formData['totalplustaxes_' . $id] ?? $line->totalplustaxes;
49
    }
50
51
    public function assets(): void
52
    {
53
    }
54
55
    /**
56
     * @param Translator $i18n
57
     * @param string $attributes
58
     * @param string $label
59
     * @param int $idlinea
60
     * @param array $options
61
     * @return string
62
     */
63
    private function getStr(Translator $i18n, string $attributes, string $label, $idlinea, array $options): string
64
    {
65
        $idlinea  = (!$idlinea) ? 0 : $idlinea;
66
        return '<div class="col-6">'
67
            . '<div class="mb-2">'
68
            . $i18n->trans($label)
69
            . '<select onchange="return salesFormAction(\'recalculate-line\', \'' . $idlinea . '\');"'
70
            . $attributes . ' class="form-select" >' . implode('', $options) . '</select>'
71
            . '</div>'
72
            . '</div>';
73
    }
74
75
    private function getLineAddedTaxes(SalesDocumentLine $line): float
0 ignored issues
show
Unused Code introduced by
The method getLineAddedTaxes() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
76
    {
77
        $rdtaxisc = isset($line->rdtaxisc) ? (float)$line->rdtaxisc : 0.0;
78
        $rdtaxcdt = isset($line->rdtaxcdt) ? (float)$line->rdtaxcdt : 0.0;
79
        $rdtaxlegaltip = isset($line->rdtaxlegaltip) ? (float)$line->rdtaxlegaltip : 0.0;
80
        $rdtaxfirstplate = isset($line->rdtaxfirstplate) ? (float)$line->rdtaxfirstplate : 0.0;
81
        $pvpsindto = isset($line->pvpsindto) ? (float)$line->pvpsindto : 0.0;
82
83
        return $pvpsindto * ($rdtaxisc + $rdtaxcdt + $rdtaxlegaltip + $rdtaxfirstplate) / 100.0;
84
    }
85
86
    public function map(array $lines, SalesDocument $model): array
87
    {
88
        $map = [];
89
        $num = 0;
90
        foreach ($lines as $line) {
91
            $num++;
92
            $idlinea = $line->idlinea ?? 'n' . $num;
93
            $map['rdtaxcodisc_' . $idlinea] = $line->rdtaxcodisc;
94
            $map['rdtaxcodcdt_' . $idlinea] = $line->rdtaxcodcdt;
95
            $map['rdtaxcodlegaltip_' . $idlinea] = $line->rdtaxcodlegaltip;
96
            $map['rdtaxcodfirstplate_' . $idlinea] = $line->rdtaxcodfirstplate;
97
            // El total de la línea es el totalplustaxes (pvptotal + IVA)
98
            $map['totalplustaxes_' . $idlinea] = $line->totalplustaxes;
99
        }
100
        return $map;
101
    }
102
103
    public function newModalFields(): array
104
    {
105
        return ['rdtaxcodisc','rdtaxcodcdt','rdtaxcodlegaltip','rdtaxcodfirstplate'];
106
    }
107
108
    public function newFields(): array
109
    {
110
        return ['totalplustaxes'];
111
    }
112
113
    public function newTitles(): array
114
    {
115
        return ['totalplustaxes'];
116
    }
117
118
    public function renderField(string $idlinea, SalesDocumentLine $line, SalesDocument $model, string $field): ?string
119
    {
120
        switch ($field) {
121
            case "rdtaxcodisc":
122
                return $this->loadCodeISC($idlinea, $line, $model->editable);
123
            case "rdtaxcodcdt":
124
                return $this->loadCodeCDT($idlinea, $line, $model->editable);
125
            case "rdtaxcodlegaltip":
126
                return $this->loadCodeLegalTip($idlinea, $line, $model->editable);
127
            case "rdtaxcodfirstplate":
128
                return $this->loadCodeFirstPlate($idlinea, $line, $model->editable);
129
            case "totalplustaxes":
130
                return $this->renderTotalField($idlinea, $line, $model, $model->editable);
0 ignored issues
show
Unused Code introduced by
The call to FacturaScripts\Plugins\f...Mod::renderTotalField() has too many arguments starting with $model->editable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
                return $this->/** @scrutinizer ignore-call */ renderTotalField($idlinea, $line, $model, $model->editable);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
131
            default:
132
                return null;
133
        }
134
    }
135
136
    private function renderTotalField(string $idlinea, SalesDocumentLine $line, SalesDocument $model): string
137
    {
138
        $nf0 = Tools::settings('default', 'decimals', 2);
139
140
        return '<div class="col col-lg-1 order-8 columnTotalPlusTaxes" lang="es-DO">'
141
            . '<div class="d-lg-none mt-2 small">' . Tools::trans('desc-total-plustaxes') . '</div>'
142
            . '<input type="number" name="totalplustaxes_' . $idlinea . '"  value="' . number_format($line->totalplustaxes, $nf0,'.', '')
143
            . '" class="form-control form-control-sm text-lg-end border-0" readonly/>'
144
            . '</div>';
145
    }
146
147
    public function renderTitle(SalesDocument $model, string $field): ?string
148
    {
149
        switch ($field) {
150
            case "totalplustaxes":
151
                return '<div class="col-lg-1 text-end order-8 columTotalPlusTaxes">' .Tools::trans('desc-total-plustaxes') . '</div>';
152
            default:
153
                return null;
154
        }
155
    }
156
157
    public function getFastLine(SalesDocument $model, array $formData): ?SalesDocumentLine
158
    {
159
        return null;
160
    }
161
162
    public function loadCodeISC(string $idlinea, SalesDocumentLine $line, $editable): string
163
    {
164
        $i18n = new Translator();
165
        $impuestosAdicionales = new ImpuestoAdicional();
166
        $where = [new DataBaseWhere('tipo_impuesto_short', 'ISC')];
167
        $iscList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
168
        if (!$iscList) {
169
            return '';
170
        }
171
172
        $invoiceLineISC = ($line->rdtaxcodisc) ? $line->rdtaxcodisc : "";
173
        $options = ['<option value="">----------'.$invoiceLineISC.'</option>'];
174
        foreach ($iscList as $row) {
175
            $options[] = ($row->codigo === $invoiceLineISC) ?
176
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
177
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
178
        }
179
180
        $attributes = ($editable) ? 'name="rdtaxcodisc_' . $idlinea . '"' : 'name="rdtaxcodisc_' . $idlinea . '" disabled=""';
181
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodisc", $idlinea, $options);
0 ignored issues
show
Bug introduced by
$idlinea of type string is incompatible with the type integer expected by parameter $idlinea of FacturaScripts\Plugins\f...esLineHTMLMod::getStr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodisc", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
182
    }
183
    public function loadCodeCDT(string $idlinea, SalesDocumentLine $line, $editable): string
184
    {
185
        $i18n = new Translator();
186
        $impuestosAdicionales = new ImpuestoAdicional();
187
        $where = [new DataBaseWhere('tipo_impuesto_short', 'CDT')];
188
        $cdtList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
189
        if (!$cdtList) {
190
            return '';
191
        }
192
193
        $invoiceLineCDT = ($line->rdtaxcodcdt) ? $line->rdtaxcodcdt : "";
194
195
        $options = ['<option value="">----------</option>'];
196
        foreach ($cdtList as $row) {
197
            $options[] = ($row->codigo === $invoiceLineCDT) ?
198
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
199
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
200
        }
201
202
        $attributes = ($editable) ? 'name="rdtaxcodcdt_' . $idlinea . '"' : 'disabled=""';
203
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodcdt", $idlinea, $options);
0 ignored issues
show
Bug introduced by
$idlinea of type string is incompatible with the type integer expected by parameter $idlinea of FacturaScripts\Plugins\f...esLineHTMLMod::getStr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

203
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodcdt", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
204
    }
205
    public function loadCodeLegalTip(string $idlinea, SalesDocumentLine $line, $editable): string
206
    {
207
        $i18n = new Translator();
208
        $impuestosAdicionales = new ImpuestoAdicional();
209
        $where = [new DataBaseWhere('tipo_impuesto_short', 'Propina Legal')];
210
        $legalTipList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
211
        if (!$legalTipList) {
212
            return '';
213
        }
214
215
        $invoiceLineLegalTip = ($line->rdtaxcodlegaltip) ? $line->rdtaxcodlegaltip : "";
216
217
        $options = ['<option value="">----------</option>'];
218
        foreach ($legalTipList as $row) {
219
            $options[] = ($row->codigo === $invoiceLineLegalTip) ?
220
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
221
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
222
        }
223
224
        $attributes = ($editable) ? 'name="rdtaxcodlegaltip_' . $idlinea . '"' : 'disabled=""';
225
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodlegaltip", $idlinea, $options);
0 ignored issues
show
Bug introduced by
$idlinea of type string is incompatible with the type integer expected by parameter $idlinea of FacturaScripts\Plugins\f...esLineHTMLMod::getStr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodlegaltip", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
226
    }
227
    public function loadCodeFirstPlate(string $idlinea, SalesDocumentLine $line, $editable): string
228
    {
229
        $i18n = new Translator();
230
        $impuestosAdicionales = new ImpuestoAdicional();
231
        $where = [new DataBaseWhere('tipo_impuesto_short', 'Primera Placa')];
232
        $firstPlateList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
233
        if (!$firstPlateList) {
234
            return '';
235
        }
236
237
        $invoiceLineFirstPlate = ($line->rdtaxcodfirstplate) ? $line->rdtaxcodfirstplate : "";
238
239
        $options = ['<option value="">----------</option>'];
240
        foreach ($firstPlateList as $row) {
241
            $options[] = ($row->codigo === $invoiceLineFirstPlate) ?
242
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
243
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
244
        }
245
246
        $attributes = ($editable) ? 'name="rdtaxcodfirstplate_' . $idlinea . '"' : 'disabled=""';
247
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodfirstplate", $idlinea, $options);
0 ignored issues
show
Bug introduced by
$idlinea of type string is incompatible with the type integer expected by parameter $idlinea of FacturaScripts\Plugins\f...esLineHTMLMod::getStr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

247
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodfirstplate", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
248
    }
249
}