SalesFooterMod::numeroncf()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 9
rs 10
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\Plugins\fsRepublicaDominicana\Model\NCFTipo;
27
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoAnulacion;
28
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoMovimiento;
29
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoPago;
30
31
class SalesFooterMod implements SalesModInterface
32
{
33
    public function apply(SalesDocument &$model, array $formData): void
34
    {
35
        
36
    }
37
38
    public function applyBefore(SalesDocument &$model, array $formData): void
39
    {
40
        if ($model->modelClassName() === 'FacturaCliente') {
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
        }
48
    }
49
50
    public function assets(): void
51
    {
52
    }
53
54
    public function newBtnFields(): array
55
    {
56
        return [];
57
    }
58
59
    public function newFields(): array
60
    {
61
        return ['numeroncf', 'tipocomprobante', 'ncffechavencimiento', 'ncftipopago', 'ncftipomovimiento', 'ncftipoanulacion'];
62
    }
63
64
    public function newModalFields(): array
65
    {
66
        return [];
67
    }
68
69
    public function renderField(SalesDocument $model, string $field): ?string
70
    {
71
        if ($model->modelClassName() === 'FacturaCliente') {
72
            $i18n = new Translator();
73
            switch ($field) {
74
                case "numeroncf":
75
                    return self::numeroncf($i18n, $model);
76
                case "tipocomprobante":
77
                    return self::tipoComprobante($i18n, $model);
78
                case "ncffechavencimiento":
79
                    return self::ncfFechaVencimiento($i18n, $model);
80
                case "ncftipopago":
81
                    return self::ncfTipoPago($i18n, $model);
82
                case "ncftipomovimiento":
83
                    return self::ncfTipoMovimiento($i18n, $model);
84
                case "ncftipoanulacion":
85
                    return self::ncfTipoAnulacion($i18n, $model);
86
                default:
87
                    return null;
88
            }
89
        }
90
        return null;
91
    }
92
93
    private static function infoCliente($codcliente)
94
    {
95
        $cliente = new Cliente();
96
        $actualCliente = $cliente::find($codcliente);
97
        if ('' !== $actualCliente) {
98
            return $actualCliente;
99
        }
100
        return null;
101
    }
102
103
    private static function tipoComprobante(Translator $i18n, SalesDocument $model): string
104
    {
105
        $tipoComprobante = NCFTipo::allVentas();
106
        if (count($tipoComprobante) === 0) {
107
            return '';
108
        }
109
110
        $cliente = self::infoCliente($model->codcliente);
111
        $cliente->tipocomprobante = ($cliente->tipocomprobante === null) ? "02" : $cliente->tipocomprobante;
112
113
        $invoiceTipoComprobante = ($model->tipocomprobante !== null) ? $model->tipocomprobante : $cliente->tipocomprobante;
114
        if (!$model->editable) {
115
            $invoiceTipoComprobante = $model->tipocomprobante;
116
        } elseif ($model->editable === true && ($cliente->tipocomprobante !== $model->tipocomprobante) && $model->tipocomprobante !== null) {
117
            $invoiceTipoComprobante = $model->tipocomprobante;
118
        } elseif ($model->editable === true && ($cliente->tipocomprobante === $model->tipocomprobante) && $model->tipocomprobante !== null) {
119
            $invoiceTipoComprobante = $cliente->tipocomprobante;
120
        }
121
122
        $options = ['<option value="">------</option>'];
123
        foreach ($tipoComprobante as $row) {
124
            $options[] = ($row->tipocomprobante === $invoiceTipoComprobante) ?
125
                '<option value="' . $row->tipocomprobante . '" selected="">' . $row->descripcion . '</option>' :
126
                '<option value="' . $row->tipocomprobante . '">' . $row->descripcion . '</option>';
127
        }
128
129
        $attributes = ($model->editable || $model->numeroncf === '') ?
130
            'id="tipocomprobante" name="tipocomprobante" required="" onChange="verificarCorrelativoNCF(this.value,\'Ventas\')"' :
131
            'disabled=""';
132
133
        return '<div class="col-sm-3">'
134
            . '<div class="mb-3">'
135
            . $i18n->trans('tipocomprobante')
136
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
137
            . '</div>'
138
            . '</div>';
139
    }
140
141
    private static function ncfTipoPago(Translator $i18n, SalesDocument $model): string
142
    {
143
        $NCFTipoPago = new NCFTipoPago();
144
        $tipoPago = $NCFTipoPago->findAllByTipopago('01');
145
        if (count($tipoPago) === 0) {
146
            return '';
147
        }
148
149
        $cliente = self::infoCliente($model->codcliente);
150
151
        if ($model->ncftipopago) {
152
            $invoiceTipoPago = $model->ncftipopago;
153
        } else {
154
            $invoiceTipoPago = ($cliente->ncftipopago !== '') ? $cliente->ncftipopago : "17";
155
        }
156
157
        $options = ['<option value="">------</option>'];
158
        foreach ($tipoPago as $row) {
159
            $options[] = ($row->codigo === $invoiceTipoPago) ?
160
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
161
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
162
        }
163
164
        $attributes = $model->editable ? 'name="ncftipopago" required=""' : 'disabled=""';
165
        return '<div class="col-sm-2">'
166
            . '<div class="mb-3">'
167
            . $i18n->trans('ncf-payment-type')
168
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
169
            . '</div>'
170
            . '</div>';
171
    }
172
173
    private static function ncfTipoMovimiento(Translator $i18n, SalesDocument $model): string
174
    {
175
        $NCFTipoMovimiento = new NCFTipoMovimiento();
176
        $tipoMovimiento = $NCFTipoMovimiento->findAllByTipomovimiento('VEN');
177
        if (count($tipoMovimiento) === 0) {
178
            return '';
179
        }
180
181
        $invoiceTipoMovimiento = ($model->ncftipomovimiento) ?: "1";
182
183
        $options = ['<option value="">------</option>'];
184
        foreach ($tipoMovimiento as $row) {
185
            $options[] = ($row->codigo === $invoiceTipoMovimiento) ?
186
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
187
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
188
        }
189
190
        $attributes = $model->editable ? 'name="ncftipomovimiento" required=""' : 'disabled=""';
191
        return '<div class="col-sm-3">'
192
            . '<div class="mb-3">'
193
            . $i18n->trans('ncf-movement-type')
194
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
195
            . '</div>'
196
            . '</div>';
197
    }
198
199
    private static function ncfTipoAnulacion(Translator $i18n, SalesDocument $model): string
200
    {
201
        $NCFTipoAnulacion = new NCFTipoAnulacion();
202
        $tipoAnulacion = $NCFTipoAnulacion->all();
203
        if (count($tipoAnulacion) === 0) {
204
            return '';
205
        }
206
207
        $invoiceTipoAnulacion = ($model->ncftipoanulacion) ?: "";
208
209
        $options = ['<option value="">------</option>'];
210
        foreach ($tipoAnulacion as $row) {
211
            $options[] = ($row->codigo === $invoiceTipoAnulacion) ?
212
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
213
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
214
        }
215
216
        $attributes = $model->editable ? 'name="ncftipoanulacion"' : 'disabled=""';
217
        return '<div class="col-sm-2">'
218
            . '<div class="mb-3">'
219
            . $i18n->trans('ncf-cancellation-type')
220
            . '<select ' . $attributes . ' class="form-select">' . implode('', $options) . '</select>'
221
            . '</div>'
222
            . '</div>';
223
    }
224
225
    private static function ncfFechaVencimiento(Translator $i18n, SalesDocument $model): string
226
    {
227
        $attributes = ($model->editable && $model->numero2 === '') ? 'name="ncffechavencimiento"' : 'name="ncffechavencimiento" disabled=""';
228
        $ncfFechaVencimiento = ($model->ncffechavencimiento)
229
            ? date('Y-m-d', strtotime($model->ncffechavencimiento))
230
            : '';
231
        return '<div class="col-sm-2">'
232
            . '<div class="mb-3">' . $i18n->trans('due-date')
233
            . '<input type="date" ' . $attributes . ' value="' . $ncfFechaVencimiento . '" class="form-control"/>'
234
            . '</div>'
235
            . '</div>';
236
    }
237
238
    private static function numeroncf(Translator $i18n, SalesDocument $model): string
239
    {
240
        $attributes = ($model->editable) ? 'name="numeroncf" maxlength="20"' : 'disabled=""';
241
        return empty($model->codcliente) ? '' : '<div class="col-sm">'
242
            . '<div class="mb-3">'
243
            . $i18n->trans('desc-numeroncf-sales')
244
            . '<input type="text" ' . $attributes . ' value="' . $model->numeroncf . '" class="form-control"/>'
245
            . '</div>'
246
            . '</div>';
247
    }
248
}
249