Total Complexity | 67 |
Total Lines | 391 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ver_residente 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 ver_residente, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ver_residente extends residentes_controller |
||
32 | { |
||
33 | public $cliente; |
||
34 | public $cliente_data; |
||
35 | public $articulos; |
||
36 | public $articulos_cobrables; |
||
37 | public $familia; |
||
38 | public $familias; |
||
39 | public $facturas; |
||
40 | public $impuesto; |
||
41 | public $residente; |
||
42 | public $forma_pago; |
||
43 | public $lista_notas; |
||
44 | public $total_facturas = 0; |
||
45 | public $offset = 0; |
||
46 | |||
47 | public function __construct() |
||
50 | } |
||
51 | |||
52 | public function initVars() |
||
53 | { |
||
54 | $this->cliente = new cliente(); |
||
55 | $this->facturas = array(); |
||
56 | $this->impuesto = new impuesto(); |
||
57 | $this->forma_pago = new forma_pago(); |
||
|
|||
58 | $this->residente = false; |
||
59 | $this->articulos = new articulo(); |
||
60 | $this->familias = new familia(); |
||
61 | $this->familia = $this->familias->get('RESIDENT'); |
||
62 | } |
||
63 | |||
64 | protected function private_core() |
||
65 | { |
||
66 | $this->initVars(); |
||
67 | |||
68 | if (\filter_input(INPUT_GET, 'id')) { |
||
69 | $res0 = new residentes_edificaciones(); |
||
70 | $this->residente = $res0->get(\filter_input(INPUT_GET, 'id')); |
||
71 | $this->cliente_data = $this->cliente->get($this->residente->codcliente); |
||
72 | $this->cliente_data->codcontacto = ''; |
||
73 | $this->total_facturas = $this->residente->totalFacturas(); |
||
74 | $this->offset = \filter_input(INPUT_GET, 'offset') ?? 0; |
||
75 | if (class_exists('contacto_cliente')) { |
||
76 | $concli = new contacto_cliente(); |
||
77 | $infoCRM = $concli->all_from_cliente($this->residente->codcliente); |
||
78 | $this->cliente_data->codcontacto = $infoCRM[0]->codcontacto; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if ($this->residente) { |
||
83 | $accion = \filter_input(INPUT_POST, 'accion'); |
||
84 | if ($accion === 'generar_factura') { |
||
85 | $this->nueva_factura(); |
||
86 | } |
||
87 | $this->informacionResidente(); |
||
88 | } else { |
||
89 | $this->new_error_msg('Residente no encontrado.'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public function url() |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return void |
||
103 | */ |
||
104 | public function informacionResidente() |
||
105 | { |
||
106 | $this->page->title = 'Residente ' . $this->residente->nombre; |
||
107 | $this->facturas = []; |
||
108 | $referencias = []; |
||
109 | $articulosCobrados = []; |
||
110 | $documentosResidentes = $this->residente->facturas_from_cliente("DESC"); |
||
111 | foreach($documentosResidentes as $doc) { |
||
112 | //convert array $doc to standard object |
||
113 | $fac = (object) $doc; |
||
114 | if(!isset($referencias[$fac->idfactura])) |
||
115 | { |
||
116 | $referencias[$fac->idfactura] = ""; |
||
117 | } |
||
118 | |||
119 | if ($fac->qdadfinal > 0 && $fac->cantidaddev >= 0) { |
||
120 | $articulosCobrados[$fac->referencia] = 1; |
||
121 | } |
||
122 | |||
123 | if ($fac->referencia) { |
||
124 | $referencias[$fac->idfactura] .= $fac->referencia . " "; |
||
125 | } else { |
||
126 | $referencias[$fac->idfactura] .= $fac->descripcion . " "; |
||
127 | } |
||
128 | $this->facturas[$fac->idfactura] = $fac; |
||
129 | } |
||
130 | |||
131 | $facturas = new factura_cliente(); |
||
132 | foreach ($this->facturas as $factura) { |
||
133 | $facturaFinal = $facturas->get($factura->idfactura); |
||
134 | $facturaFinal->referencias = $referencias[$factura->idfactura]; |
||
135 | |||
136 | $this->facturas[$factura->idfactura] = $facturaFinal; |
||
137 | } |
||
138 | $this->generarArticulosCobrables($articulosCobrados); |
||
139 | } |
||
140 | // |
||
141 | // public function validarArticulos(&$articulosCobrados, &$fac, &$linea) |
||
142 | // { |
||
143 | // if (!in_array($fac->idfacturarect, ['', null], true)) { |
||
144 | // $rectificativas = $fac->get_rectificativas(); |
||
145 | // $articulosDevueltos = array(); |
||
146 | // $this->validarDevoluciones($articulosDevueltos, $rectificativas); |
||
147 | // |
||
148 | // if (!isset($articulosDevueltos[$linea->referencia])) { |
||
149 | // $articulosCobrados[$linea->referencia] = 1; |
||
150 | // } |
||
151 | // } |
||
152 | // } |
||
153 | |||
154 | public function validarDevoluciones(&$articulosDevueltos, $rectificativas) |
||
155 | { |
||
156 | foreach ($rectificativas as $rectificativa) { |
||
157 | $lineas_r = $rectificativa->get_lineas(); |
||
158 | foreach ($lineas_r as $linea_r) { |
||
159 | $articulosDevueltos[$linea_r->referencia] = 1; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | public function generarArticulosCobrables($articulos_cobrados) |
||
165 | { |
||
166 | $articulos = $this->familia->get_articulos(0, 1000); |
||
167 | foreach ($articulos as $art) { |
||
168 | if (isset($articulos_cobrados[$art->referencia]) === false && !$art->bloqueado) { |
||
169 | $this->articulos_cobrables[] = $art; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | private function nueva_factura() |
||
175 | { |
||
176 | $cliente = $this->cliente->get($this->residente->codcliente); |
||
177 | if ($cliente) { |
||
178 | $factura = new factura_cliente(); |
||
179 | |||
180 | $this->cabeceraFactura($factura, $cliente); |
||
181 | |||
182 | $this->datosClienteFactura($factura, $cliente); |
||
183 | |||
184 | /// función auxiliar para implementar en los plugins que lo necesiten |
||
185 | if (!fs_generar_numero2($factura)) { |
||
186 | $factura->numero2 = ''; |
||
187 | } |
||
188 | if ($factura->save()) { |
||
189 | $this->lineasFactura($factura); |
||
190 | |||
191 | $this->lineasLibresFactura($factura, 'otro'); |
||
192 | $this->lineasLibresFactura($factura, 'otro2'); |
||
193 | |||
194 | $this->totalFactura($factura); |
||
195 | } else { |
||
196 | $this->new_error_msg('Imposible guardar la factura.'); |
||
197 | } |
||
198 | } else { |
||
199 | $this->new_error_msg('Cliente no encontrado.'); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | private function cabeceraFactura(&$factura, $cliente) |
||
204 | { |
||
205 | $factura->codserie = ($cliente->codserie) ?: $this->empresa->codserie; |
||
206 | $factura->codagente = $this->user->codagente; |
||
207 | $factura->codpago = \filter_input(INPUT_POST, 'forma_pago'); |
||
208 | $factura->codalmacen = $this->empresa->codalmacen; |
||
209 | $factura->set_fecha_hora($this->today(), $this->hour()); |
||
210 | $factura->observaciones = htmlentities(trim(\filter_input(INPUT_POST, 'observaciones'))); |
||
211 | $this->datosContabilidadFactura($factura, $cliente); |
||
212 | |||
213 | $div0 = new divisa(); |
||
214 | $divisa = $div0->get($cliente->coddivisa); |
||
215 | if ($divisa) { |
||
216 | $factura->coddivisa = $divisa->coddivisa; |
||
217 | $factura->tasaconv = $divisa->tasaconv; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | private function datosContabilidadFactura(&$factura, $cliente) |
||
222 | { |
||
223 | $eje0 = new ejercicio(); |
||
224 | $ejercicio = $eje0->get_by_fecha(date('d-m-Y')); |
||
225 | if ($ejercicio) { |
||
226 | $factura->codejercicio = $ejercicio->codejercicio; |
||
227 | } |
||
228 | if ($this->empresa->contintegrada) { |
||
229 | $cliente->get_subcuenta($this->empresa->codejercicio); |
||
230 | } |
||
231 | |||
232 | $forma_pago = $this->forma_pago->get($factura->codpago); |
||
233 | if ($forma_pago->genrecibos === 'Pagados') { |
||
234 | $factura->pagada = true; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | private function datosClienteFactura(&$factura, $cliente) |
||
239 | { |
||
240 | $factura->codcliente = $cliente->codcliente; |
||
241 | foreach ($cliente->get_direcciones() as $d) { |
||
242 | if ($d->domfacturacion) { |
||
243 | $factura->codcliente = $cliente->codcliente; |
||
244 | $factura->cifnif = $cliente->cifnif; |
||
245 | $factura->nombrecliente = $cliente->nombre; |
||
246 | $factura->apartado = $d->apartado; |
||
247 | $factura->ciudad = $d->ciudad; |
||
248 | $factura->coddir = $d->id; |
||
249 | $factura->codpais = $d->codpais; |
||
250 | $factura->codpostal = $d->codpostal; |
||
251 | $factura->direccion = $d->direccion; |
||
252 | $factura->provincia = $d->provincia; |
||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | private function lineasFactura(&$factura) |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | private function lineasLibresFactura(&$factura, $linea_nombre) |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | |||
319 | private function totalFactura(&$factura) |
||
320 | { |
||
321 | /// redondeamos |
||
322 | $factura->neto = round($factura->neto, FS_NF0); |
||
323 | $factura->totaliva = round($factura->totaliva, FS_NF0); |
||
324 | $factura->totalirpf = round($factura->totalirpf, FS_NF0); |
||
325 | $factura->totalrecargo = round($factura->totalrecargo, FS_NF0); |
||
326 | $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; |
||
327 | |||
328 | if (abs((float)(\filter_input(INPUT_POST, 'total_importe')) - $factura->total) > .01) { |
||
329 | $this->new_error_msg("El total difiere entre la vista y el controlador (" . |
||
330 | \filter_input(INPUT_POST, 'total_importe') . |
||
331 | " frente a " . $factura->total . "). Debes informar del error."); |
||
332 | $factura->delete(); |
||
333 | } elseif ($factura->save()) { |
||
334 | $this->generar_asiento($factura); |
||
335 | /// Función de ejecución de tareas post guardado correcto de la factura |
||
336 | fs_documento_post_save($factura); |
||
337 | $this->new_message("<a href='" . $factura->url() . "'>Factura</a> guardada correctamente."); |
||
338 | $this->new_change('Factura Cliente ' . $factura->codigo, $factura->url(), true); |
||
339 | } else { |
||
340 | $this->new_error_msg("¡Imposible actualizar la <a href='" . $factura->url() . "'>Factura</a>!"); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Genera el asiento para la factura, si procede |
||
346 | * @param factura_cliente $factura |
||
347 | */ |
||
348 | private function generar_asiento(&$factura) |
||
356 | } |
||
357 | } |
||
358 | |||
359 | private function buscar_referencia() |
||
360 | { |
||
361 | /// desactivamos la plantilla HTML |
||
362 | $this->template = false; |
||
363 | |||
364 | $articulo = new articulo(); |
||
365 | $json = array(); |
||
366 | foreach ($articulo->search($_REQUEST['buscar_referencia']) as $art) { |
||
367 | $json[] = array( |
||
368 | 'value' => $art->referencia, |
||
369 | 'data' => $art->referencia, |
||
370 | 'pvpi' => $art->pvp_iva(false), |
||
371 | 'codimpuesto' => $art->codimpuesto, |
||
372 | 'descripcion' => $art->descripcion |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | header('Content-Type: application/json'); |
||
377 | echo json_encode(array('query' => $_REQUEST['buscar_referencia'], 'suggestions' => $json), JSON_THROW_ON_ERROR); |
||
378 | } |
||
379 | |||
380 | public function paginas() |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths