| Total Complexity | 92 |
| Total Lines | 565 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Complex classes like ventas_factura 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 ventas_factura, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ventas_factura extends rd_controller |
||
| 23 | { |
||
| 24 | |||
| 25 | public $agencia; |
||
| 26 | public $agente; |
||
| 27 | public $agentes; |
||
| 28 | public $almacen; |
||
| 29 | public $cliente; |
||
| 30 | public $divisa; |
||
| 31 | public $ejercicio; |
||
| 32 | public $factura; |
||
| 33 | public $forma_pago; |
||
| 34 | public $mostrar_boton_pagada; |
||
| 35 | public $pais; |
||
| 36 | public $rectificada; |
||
| 37 | public $rectificativa; |
||
| 38 | public $serie; |
||
| 39 | public $ncf; |
||
| 40 | public $impuesto; |
||
| 41 | |||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | parent::__construct(__CLASS__, 'Factura de cliente', 'ventas', FALSE, FALSE); |
||
| 45 | } |
||
| 46 | |||
| 47 | protected function private_core() |
||
| 48 | { |
||
| 49 | parent::private_core(); |
||
| 50 | |||
| 51 | $this->ppage = $this->page->get('ventas_facturas'); |
||
|
|
|||
| 52 | $this->shared_extensions(); |
||
| 53 | $this->agencia = new agencia_transporte(); |
||
| 54 | $this->agente = FALSE; |
||
| 55 | $this->agentes = array(); |
||
| 56 | $this->almacen = new almacen(); |
||
| 57 | $this->cliente = FALSE; |
||
| 58 | $this->divisa = new divisa(); |
||
| 59 | $this->ejercicio = new ejercicio(); |
||
| 60 | $this->factura = FALSE; |
||
| 61 | $this->forma_pago = new forma_pago(); |
||
| 62 | $this->pais = new pais(); |
||
| 63 | $this->rectificada = FALSE; |
||
| 64 | $this->rectificativa = FALSE; |
||
| 65 | $this->serie = new serie(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Si hay alguna extensión de tipo config y texto no_button_pagada, |
||
| 69 | * desactivamos el botón de pagada/sin pagar. |
||
| 70 | */ |
||
| 71 | $this->mostrar_boton_pagada = TRUE; |
||
| 72 | foreach ($this->extensions as $ext) { |
||
| 73 | if ($ext->type == 'config' && $ext->text == 'no_button_pagada') { |
||
| 74 | $this->mostrar_boton_pagada = FALSE; |
||
| 75 | break; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * ¿Modificamos la factura? |
||
| 81 | */ |
||
| 82 | $factura = new factura_cliente(); |
||
| 83 | if (isset($_POST['idfactura'])) { |
||
| 84 | $this->factura = $factura->get($_POST['idfactura']); |
||
| 85 | $this->modificar(); |
||
| 86 | } else if (isset($_GET['id'])) { |
||
| 87 | $this->factura = $factura->get($_GET['id']); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($this->factura) { |
||
| 91 | $this->page->title = $this->factura->codigo; |
||
| 92 | |||
| 93 | /// cargamos el agente |
||
| 94 | $agente = new agente(); |
||
| 95 | if (!is_null($this->factura->codagente)) { |
||
| 96 | $this->agente = $agente->get($this->factura->codagente); |
||
| 97 | } |
||
| 98 | $this->agentes = $agente->all(); |
||
| 99 | |||
| 100 | /// cargamos el cliente |
||
| 101 | $cliente = new cliente(); |
||
| 102 | $this->cliente = $cliente->get($this->factura->codcliente); |
||
| 103 | |||
| 104 | if (isset($_GET['gen_asiento']) && isset($_GET['petid'])) { |
||
| 105 | if ($this->duplicated_petition($_GET['petid'])) { |
||
| 106 | $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); |
||
| 107 | } else { |
||
| 108 | $this->generar_asiento($this->factura); |
||
| 109 | } |
||
| 110 | } else if (isset($_GET['updatedir'])) { |
||
| 111 | $this->actualizar_direccion(); |
||
| 112 | } else if (isset($_REQUEST['pagada'])) { |
||
| 113 | $this->pagar(($_REQUEST['pagada'] == 'TRUE')); |
||
| 114 | } else if (isset($_POST['anular'])) { |
||
| 115 | $this->anular_factura(); |
||
| 116 | } else if (isset($_POST['rectificar'])) { |
||
| 117 | $this->rectificar_factura(); |
||
| 118 | } else if (isset($_GET['fix_ncf'])) { |
||
| 119 | $this->fix_ncf(); |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($this->factura->idfacturarect) { |
||
| 123 | $this->rectificada = $factura->get($this->factura->idfacturarect); |
||
| 124 | } else { |
||
| 125 | $this->get_factura_rectificativa(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /// comprobamos la factura |
||
| 129 | $this->factura->full_test(); |
||
| 130 | } else { |
||
| 131 | $this->new_error_msg("¡Factura de cliente no encontrada!", 'error', FALSE, FALSE); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function url() |
||
| 136 | { |
||
| 137 | if (!isset($this->factura)) { |
||
| 138 | return parent::url(); |
||
| 139 | } else if ($this->factura) { |
||
| 140 | return $this->factura->url(); |
||
| 141 | } |
||
| 142 | |||
| 143 | return $this->ppage->url(); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function fix_ncf() |
||
| 147 | { |
||
| 148 | $this->ncf_length = (\strtotime($this->factura->fecha) < (\strtotime('01-05-2018'))) ? 19 : $this->ncf_length; |
||
| 149 | $funcion_generar_numero = (\strtotime($this->factura->fecha) < (\strtotime('01-05-2018'))) ? 'generar_numero_ncf_old':'generar_numero_ncf'; |
||
| 150 | if ($this->factura->numero2 != '' and strlen($this->factura->numero2) == $this->ncf_length) { |
||
| 151 | $this->new_error_msg('¡La Factura ya posee un NCF valido, no se hace ninguna modificación!'); |
||
| 152 | } else { |
||
| 153 | /* |
||
| 154 | * Verificación de disponibilidad del Número de NCF para República Dominicana |
||
| 155 | */ |
||
| 156 | //Obtenemos el tipo de comprobante a generar para el cliente |
||
| 157 | $tipo_comprobante = $this->ncf_tipo_comprobante($this->empresa->id, $this->cliente->codcliente); |
||
| 158 | if($this->factura->idfacturarect){ |
||
| 159 | $tipo_comprobante = '04'; |
||
| 160 | } |
||
| 161 | //Con el codigo del almacen desde donde facturaremos generamos el número de NCF |
||
| 162 | $numero_ncf = $this->$funcion_generar_numero($this->empresa->id, $this->factura->codalmacen, $tipo_comprobante, $this->factura->codpago); |
||
| 163 | $this->factura->numero2 = $numero_ncf['NCF']; |
||
| 164 | if ($this->factura->save()) { |
||
| 165 | $this->guardar_ncf($this->empresa->id, $this->factura, $tipo_comprobante, $numero_ncf); |
||
| 166 | $this->new_message('¡NCF corregido correctamente!'); |
||
| 167 | } else { |
||
| 168 | $this->new_error_msg('Ocurrio un error y no se pudo generar el NCF correctamente, intentelo nuevamente revisando los datos del Cliente.'); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | private function modificar() |
||
| 174 | { |
||
| 175 | $this->factura->observaciones = $_POST['observaciones']; |
||
| 176 | $this->factura->nombrecliente = $_POST['nombrecliente']; |
||
| 177 | $this->factura->cifnif = $_POST['cifnif']; |
||
| 178 | $this->factura->codpais = $_POST['codpais']; |
||
| 179 | $this->factura->provincia = $_POST['provincia']; |
||
| 180 | $this->factura->ciudad = $_POST['ciudad']; |
||
| 181 | $this->factura->codpostal = $_POST['codpostal']; |
||
| 182 | $this->factura->direccion = $_POST['direccion']; |
||
| 183 | $this->factura->apartado = $_POST['apartado']; |
||
| 184 | |||
| 185 | $this->factura->envio_nombre = $_POST['envio_nombre']; |
||
| 186 | $this->factura->envio_apellidos = $_POST['envio_apellidos']; |
||
| 187 | $this->factura->envio_codtrans = NULL; |
||
| 188 | if ($_POST['envio_codtrans'] != '') { |
||
| 189 | $this->factura->envio_codtrans = $_POST['envio_codtrans']; |
||
| 190 | } |
||
| 191 | $this->factura->envio_codigo = $_POST['envio_codigo']; |
||
| 192 | $this->factura->envio_codpais = $_POST['envio_codpais']; |
||
| 193 | $this->factura->envio_provincia = $_POST['envio_provincia']; |
||
| 194 | $this->factura->envio_ciudad = $_POST['envio_ciudad']; |
||
| 195 | $this->factura->envio_codpostal = $_POST['envio_codpostal']; |
||
| 196 | $this->factura->envio_direccion = $_POST['envio_direccion']; |
||
| 197 | $this->factura->envio_apartado = $_POST['envio_apartado']; |
||
| 198 | |||
| 199 | $this->factura->codagente = NULL; |
||
| 200 | $this->factura->porcomision = 0; |
||
| 201 | if ($_POST['codagente'] != '') { |
||
| 202 | $this->factura->codagente = $_POST['codagente']; |
||
| 203 | $this->factura->porcomision = floatval($_POST['porcomision']); |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->factura->set_fecha_hora($_POST['fecha'], $_POST['hora']); |
||
| 207 | |||
| 208 | /// ¿cambiamos la forma de pago? |
||
| 209 | if ($this->factura->codpago != $_POST['forma_pago']) { |
||
| 210 | $this->factura->codpago = $_POST['forma_pago']; |
||
| 211 | $this->factura->vencimiento = $this->nuevo_vencimiento($this->factura->fecha, $this->factura->codpago); |
||
| 212 | } else { |
||
| 213 | $this->factura->vencimiento = $_POST['vencimiento']; |
||
| 214 | } |
||
| 215 | |||
| 216 | /// función auxiliar para implementar en los plugins que lo necesiten |
||
| 217 | if (!fs_generar_numero2($this->factura)) { |
||
| 218 | $this->factura->numero2 = $_POST['numero2']; |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($this->factura->save()) { |
||
| 222 | $asiento = $this->factura->get_asiento(); |
||
| 223 | if ($asiento) { |
||
| 224 | $asiento->fecha = $this->factura->fecha; |
||
| 225 | if (!$asiento->save()) { |
||
| 226 | $this->new_error_msg("Imposible modificar la fecha del asiento."); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /// Función de ejecución de tareas post guardado correcto del albarán |
||
| 231 | fs_documento_post_save($this->factura); |
||
| 232 | |||
| 233 | $this->new_message("Factura modificada correctamente."); |
||
| 234 | $this->new_change('Factura Cliente ' . $this->factura->codigo, $this->factura->url()); |
||
| 235 | } else { |
||
| 236 | $this->new_error_msg("¡Imposible modificar la factura!"); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | private function actualizar_direccion() |
||
| 241 | { |
||
| 242 | foreach ($this->cliente->get_direcciones() as $dir) { |
||
| 243 | if ($dir->domfacturacion) { |
||
| 244 | $this->factura->cifnif = $this->cliente->cifnif; |
||
| 245 | $this->factura->nombrecliente = $this->cliente->razonsocial; |
||
| 246 | |||
| 247 | $this->factura->apartado = $dir->apartado; |
||
| 248 | $this->factura->ciudad = $dir->ciudad; |
||
| 249 | $this->factura->coddir = $dir->id; |
||
| 250 | $this->factura->codpais = $dir->codpais; |
||
| 251 | $this->factura->codpostal = $dir->codpostal; |
||
| 252 | $this->factura->direccion = $dir->direccion; |
||
| 253 | $this->factura->provincia = $dir->provincia; |
||
| 254 | |||
| 255 | if ($this->factura->save()) { |
||
| 256 | $this->new_message('Dirección actualizada correctamente.'); |
||
| 257 | } else { |
||
| 258 | $this->new_error_msg('Imposible actualizar la dirección de la factura.'); |
||
| 259 | } |
||
| 260 | |||
| 261 | break; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | private function generar_asiento(&$factura) |
||
| 267 | { |
||
| 268 | if ($factura->get_asiento()) { |
||
| 269 | $this->new_error_msg('Ya hay un asiento asociado a esta factura.'); |
||
| 270 | } else { |
||
| 271 | $asiento_factura = new asiento_factura(); |
||
| 272 | $asiento_factura->soloasiento = TRUE; |
||
| 273 | if ($asiento_factura->generar_asiento_venta($factura)) { |
||
| 274 | $this->new_message("<a href='" . $asiento_factura->asiento->url() . "'>Asiento</a> generado correctamente."); |
||
| 275 | |||
| 276 | if (!$this->empresa->contintegrada) { |
||
| 277 | $this->new_message("¿Quieres que los asientos se generen automáticamente?" |
||
| 278 | . " Activa la <a href='index.php?page=admin_empresa#facturacion'>Contabilidad integrada</a>."); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | foreach ($asiento_factura->errors as $err) { |
||
| 283 | $this->new_error_msg($err); |
||
| 284 | } |
||
| 285 | |||
| 286 | foreach ($asiento_factura->messages as $msg) { |
||
| 287 | $this->new_message($msg); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | private function pagar($pagada = TRUE) |
||
| 293 | { |
||
| 294 | /// ¿Hay asiento? |
||
| 295 | if (is_null($this->factura->idasiento)) { |
||
| 296 | $this->factura->pagada = $pagada; |
||
| 297 | $this->factura->save(); |
||
| 298 | } else if (!$pagada && $this->factura->pagada) { |
||
| 299 | /// marcar como impagada |
||
| 300 | $this->factura->pagada = FALSE; |
||
| 301 | |||
| 302 | /// ¿Eliminamos el asiento de pago? |
||
| 303 | $as1 = new asiento(); |
||
| 304 | $asiento = $as1->get($this->factura->idasientop); |
||
| 305 | if ($asiento) { |
||
| 306 | $asiento->delete(); |
||
| 307 | $this->new_message('Asiento de pago eliminado.'); |
||
| 308 | } |
||
| 309 | |||
| 310 | $this->factura->idasientop = NULL; |
||
| 311 | if ($this->factura->save()) { |
||
| 312 | $this->new_message('Factura marcada como impagada.'); |
||
| 313 | } else { |
||
| 314 | $this->new_error_msg('Error al modificar la factura.'); |
||
| 315 | } |
||
| 316 | } else if ($pagada && !$this->factura->pagada) { |
||
| 317 | /// marcar como pagada |
||
| 318 | $asiento = $this->factura->get_asiento(); |
||
| 319 | if ($asiento) { |
||
| 320 | /// nos aseguramos que el cliente tenga subcuenta en el ejercicio actual |
||
| 321 | $subcli = FALSE; |
||
| 322 | $eje = $this->ejercicio->get_by_fecha($_POST['fpagada']); |
||
| 323 | if ($eje && $this->cliente) { |
||
| 324 | $subcli = $this->cliente->get_subcuenta($eje->codejercicio); |
||
| 325 | } |
||
| 326 | |||
| 327 | $importe = $this->euro_convert($this->factura->totaleuros, $this->factura->coddivisa, $this->factura->tasaconv); |
||
| 328 | |||
| 329 | $asiento_factura = new asiento_factura(); |
||
| 330 | $this->factura->idasientop = $asiento_factura->generar_asiento_pago($asiento, $this->factura->codpago, $_POST['fpagada'], $subcli, $importe); |
||
| 331 | if ($this->factura->idasientop !== NULL) { |
||
| 332 | $this->factura->pagada = TRUE; |
||
| 333 | if ($this->factura->save()) { |
||
| 334 | $this->new_message('<a href="' . $this->factura->asiento_pago_url() . '">Asiento de pago</a> generado.'); |
||
| 335 | } else { |
||
| 336 | $this->new_error_msg('Error al marcar la factura como pagada.'); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | foreach ($asiento_factura->errors as $err) { |
||
| 341 | $this->new_error_msg($err); |
||
| 342 | } |
||
| 343 | } else { |
||
| 344 | $this->new_error_msg('No se ha encontrado el asiento de la factura.'); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | private function nuevo_vencimiento($fecha, $codpago) |
||
| 350 | { |
||
| 351 | $vencimiento = $fecha; |
||
| 352 | |||
| 353 | $formap = $this->forma_pago->get($codpago); |
||
| 354 | if ($formap) { |
||
| 355 | $cli0 = new cliente(); |
||
| 356 | $cliente = $cli0->get($this->factura->codcliente); |
||
| 357 | if ($cliente) { |
||
| 358 | $vencimiento = $formap->calcular_vencimiento($fecha, $cliente->diaspago); |
||
| 359 | } else { |
||
| 360 | $vencimiento = $formap->calcular_vencimiento($fecha); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | return $vencimiento; |
||
| 365 | } |
||
| 366 | |||
| 367 | private function anular_factura() |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | public function rectificar_factura() |
||
| 537 | } |
||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | private function get_factura_rectificativa() |
||
| 542 | { |
||
| 543 | $sql = "SELECT * FROM facturascli WHERE idfacturarect = " . $this->factura->var2str($this->factura->idfactura); |
||
| 544 | |||
| 545 | $data = $this->db->select($sql); |
||
| 546 | if ($data) { |
||
| 547 | $this->rectificativa = new factura_cliente($data[0]); |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | public function get_cuentas_bancarias() |
||
| 561 | } |
||
| 562 | |||
| 563 | public function shared_extensions() |
||
| 564 | { |
||
| 565 | $extensiones = array( |
||
| 566 | array( |
||
| 567 | 'name' => 'ncf_functions_js', |
||
| 568 | 'page_from' => __CLASS__, |
||
| 591 |