PurchasesFooterHTMLMod::newBtnFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2022 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\PurchasesModInterface;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Contract\PurchasesModInterface 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\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...
23
use FacturaScripts\Core\Model\User;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\User 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\Tools;
25
26
use FacturaScripts\Dinamic\Model\Proveedor;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Proveedor 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...
27
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipo;
28
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoAnulacion;
29
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoMovimiento;
30
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoPago;
31
32
class PurchasesFooterHTMLMod implements PurchasesModInterface
33
{
34
    public function apply(PurchaseDocument &$model, array $formData): void
35
    {
36
    }
37
38
    public function applyBefore(PurchaseDocument &$model, array $formData): void
39
    {
40
        if ($model->modelClassName() === 'FacturaProveedor') {
41
            $model->numeroncf = isset($formData['numeroncf']) ? (string)$formData['numeroncf'] : $model->numeroncf;
42
            $model->tipocomprobante = isset($formData['tipocomprobante']) ? (string)$formData['tipocomprobante'] : $model->tipocomprobante;
43
            $model->ncffechavencimiento = isset($formData['ncffechavencimiento']) ? (string)$formData['ncffechavencimiento'] : $model->ncffechavencimiento;
44
            $model->ncftipopago = isset($formData['ncftipopago']) ? (string)$formData['ncftipopago'] : $model->ncftipopago;
45
            $model->ncftipomovimiento = isset($formData['ncftipomovimiento']) ? (string)$formData['ncftipomovimiento'] : $model->ncftipomovimiento;
46
            $model->ncftipoanulacion = isset($formData['ncftipoanulacion']) ? (string)$formData['ncftipoanulacion'] : $model->ncftipoanulacion;
47
            $model->ecf_fecha_firma = isset($formData['ecf_fecha_firma']) ? (string)$formData['ecf_fecha_firma'] : $model->ecf_fecha_firma;
48
            $model->ecf_codigo_seguridad = isset($formData['ecf_codigo_seguridad']) ? (string)$formData['ecf_codigo_seguridad'] : $model->ecf_codigo_seguridad;
49
        }
50
        $model->totalexento = isset($formData['totalexento']) ? (float)$formData['totalexento'] : $model->totalexento;
51
        $model->totaladdedtaxes = isset($formData['totaladdedtaxes']) ? (float)$formData['totaladdedtaxes'] : $model->totaladdedtaxes;
52
        $model->totalplustaxes = isset($formData['totalplustaxes']) ? (float)$formData['totalplustaxes'] : $model->totalplustaxes;
53
    }
54
55
    public function assets(): void
56
    {
57
    }
58
59
    public function newBtnFields(): array
60
    {
61
        return ['btnLoadXmlEcf','btnLoadXmlAck','btnLoadPdfEcf'];
62
    }
63
64
    public function newFields(): array
65
    {
66
        return ['numeroncf', 'tipocomprobante', 'ncffechavencimiento', 'ncftipopago', 'ncftipomovimiento',
67
            'ncftipoanulacion'];
68
    }
69
70
    public function newModalFields(): array
71
    {
72
        return [];
73
    }
74
75
    public function renderField(PurchaseDocument $model, string $field): ?string
76
    {
77
        $i18n = new Translator();
78
        switch ($field) {
79
            case "total":
80
                return self::renderTotal($model)
81
                    . '</div><div class="row g-2">'
82
                    . self::ecfFechaFirma($i18n, $model)
83
                    . self::ecfCodigoSeguridad($i18n, $model)
84
                    . self::inputTotalExento($i18n, $model)
85
                    . self::inputTotalAddedTaxes($i18n, $model)
86
                    . self::inputTotalPlusTaxes($i18n, $model);
87
        }
88
89
        if ($model->modelClassName() === 'FacturaProveedor') {
90
            switch ($field) {
91
                case "numeroncf":
92
                    return self::numeroNCF($i18n, $model);
93
                case "tipocomprobante":
94
                    return self::tipoComprobante($i18n, $model);
95
                case "ncffechavencimiento":
96
                    return self::ncfFechaVencimiento($i18n, $model). '</div><div class="row g-2">';
97
                case "ncftipopago":
98
                    return self::ncfTipoPago($i18n, $model);
99
                case "ncftipomovimiento":
100
                    return self::ncfTipoMovimiento($i18n, $model);
101
                case "ncftipoanulacion":
102
                    return self::ncfTipoAnulacion($i18n, $model) . '</div><div class="row g-2">';
103
                case "btnLoadXmlEcf":
104
                    return self::btnLoadXmlEcf($i18n, $model);
105
            }
106
        }
107
        return null;
108
    }
109
110
    private static function btnLoadXmlEcf(Translator $i18n, PurchaseDocument $model): string
111
    {
112
        return '<div class="row align-items-start">'
113
            . '<div class="col-sm-5 text-start">'
114
            . '<label for="xmlFile" class="form-label">'
115
            . '<i class="fa-solid fa-file-code me-1 text-primary"></i> Archivo XML de e-Factura'
116
            . '</label>'
117
            . '<div class="input-group">'
118
            . '    <input type="file" class="form-control" id="xmlFile" name="xmlFile" onchange="enableParseIfReady()" '
119
            . ' accept=".xml,text/xml,application/xml">'
120
            . '    <div class="invalid-feedback">Seleccione un archivo XML válido.</div>'
121
            . '<button type="button" id="btnParseXml" onclick="btnParseClick(\'xmlFile\')" class="btn btn-danger" disabled>'
122
            . '<i class="fas fa-fw fa-file-code"></i>'
123
            . $i18n->trans('btn-load-xml-ecf')
124
            . '</button>'
125
            . '</div>'
126
            . '</div></div>';
127
    }
128
129
    private static function infoProveedor($codproveedor)
130
    {
131
        $proveedor = new Proveedor();
132
        $actualProveedor = $proveedor::find($codproveedor);
133
        if ('' !== $actualProveedor) {
134
            return $actualProveedor;
135
        }
136
        return null;
137
    }
138
139
    private static function tipoComprobante(Translator $i18n, PurchaseDocument $model): string
140
    {
141
        $tipoComprobante = NCFTipo::allCompras();
142
        if (count($tipoComprobante) === 0) {
143
            return '';
144
        }
145
146
        $invoiceTipoComprobante = ($model->tipocomprobante) ? $model->tipocomprobante : "";
147
148
        $options = ['<option value="">------</option>'];
149
        foreach ($tipoComprobante as $row) {
150
            $options[] = ($row->tipocomprobante === $invoiceTipoComprobante) ?
151
                '<option value="' . $row->tipocomprobante . '" selected="">' . $row->descripcion . '</option>' :
152
                '<option value="' . $row->tipocomprobante . '">' . $row->descripcion . '</option>';
153
        }
154
155
        $attributes = ($model->editable || $model->numeroncf === '') ? 'name="tipocomprobante" required=""' : 'disabled=""';
156
        return '<div class="col-sm-4">'
157
            . '<div class="mb-3">'
158
            . $i18n->trans('tipocomprobante')
159
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
160
            . '</div>'
161
            . '</div>';
162
    }
163
164
    private static function ncfTipoPago(Translator $i18n, PurchaseDocument $model): string
165
    {
166
        $NCFTipoPago = new NCFTipoPago();
167
        $tipoPago = $NCFTipoPago->findAllByTipopago('02');
168
        if (count($tipoPago) === 0) {
169
            return '';
170
        }
171
172
        $proveedor = self::infoProveedor($model->codproveedor);
173
174
        if ($model->ncftipopago) {
175
            $invoiceTipoPago = $model->ncftipopago;
176
        } else {
177
            $invoiceTipoPago = ($proveedor->ncftipopago !== '') ? $proveedor->ncftipopago : "01";
178
        }
179
180
        $options = ['<option value="">------</option>'];
181
        foreach ($tipoPago as $row) {
182
            $options[] = ($row->codigo === $invoiceTipoPago) ?
183
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
184
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
185
        }
186
187
        $attributes = $model->editable ? 'name="ncftipopago" required=""' : 'disabled=""';
188
        return '<div class="col-sm-4">'
189
            . '<div class="mb-3">'
190
            . $i18n->trans('ncf-payment-type')
191
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
192
            . '</div>'
193
            . '</div>';
194
    }
195
196
    private static function ncfTipoMovimiento(Translator $i18n, PurchaseDocument $model): string
197
    {
198
        $NCFTipoMovimiento = new NCFTipoMovimiento();
199
        $tipoMovimiento = $NCFTipoMovimiento->findAllByTipomovimiento('COM');
200
        if (count($tipoMovimiento) === 0) {
201
            return '';
202
        }
203
204
        $invoiceTipoMovimiento = ($model->ncftipomovimiento) ?: "09";
205
206
        $options = ['<option value="">------</option>'];
207
        foreach ($tipoMovimiento as $row) {
208
            $options[] = ($row->codigo === $invoiceTipoMovimiento) ?
209
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
210
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
211
        }
212
213
        $attributes = $model->editable ? 'name="ncftipomovimiento" required=""' : 'disabled=""';
214
        return '<div class="col-sm-4">'
215
            . '<div class="mb-3">'
216
            . $i18n->trans('ncf-movement-type')
217
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
218
            . '</div>'
219
            . '</div>';
220
    }
221
222
    private static function ncfTipoAnulacion(Translator $i18n, PurchaseDocument $model): string
223
    {
224
        $NCFTipoAnulacion = new NCFTipoAnulacion();
225
        $tipoAnulacion = $NCFTipoAnulacion->all();
226
        if (count($tipoAnulacion) === 0) {
227
            return '';
228
        }
229
230
        $invoiceTipoAnulacion = ($model->ncftipoanulacion) ?: "";
231
232
        $options = ['<option value="">------</option>'];
233
        foreach ($tipoAnulacion as $row) {
234
            $options[] = ($row->codigo === $invoiceTipoAnulacion) ?
235
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
236
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
237
        }
238
239
        $attributes = $model->editable ? 'name="ncftipoanulacion"' : 'name="ncftipoanulacion" readonly=""';
240
        return '<div class="col-sm-4">'
241
            . '<div class="mb-3">'
242
            . $i18n->trans('ncf-cancellation-type')
243
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
244
            . '</div>'
245
            . '</div>';
246
    }
247
248
    private static function ncfFechaVencimiento(Translator $i18n, PurchaseDocument $model): string
249
    {
250
        $attributes = ($model->editable) ? 'name="ncffechavencimiento"' : 'disabled=""';
251
        $ncfFechaVencimiento = ($model->ncffechavencimiento)
252
            ? date('Y-m-d', strtotime($model->ncffechavencimiento))
253
            : '';
254
        return '<div class="col-sm-2">'
255
            . '<div class="mb-3">' . $i18n->trans('due-date')
256
            . '<input type="date" ' . $attributes . ' value="'
257
            . $ncfFechaVencimiento . '" class="form-control"/>'
258
            . '</div>'
259
            . '</div>';
260
    }
261
262
    private static function numeroNCF(Translator $i18n, PurchaseDocument $model): string
263
    {
264
        $attributes = ($model->editable) ? 'name="numeroncf" maxlength="20"' : 'disabled=""';
265
        $btnColor = (in_array($model->numeroncf, ['', null], true)) ? "btn-secondary" : "btn-success";
266
        return empty($model->codproveedor) ? '' : '<div class="col-sm-3">'
267
            . '<div class="mb-3">'
268
            . $i18n->trans('desc-numeroncf-purchases')
269
            . '<div class="input-group">'
270
            . '<input type="text" ' . $attributes . ' value="' . $model->numeroncf . '" class="form-control"/>'
271
            . '<button class="btn ' . $btnColor . ' btn-spin-action" id="btnVerifyNCF"'
272
            . 'onclick="purchasesNCFVerify()" '
273
            . 'title="' . $i18n->trans('verify-numproveedor')
274
            . '" type="button">'
275
            . '<i id="iconBtnVerify" class="fa-solid fa-search fa-fw"></i>'
276
            . '</button>'
277
            . '</div>'
278
            . '</div>'
279
            . '</div>';
280
    }
281
282
    private static function ecfFechaFirma(Translator $i18n, PurchaseDocument $model): string
283
    {
284
        $attributes = ($model->editable) ? 'name="ecf_fecha_firma" maxlength="32"' : 'disabled=""';
285
        $ecfFechaFirma = ($model->ecf_fecha_firma)
286
            ? date('Y-m-d\TH:i', strtotime($model->ecf_fecha_firma))
287
            : '';
288
        return '<div class="col-sm-6 col-md-4 col-lg">'
289
            . '<div class="mb-2">'
290
            . $i18n->trans('desc-ecf_fecha_firma')
291
            . '<div class="input-group">'
292
            . '<input type="datetime-local" ' . $attributes . ' value="' . $ecfFechaFirma . '" step="1" class="form-control"/>'
293
            . '</div>'
294
            . '</div>'
295
            . '</div>';
296
    }
297
298
    private static function ecfCodigoSeguridad(Translator $i18n, PurchaseDocument $model): string
299
    {
300
        $attributes = ($model->editable) ? 'name="ecf_codigo_seguridad" maxlength="64"' : 'disabled=""';
301
        return '<div class="col-sm-6 col-md-4 col-lg">'
302
            . '<div class="mb-2">'
303
            . $i18n->trans('desc-ecf_codigo_seguridad')
304
            . '<div class="input-group">'
305
            . '<input type="text" ' . $attributes . ' value="' . $model->ecf_codigo_seguridad . '" class="form-control"/>'
306
            . '</div>'
307
            . '</div>'
308
            . '</div>';
309
    }
310
311
    private static function renderTotal(PurchaseDocument $model): string
312
    {
313
        $nf0 = Tools::settings('default', 'decimals', 2);
314
        return '<div class="col-sm-6 col-md-4 col-lg-2">'
315
            . '<div class="mb-2">' . Tools::trans('total')
316
            . '<input type="text" name="total" value="' . Tools::number($model->total, $nf0) . '" class="form-control" disabled/>'
317
            . '</div>'
318
            . '</div>';
319
    }
320
321
    private static function inputTotalExento(Translator $i18n, PurchaseDocument $model): string
322
    {
323
        $attributes = 'name="totalexento" disabled=""';
324
        return '<div class="col-sm-6 col-md-4 col-lg">'
325
            . '<div class="mb-2">'
326
            . $i18n->trans('desc-total-exento')
327
            . '<input type="text" '. $attributes. ' value="'.$model->totalexento.'" class="form-control">'
328
            . '</div>'
329
            . '</div>';
330
    }
331
332
    private static function inputTotalAddedTaxes(Translator $i18n, PurchaseDocument $model): string
333
    {
334
        $attributes = 'name="totaladdedtaxes" disabled=""';
335
        return '<div class="col-sm-6 col-md-4 col-lg">'
336
            . '<div class="mb-2">'
337
            . $i18n->trans('desc-total-added-taxes')
338
            . '<input type="text" '. $attributes. ' value="'.$model->totaladdedtaxes.'" class="form-control">'
339
            . '</div>'
340
            . '</div>';
341
    }
342
343
    private static function inputTotalPlusTaxes(Translator $i18n, PurchaseDocument $model): string
344
    {
345
        $attributes = 'name="totalplustaxes" disabled=""';
346
        return '<div class="col-sm-6 col-md-4 col-lg">'
347
            . '<div class="mb-2">'
348
            . $i18n->trans('desc-total-plustaxes')
349
            . '<input type="text" '. $attributes. ' value="'.$model->totalplustaxes.'" class="form-control">'
350
            . '</div>'
351
            . '</div>';
352
    }
353
}