SalesFooterHTMLMod::ncfTipoMovimiento()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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