Passed
Push — master ( d29869...e61a0d )
by Joe Nilson
03:35
created

PurchasesFooterMod   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 126
dl 0
loc 204
rs 6.96
c 1
b 0
f 0
wmc 53

12 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 2 1
A ncfFechaVencimiento() 0 8 2
A numeroNCF() 0 20 4
B ncfTipoPago() 0 30 7
A ncfTipoAnulacion() 0 24 6
A infoProveedor() 0 8 2
B renderField() 0 22 8
B tipoComprobante() 0 23 7
A newFields() 0 4 1
A assets() 0 2 1
A ncfTipoMovimiento() 0 24 6
B applyBefore() 0 10 8

How to fix   Complexity   

Complex Class

Complex classes like PurchasesFooterMod often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PurchasesFooterMod, and based on these observations, apply Extract Interface, too.

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\Base\Contract\PurchasesModInterface;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base...t\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\Base\Translator;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\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\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...
25
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipo;
26
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoAnulacion;
27
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoMovimiento;
28
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoPago;
29
30
class PurchasesFooterMod implements PurchasesModInterface
31
{
32
    public function apply(PurchaseDocument &$model, array $formData, User $user)
33
    {
34
        // TODO: Implement apply() method.
35
    }
36
37
    public function applyBefore(PurchaseDocument &$model, array $formData, User $user)
38
    {
39
        // TODO: Implement applyBefore() method.
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
        }
48
    }
49
50
    public function assets(): void
51
    {
52
        // TODO: Implement assets() method.
53
    }
54
55
    public function newFields(): array
56
    {
57
        // TODO: Implement newFields() method.
58
        return ['numeroncf','tipocomprobante', 'ncffechavencimiento', 'ncftipopago', 'ncftipomovimiento', 'ncftipoanulacion'];
59
    }
60
61
    public function renderField(Translator $i18n, PurchaseDocument $model, string $field): ?string
62
    {
63
        // TODO: Implement renderField() method.
64
        if ($model->modelClassName() === 'FacturaProveedor') {
65
            switch ($field) {
66
                case "numeroncf":
67
                    return $this->numeroNCF($i18n, $model);
68
                case "tipocomprobante":
69
                    return $this->tipoComprobante($i18n, $model);
70
                case "ncffechavencimiento":
71
                    return $this->ncfFechaVencimiento($i18n, $model);
72
                case "ncftipopago":
73
                    return $this->ncfTipoPago($i18n, $model);
74
                case "ncftipomovimiento":
75
                    return $this->ncfTipoMovimiento($i18n, $model);
76
                case "ncftipoanulacion":
77
                    return $this->ncfTipoAnulacion($i18n, $model);
78
                default:
79
                    return null;
80
            }
81
        }
82
        return null;
83
    }
84
85
    private static function infoProveedor($codproveedor)
86
    {
87
        $proveedor = new Proveedor();
88
        $actualProveedor = $proveedor->get($codproveedor);
89
        if ('' !== $actualProveedor) {
90
            return $actualProveedor;
91
        }
92
        return null;
93
    }
94
95
    private static function tipoComprobante(Translator $i18n, PurchaseDocument $model): string
96
    {
97
        $tipoComprobante = NCFTipo::allCompras();
98
        if (count($tipoComprobante) === 0) {
99
            return '';
100
        }
101
102
        $invoiceTipoComprobante = ($model->tipocomprobante) ? $model->tipocomprobante : "";
103
104
        $options = ['<option value="">------</option>'];
105
        foreach ($tipoComprobante as $row) {
106
            $options[] = ($row->tipocomprobante === $invoiceTipoComprobante) ?
107
                '<option value="' . $row->tipocomprobante . '" selected="">' . $row->descripcion . '</option>' :
108
                '<option value="' . $row->tipocomprobante . '">' . $row->descripcion . '</option>';
109
        }
110
111
        $attributes = ($model->editable || $model->numeroncf === '') ? 'name="tipocomprobante" required=""' : 'disabled=""';
112
        return '<div class="col-sm-3">'
113
            . '<div class="form-group">'
114
            .  $i18n->trans('tipocomprobante')
115
            . '<select ' . $attributes . ' class="form-control">' . implode('', $options) . '</select>'
116
            . '</div>'
117
            . '</div>';
118
    }
119
120
    private static function ncfTipoPago(Translator $i18n, PurchaseDocument $model): string
121
    {
122
        $NCFTipoPago = new NCFTipoPago();
123
        $tipoPago = $NCFTipoPago->findAllByTipopago('02');
124
        if (count($tipoPago) === 0) {
125
            return '';
126
        }
127
128
        $proveedor = self::infoProveedor($model->codproveedor);
129
130
        if ($model->ncftipopago) {
131
            $invoiceTipoPago = $model->ncftipopago;
132
        } else {
133
            $invoiceTipoPago = ($proveedor->ncftipopago !== '') ? $proveedor->ncftipopago : "01";
134
        }
135
136
        $options = ['<option value="">------</option>'];
137
        foreach ($tipoPago as $row) {
138
            $options[] = ($row->codigo === $invoiceTipoPago) ?
139
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
140
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
141
        }
142
143
        $attributes = $model->editable ? 'name="ncftipopago" required=""' : 'disabled=""';
144
        return '<div class="col-sm-2">'
145
            . '<div class="form-group">'
146
            .  $i18n->trans('ncf-payment-type')
147
            . '<select ' . $attributes . ' class="form-control">' . implode('', $options) . '</select>'
148
            . '</div>'
149
            . '</div>';
150
    }
151
152
    private static function ncfTipoMovimiento(Translator $i18n, PurchaseDocument $model): string
153
    {
154
        $NCFTipoMovimiento = new NCFTipoMovimiento();
155
        $tipoMovimiento = $NCFTipoMovimiento->findAllByTipomovimiento('COM');
156
        if (count($tipoMovimiento) === 0) {
157
            return '';
158
        }
159
160
        $invoiceTipoMovimiento = ($model->ncftipomovimiento) ?: "09";
161
162
        $options = ['<option value="">------</option>'];
163
        foreach ($tipoMovimiento as $row) {
164
            $options[] = ($row->codigo === $invoiceTipoMovimiento) ?
165
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
166
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
167
        }
168
169
        $attributes = $model->editable ? 'name="ncftipomovimiento" required=""' : 'disabled=""';
170
        return '<div class="col-sm-3">'
171
            . '<div class="form-group">'
172
            .  $i18n->trans('ncf-movement-type')
173
            . '<select ' . $attributes . ' class="form-control">' . implode('', $options) . '</select>'
174
            . '</div>'
175
            . '</div>';
176
    }
177
178
    private static function ncfTipoAnulacion(Translator $i18n, PurchaseDocument $model): string
179
    {
180
        $NCFTipoAnulacion = new NCFTipoAnulacion();
181
        $tipoAnulacion = $NCFTipoAnulacion->all();
182
        if (count($tipoAnulacion) === 0) {
183
            return '';
184
        }
185
186
        $invoiceTipoAnulacion = ($model->ncftipoanulacion) ?: "";
187
188
        $options = ['<option value="">------</option>'];
189
        foreach ($tipoAnulacion as $row) {
190
            $options[] = ($row->codigo === $invoiceTipoAnulacion) ?
191
                '<option value="' . $row->codigo . '" selected="">' . $row->descripcion . '</option>' :
192
                '<option value="' . $row->codigo . '">' . $row->descripcion . '</option>';
193
        }
194
195
        $attributes = $model->editable ? 'name="ncftipoanulacion"' : 'disabled=""';
196
        return '<div class="col-sm-2">'
197
            . '<div class="form-group">'
198
            .  $i18n->trans('ncf-cancellation-type')
199
            . '<select ' . $attributes . ' class="form-control">' . implode('', $options) . '</select>'
200
            . '</div>'
201
            . '</div>';
202
    }
203
204
    private static function ncfFechaVencimiento(Translator $i18n, PurchaseDocument $model): string
205
    {
206
        $attributes = ($model->editable) ? 'name="ncffechavencimiento"' : 'disabled=""';
207
        return '<div class="col-sm-2">'
208
            . '<div class="form-group">' . $i18n->trans('due-date')
209
            . '<input type="date" ' . $attributes . ' value="' . date('Y-m-d', strtotime($model->ncffechavencimiento)) . '" class="form-control"/>'
210
            . '</div>'
211
            . '</div>';
212
    }
213
214
    private static function numeroNCF(Translator $i18n, PurchaseDocument $model): string
215
    {
216
        $attributes = ($model->editable) ? 'name="numeroncf" maxlength="20"' : 'disabled=""';
217
        $btnColor = (in_array($model->numeroncf, ['', null], true)) ? "btn-secondary" : "btn-success";
218
        return empty($model->codproveedor) ? '' : '<div class="col-sm">'
219
            . '<div class="form-group">'
220
            . $i18n->trans('desc-numeroncf-purchases')
221
            . '<div class="input-group">'
222
            . '<input type="text" ' . $attributes . ' value="' . $model->numeroncf . '" class="form-control"/>'
223
            . '<div class="input-group-append">'
224
            . '<button class="btn ' . $btnColor .' btn-spin-action" id="btnVerifyNCF"'
225
            . 'onclick="purchasesNCFVerify()" '
226
            . 'title="'. $i18n->trans('verify-numproveedor')
227
            .'" type="button">'
228
            . '<i id="iconBtnVerify" class="fas fa-search fa-fw"></i>'
229
            . '</button>'
230
            . '</div>'
231
            . '</div>'
232
            . '</div>'
233
            . '</div>';
234
    }
235
}