PurchasesLineHTMLMod::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) 2025-2026 Joe Nilson <joenilson at gmail.com>
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\Base\DataBase\DataBaseWhere;
21
use FacturaScripts\Core\Contract\PurchasesLineModInterface;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Cont...rchasesLineModInterface 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\Model\Base\BusinessDocumentLine;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Mode...se\BusinessDocumentLine 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...
23
use FacturaScripts\Core\Model\Base\PurchaseDocument;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\PurchaseDocument 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\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...
25
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\ImpuestoAdicional;
26
use FacturaScripts\Core\Tools;
27
28
class PurchasesLineHTMLMod implements PurchasesLineModInterface
29
{
30
31
    public function apply(PurchaseDocument &$model, array &$lines, array $formData): void
32
    {
33
        // TODO: Implement apply() method.
34
    }
35
36
    public function applyToLine(array $formData, BusinessDocumentLine &$line, string $id): void
37
    {
38
        // TODO: Implement applyToLine() method.
39
        $line->rdtaxcodisc = $formData['rdtaxcodisc_' . $id] ?? $line->rdtaxcodisc;
40
        $line->rdtaxcodcdt = $formData['rdtaxcodcdt_' . $id] ?? $line->rdtaxcodcdt;
41
        $line->rdtaxcodlegaltip = $formData['rdtaxcodlegaltip_' . $id] ?? $line->rdtaxcodlegaltip;
42
        $line->rdtaxcodfirstplate = $formData['rdtaxcodfirstplate_' . $id] ?? $line->rdtaxcodfirstplate;
43
        $line->totalplustaxes = $formData['totalplustaxes_' . $id] ?? $line->totalplustaxes;
44
    }
45
46
    public function assets(): void
47
    {
48
        // TODO: Implement assets() method.
49
    }
50
51
    /**
52
     * @param Translator $i18n
53
     * @param string $attributes
54
     * @param string $label
55
     * @param int $idlinea
56
     * @param array $options
57
     * @return string
58
     */
59
    private function getStr(Translator $i18n, string $attributes, string $label, $idlinea, array $options): string
60
    {
61
        $idlinea  = (!$idlinea) ? 0 : $idlinea;
62
        return '<div class="col-6">'
63
            . '<div class="mb-2">'
64
            . $i18n->trans($label)
65
            . '<select onchange="return purchasesFormAction(\'recalculate-line\', \'' . $idlinea . '\');"'
66
            . $attributes . ' class="form-select" >' . implode('', $options) . '</select>'
67
            . '</div>'
68
            . '</div>';
69
    }
70
71
    public function getFastLine(PurchaseDocument $model, array $formData): ?BusinessDocumentLine
72
    {
73
        return null;
74
    }
75
76
    private function getLineAddedTaxes(BusinessDocumentLine $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...
77
    {
78
        $rdtaxisc = isset($line->rdtaxisc) ? (float)$line->rdtaxisc : 0.0;
79
        $rdtaxcdt = isset($line->rdtaxcdt) ? (float)$line->rdtaxcdt : 0.0;
80
        $rdtaxlegaltip = isset($line->rdtaxlegaltip) ? (float)$line->rdtaxlegaltip : 0.0;
81
        $rdtaxfirstplate = isset($line->rdtaxfirstplate) ? (float)$line->rdtaxfirstplate : 0.0;
82
        $pvpsindto = isset($line->pvpsindto) ? (float)$line->pvpsindto : 0.0;
83
84
        return $pvpsindto * ($rdtaxisc + $rdtaxcdt + $rdtaxlegaltip + $rdtaxfirstplate) / 100.0;
85
    }
86
87
    public function map(array $lines, PurchaseDocument $model): array
88
    {
89
        $map = [];
90
        $num = 0;
91
        foreach ($lines as $line) {
92
            $num++;
93
            $idlinea = $line->idlinea ?? 'n' . $num;
94
            $map['rdtaxcodisc_' . $idlinea] = $line->rdtaxcodisc;
95
            $map['rdtaxcodcdt_' . $idlinea] = $line->rdtaxcodcdt;
96
            $map['rdtaxcodlegaltip_' . $idlinea] = $line->rdtaxcodlegaltip;
97
            $map['rdtaxcodfirstplate_' . $idlinea] = $line->rdtaxcodfirstplate;
98
            // El total de la línea es el totalplustaxes (pvptotal + IVA)
99
            $map['totalplustaxes_' . $idlinea] = $line->totalplustaxes;
100
        }
101
        return $map;
102
    }
103
104
    public function newFields(): array
105
    {
106
        return ['totalplustaxes'];
107
    }
108
109
    public function newModalFields(): array
110
    {
111
        return ['rdtaxcodisc','rdtaxcodcdt','rdtaxcodlegaltip','rdtaxcodfirstplate'];
112
    }
113
114
    public function newTitles(): array
115
    {
116
        return ['totalplustaxes'];
117
    }
118
119
    public function renderField(string $idlinea, BusinessDocumentLine $line, PurchaseDocument $model, string $field): ?string
120
    {
121
        $i18n = new Translator();
0 ignored issues
show
Unused Code introduced by
The assignment to $i18n is dead and can be removed.
Loading history...
122
        switch ($field) {
123
            case "rdtaxcodisc":
124
                return $this->loadCodeISC($idlinea, $line, $model->editable);
125
            case "rdtaxcodcdt":
126
                return $this->loadCodeCDT($idlinea, $line, $model->editable);
127
            case "rdtaxcodlegaltip":
128
                return $this->loadCodeLegalTip($idlinea, $line, $model->editable);
129
            case "rdtaxcodfirstplate":
130
                return $this->loadCodeFirstPlate($idlinea, $line, $model->editable);
131
            case "totalplustaxes":
132
                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

132
                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...
133
            default:
134
                return null;
135
        }
136
    }
137
138
    private function renderTotalField(string $idlinea, BusinessDocumentLine $line, PurchaseDocument $model): string
139
    {
140
        $nf0 = Tools::settings('default', 'decimals', 2);
141
142
        return '<div class="col col-lg-1 order-8 columnTotalPlusTaxes" lang="es-DO">'
143
            . '<div class="d-lg-none mt-2 small">' . Tools::trans('desc-total-plustaxes') . '</div>'
144
            . '<input type="number" name="totalplustaxes_' . $idlinea . '"  value="' . number_format($line->totalplustaxes, $nf0, '.', '')
145
            . '" class="form-control form-control-sm text-lg-end border-0" readonly/></div>';
146
    }
147
148
    public function loadCodeISC(string $idlinea, BusinessDocumentLine $line, $editable): string
149
    {
150
        $i18n = new Translator();
151
        $impuestosAdicionales = new ImpuestoAdicional();
152
        $where = [new DataBaseWhere('tipo_impuesto_short', 'ISC')];
153
        $iscList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
154
        if (!$iscList) {
155
            return '';
156
        }
157
158
        $invoiceLineISC = ($line->rdtaxcodisc) ? $line->rdtaxcodisc : "";
159
        $options = ['<option value="">----------'.$invoiceLineISC.'</option>'];
160
        foreach ($iscList as $row) {
161
            $options[] = ($row->codigo === $invoiceLineISC) ?
162
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
163
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
164
        }
165
166
        $attributes = ($editable) ? 'name="rdtaxcodisc_' . $idlinea . '"' : 'name="rdtaxcodisc_' . $idlinea . '" disabled=""';
167
        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

167
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodisc", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
168
    }
169
    public function loadCodeCDT(string $idlinea, BusinessDocumentLine $line, $editable): string
170
    {
171
        $i18n = new Translator();
172
        $impuestosAdicionales = new ImpuestoAdicional();
173
        $where = [new DataBaseWhere('tipo_impuesto_short', 'CDT')];
174
        $cdtList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
175
        if (!$cdtList) {
176
            return '';
177
        }
178
179
        $invoiceLineCDT = ($line->rdtaxcodcdt) ? $line->rdtaxcodcdt : "";
180
181
        $options = ['<option value="">----------</option>'];
182
        foreach ($cdtList as $row) {
183
            $options[] = ($row->codigo === $invoiceLineCDT) ?
184
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
185
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
186
        }
187
188
        $attributes = ($editable) ? 'name="rdtaxcodcdt_' . $idlinea . '"' : 'name="rdtaxcodcdt_' . $idlinea . '" disabled=""';
189
        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

189
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodcdt", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
190
    }
191
    public function loadCodeLegalTip(string $idlinea, BusinessDocumentLine $line, $editable): string
192
    {
193
        $i18n = new Translator();
194
        $impuestosAdicionales = new ImpuestoAdicional();
195
        $where = [new DataBaseWhere('tipo_impuesto_short', 'Propina Legal')];
196
        $legalTipList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
197
        if (!$legalTipList) {
198
            return '';
199
        }
200
201
        $invoiceLineLegalTip = ($line->rdtaxcodlegaltip) ? $line->rdtaxcodlegaltip : "";
202
203
        $options = ['<option value="">----------</option>'];
204
        foreach ($legalTipList as $row) {
205
            $options[] = ($row->codigo === $invoiceLineLegalTip) ?
206
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
207
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
208
        }
209
210
        $attributes = ($editable) ? 'name="rdtaxcodlegaltip_' . $idlinea . '"' : 'name="rdtaxcodlegaltip_' . $idlinea . '" disabled=""';
211
        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

211
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodlegaltip", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
212
    }
213
    public function loadCodeFirstPlate(string $idlinea, BusinessDocumentLine $line, $editable): string
214
    {
215
        $i18n = new Translator();
216
        $impuestosAdicionales = new ImpuestoAdicional();
217
        $where = [new DataBaseWhere('tipo_impuesto_short', 'Primera Placa')];
218
        $firstPlateList = $impuestosAdicionales::all($where, ['codigo'=>'DESC']);
219
        if (!$firstPlateList) {
220
            return '';
221
        }
222
223
        $invoiceLineFirstPlate = ($line->rdtaxcodfirstplate) ? $line->rdtaxcodfirstplate : "";
224
225
        $options = ['<option value="">----------</option>'];
226
        foreach ($firstPlateList as $row) {
227
            $options[] = ($row->codigo === $invoiceLineFirstPlate) ?
228
                '<option value="' . $row->codigo . '" selected="">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>' :
229
                '<option value="' . $row->codigo . '">' . $row->tipo_impuesto_short . ' - ' . $row->descripcion . '</option>';
230
        }
231
232
        $attributes = ($editable) ? 'name="rdtaxcodfirstplate_' . $idlinea . '"' : 'name="rdtaxcodfirstplate_' . $idlinea . '" disabled=""';
233
        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

233
        return $this->getStr($i18n, $attributes, "label-desc-rdtaxcodfirstplate", /** @scrutinizer ignore-type */ $idlinea, $options);
Loading history...
234
    }
235
236
    public function renderTitle(PurchaseDocument $model, string $field): ?string
237
    {
238
        switch ($field) {
239
            case "totalplustaxes":
240
                return '<div class="col-lg-1 text-end order-8 columTotalPlusTaxes">' .Tools::trans('desc-total-plustaxes') . '</div>';
241
            default:
242
                return null;
243
        }
244
    }
245
}