Passed
Push — master ( ff2dcb...dea4eb )
by Joe Nilson
01:34
created

PurchasesFooterMod::ecfFechaFirma()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 9.9332
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 PurchasesFooterMod 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
    }
51
52
    public function assets(): void
53
    {
54
    }
55
56
    public function newBtnFields(): array
57
    {
58
        return ['btnLoadXmlEcf','btnLoadXmlAck','btnLoadPdfEcf'];
59
    }
60
61
    public function newFields(): array
62
    {
63
        return ['numeroncf', 'tipocomprobante', 'ncffechavencimiento', 'ncftipopago', 'ncftipomovimiento', 'ncftipoanulacion', 'ecf_xml_firmado','ecf_fecha_firma','ecf_codigo_seguridad'];
64
    }
65
66
    public function newModalFields(): array
67
    {
68
        return [];
69
    }
70
71
    public function renderField(PurchaseDocument $model, string $field): ?string
72
    {
73
        $i18n = new Translator();
74
        if ($model->modelClassName() === 'FacturaProveedor') {
75
            switch ($field) {
76
                case "numeroncf":
77
                    return self::numeroNCF($i18n, $model);
78
                case "tipocomprobante":
79
                    return self::tipoComprobante($i18n, $model);
80
                case "ncffechavencimiento":
81
                    return self::ncfFechaVencimiento($i18n, $model);
82
                case "ncftipopago":
83
                    return self::ncfTipoPago($i18n, $model);
84
                case "ncftipomovimiento":
85
                    return self::ncfTipoMovimiento($i18n, $model);
86
                case "ncftipoanulacion":
87
                    return self::ncfTipoAnulacion($i18n, $model);
88
                case "ecf_fecha_firma":
89
                    return self::ecfFechaFirma($i18n, $model);
90
                case "ecf_codigo_seguridad":
91
                    return self::ecfCodigoSeguridad($i18n, $model);
92
                case "btnLoadXmlEcf":
93
                    return self::btnLoadXmlEcf($i18n, $model);
94
                default:
95
                    return null;
96
            }
97
        }
98
        return null;
99
    }
100
101
    private static function btnLoadXmlEcf(Translator $i18n, PurchaseDocument $model): string
102
    {
103
        $html = '<div class="row align-items-start">'
104
            . '<div class="col-sm-5 text-start">'
105
            . '<label for="xmlFile" class="form-label">'
106
            . '<i class="fa-solid fa-file-code me-1 text-primary"></i> Archivo XML de e-Factura'
107
            . '</label>'
108
            . '<div class="input-group">'
109
            . '    <input type="file" class="form-control" id="xmlFile" name="xmlFile" onchange="enableParseIfReady()" accept=".xml,text/xml,application/xml">'
110
//            . '    <label class="input-group-text" for="xmlFile"><i class="fa-solid fa-upload"></i></label>'
111
            . '    <div class="invalid-feedback">Seleccione un archivo XML válido.</div>'
112
//            . '</div>'
113
//            . '<div class="form-group text-start">'
114
            . '<button type="button" id="btnParseXml" onclick="btnParseClick()" class="btn btn-danger" disabled>'
115
            . '<i class="fas fa-fw fa-file-code"></i>'
116
            . $i18n->trans('btn-load-xml-ecf')
117
            . '</input>'
118
            . '</div>'
119
            . '</div></div>';
120
121
        return $html;
122
    }
123
124
    private static function infoProveedor($codproveedor)
125
    {
126
        $proveedor = new Proveedor();
127
        $actualProveedor = $proveedor::find($codproveedor);
128
        if ('' !== $actualProveedor) {
129
            return $actualProveedor;
130
        }
131
        return null;
132
    }
133
134
    private static function tipoComprobante(Translator $i18n, PurchaseDocument $model): string
135
    {
136
        $tipoComprobante = NCFTipo::allCompras();
137
        if (count($tipoComprobante) === 0) {
138
            return '';
139
        }
140
141
        $invoiceTipoComprobante = ($model->tipocomprobante) ? $model->tipocomprobante : "";
142
143
        $options = ['<option value="">------</option>'];
144
        foreach ($tipoComprobante as $row) {
145
            $options[] = ($row->tipocomprobante === $invoiceTipoComprobante) ?
146
                '<option value="' . $row->tipocomprobante . '" selected="">' . $row->descripcion . '</option>' :
147
                '<option value="' . $row->tipocomprobante . '">' . $row->descripcion . '</option>';
148
        }
149
150
        $attributes = ($model->editable || $model->numeroncf === '') ? 'name="tipocomprobante" required=""' : 'disabled=""';
151
        return '<div class="col-sm-3">'
152
            . '<div class="mb-3">'
153
            . $i18n->trans('tipocomprobante')
154
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
155
            . '</div>'
156
            . '</div>';
157
    }
158
159
    private static function ncfTipoPago(Translator $i18n, PurchaseDocument $model): string
160
    {
161
        $NCFTipoPago = new NCFTipoPago();
162
        $tipoPago = $NCFTipoPago->findAllByTipopago('02');
163
        if (count($tipoPago) === 0) {
164
            return '';
165
        }
166
167
        $proveedor = self::infoProveedor($model->codproveedor);
168
169
        if ($model->ncftipopago) {
170
            $invoiceTipoPago = $model->ncftipopago;
171
        } else {
172
            $invoiceTipoPago = ($proveedor->ncftipopago !== '') ? $proveedor->ncftipopago : "01";
173
        }
174
175
        $options = ['<option value="">------</option>'];
176
        foreach ($tipoPago as $row) {
177
            $options[] = ($row->codigo === $invoiceTipoPago) ?
178
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
179
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
180
        }
181
182
        $attributes = $model->editable ? 'name="ncftipopago" required=""' : 'disabled=""';
183
        return '<div class="col-sm-2">'
184
            . '<div class="mb-3">'
185
            . $i18n->trans('ncf-payment-type')
186
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
187
            . '</div>'
188
            . '</div>';
189
    }
190
191
    private static function ncfTipoMovimiento(Translator $i18n, PurchaseDocument $model): string
192
    {
193
        $NCFTipoMovimiento = new NCFTipoMovimiento();
194
        $tipoMovimiento = $NCFTipoMovimiento->findAllByTipomovimiento('COM');
195
        if (count($tipoMovimiento) === 0) {
196
            return '';
197
        }
198
199
        $invoiceTipoMovimiento = ($model->ncftipomovimiento) ?: "09";
200
201
        $options = ['<option value="">------</option>'];
202
        foreach ($tipoMovimiento as $row) {
203
            $options[] = ($row->codigo === $invoiceTipoMovimiento) ?
204
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
205
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
206
        }
207
208
        $attributes = $model->editable ? 'name="ncftipomovimiento" required=""' : 'disabled=""';
209
        return '<div class="col-sm-3">'
210
            . '<div class="mb-3">'
211
            . $i18n->trans('ncf-movement-type')
212
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
213
            . '</div>'
214
            . '</div>';
215
    }
216
217
    private static function ncfTipoAnulacion(Translator $i18n, PurchaseDocument $model): string
218
    {
219
        $NCFTipoAnulacion = new NCFTipoAnulacion();
220
        $tipoAnulacion = $NCFTipoAnulacion->all();
221
        if (count($tipoAnulacion) === 0) {
222
            return '';
223
        }
224
225
        $invoiceTipoAnulacion = ($model->ncftipoanulacion) ?: "";
226
227
        $options = ['<option value="">------</option>'];
228
        foreach ($tipoAnulacion as $row) {
229
            $options[] = ($row->codigo === $invoiceTipoAnulacion) ?
230
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
231
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
232
        }
233
234
        $attributes = $model->editable ? 'name="ncftipoanulacion"' : 'name="ncftipoanulacion" readonly=""';
235
        return '<div class="col-sm-2">'
236
            . '<div class="mb-3">'
237
            . $i18n->trans('ncf-cancellation-type')
238
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
239
            . '</div>'
240
            . '</div>';
241
    }
242
243
    private static function ncfFechaVencimiento(Translator $i18n, PurchaseDocument $model): string
244
    {
245
        $attributes = ($model->editable) ? 'name="ncffechavencimiento"' : 'disabled=""';
246
        $ncfFechaVencimiento = ($model->ncffechavencimiento)
247
            ? date('Y-m-d', strtotime($model->ncffechavencimiento))
248
            : '';
249
        return '<div class="col-sm-2">'
250
            . '<div class="mb-3">' . $i18n->trans('due-date')
251
            . '<input type="date" ' . $attributes . ' value="'
252
            . $ncfFechaVencimiento . '" class="form-control"/>'
253
            . '</div>'
254
            . '</div>';
255
    }
256
257
    private static function numeroNCF(Translator $i18n, PurchaseDocument $model): string
258
    {
259
        $attributes = ($model->editable) ? 'name="numeroncf" maxlength="20"' : 'disabled=""';
260
        $btnColor = (in_array($model->numeroncf, ['', null], true)) ? "btn-secondary" : "btn-success";
261
        return empty($model->codproveedor) ? '' : '<div class="col-sm">'
262
            . '<div class="mb-3">'
263
            . $i18n->trans('desc-numeroncf-purchases')
264
            . '<div class="input-group">'
265
            . '<input type="text" ' . $attributes . ' value="' . $model->numeroncf . '" class="form-control"/>'
266
            . '<button class="btn ' . $btnColor . ' btn-spin-action" id="btnVerifyNCF"'
267
            . 'onclick="purchasesNCFVerify()" '
268
            . 'title="' . $i18n->trans('verify-numproveedor')
269
            . '" type="button">'
270
            . '<i id="iconBtnVerify" class="fa-solid fa-search fa-fw"></i>'
271
            . '</button>'
272
            . '</div>'
273
            . '</div>'
274
            . '</div>';
275
    }
276
277
    private static function ecfFechaFirma(Translator $i18n, PurchaseDocument $model): string
278
    {
279
        $attributes = ($model->editable) ? 'name="ecf_fecha_firma" maxlength="32"' : 'disabled=""';
280
        $btnColor = (in_array($model->ecf_recibido, ['', null], true)) ? "btn-secondary" : "btn-success";
0 ignored issues
show
Unused Code introduced by
The assignment to $btnColor is dead and can be removed.
Loading history...
281
        return '<div class="col-sm">'
282
            . '<div class="mb-4">'
283
            . $i18n->trans('desc-ecf_fecha_firma')
284
            . '<div class="input-group">'
285
            . '<input type="text" ' . $attributes . ' value="' . $model->ecf_fecha_firma . '" class="form-control"/>'
286
            . '</div>'
287
            . '</div>'
288
            . '</div>';
289
    }
290
291
    private static function ecfCodigoSeguridad(Translator $i18n, PurchaseDocument $model): string
292
    {
293
        $attributes = ($model->editable) ? 'name="ecf_codigo_seguridad" maxlength="64"' : 'disabled=""';
294
        $btnColor = (in_array($model->ecf_recibido, ['', null], true)) ? "btn-secondary" : "btn-success";
0 ignored issues
show
Unused Code introduced by
The assignment to $btnColor is dead and can be removed.
Loading history...
295
        return '<div class="col-sm">'
296
            . '<div class="mb-4">'
297
            . $i18n->trans('desc-ecf_codigo_seguridad')
298
            . '<div class="input-group">'
299
            . '<input type="text" ' . $attributes . ' value="' . $model->ecf_codigo_seguridad . '" class="form-control"/>'
300
            . '</div>'
301
            . '</div>'
302
            . '</div>';
303
    }
304
}