protected function private_core() { $this->share_extensions(); /// obtenemos los datos de configuración de impresión $this->impresion = array('print_ref' => '1', 'print_dto' => '1', 'print_alb' => '0', 'print_formapago' => '1'); $fsvar = new fs_var(); $this->impresion = $fsvar->array_get($this->impresion, FALSE); $this->factura = FALSE; if (isset($_GET['id'])) { $factura = new factura_cliente(); $this->factura = $factura->get($_GET['id']); } if (isset($_GET['abreviatura'])) { $traduccion = new traduccion_fac_det(); $this->traduccion = $traduccion->get($_GET['abreviatura']); } else { $traduccion = new traduccion_fac_det(); $this->traduccion = $traduccion->get('es_ES'); } if ($this->factura) { $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); if (isset($_POST['email'])) { $this->enviar_email('factura', $_REQUEST['tipo']); } else { $filename = 'factura_' . $this->factura->codigo . '.pdf'; $this->generar_pdf(FALSE, $filename); } } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
protected function private_core() { $this->cliente = FALSE; $this->logo = FALSE; if (file_exists('tmp/' . FS_TMP_NAME . 'logo.png')) { $this->logo = 'tmp/' . FS_TMP_NAME . 'logo.png'; } else { if (file_exists('tmp/' . FS_TMP_NAME . 'logo.jpg')) { $this->logo = 'tmp/' . FS_TMP_NAME . 'logo.jpg'; } } $this->template = FALSE; if (isset($_REQUEST['id'])) { $recibo0 = new recibo_cliente(); $this->recibo = $recibo0->get($_REQUEST['id']); if ($this->recibo) { $cliente = new cliente(); $this->cliente = $cliente->get($this->recibo->codcliente); $fact0 = new factura_cliente(); $this->factura = $fact0->get($this->recibo->idfactura); $this->generar_pdf_recibo(); } else { echo 'ERROR - Recibo no encontrado.'; } } else { $this->share_extensions(); } }
protected function process() { $this->share_extensions(); $this->factura = FALSE; if (isset($_GET['id'])) { $factura = new factura_cliente(); $this->factura = $factura->get($_GET['id']); } if ($this->factura) { $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); $this->generar_pdf(); } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
protected function private_core() { $this->albaran = FALSE; $this->cliente = FALSE; $this->factura = FALSE; $this->impuesto = new impuesto(); /// obtenemos los datos de configuración de impresión $this->impresion = array('print_ref' => '1', 'print_dto' => '1', 'print_alb' => '0'); $fsvar = new fs_var(); $this->impresion = $fsvar->array_get($this->impresion, FALSE); $this->logo = FALSE; if (file_exists('tmp/' . FS_TMP_NAME . 'logo.png')) { $this->logo = 'tmp/' . FS_TMP_NAME . 'logo.png'; } else { if (file_exists('tmp/' . FS_TMP_NAME . 'logo.jpg')) { $this->logo = 'tmp/' . FS_TMP_NAME . 'logo.jpg'; } } if (isset($_REQUEST['albaran']) and isset($_REQUEST['id'])) { $alb = new albaran_cliente(); $this->albaran = $alb->get($_REQUEST['id']); if ($this->albaran) { $cliente = new cliente(); $this->cliente = $cliente->get($this->albaran->codcliente); } if (isset($_POST['email'])) { $this->enviar_email('albaran'); } else { $this->generar_pdf_albaran(); } } else { if (isset($_REQUEST['factura']) and isset($_REQUEST['id'])) { $fac = new factura_cliente(); $this->factura = $fac->get($_REQUEST['id']); if ($this->factura) { $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); } if (isset($_POST['email'])) { $this->enviar_email('factura', $_REQUEST['tipo']); } else { $this->generar_pdf_factura($_REQUEST['tipo']); } } } $this->share_extensions(); }
protected function private_core() { $this->share_extension(); $this->serie = new serie(); $fact0 = new factura_cliente(); $this->factura = FALSE; if (isset($_REQUEST['id'])) { $this->factura = $fact0->get($_REQUEST['id']); } if ($this->factura) { if (isset($_POST['id'])) { $this->nueva_rectificativa(); } } else { $this->new_error_msg('Factura no encontrada.'); } }
protected function process() { $this->show_fs_toolbar = FALSE; $this->serie = new serie(); $this->share_extensions(); if (isset($_REQUEST['buscar_cliente'])) { $this->buscar_cliente(); } $this->desde = Date('01-m-Y'); if (isset($_POST['desde'])) { $this->desde = $_POST['desde']; } $this->hasta = Date('d-m-Y'); if (isset($_POST['hasta'])) { $this->hasta = $_POST['hasta']; } $this->codcliente = FALSE; if (!isset($_POST['todos']) and isset($_POST['codcliente'])) { $this->codcliente = $_POST['codcliente']; } $this->codserie = FALSE; if (isset($_POST['codserie'])) { $this->codserie = $_POST['codserie']; } /// ¿Marcamos ya las facturas? if (isset($_POST['idfactura'])) { $num = 0; $fact0 = new factura_cliente(); foreach ($_POST['idfactura'] as $id) { $factura = $fact0->get($id); if ($factura) { $factura->pagada = TRUE; $factura->save(); $num++; } } $this->new_message($num . ' facturas marcadas como pagadas, estas son las siguientes.'); } $this->resultados = FALSE; if (isset($_POST['desde'])) { $this->resultados = $this->buscar_facturas(); } }
protected function private_core() { /// ¿El usuario tiene permiso para eliminar en esta página? $this->allow_delete = $this->user->allow_delete_on(__CLASS__); $this->share_extensions(); $this->modelonumero = new modelonumero(); $this->ppage = $this->page->get('ventas_facturas'); $this->ejercicio = new ejercicio(); $this->agente = FALSE; $this->agentes = array(); $this->cliente = FALSE; $this->divisa = new divisa(); $factura = new factura_cliente(); $this->factura = FALSE; $this->forma_pago = new forma_pago(); $this->pais = new pais(); $this->rectificada = FALSE; $this->rectificativa = FALSE; $this->serie = new serie(); if (isset($_GET['id'])) { $this->factura = $factura->get($_GET['id']); } if ($this->factura) { $this->page->title = $this->factura->codigo; /// cargamos el agente $agente = new agente(); if (!is_null($this->factura->codagente)) { $this->agente = $agente->get($this->factura->codagente); } $this->agentes = $agente->all(); /// cargamos el cliente $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); if ($this->factura->idfacturarect) { $this->rectificada = $factura->get($this->factura->idfacturarect); } else { $this->get_factura_rectificativa(); } } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
protected function process() { $this->show_fs_toolbar = FALSE; $this->albaran = FALSE; $this->cliente = FALSE; $this->factura = FALSE; $this->impuesto = new impuesto(); if (isset($_REQUEST['albaran']) and isset($_REQUEST['id'])) { $alb = new albaran_cliente(); $this->albaran = $alb->get($_REQUEST['id']); if ($this->albaran) { $cliente = new cliente(); $this->cliente = $cliente->get($this->albaran->codcliente); } if (isset($_POST['email'])) { $this->enviar_email('albaran'); } else { $this->generar_pdf_albaran(); } } else { if (isset($_REQUEST['factura']) and isset($_REQUEST['id'])) { $fac = new factura_cliente(); $this->factura = $fac->get($_REQUEST['id']); if ($this->factura) { $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); } if (isset($_POST['email'])) { $this->enviar_email('factura', $_REQUEST['tipo']); } else { $this->generar_pdf_factura($_REQUEST['tipo']); } } } $this->share_extensions(); }
protected function process() { $this->ppage = $this->page->get('ventas_facturas'); $this->ejercicio = new ejercicio(); $this->agente = FALSE; $this->cliente = FALSE; $factura = new factura_cliente(); $this->factura = FALSE; $this->forma_pago = new forma_pago(); /// ¿El usuario tiene permiso para eliminar en esta página? $this->allow_delete = $this->user->allow_delete_on(__CLASS__); /** * Si hay alguna extensión de tipo config y texto no_button_pagada, * desactivamos el botón de pagada/sin pagar. */ $this->mostrar_boton_pagada = TRUE; foreach ($this->extensions as $ext) { if ($ext->type == 'config' and $ext->text == 'no_button_pagada') { $this->mostrar_boton_pagada = FALSE; break; } } /** * ¿Modificamos la factura? */ if (isset($_POST['idfactura'])) { $this->factura = $factura->get($_POST['idfactura']); $this->factura->observaciones = $_POST['observaciones']; $this->factura->numero2 = $_POST['numero2']; /// obtenemos el ejercicio para poder acotar la fecha $eje0 = $this->ejercicio->get($this->factura->codejercicio); if ($eje0) { $this->factura->fecha = $eje0->get_best_fecha($_POST['fecha'], TRUE); $this->factura->hora = $_POST['hora']; } else { $this->new_error_msg('No se encuentra el ejercicio asociado a la factura.'); } /// ¿cambiamos la forma de pago? if ($this->factura->codpago != $_POST['forma_pago']) { $this->factura->codpago = $_POST['forma_pago']; $this->factura->vencimiento = $this->nuevo_vencimiento($this->factura->fecha, $this->factura->codpago); } else { $this->factura->vencimiento = $_POST['vencimiento']; } if ($this->factura->save()) { $asiento = $this->factura->get_asiento(); if ($asiento) { $asiento->fecha = $this->factura->fecha; if (!$asiento->save()) { $this->new_error_msg("Imposible modificar la fecha del asiento."); } } $this->new_message("Factura modificada correctamente."); $this->new_change('Factura Cliente ' . $this->factura->codigo, $this->factura->url()); } else { $this->new_error_msg("¡Imposible modificar la factura!"); } } else { if (isset($_GET['id'])) { $this->factura = $factura->get($_GET['id']); } } if ($this->factura) { $this->page->title = $this->factura->codigo; /// cargamos el agente if (!is_null($this->factura->codagente)) { $agente = new agente(); $this->agente = $agente->get($this->factura->codagente); } /// cargamos el cliente $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); if (isset($_GET['gen_asiento']) and isset($_GET['petid'])) { if ($this->duplicated_petition($_GET['petid'])) { $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); } else { $this->generar_asiento(); } } else { if (isset($_GET['updatedir'])) { $this->actualizar_direccion(); } else { if (isset($_REQUEST['pagada'])) { $this->factura->pagada = $_REQUEST['pagada'] == 'TRUE'; if ($this->factura->save()) { $this->new_message("Factura modificada correctamente."); } else { $this->new_error_msg("¡Imposible modificar la factura!"); } } } } /// comprobamos la factura $this->factura->full_test(); } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
public function get_factura() { if ($this->tipodocumento == 'Factura de cliente') { $fac = new factura_cliente(); return $fac->get_by_codigo($this->documento); } else { if ($this->tipodocumento == 'Factura de proveedor') { $fac = new factura_proveedor(); return $fac->get_by_codigo($this->documento); } else { return FALSE; } } }
protected function private_core() { /// ¿El usuario tiene permiso para eliminar en esta página? $this->allow_delete = $this->user->allow_delete_on(__CLASS__); $this->ppage = $this->page->get('ventas_facturas'); $this->ejercicio = new ejercicio(); $this->agente = FALSE; $this->agentes = array(); $this->cliente = FALSE; $this->divisa = new divisa(); $this->factura = FALSE; $this->forma_pago = new forma_pago(); $this->pais = new pais(); $this->rectificada = FALSE; $this->rectificativa = FALSE; $this->serie = new serie(); /** * Si hay alguna extensión de tipo config y texto no_button_pagada, * desactivamos el botón de pagada/sin pagar. */ $this->mostrar_boton_pagada = TRUE; foreach ($this->extensions as $ext) { if ($ext->type == 'config' and $ext->text == 'no_button_pagada') { $this->mostrar_boton_pagada = FALSE; break; } } /** * ¿Modificamos la factura? */ $factura = new factura_cliente(); if (isset($_POST['idfactura'])) { $this->factura = $factura->get($_POST['idfactura']); $this->modificar(); } else { if (isset($_GET['id'])) { $this->factura = $factura->get($_GET['id']); } } if ($this->factura) { $this->page->title = $this->factura->codigo; /// cargamos el agente $agente = new agente(); if (!is_null($this->factura->codagente)) { $this->agente = $agente->get($this->factura->codagente); } $this->agentes = $agente->all(); /// cargamos el cliente $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); if (isset($_GET['gen_asiento']) and isset($_GET['petid'])) { if ($this->duplicated_petition($_GET['petid'])) { $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); } else { $this->generar_asiento($this->factura); } } else { if (isset($_GET['updatedir'])) { $this->actualizar_direccion(); } else { if (isset($_REQUEST['pagada'])) { $this->pagar($_REQUEST['pagada'] == 'TRUE'); } else { if (isset($_POST['anular'])) { $this->anular_factura(); } } } } if ($this->factura->idfacturarect) { $this->rectificada = $factura->get($this->factura->idfacturarect); } else { $this->get_factura_rectificativa(); } /// comprobamos la factura $this->factura->full_test(); } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
private function test_models() { $mpp = 100; $last_errores = array(); switch ($this->informe['model']) { default: /// tablas $this->test_tablas(); break; case 'asiento': $asiento = new asiento(); $asientos = $asiento->all($this->informe['offset'], $mpp); if ($asientos) { if ($this->informe['offset'] == 0) { foreach ($this->check_partidas_erroneas() as $err) { $last_errores[] = $err; } } foreach ($asientos as $asi) { if ($asi->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'factura cliente'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$asi->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $asi->codejercicio, 'id' => $asi->numero, 'url' => $asi->url(), 'fecha' => $asi->fecha, 'fix' => $asi->fix()); } } } $this->informe['offset'] += $mpp; } else { if ($this->informe['all']) { $this->informe['model'] = 'factura cliente'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'factura cliente': $factura = new factura_cliente(); $facturas = $factura->all($this->informe['offset'], $mpp); if ($facturas) { foreach ($facturas as $fac) { if ($fac->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'factura proveedor'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$fac->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $fac->codejercicio, 'id' => $fac->codigo, 'url' => $fac->url(), 'fecha' => $fac->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += $mpp; } else { if ($this->informe['all']) { $this->informe['model'] = 'factura proveedor'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'factura proveedor': $factura = new factura_proveedor(); $facturas = $factura->all($this->informe['offset'], $mpp); if ($facturas) { foreach ($facturas as $fac) { if ($fac->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'albaran cliente'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$fac->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $fac->codejercicio, 'id' => $fac->codigo, 'url' => $fac->url(), 'fecha' => $fac->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += $mpp; } else { if ($this->informe['all']) { $this->informe['model'] = 'albaran cliente'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'albaran cliente': $albaran = new albaran_cliente(); $albaranes = $albaran->all($this->informe['offset'], $mpp); if ($albaranes) { foreach ($albaranes as $alb) { if ($alb->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'albaran proveedor'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$alb->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $alb->codejercicio, 'id' => $alb->codigo, 'url' => $alb->url(), 'fecha' => $alb->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += $mpp; } else { if ($this->informe['all']) { $this->informe['model'] = 'albaran proveedor'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'albaran proveedor': $albaran = new albaran_proveedor(); $albaranes = $albaran->all($this->informe['offset'], $mpp); if ($albaranes) { foreach ($albaranes as $alb) { if ($alb->codejercicio == $this->informe['ejercicio']) { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; break; } else { if (!$alb->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $alb->codejercicio, 'id' => $alb->codigo, 'url' => $alb->url(), 'fecha' => $alb->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += $mpp; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } break; case 'fin': break; } return $last_errores; }
private function test_models() { $last_errores = array(); switch ($this->informe['model']) { default: /// tablas $this->test_tablas(); break; case 'asiento': $asiento = new asiento(); $asientos = $asiento->all($this->informe['offset']); if ($asientos) { if ($this->informe['offset'] == 0) { foreach ($this->check_partidas_erroneas() as $err) { $last_errores[] = $err; } } foreach ($asientos as $asi) { if ($asi->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'factura cliente'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$asi->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $asi->codejercicio, 'id' => $asi->numero, 'url' => $asi->url(), 'fecha' => $asi->fecha, 'fix' => $asi->fix()); } } } $this->informe['offset'] += FS_ITEM_LIMIT; } else { if ($this->informe['all']) { $this->informe['model'] = 'factura cliente'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'factura cliente': $factura = new factura_cliente(); $facturas = $factura->all($this->informe['offset']); if ($facturas) { foreach ($facturas as $fac) { if ($fac->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'factura proveedor'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$fac->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $fac->codejercicio, 'id' => $fac->codigo, 'url' => $fac->url(), 'fecha' => $fac->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += FS_ITEM_LIMIT; } else { if ($this->informe['all']) { $this->informe['model'] = 'factura proveedor'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'factura proveedor': $factura = new factura_proveedor(); $facturas = $factura->all($this->informe['offset']); if ($facturas) { foreach ($facturas as $fac) { if ($fac->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'albaran cliente'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$fac->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $fac->codejercicio, 'id' => $fac->codigo, 'url' => $fac->url(), 'fecha' => $fac->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += FS_ITEM_LIMIT; } else { if ($this->informe['all']) { $this->informe['model'] = 'albaran cliente'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'albaran cliente': $albaran = new albaran_cliente(); $albaranes = $albaran->all($this->informe['offset']); if ($albaranes) { foreach ($albaranes as $alb) { if ($alb->codejercicio == $this->informe['ejercicio']) { if ($this->informe['all']) { $this->informe['model'] = 'albaran proveedor'; } else { $this->informe['model'] = 'fin'; } $this->informe['offset'] = 0; break; } else { if (!$alb->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $alb->codejercicio, 'id' => $alb->codigo, 'url' => $alb->url(), 'fecha' => $alb->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += FS_ITEM_LIMIT; } else { if ($this->informe['all']) { $this->informe['model'] = 'albaran proveedor'; $this->informe['offset'] = 0; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } } break; case 'albaran proveedor': $albaran = new albaran_proveedor(); $albaranes = $albaran->all($this->informe['offset']); if ($albaranes) { foreach ($albaranes as $alb) { if ($alb->codejercicio == $this->informe['ejercicio']) { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; break; } else { if (!$alb->full_test($this->informe['duplicados'])) { $last_errores[] = array('error' => 'Fallo en full_test()', 'model' => $this->informe['model'], 'ejercicio' => $alb->codejercicio, 'id' => $alb->codigo, 'url' => $alb->url(), 'fecha' => $alb->fecha, 'fix' => FALSE); } } } $this->informe['offset'] += FS_ITEM_LIMIT; } else { $this->informe['model'] = 'dirclientes'; $this->informe['offset'] = 0; } break; case 'dirclientes': $dircli0 = new direccion_cliente(); $direcciones = $dircli0->all($this->informe['offset']); if ($direcciones) { foreach ($direcciones as $dir) { /// simplemente guardamos para que se eliminen espacios de ciudades, provincias, etc... $dir->save(); } $this->informe['offset'] += FS_ITEM_LIMIT; } else { $this->informe['model'] = 'fin'; $this->informe['offset'] = 0; } break; case 'fin': break; } return $last_errores; }
public function delete() { if ($this->db->exec("DELETE FROM " . $this->table_name . " WHERE idalbaran = " . $this->var2str($this->idalbaran) . ";")) { if ($this->idfactura) { /** * Delegamos la eliminación de la factura en la clase correspondiente, * que tendrá que hacer más cosas. */ $factura = new factura_cliente(); $factura0 = $factura->get($this->idfactura); if ($factura0) { $factura0->delete(); } } return TRUE; } else { return FALSE; } }
/** * Genera el asiento contable para una factura de venta. * Devuelve TRUE si el asiento se ha generado correctamente, False en caso contrario. * Si genera el asiento, este es accesible desde $this->asiento. * @param factura_cliente $factura */ public function generar_asiento_venta(&$factura) { $ok = FALSE; $this->asiento = FALSE; $cliente0 = new cliente(); $subcuenta_cli = FALSE; /// obtenemos las tasas de conversión, para las ocasiones en que la factura está en otra divisa $tasaconv = 1; $tasaconv2 = $factura->tasaconv; if ($factura->coddivisa != $this->empresa->coddivisa) { $divisa = $this->divisa->get($this->empresa->coddivisa); if ($divisa) { $tasaconv = $divisa->tasaconv / $factura->tasaconv; $tasaconv2 = $divisa->tasaconv_compra; } } /// obtenemos el clientes y su subcuenta $cliente = $cliente0->get($factura->codcliente); if ($cliente) { $subcuenta_cli = $cliente->get_subcuenta($factura->codejercicio); } if (!$subcuenta_cli) { $eje0 = $this->ejercicio->get($factura->codejercicio); $this->new_message("No se ha podido generar una subcuenta para el cliente\n <a href='" . $eje0->url() . "'>¿Has importado los datos del ejercicio?</a>"); if (!$this->soloasiento) { $this->new_message("Aun así la <a href='" . $factura->url() . "'>factura</a> se ha generado correctamente,\n pero sin asiento contable."); } } else { $asiento = new asiento(); $asiento->codejercicio = $factura->codejercicio; if ($factura->idfacturarect) { $asiento->concepto = ucfirst(FS_FACTURA_RECTIFICATIVA) . " de " . $factura->codigo . " (ventas) - " . $factura->nombrecliente; } else { $asiento->concepto = "Factura de venta " . $factura->codigo . " - " . $factura->nombrecliente; } $asiento->documento = $factura->codigo; $asiento->editable = FALSE; $asiento->fecha = $factura->fecha; $asiento->importe = abs($factura->total * $tasaconv); $asiento->tipodocumento = 'Factura de cliente'; if ($asiento->save()) { $asiento_correcto = TRUE; $partida0 = new partida(); $partida0->idasiento = $asiento->idasiento; $partida0->concepto = $asiento->concepto; $partida0->idsubcuenta = $subcuenta_cli->idsubcuenta; $partida0->codsubcuenta = $subcuenta_cli->codsubcuenta; $partida0->debe = $factura->total * $tasaconv; $partida0->coddivisa = $this->empresa->coddivisa; $partida0->tasaconv = $tasaconv2; $partida0->codserie = $factura->codserie; if (!$partida0->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta " . $partida0->codsubcuenta . "!"); } /// generamos una partida por cada impuesto foreach ($factura->get_lineas_iva() as $li) { $subcuenta_iva = FALSE; /// ¿El impuesto tiene una subcuenta específica? if (isset($this->impuestos[$li->codimpuesto])) { if ($this->impuestos[$li->codimpuesto]->codsubcuentarep) { $subcuenta_iva = $this->subcuenta->get_by_codigo($this->impuestos[$li->codimpuesto]->codsubcuentarep, $asiento->codejercicio); } } if (!$subcuenta_iva) { $subcuenta_iva = $this->subcuenta->get_cuentaesp('IVAREP', $asiento->codejercicio); } if ($li->totaliva == 0 and $li->totalrecargo == 0) { /// no hacemos nada si no hay IVA ni RE } else { if ($subcuenta_iva and $asiento_correcto) { $partida1 = new partida(); $partida1->idasiento = $asiento->idasiento; $partida1->concepto = $asiento->concepto; $partida1->idsubcuenta = $subcuenta_iva->idsubcuenta; $partida1->codsubcuenta = $subcuenta_iva->codsubcuenta; $partida1->haber = $li->totaliva * $tasaconv; $partida1->idcontrapartida = $subcuenta_cli->idsubcuenta; $partida1->codcontrapartida = $subcuenta_cli->codsubcuenta; $partida1->cifnif = $cliente->cifnif; $partida1->documento = $asiento->documento; $partida1->tipodocumento = $asiento->tipodocumento; $partida1->codserie = $factura->codserie; $partida1->factura = $factura->numero; $partida1->baseimponible = $li->neto * $tasaconv; $partida1->iva = $li->iva; $partida1->coddivisa = $this->empresa->coddivisa; $partida1->tasaconv = $tasaconv2; if (!$partida1->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta " . $partida1->codsubcuenta . "!"); } if ($li->recargo != 0) { $partida11 = new partida(); $partida11->idasiento = $asiento->idasiento; $partida11->concepto = $asiento->concepto; $partida11->idsubcuenta = $subcuenta_iva->idsubcuenta; $partida11->codsubcuenta = $subcuenta_iva->codsubcuenta; $partida11->haber = $li->totalrecargo * $tasaconv; $partida11->idcontrapartida = $subcuenta_cli->idsubcuenta; $partida11->codcontrapartida = $subcuenta_cli->codsubcuenta; $partida11->cifnif = $cliente->cifnif; $partida11->documento = $asiento->documento; $partida11->tipodocumento = $asiento->tipodocumento; $partida11->codserie = $factura->codserie; $partida11->factura = $factura->numero; $partida11->baseimponible = $li->neto * $tasaconv; $partida11->recargo = $li->recargo; $partida11->coddivisa = $this->empresa->coddivisa; $partida11->tasaconv = $tasaconv2; if (!$partida11->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta " . $partida11->codsubcuenta . "!"); } } } else { if (!$subcuenta_iva) { $asiento_correcto = FALSE; $this->new_error_msg('No se encuentra la subcuenta de ' . FS_IVA); } } } } $subcuenta_ventas = $this->subcuenta->get_cuentaesp('VENTAS', $asiento->codejercicio); if ($subcuenta_ventas and $asiento_correcto) { $partida2 = new partida(); $partida2->idasiento = $asiento->idasiento; $partida2->concepto = $asiento->concepto; $partida2->idsubcuenta = $subcuenta_ventas->idsubcuenta; $partida2->codsubcuenta = $subcuenta_ventas->codsubcuenta; $partida2->haber = $factura->neto * $tasaconv; $partida2->coddivisa = $this->empresa->coddivisa; $partida2->tasaconv = $tasaconv2; $partida2->codserie = $factura->codserie; if (!$partida2->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta " . $partida2->codsubcuenta . "!"); } } else { if (!$subcuenta_ventas) { $asiento_correcto = FALSE; $this->new_error_msg('No se encuentra la subcuenta de ventas'); } } /// ¿IRPF? if ($factura->totalirpf != 0 and $asiento_correcto) { $subcuenta_irpf = $this->subcuenta->get_cuentaesp('IRPF', $asiento->codejercicio); if (!$subcuenta_irpf) { $subcuenta_irpf = $this->subcuenta->get_by_codigo('4730000000', $asiento->codejercicio); } if ($subcuenta_irpf) { $partida3 = new partida(); $partida3->idasiento = $asiento->idasiento; $partida3->concepto = $asiento->concepto; $partida3->idsubcuenta = $subcuenta_irpf->idsubcuenta; $partida3->codsubcuenta = $subcuenta_irpf->codsubcuenta; $partida3->debe = $factura->totalirpf * $tasaconv; $partida3->coddivisa = $this->empresa->coddivisa; $partida3->tasaconv = $tasaconv2; $partida3->codserie = $factura->codserie; if (!$partida3->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta " . $partida3->codsubcuenta . "!"); } } else { if (!$subcuenta_irpf) { $asiento_correcto = FALSE; $this->new_error_msg('No se encuentra la subcuenta de ' . FS_IRPF); } } } /// comprobamos si algún artículo tiene una subcuenta asociada if ($asiento_correcto) { $partidaA = new partida(); $partidaA->idasiento = $asiento->idasiento; $partidaA->concepto = $asiento->concepto; $partidaA->coddivisa = $this->empresa->coddivisa; $partidaA->tasaconv = $tasaconv2; /// importe a restar a la partida2 $restar = 0; /** * Para cada artículo de la factura, buscamos su subcuenta de compra o compra con irpf */ $ap = new articulo_propiedad(); foreach ($factura->get_lineas() as $lin) { $subcart = FALSE; $aprops = $ap->array_get($lin->referencia); if (isset($aprops['codsubcuentaventa'])) { $subcart = $this->subcuenta->get_by_codigo($aprops['codsubcuentaventa'], $factura->codejercicio); } if (!$subcart) { /// no hay / no se encuentra ninguna subcuenta asignada al artículo } else { if ($subcart->idsubcuenta != $subcuenta_ventas->idsubcuenta) { if (is_null($partidaA->idsubcuenta)) { $partidaA->idsubcuenta = $subcart->idsubcuenta; $partidaA->codsubcuenta = $subcart->codsubcuenta; $partidaA->haber = $lin->pvptotal * $tasaconv; } else { if ($partidaA->idsubcuenta == $subcart->idsubcuenta) { $partidaA->haber += $lin->pvptotal * $tasaconv; } else { $partidaA->haber = round($partidaA->haber, FS_NF0); $restar += $partidaA->haber; if (!$partidaA->save()) { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta del artículo " . $lin->referencia . "!"); } $partidaA = new partida(); $partidaA->idasiento = $asiento->idasiento; $partidaA->concepto = $asiento->concepto; $partidaA->idsubcuenta = $subcart->idsubcuenta; $partidaA->codsubcuenta = $subcart->codsubcuenta; $partidaA->haber = $lin->pvptotal * $tasaconv; $partidaA->coddivisa = $this->empresa->coddivisa; $partidaA->tasaconv = $tasaconv2; } } } } } if ($partidaA->idsubcuenta and $partidaA->codsubcuenta) { $partidaA->haber = round($partidaA->haber, FS_NF0); $restar += $partidaA->haber; if ($partidaA->save()) { $partida2->haber -= $restar; $partida2->save(); } else { $asiento_correcto = FALSE; $this->new_error_msg("¡Imposible generar la partida para la subcuenta del artículo " . $lin->referencia . "!"); } } } if ($asiento_correcto) { /// si es una factura rectificativa, invertimos los importes if ($factura->idfacturarect) { $this->invertir_asiento($asiento); } $factura->idasiento = $asiento->idasiento; if ($factura->pagada) { $factura->idasientop = $this->generar_asiento_pago($asiento, $factura->codpago, $factura->fecha); } if ($factura->save()) { $ok = $this->check_asiento($asiento); if (!$ok) { $this->new_error_msg('El asiento está descuadrado.'); } $this->asiento = $asiento; } else { $this->new_error_msg("¡Imposible añadir el asiento a la factura!"); } } else { if ($asiento->delete()) { $this->new_message("El asiento se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar el asiento!"); } } } else { $this->new_error_msg("¡Imposible guardar el asiento!"); } } return $ok; }
protected function private_core() { $this->desde = Date('01-m-Y'); if (isset($_POST['desde'])) { $this->desde = $_POST['desde']; } $this->hasta = Date('d-m-Y'); if (isset($_POST['hasta'])) { $this->hasta = $_POST['hasta']; } $this->cliente = FALSE; $this->codcliente = FALSE; if (!isset($_POST['todos']) and isset($_POST['codcliente'])) { $this->codcliente = $_POST['codcliente']; $cli0 = new cliente(); $this->cliente = $cli0->get($this->codcliente); } $this->serie = new serie(); $this->codserie = FALSE; if (isset($_POST['codserie'])) { $this->codserie = $_POST['codserie']; } if (isset($_REQUEST['buscar_cliente'])) { $this->buscar_cliente(); } else { if (in_array('tesoreria', $GLOBALS['plugins'])) { $this->new_error_msg('Si usas el <b>plugin Tesorería</b> no puedes usar este' . ' asistente para pagar todas las facturas.'); } else { if (isset($_POST['idfactura'])) { /// ¿Marcamos ya las facturas? $num = 0; $asi0 = new asiento(); $asifac = new asiento_factura(); $fact0 = new factura_cliente(); foreach ($_POST['idfactura'] as $id) { $factura = $fact0->get($id); if ($factura) { $asiento = $asi0->get($factura->idasiento); if ($asiento) { $factura->idasientop = $asifac->generar_asiento_pago($asiento, $factura->codpago); if ($factura->idasientop) { $factura->pagada = TRUE; if ($factura->save()) { $num++; } } } else { $factura->pagada = TRUE; if ($factura->save()) { $num++; } } } } foreach ($asifac->errors as $err) { $this->new_error_msg($err); } $this->new_message($num . ' facturas marcadas como pagadas, estas son las siguientes.'); } else { $this->share_extensions(); } } } $this->resultados = FALSE; if (isset($_POST['desde'])) { $this->resultados = $this->buscar_facturas(); } }
/** * A partir de una factura añade un ticket regalo a la cola de impresión de este terminal. * @param \factura_cliente $factura * @param \empresa $empresa */ public function imprimir_ticket_regalo(&$factura, &$empresa, $imprimir_descripciones = TRUE, $imprimir_observaciones = FALSE) { $medio = $this->anchopapel / 2.5; $this->add_linea_big($this->center_text($this->sanitize($empresa->nombre), $medio) . "\n"); if ($empresa->lema != '') { $this->add_linea($this->center_text($this->sanitize($empresa->lema)) . "\n\n"); } else { $this->add_linea("\n"); } $this->add_linea($this->center_text($this->sanitize($empresa->direccion) . " - " . $this->sanitize($empresa->ciudad)) . "\n"); $this->add_linea($this->center_text(FS_CIFNIF . ": " . $empresa->cifnif)); $this->add_linea("\n\n"); if ($empresa->horario != '') { $this->add_linea($this->center_text($this->sanitize($empresa->horario)) . "\n\n"); } $linea = "\n" . ucfirst(FS_FACTURA_SIMPLIFICADA) . ": " . $factura->codigo . "\n"; $linea .= "\n" . $factura->tipo_comprobante . "\n"; $linea .= "NCF: " . $factura->numero2 . "\n"; $linea .= $factura->fecha . " " . Date('H:i', strtotime($factura->hora)) . "\n"; $this->add_linea($linea); $this->add_linea("Cliente: " . $this->sanitize($factura->nombrecliente) . "\n"); $this->add_linea("Empleado: " . $factura->codagente . "\n\n"); if ($imprimir_observaciones) { $this->add_linea('Observaciones: ' . $this->sanitize($factura->observaciones) . "\n\n"); } $width = $this->anchopapel - 15; $this->add_linea(sprintf("%3s", "Ud.") . " " . sprintf("%-" . $width . "s", "Articulo") . " " . sprintf("%10s", "TOTAL") . "\n"); $this->add_linea(sprintf("%3s", "---") . " " . sprintf("%-" . $width . "s", substr("--------------------------------------------------------", 0, $width - 1)) . " " . sprintf("%10s", "----------") . "\n"); foreach ($factura->get_lineas() as $col) { if ($imprimir_descripciones) { $linea = sprintf("%3s", $col->cantidad) . " " . sprintf("%-" . $width . "s", substr($this->sanitize($col->descripcion), 0, $width - 1)) . " " . sprintf("%10s", '-') . "\n"; } else { $linea = sprintf("%3s", $col->cantidad) . " " . sprintf("%-" . $width . "s", $this->sanitize($col->referencia)) . " " . sprintf("%10s", '-') . "\n"; } $this->add_linea($linea); } $lineaiguales = ''; for ($i = 0; $i < $this->anchopapel; $i++) { $lineaiguales .= '='; } $this->add_linea($lineaiguales); $this->add_linea($this->center_text('TICKET REGALO')); $lineaiguales .= "\n\n\n\n\n\n\n\n"; $this->add_linea($lineaiguales); $this->cortar_papel(); }
/** * * @param factura_cliente $factura */ public function sync_factura_cli(&$factura) { if ($factura) { $recibos = $this->recibo_cliente->all_from_factura($factura->idfactura); if ($factura->pagada and count($recibos) == 0) { /// no hacemos nada } else { if (count($recibos) == 0) { $formap = $this->forma_pago->get($factura->codpago); if ($formap) { $plazos = $this->plazo_pago->all_from($formap->codpago); if ($plazos) { $pendiente = $factura->total; foreach ($plazos as $i => $pla) { $recibo = new recibo_cliente(); $recibo->cifnif = $factura->cifnif; $recibo->coddivisa = $factura->coddivisa; $recibo->tasaconv = $factura->tasaconv; $recibo->codpago = $factura->codpago; $recibo->codserie = $factura->codserie; $recibo->codcliente = $factura->codcliente; $recibo->nombrecliente = $factura->nombrecliente; $recibo->estado = 'Emitido'; $recibo->fecha = $factura->fecha; $recibo->fechav = Date('d-m-Y', strtotime($factura->fecha . ' +' . $pla->dias . 'days')); $recibo->idfactura = $factura->idfactura; if ($i + 1 == count($plazos)) { $recibo->importe = round($pendiente, FS_NF0); } else { $recibo->importe = round($factura->total * $pla->aplazado / 100, FS_NF0); $pendiente -= $recibo->importe; } $recibo->numero = $recibo->new_numero($recibo->idfactura); $recibo->codigo = $factura->codigo . '-' . sprintf('%02s', $recibo->numero); foreach ($this->cbc->all_from_cliente($factura->codcliente) as $cuenta) { if (is_null($recibo->codcuenta) or $cuenta->principal) { $recibo->codcuenta = $cuenta->codcuenta; $recibo->iban = $cuenta->iban; $recibo->swift = $cuenta->swift; } } if ($recibo->save()) { $recibos[] = $recibo; } else { $this->new_error_msg('Error al generar el recibo.'); } } } else { $recibo = new recibo_cliente(); $recibo->cifnif = $factura->cifnif; $recibo->coddivisa = $factura->coddivisa; $recibo->tasaconv = $factura->tasaconv; $recibo->codpago = $factura->codpago; $recibo->codserie = $factura->codserie; $recibo->codcliente = $factura->codcliente; $recibo->nombrecliente = $factura->nombrecliente; $recibo->estado = 'Emitido'; $recibo->fecha = $factura->fecha; $recibo->fechav = Date('d-m-Y', strtotime($factura->fecha . ' ' . $formap->vencimiento)); $recibo->idfactura = $factura->idfactura; $recibo->importe = $factura->total; $recibo->numero = $recibo->new_numero($recibo->idfactura); $recibo->codigo = $factura->codigo . '-' . sprintf('%02s', $recibo->numero); foreach ($this->cbc->all_from_cliente($factura->codcliente) as $cuenta) { if (is_null($recibo->codcuenta) or $cuenta->principal) { $recibo->codcuenta = $cuenta->codcuenta; $recibo->iban = $cuenta->iban; $recibo->swift = $cuenta->swift; } } if ($recibo->save()) { $recibos[] = $recibo; } else { $this->new_error_msg('Error al generar el recibo.'); } } } } else { $pagado = 0; foreach ($recibos as $res) { if ($res->estado == 'Pagado') { $pagado += $res->importe; } } $factura->pagada = $pagado >= $factura->total; $factura->save(); } } return $recibos; } else { return array(); } }
public function extornar_factura() { $value_factura = \filter_input(INPUT_GET, 'factura'); $lista_facturas = explode(',', $value_factura); $fact0 = new factura_cliente(); foreach ($lista_facturas as $factura) { $datos_factura = explode('-', $value_factura); $factura = $fact0->get($datos_factura[0]); $num = 0; if ($factura) { $factura->pagada = FALSE; $factura->save(); $num++; } } $data['success'] = TRUE; $data['facturas_procesadas'] = $num; $this->template = false; header('Content-Type: application/json'); echo json_encode($data); }
private function nuevo_recibo() { $factura = new factura_cliente(); $this->factura = $factura->get($_POST['idfactura']); if ($this->factura) { $recibo = new recibo_cliente(); $recibo->apartado = $this->factura->apartado; $recibo->cifnif = $this->factura->cifnif; $recibo->ciudad = $this->factura->ciudad; $recibo->codcliente = $this->factura->codcliente; $recibo->coddir = $this->factura->coddir; $recibo->coddivisa = $this->factura->coddivisa; $recibo->tasaconv = $this->factura->tasaconv; $recibo->codpago = $this->factura->codpago; $recibo->codserie = $this->factura->codserie; $recibo->numero = $recibo->new_numero($_POST['idfactura']); $recibo->codigo = $this->factura->codigo . '-' . sprintf('%02s', $recibo->numero); $recibo->codpais = $this->factura->codpais; $recibo->codpostal = $this->factura->codpostal; $recibo->direccion = $this->factura->direccion; $recibo->estado = 'Emitido'; $recibo->fecha = $_POST['fecha']; $recibo->fechav = $_POST['fechav']; $recibo->idfactura = $_POST['idfactura']; $recibo->importe = floatval($_POST['importe']); $recibo->nombrecliente = $this->factura->nombrecliente; $recibo->provincia = $this->factura->provincia; $cbc = new cuenta_banco_cliente(); foreach ($cbc->all_from_cliente($this->factura->codcliente) as $cuenta) { if (is_null($recibo->codcuenta) or $cuenta->principal) { $recibo->codcuenta = $cuenta->codcuenta; $recibo->iban = $cuenta->iban; $recibo->swift = $cuenta->swift; } } if ($recibo->save()) { $this->new_message('Recibo creado correctamente.'); header('Location: ' . $recibo->url()); } else { $this->new_error_msg('Error al guardar el recibo.'); } } else { $this->new_error_msg('Factura no encontrada.'); } }
public function facturas_from_albaran($id) { $facturalist = array(); $lineas = $this->db->select("SELECT DISTINCT idfactura FROM " . $this->table_name . " WHERE idalbaran = " . $this->var2str($id) . ";"); if ($lineas) { $factura = new factura_cliente(); foreach ($lineas as $l) { $facturalist[] = $factura->get($l['idfactura']); } } return $facturalist; }
private function enviar_email($doc, $tipo = 'ncf') { $factura = new factura_cliente(); $factura_enviar = $factura->get($doc); if ($this->empresa->can_send_mail()) { if ($_POST['email'] != $this->cliente->email and isset($_POST['guardar'])) { $this->cliente->email = $_POST['email']; $this->cliente->save(); } $filename = 'factura_' . $factura_enviar->numero2 . '.pdf'; if ($tipo == 'ncf') { $this->procesar_facturas(array($factura_enviar->idfactura), $filename); } if (file_exists('tmp/' . FS_TMP_NAME . 'enviar/' . $filename)) { $mail = $this->empresa->new_mail(); $mail->FromName = $this->user->get_agente_fullname(); $mail->addReplyTo($_POST['de'], $mail->FromName); $mail->addAddress($_POST['email'], $this->cliente->razonsocial); if ($_POST['email_copia']) { if (isset($_POST['cco'])) { $mail->addBCC($_POST['email_copia'], $this->cliente->razonsocial); } else { $mail->addCC($_POST['email_copia'], $this->cliente->razonsocial); } } $mail->Subject = $this->empresa->nombre . ': Su factura ' . $this->factura->codigo; $mail->AltBody = $_POST['mensaje']; $mail->msgHTML(nl2br($_POST['mensaje'])); $mail->isHTML(TRUE); $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); if (is_uploaded_file($_FILES['adjunto']['tmp_name'])) { $mail->addAttachment($_FILES['adjunto']['tmp_name'], $_FILES['adjunto']['name']); } if ($mail->smtpConnect($this->empresa->smtp_options())) { if ($mail->send()) { $this->template = 'ventas_imprimir'; $this->new_message('Mensaje enviado correctamente.'); /// nos guardamos la fecha de envío $factura_enviar->femail = $this->today(); $factura_enviar->save(); $this->empresa->save_mail($mail); } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); } else { $this->new_error_msg('Imposible generar el PDF.'); } } }
protected function private_core() { $this->share_extensions(); $this->documento = FALSE; $this->editable = FALSE; if (isset($_REQUEST['albaran'])) { $alb0 = new albaran_cliente(); $this->documento = $alb0->get($_REQUEST['id']); if ($this->documento) { $this->titulo = FS_ALBARAN . ' ' . $this->documento->codigo; $this->lineas = $this->documento->get_lineas(); $this->editable = $this->documento->ptefactura; if (isset($_POST['idlinea'])) { if ($this->editable) { $orden = 1 + count($_POST['idlinea']); foreach ($_POST['idlinea'] as $idl) { foreach ($this->lineas as $lin) { if ($lin->idlinea == $idl) { $lin->orden = $orden; $lin->save(); break; } } $orden--; } $this->new_message('Datos guardados correctamente.'); $this->lineas = $this->documento->get_lineas(); } else { $this->new_error_msg('El documento ya no es editable.'); } } } } else { if (isset($_REQUEST['factura'])) { $fact0 = new factura_cliente(); $this->documento = $fact0->get($_REQUEST['id']); if ($this->documento) { $this->titulo = 'Factura ' . $this->documento->codigo; $this->lineas = $this->documento->get_lineas(); $this->editable = TRUE; if (isset($_POST['idlinea'])) { if ($this->editable) { $orden = 1 + count($_POST['idlinea']); foreach ($_POST['idlinea'] as $idl) { foreach ($this->lineas as $lin) { if ($lin->idlinea == $idl) { $lin->orden = $orden; $lin->save(); break; } } $orden--; } $this->new_message('Datos guardados correctamente.'); $this->lineas = $this->documento->get_lineas(); } else { $this->new_error_msg('El documento ya no es editable.'); } } } } } }
protected function process() { $this->ppage = $this->page->get('ventas_facturas'); $this->ejercicio = new ejercicio(); $this->agente = FALSE; $this->cliente = FALSE; $factura = new factura_cliente(); $this->factura = FALSE; /// desactivamos la barra de botones $this->show_fs_toolbar = FALSE; if (isset($_POST['idfactura'])) { $this->factura = $factura->get($_POST['idfactura']); $this->factura->observaciones = $_POST['observaciones']; $this->factura->numero2 = $_POST['numero2']; $this->cambiar_numero_factura(); /// obtenemos el ejercicio para poder acotar la fecha $eje0 = $this->ejercicio->get($this->factura->codejercicio); if ($eje0) { $this->factura->fecha = $eje0->get_best_fecha($_POST['fecha'], TRUE); } else { $this->new_error_msg('No se encuentra el ejercicio asociado a la factura.'); } if ($this->factura->save()) { $asiento = $this->factura->get_asiento(); if ($asiento) { $asiento->fecha = $_POST['fecha']; if (!$asiento->save()) { $this->new_error_msg("Imposible modificar la fecha del asiento."); } } $this->new_message("Factura modificada correctamente."); $this->new_change('Factura Cliente ' . $this->factura->codigo, $this->factura->url()); } else { $this->new_error_msg("¡Imposible modificar la factura!"); } } else { if (isset($_GET['id'])) { $this->factura = $factura->get($_GET['id']); } } if ($this->factura) { $this->page->title = $this->factura->codigo; /// cargamos el agente if (!is_null($this->factura->codagente)) { $agente = new agente(); $this->agente = $agente->get($this->factura->codagente); } /// cargamos el cliente $cliente = new cliente(); $this->cliente = $cliente->get($this->factura->codcliente); if (isset($_GET['gen_asiento']) and isset($_GET['petid'])) { if ($this->duplicated_petition($_GET['petid'])) { $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); } else { $this->generar_asiento(); } } else { if (isset($_GET['updatedir'])) { $this->actualizar_direccion(); } else { if (isset($_REQUEST['pagada'])) { $this->factura->pagada = $_REQUEST['pagada'] == 'TRUE'; if ($this->factura->save()) { $this->new_message("Factura modificada correctamente."); } else { $this->new_error_msg("¡Imposible modificar la factura!"); } } } } /// comprobamos la factura $this->factura->full_test(); } else { $this->new_error_msg("¡Factura de cliente no encontrada!"); } }
private function reimprimir_ticket() { $factura = new factura_cliente(); $fac0 = FALSE; if ($_GET['reticket'] == '') { foreach ($factura->all() as $fac) { $fac0 = $fac; break; } } else { $fac0 = $factura->get_by_codigo($_GET['reticket']); } if ($fac0) { $this->imprimir_ticket($fac0, 1, FALSE); } else { $this->new_error_msg("Ticket no encontrado."); } }
private function generar_factura() { /* * Verificación de disponibilidad del Número de NCF para República Dominicana */ //Obtenemos el tipo de comprobante a generar para el cliente $tipo_comprobante_d = $this->ncf_entidad_tipo->get($this->empresa->id, $this->albaran->codcliente, 'CLI'); $tipo_comprobante = $tipo_comprobante_d->tipo_comprobante; if (strlen($this->albaran->cifnif) < 9 and $tipo_comprobante == '01') { return $this->new_error_msg('El cliente tiene un tipo de comprobante 01 pero no tiene Cédula o RNC Válido, por favor corrija esta información!'); } //Con el codigo del almacen desde donde facturaremos generamos el número de NCF $numero_ncf = $this->ncf_rango->generate($this->empresa->id, $this->albaran->codalmacen, $tipo_comprobante, $this->albaran->codpago); if ($numero_ncf['NCF'] == 'NO_DISPONIBLE') { return $this->new_error_msg('No hay números NCF disponibles del tipo ' . $tipo_comprobante . ', el ' . FS_ALBARAN . ' no será facturado.'); } $factura = new factura_cliente(); $factura->apartado = $this->albaran->apartado; $factura->cifnif = $this->albaran->cifnif; $factura->ciudad = $this->albaran->ciudad; $factura->codagente = $this->albaran->codagente; $factura->codalmacen = $this->albaran->codalmacen; $factura->codcliente = $this->albaran->codcliente; $factura->coddir = $this->albaran->coddir; $factura->coddivisa = $this->albaran->coddivisa; $factura->tasaconv = $this->albaran->tasaconv; $factura->codpago = $this->albaran->codpago; $factura->codpais = $this->albaran->codpais; $factura->codpostal = $this->albaran->codpostal; $factura->codserie = $this->albaran->codserie; $factura->direccion = $this->albaran->direccion; $factura->neto = $this->albaran->neto; $factura->nombrecliente = $this->albaran->nombrecliente; $factura->observaciones = $this->albaran->observaciones; $factura->provincia = $this->albaran->provincia; $factura->envio_apartado = $this->albaran->envio_apartado; $factura->envio_apellidos = $this->albaran->envio_apellidos; $factura->envio_ciudad = $this->albaran->envio_ciudad; $factura->envio_codigo = $this->albaran->envio_codigo; $factura->envio_codpais = $this->albaran->envio_codpais; $factura->envio_codpostal = $this->albaran->envio_codpostal; $factura->envio_codtrans = $this->albaran->envio_codtrans; $factura->envio_direccion = $this->albaran->envio_direccion; $factura->envio_nombre = $this->albaran->envio_nombre; $factura->envio_provincia = $this->albaran->envio_provincia; $factura->total = $this->albaran->total; $factura->totaliva = $this->albaran->totaliva; $factura->numero2 = $numero_ncf['NCF']; $factura->irpf = $this->albaran->irpf; $factura->totalirpf = $this->albaran->totalirpf; $factura->totalrecargo = $this->albaran->totalrecargo; $factura->porcomision = $this->albaran->porcomision; if (is_null($factura->codagente)) { $factura->codagente = $this->user->codagente; } /// asignamos el ejercicio que corresponde a la fecha elegida $eje0 = $this->ejercicio->get_by_fecha($_REQUEST['facturar']); if ($eje0) { $factura->codejercicio = $eje0->codejercicio; $factura->set_fecha_hora($_REQUEST['facturar'], $factura->hora); } /// comprobamos la forma de pago para saber si hay que marcar la factura como pagada $forma0 = new forma_pago(); $formapago = $forma0->get($factura->codpago); if ($formapago) { if ($formapago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } $factura->vencimiento = Date('d-m-Y', strtotime($factura->fecha . ' ' . $formapago->vencimiento)); } $regularizacion = new regularizacion_iva(); if (!$eje0) { $this->new_error_msg("Ejercicio no encontrado o está cerrado."); } else { if (!$eje0->abierto()) { $this->new_error_msg("El ejercicio está cerrado."); } else { if ($regularizacion->get_fecha_inside($factura->fecha)) { $this->new_error_msg("El " . FS_IVA . " de ese periodo ya ha sido regularizado. No se pueden añadir más facturas en esa fecha."); } else { if ($factura->save()) { $continuar = TRUE; $ncf_controller = new helper_ncf(); $ncf_controller->guardar_ncf($this->empresa->id, $factura, $tipo_comprobante, $numero_ncf); foreach ($this->albaran->get_lineas() as $l) { $n = new linea_factura_cliente(); $n->idalbaran = $l->idalbaran; $n->idfactura = $factura->idfactura; $n->cantidad = $l->cantidad; $n->codimpuesto = $l->codimpuesto; $n->descripcion = $l->descripcion; $n->dtopor = $l->dtopor; $n->irpf = $l->irpf; $n->iva = $l->iva; $n->pvpsindto = $l->pvpsindto; $n->pvptotal = $l->pvptotal; $n->pvpunitario = $l->pvpunitario; $n->recargo = $l->recargo; $n->referencia = $l->referencia; $n->orden = $l->orden; $n->mostrar_cantidad = $l->mostrar_cantidad; $n->mostrar_precio = $l->mostrar_precio; if (!$n->save()) { $continuar = FALSE; $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! "); break; } } if ($continuar) { $this->albaran->idfactura = $factura->idfactura; $this->albaran->ptefactura = FALSE; if ($this->albaran->save()) { $this->generar_asiento($factura); } else { $this->new_error_msg("¡Imposible vincular el " . FS_ALBARAN . " con la nueva factura!"); if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { $this->new_error_msg("¡Imposible guardar la factura!"); } } } } }
private function generar_factura($albaranes) { $continuar = TRUE; $factura = new factura_cliente(); $factura->automatica = TRUE; $factura->codalmacen = $albaranes[0]->codalmacen; $factura->coddivisa = $albaranes[0]->coddivisa; $factura->tasaconv = $albaranes[0]->tasaconv; $factura->codejercicio = $albaranes[0]->codejercicio; $factura->codpago = $albaranes[0]->codpago; $factura->codserie = $albaranes[0]->codserie; $factura->editable = FALSE; $factura->irpf = $albaranes[0]->irpf; $factura->numero2 = $albaranes[0]->numero2; $factura->observaciones = $albaranes[0]->observaciones; $factura->recfinanciero = $albaranes[0]->recfinanciero; /// comprobamos la forma de pago para saber si hay que marcar la factura como pagada $formapago = $this->forma_pago->get($factura->codpago); if ($formapago) { if ($formapago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } } /// obtenemos los datos actuales del cliente, por si ha habido cambios $cliente = $this->cliente->get($albaranes[0]->codcliente); if ($cliente) { foreach ($cliente->get_direcciones() as $dir) { if ($dir->domfacturacion) { $factura->apartado = $dir->apartado; $factura->cifnif = $cliente->cifnif; $factura->ciudad = $dir->ciudad; $factura->codcliente = $cliente->codcliente; $factura->coddir = $dir->id; $factura->codpais = $dir->codpais; $factura->codpostal = $dir->codpostal; $factura->direccion = $dir->direccion; $factura->nombrecliente = $cliente->nombrecomercial; $factura->provincia = $dir->provincia; break; } } } /// calculamos neto e iva foreach ($albaranes as $alb) { foreach ($alb->get_lineas() as $l) { $factura->neto += $l->pvptotal; $factura->totaliva += $l->pvptotal * $l->iva / 100; $factura->totalirpf += $l->pvptotal * $l->irpf / 100; $factura->totalrecargo += $l->pvptotal * $l->recargo / 100; } } /// redondeamos $factura->neto = round($factura->neto, FS_NF0); $factura->totaliva = round($factura->totaliva, FS_NF0); $factura->totalirpf = round($factura->totalirpf, FS_NF0); $factura->totalrecargo = round($factura->totalrecargo, FS_NF0); $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; /// asignamos la mejor fecha posible, pero dentro del ejercicio $ejercicio = new ejercicio(); $eje0 = $ejercicio->get($factura->codejercicio); $factura->fecha = $eje0->get_best_fecha($factura->fecha); $regularizacion = new regularizacion_iva(); if (!$eje0->abierto()) { $this->new_error_msg('El ejercicio ' . $eje0->codejercicio . ' está cerrado.'); } else { if ($regularizacion->get_fecha_inside($factura->fecha)) { /* * comprobamos que la fecha de la factura no esté dentro de un periodo de * IVA regularizado. */ $this->new_error_msg('El IVA de ese periodo ya ha sido regularizado. No se pueden añadir más facturas en esa fecha.'); } else { if ($factura->save()) { foreach ($albaranes as $alb) { foreach ($alb->get_lineas() as $l) { $n = new linea_factura_cliente(); $n->idalbaran = $alb->idalbaran; $n->idfactura = $factura->idfactura; $n->cantidad = $l->cantidad; $n->codimpuesto = $l->codimpuesto; $n->descripcion = $l->descripcion; $n->dtolineal = $l->dtolineal; $n->dtopor = $l->dtopor; $n->irpf = $l->irpf; $n->iva = $l->iva; $n->pvpsindto = $l->pvpsindto; $n->pvptotal = $l->pvptotal; $n->pvpunitario = $l->pvpunitario; $n->recargo = $l->recargo; $n->referencia = $l->referencia; if (!$n->save()) { $continuar = FALSE; $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! "); break; } } } if ($continuar) { foreach ($albaranes as $alb) { $alb->idfactura = $factura->idfactura; $alb->ptefactura = FALSE; if (!$alb->save()) { $this->new_error_msg("¡Imposible vincular el " . FS_ALBARAN . " con la nueva factura!"); $continuar = FALSE; break; } } if ($continuar) { $this->generar_asiento($factura); } else { if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { $this->new_error_msg("¡Imposible guardar la factura!"); } } } }
private function generar_factura() { $factura = new factura_cliente(); $factura->apartado = $this->albaran->apartado; $factura->cifnif = $this->albaran->cifnif; $factura->ciudad = $this->albaran->ciudad; $factura->codagente = $this->albaran->codagente; $factura->codalmacen = $this->albaran->codalmacen; $factura->codcliente = $this->albaran->codcliente; $factura->coddir = $this->albaran->coddir; $factura->coddivisa = $this->albaran->coddivisa; $factura->tasaconv = $this->albaran->tasaconv; $factura->codejercicio = $this->albaran->codejercicio; $factura->codpago = $this->albaran->codpago; $factura->codpais = $this->albaran->codpais; $factura->codpostal = $this->albaran->codpostal; $factura->codserie = $this->albaran->codserie; $factura->direccion = $this->albaran->direccion; $factura->neto = $this->albaran->neto; $factura->nombrecliente = $this->albaran->nombrecliente; $factura->observaciones = $this->albaran->observaciones; $factura->provincia = $this->albaran->provincia; $factura->total = $this->albaran->total; $factura->totaliva = $this->albaran->totaliva; $factura->numero2 = $this->albaran->numero2; $factura->irpf = $this->albaran->irpf; $factura->totalirpf = $this->albaran->totalirpf; $factura->totalrecargo = $this->albaran->totalrecargo; $factura->porcomision = $this->albaran->porcomision; /// comprobamos la forma de pago para saber si hay que marcar la factura como pagada $forma0 = new forma_pago(); $formapago = $forma0->get($factura->codpago); if ($formapago) { if ($formapago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } $factura->vencimiento = Date('d-m-Y', strtotime($factura->fecha . ' ' . $formapago->vencimiento)); } /// asignamos la mejor fecha posible, pero dentro del ejercicio $eje0 = $this->ejercicio->get($factura->codejercicio); $factura->fecha = $eje0->get_best_fecha($factura->fecha); $regularizacion = new regularizacion_iva(); if (!$eje0->abierto()) { $this->new_error_msg("El ejercicio está cerrado."); } else { if ($regularizacion->get_fecha_inside($factura->fecha)) { $this->new_error_msg("El IVA de ese periodo ya ha sido regularizado. No se pueden añadir más " . FS_FACTURAS . " en esa fecha."); } else { if ($factura->save()) { $continuar = TRUE; foreach ($this->albaran->get_lineas() as $l) { $n = new linea_factura_cliente(); $n->idalbaran = $l->idalbaran; $n->idfactura = $factura->idfactura; $n->cantidad = $l->cantidad; $n->codimpuesto = $l->codimpuesto; $n->descripcion = $l->descripcion; $n->dtopor = $l->dtopor; $n->irpf = $l->irpf; $n->iva = $l->iva; $n->pvpsindto = $l->pvpsindto; $n->pvptotal = $l->pvptotal; $n->pvpunitario = $l->pvpunitario; $n->recargo = $l->recargo; $n->referencia = $l->referencia; if (!$n->save()) { $continuar = FALSE; $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! "); break; } } if ($continuar) { $this->albaran->idfactura = $factura->idfactura; $this->albaran->ptefactura = FALSE; if ($this->albaran->save()) { $this->generar_asiento($factura); } else { $this->new_error_msg("¡Imposible vincular el " . FS_ALBARAN . " con la nueva " . FS_FACTURA . "!"); if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la " . FS_FACTURA . "!"); } } } else { if ($factura->delete()) { $this->new_error_msg("La " . FS_FACTURA . " se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la " . FS_FACTURA . "!"); } } } else { $this->new_error_msg("¡Imposible guardar la " . FS_FACTURA . "!"); } } } }
private function nueva_factura_cliente() { $continuar = TRUE; $cliente = $this->cliente->get($_POST['cliente']); if (!$cliente) { $this->new_error_msg('Cliente no encontrado.'); $continuar = FALSE; } $almacen = $this->almacen->get($_POST['almacen']); if ($almacen) { $this->save_codalmacen($_POST['almacen']); } else { $this->new_error_msg('Almacén no encontrado.'); $continuar = FALSE; } $eje0 = new ejercicio(); $ejercicio = $eje0->get_by_fecha($_POST['fecha']); if (!$ejercicio) { $this->new_error_msg('Ejercicio no encontrado.'); $continuar = FALSE; } $serie = $this->serie->get($_POST['serie']); if (!$serie) { $this->new_error_msg('Serie no encontrada.'); $continuar = FALSE; } $forma_pago = $this->forma_pago->get($_POST['forma_pago']); if ($forma_pago) { $this->save_codpago($_POST['forma_pago']); } else { $this->new_error_msg('Forma de pago no encontrada.'); $continuar = FALSE; } $divisa = $this->divisa->get($_POST['divisa']); if (!$divisa) { $this->new_error_msg('Divisa no encontrada.'); $continuar = FALSE; } $factura = new factura_cliente(); if ($this->duplicated_petition($_POST['petition_id'])) { $this->new_error_msg('Petición duplicada. Has hecho doble clic sobre el botón guardar y se han enviado dos peticiones. Mira en <a href="' . $factura->url() . '">Facturas</a> para ver si la factura se ha guardado correctamente.'); $continuar = FALSE; } if ($continuar) { $factura->fecha = $_POST['fecha']; $factura->hora = $_POST['hora']; $factura->codalmacen = $almacen->codalmacen; $factura->codejercicio = $ejercicio->codejercicio; $factura->codserie = $serie->codserie; $factura->codpago = $forma_pago->codpago; $factura->coddivisa = $divisa->coddivisa; $factura->tasaconv = $divisa->tasaconv; if ($_POST['tasaconv'] != '') { $factura->tasaconv = floatval($_POST['tasaconv']); } $factura->codagente = $this->agente->codagente; $factura->observaciones = $_POST['observaciones']; $factura->numero2 = $_POST['numero2']; $factura->irpf = $serie->irpf; $factura->porcomision = $this->agente->porcomision; if ($forma_pago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } $factura->vencimiento = Date('d-m-Y', strtotime($factura->fecha . ' ' . $forma_pago->vencimiento)); $factura->codcliente = $cliente->codcliente; $factura->cifnif = $cliente->cifnif; $factura->nombrecliente = $cliente->razonsocial; $factura->ciudad = $_POST['ciudad']; $factura->codpais = $_POST['codpais']; $factura->codpostal = $_POST['codpostal']; $factura->direccion = $_POST['direccion']; $factura->provincia = $_POST['provincia']; if ($factura->save()) { $art0 = new articulo(); $n = floatval($_POST['numlineas']); for ($i = 0; $i <= $n; $i++) { if (isset($_POST['referencia_' . $i])) { $linea = new linea_factura_cliente(); $linea->idfactura = $factura->idfactura; $linea->descripcion = $_POST['desc_' . $i]; if (!$serie->siniva and $cliente->regimeniva != 'Exento') { $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $i]); if ($imp0) { $linea->codimpuesto = $imp0->codimpuesto; $linea->iva = floatval($_POST['iva_' . $i]); $linea->recargo = floatval($_POST['recargo_' . $i]); } else { $linea->iva = floatval($_POST['iva_' . $i]); $linea->recargo = floatval($_POST['recargo_' . $i]); } } $linea->irpf = floatval($_POST['irpf_' . $i]); $linea->pvpunitario = floatval($_POST['pvp_' . $i]); $linea->cantidad = floatval($_POST['cantidad_' . $i]); $linea->dtopor = floatval($_POST['dto_' . $i]); $linea->pvpsindto = $linea->pvpunitario * $linea->cantidad; $linea->pvptotal = floatval($_POST['neto_' . $i]); $articulo = $art0->get($_POST['referencia_' . $i]); if ($articulo) { $linea->referencia = $articulo->referencia; } if ($linea->save()) { if ($articulo and isset($_POST['stock'])) { /// descontamos del stock $articulo->sum_stock($factura->codalmacen, 0 - $linea->cantidad); } $factura->neto += $linea->pvptotal; $factura->totaliva += $linea->pvptotal * $linea->iva / 100; $factura->totalirpf += $linea->pvptotal * $linea->irpf / 100; $factura->totalrecargo += $linea->pvptotal * $linea->recargo / 100; } else { $this->new_error_msg("¡Imposible guardar la linea con referencia: " . $linea->referencia); $continuar = FALSE; } } } if ($continuar) { /// redondeamos $factura->neto = round($factura->neto, FS_NF0); $factura->totaliva = round($factura->totaliva, FS_NF0); $factura->totalirpf = round($factura->totalirpf, FS_NF0); $factura->totalrecargo = round($factura->totalrecargo, FS_NF0); $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; if (abs(floatval($_POST['atotal']) - $factura->total) >= 0.02) { $this->new_error_msg("El total difiere entre la vista y el controlador (" . $_POST['atotal'] . " frente a " . $factura->total . "). Debes informar del error."); $factura->delete(); } else { if ($factura->save()) { $this->generar_asiento($factura); $this->new_message("<a href='" . $factura->url() . "'>Factura</a> guardada correctamente."); $this->new_change('Factura Cliente ' . $factura->codigo, $factura->url(), TRUE); if ($_POST['redir'] == 'TRUE') { header('Location: ' . $factura->url()); } } else { $this->new_error_msg("¡Imposible actualizar la <a href='" . $factura->url() . "'>Factura</a>!"); } } } else { if ($factura->delete()) { $this->new_message("Factura eliminada correctamente."); } else { $this->new_error_msg("¡Imposible eliminar la <a href='" . $factura->url() . "'>Factura</a>!"); } } } else { $this->new_error_msg("¡Imposible guardar la Factura!"); } } }
private function generar_factura_cliente($albaranes) { $continuar = TRUE; $factura = new factura_cliente(); $factura->codagente = $albaranes[0]->codagente; $factura->codalmacen = $albaranes[0]->codalmacen; $factura->coddivisa = $albaranes[0]->coddivisa; $factura->tasaconv = $albaranes[0]->tasaconv; $factura->codpago = $albaranes[0]->codpago; $factura->codserie = $albaranes[0]->codserie; $factura->irpf = $albaranes[0]->irpf; $factura->numero2 = $albaranes[0]->numero2; $factura->observaciones = $albaranes[0]->observaciones; /// asignamos fecha y ejercicio usando la del albarán if ($_REQUEST['fecha'] == 'albaran') { $eje0 = $this->ejercicio->get($albaranes[0]->codejercicio); if ($eje0) { if ($eje0->abierto()) { $factura->codejercicio = $albaranes[0]->codejercicio; $factura->set_fecha_hora($albaranes[0]->fecha, $albaranes[0]->hora); } } } /** * Si se ha elegido fecha de hoy o no se ha podido usar la del albarán porque * el ejercicio estaba cerrado, asignamos ejercicio para hoy y usamos la mejor * fecha y hora. */ if (is_null($factura->codejercicio)) { $eje0 = $this->ejercicio->get_by_fecha($factura->fecha); if ($eje0) { $factura->codejercicio = $eje0->codejercicio; $factura->set_fecha_hora($factura->fecha, $factura->hora); } } /// obtenemos los datos actuales del cliente, por si ha habido cambios $cliente = $this->cliente->get($albaranes[0]->codcliente); if ($cliente) { foreach ($cliente->get_direcciones() as $dir) { if ($dir->domfacturacion) { $factura->apartado = $dir->apartado; $factura->cifnif = $cliente->cifnif; $factura->ciudad = $dir->ciudad; $factura->codcliente = $cliente->codcliente; $factura->coddir = $dir->id; $factura->codpais = $dir->codpais; $factura->codpostal = $dir->codpostal; $factura->direccion = $dir->direccion; $factura->nombrecliente = $cliente->razonsocial; $factura->provincia = $dir->provincia; break; } } } /// calculamos neto e iva foreach ($albaranes as $alb) { foreach ($alb->get_lineas() as $l) { $factura->neto += $l->pvptotal; $factura->totaliva += $l->pvptotal * $l->iva / 100; $factura->totalirpf += $l->pvptotal * $l->irpf / 100; $factura->totalrecargo += $l->pvptotal * $l->recargo / 100; } } /// redondeamos $factura->neto = round($factura->neto, FS_NF0); $factura->totaliva = round($factura->totaliva, FS_NF0); $factura->totalirpf = round($factura->totalirpf, FS_NF0); $factura->totalrecargo = round($factura->totalrecargo, FS_NF0); $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; /// comprobamos la forma de pago para saber si hay que marcar la factura como pagada $formapago = $this->forma_pago->get($factura->codpago); if ($formapago) { if ($formapago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } $factura->vencimiento = Date('d-m-Y', strtotime($factura->fecha . ' ' . $formapago->vencimiento)); } if (!$eje0) { $this->new_error_msg("Ningún ejercicio encontrado."); } else { if (!$eje0->abierto()) { $this->new_error_msg('El ejercicio ' . $eje0->codejercicio . ' está cerrado.'); } else { if ($this->regularizacion->get_fecha_inside($factura->fecha)) { /* * comprobamos que la fecha de la factura no esté dentro de un periodo de * IVA regularizado. */ $this->new_error_msg('El IVA de ese periodo ya ha sido regularizado. No se pueden añadir más facturas en esa fecha.'); } else { if ($factura->save()) { foreach ($albaranes as $alb) { foreach ($alb->get_lineas() as $l) { $n = new linea_factura_cliente(); $n->idalbaran = $alb->idalbaran; $n->idfactura = $factura->idfactura; $n->cantidad = $l->cantidad; $n->codimpuesto = $l->codimpuesto; $n->descripcion = $l->descripcion; $n->dtopor = $l->dtopor; $n->irpf = $l->irpf; $n->iva = $l->iva; $n->pvpsindto = $l->pvpsindto; $n->pvptotal = $l->pvptotal; $n->pvpunitario = $l->pvpunitario; $n->recargo = $l->recargo; $n->referencia = $l->referencia; if (!$n->save()) { $continuar = FALSE; $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! "); break; } } } if ($continuar) { foreach ($albaranes as $alb) { $alb->idfactura = $factura->idfactura; $alb->ptefactura = FALSE; if (!$alb->save()) { $this->new_error_msg("¡Imposible vincular el " . FS_ALBARAN . " con la nueva factura!"); $continuar = FALSE; break; } } if ($continuar) { $this->generar_asiento_cliente($factura); $this->total++; } else { if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { if ($factura->delete()) { $this->new_error_msg("La factura se ha borrado."); } else { $this->new_error_msg("¡Imposible borrar la factura!"); } } } else { $this->new_error_msg("¡Imposible guardar la factura!"); } } } } }