Total Complexity | 48 |
Total Lines | 382 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 0 |
Complex classes like documentos_residentes 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 documentos_residentes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class documentos_residentes extends residentes_controller |
||
30 | { |
||
31 | public $archivo; |
||
32 | public $cliente_residente; |
||
33 | public $documento; |
||
34 | public $pagado; |
||
35 | public $pendiente; |
||
36 | public $tipo_accion; |
||
37 | public $idprogramacion; |
||
38 | public $idfactura; |
||
39 | public $envio_masivo; |
||
40 | public $factura; |
||
41 | public $facturas; |
||
42 | public $contador_facturas; |
||
43 | public function __construct() |
||
44 | { |
||
45 | parent::__construct(__CLASS__, 'Documentos Residentes', 'admin', false, false, false); |
||
46 | } |
||
47 | |||
48 | protected function private_core() |
||
49 | { |
||
50 | parent::private_core(); |
||
51 | $this->init(); |
||
52 | $cod = $this->filter_request('codcliente'); |
||
53 | if ($cod) { |
||
54 | $this->obtenerInformacionResidente($cod); |
||
|
|||
55 | } |
||
56 | $info_accion = $this->filter_request('info_accion'); |
||
57 | $tipo_documento = $this->filter_request('tipo_documento'); |
||
58 | $this->idprogramacion = $this->filter_request('idprogramacion'); |
||
59 | $this->envio_masivo = !(($this->filter_request('envio_masivo') === 'false')); |
||
60 | $this->tipo_accion = $tipo_documento; |
||
61 | $this->verificarCantidadFacturas(); |
||
62 | $this->archivo = $tipo_documento.'_'.\date('dmYhis') . '.pdf'; |
||
63 | if ($this->cliente_residente && $info_accion) { |
||
64 | switch ($info_accion) { |
||
65 | case 'imprimir': |
||
66 | $this->template = false; |
||
1 ignored issue
–
show
|
|||
67 | $this->imprimir_documento($tipo_documento); |
||
68 | break; |
||
69 | case 'enviar': |
||
70 | $this->enviar_documento($tipo_documento); |
||
71 | break; |
||
72 | default: |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $cod |
||
80 | */ |
||
81 | public function obtenerInformacionResidente($cod) |
||
82 | { |
||
83 | $cliente = new cliente(); |
||
84 | $this->cliente_residente = $cliente->get($cod); |
||
85 | $residenteInformacion = new residentes_informacion(); |
||
86 | $informacion = $residenteInformacion->get($cod); |
||
87 | $residenteEdificacion = new residentes_edificaciones(); |
||
88 | $residente = $residenteEdificacion->get_by_field('codcliente', $cod); |
||
89 | $this->cliente_residente->inmueble = $residente[0]; |
||
90 | $this->cliente_residente->informacion = $informacion; |
||
91 | } |
||
92 | |||
93 | public function crear_documento($tipo_documento) |
||
94 | { |
||
95 | switch ($tipo_documento) { |
||
96 | case 'informacion_cobros': |
||
97 | $this->crearEstadoCuenta(); |
||
98 | break; |
||
99 | case 'factura_residente_detallada': |
||
100 | //$this->verificarCantidadFacturas(); |
||
101 | $this->crearFacturaDetallada(); |
||
102 | break; |
||
103 | default: |
||
104 | $this->documento = false; |
||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | private function verificarCantidadFacturas() |
||
110 | { |
||
111 | |||
112 | $facturas = $this->filter_request('idfactura'); |
||
113 | if ($facturas === '') { |
||
114 | $this->new_message('No hay facturas para enviar.'); |
||
115 | return; |
||
116 | } |
||
117 | $this->getFacturaProgramadaPendiente($facturas); |
||
118 | //$this->crearFactura(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param array|string $facturas |
||
123 | */ |
||
124 | private function getFacturaProgramadaPendiente($facturas) |
||
125 | { |
||
126 | $lista_facturas = explode(',', $facturas); |
||
127 | $this->idfactura = (is_array($lista_facturas)) ? $lista_facturas[0] : $facturas; |
||
128 | if (is_array($lista_facturas)) { |
||
129 | unset($lista_facturas[0]); |
||
130 | $this->contador_facturas = count($lista_facturas); |
||
131 | $this->facturas = implode(',', $lista_facturas); |
||
132 | } else { |
||
133 | $this->contador_facturas = 0; |
||
134 | $this->facturas = ''; |
||
135 | } |
||
136 | |||
137 | |||
138 | $facturasCliente = new factura_cliente(); |
||
139 | $f = $facturasCliente->get($this->idfactura); |
||
140 | $this->factura = $f; |
||
141 | $this->obtenerInformacionResidente($f->codcliente); |
||
142 | } |
||
143 | |||
144 | private function datosFactura() |
||
145 | { |
||
146 | $datosFacturaCabecera = []; |
||
147 | $datosFacturaDetalle = []; |
||
148 | if ($this->idfactura !== '') { |
||
149 | $facturas = new factura_cliente(); |
||
150 | $factura = $facturas->get($this->idfactura); |
||
151 | $datosFacturaCabecera = (array) $factura; |
||
152 | if ($this->RD_plugin) { |
||
153 | $ncf = new ncf_ventas(); |
||
154 | $ncfTipo = $ncf->get($this->empresa->id, $factura->numero2); |
||
155 | $datosFacturaCabecera['tiponcf'] = $ncfTipo[0]->tipo_descripcion; |
||
156 | $datosFacturaCabecera['vencimientoncf'] = $ncfTipo[0]->fecha_vencimiento; |
||
157 | } |
||
158 | $lineas = $factura->get_lineas(); |
||
159 | $totalAntesDescuento = 0; |
||
160 | $totalDescuento = 0; |
||
161 | foreach ($lineas as $linea) { |
||
162 | $totalAntesDescuento += $linea->pvpsindto; |
||
163 | $totalDescuento += ($linea->pvpsindto - $linea->pvptotal); |
||
164 | $datosFacturaDetalle[] = (array) $linea; |
||
165 | } |
||
166 | $datosFacturaCabecera['total_antes_descuento'] = $totalAntesDescuento; |
||
167 | $datosFacturaCabecera['total_descuento'] = $totalDescuento; |
||
168 | } |
||
169 | return [$datosFacturaCabecera, $datosFacturaDetalle]; |
||
170 | } |
||
171 | public function crearEstadoCuenta() |
||
172 | { |
||
173 | $this->documento = new residentes_pdf('letter', 'portrait'); |
||
174 | $this->documento->cliente_residente = $this->cliente_residente; |
||
175 | $this->documento->pdf->addInfo('Title', 'Pagos Residente ' . |
||
176 | $this->cliente_residente->codcliente); |
||
177 | $this->documento->pdf->addInfo('Subject', 'Pagos del Residente ' . |
||
178 | $this->cliente_residente->codcliente); |
||
179 | $this->documento->pdf->addInfo('Author', $this->empresa->nombre); |
||
180 | $this->documento->pdf->ezSetMargins(10, 10, 10, 10); |
||
181 | $this->crear_documento_cobros(); |
||
182 | } |
||
183 | |||
184 | public function crearFacturaDetallada() |
||
185 | { |
||
186 | $customerInfo = (array) $this->cliente_residente; |
||
187 | $customerInfo['direccion'] = trim($this->cliente_residente->inmueble->codigo_externo()) . ' numero ' |
||
188 | . $this->cliente_residente->inmueble->numero; |
||
189 | $datosFactura = $this->datosFactura(); |
||
190 | $datosEmpresa = (array) $this->empresa; |
||
191 | $this->documento = new ResidentesFpdf('L', 'mm', 'A5'); |
||
192 | // $this->documento = new ResidentesFpdf('P', 'mm', 'letter'); |
||
193 | $this->documento->createDocument($datosEmpresa, $datosFactura[0], $datosFactura[1], $customerInfo); |
||
194 | $this->pendiente = $this->pagosFactura(false); |
||
195 | $this->documento->addEstadoCuentaPendiente($this->pendiente); |
||
196 | |||
197 | if ($this->filter_request('info_accion') === 'enviar') { |
||
198 | $this->documento->Output( |
||
199 | 'F', |
||
200 | 'tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo, |
||
201 | true |
||
202 | ); |
||
203 | } else { |
||
204 | $this->documento->Output( |
||
205 | 'I', |
||
206 | 'factura_' .$datosFactura[0]['numero2']. '_' . \date('dmYhis') . '.pdf' |
||
207 | ); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | public function crear_documento_cobros() |
||
212 | { |
||
213 | $this->pendiente = $this->pagosFactura(false); |
||
214 | $this->pagado = $this->pagosFactura(true); |
||
215 | |||
216 | $linea_actual = 0; |
||
217 | $pagina = 1; |
||
218 | $lppag = 32; /// líneas por página |
||
219 | while ($linea_actual < count($this->pendiente)) { |
||
220 | /// salto de página |
||
221 | if ($linea_actual > 0) { |
||
222 | $this->documento->pdf->ezNewPage(); |
||
223 | } |
||
224 | $this->documento->generar_pdf_cabecera($this->empresa, $lppag); |
||
225 | $this->documento->generar_datos_residente($this->documento, 'informe_cobros', $lppag); |
||
226 | $this->documento->generar_pdf_lineas( |
||
227 | $this->documento, |
||
228 | $this->pendiente, |
||
229 | $linea_actual, |
||
230 | $lppag, |
||
231 | 'pendiente' |
||
232 | ); |
||
233 | $this->documento->set_y($this->documento->pdf->y - 16); |
||
234 | } |
||
235 | |||
236 | $linea_actual2 = 0; |
||
237 | while ($linea_actual2 < count($this->pagado)) { |
||
238 | if ($linea_actual2 > 0) { |
||
239 | $this->documento->pdf->ezNewPage(); |
||
240 | } elseif ($linea_actual === 0) { |
||
241 | $this->documento->generar_pdf_cabecera($this->empresa, $lppag); |
||
242 | $this->documento->generar_datos_residente($this->documento, 'informe_cobros', $lppag); |
||
243 | } |
||
244 | $this->documento->generar_pdf_lineas( |
||
245 | $this->documento, |
||
246 | $this->pagado, |
||
247 | $linea_actual2, |
||
248 | $lppag, |
||
249 | 'pagado' |
||
250 | ); |
||
251 | $pagina++; |
||
252 | } |
||
253 | $this->documento->set_y(80); |
||
254 | if ($this->empresa->pie_factura) { |
||
255 | $this->documento->pdf->addText(20, 40, 8, fs_fix_html('<b>Generado por:</b> ' . |
||
256 | $this->user->get_agente_fullname()), 0); |
||
257 | $this->documento->pdf->addText( |
||
258 | 10, |
||
259 | 30, |
||
260 | 8, |
||
261 | $this->documento->center_text(fs_fix_html($this->empresa->pie_factura), 180) |
||
262 | ); |
||
263 | } |
||
264 | |||
265 | if ($this->filter_request('info_accion') == 'enviar') { |
||
266 | $this->documento->save('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
||
267 | } else { |
||
268 | $this->documento->show('documento_cobros_' . \date('dmYhis') . '.pdf'); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @throws phpmailerException |
||
274 | */ |
||
275 | public function enviar_documento($tipo_documento) |
||
375 | } |
||
376 | } |
||
377 | |||
378 | public function imprimir_documento($tipo_documento) |
||
379 | { |
||
382 | } |
||
383 | |||
384 | public function init() |
||
393 | } |
||
394 | } |
||
395 | |||
396 | public function is_html($txt) |
||
399 | } |
||
400 | |||
401 | // public function url() |
||
402 | // { |
||
403 | // return parent::url(); |
||
404 | // } |
||
405 | /** |
||
406 | * @param $factura |
||
407 | */ |
||
408 | private function crearFactura(): void |
||
413 |