private function generar_factura() { $factura = new factura_proveedor(); $factura->cifnif = $this->albaran->cifnif; $factura->codalmacen = $this->albaran->codalmacen; $factura->coddivisa = $this->albaran->coddivisa; $factura->tasaconv = $this->albaran->tasaconv; $factura->codpago = $this->albaran->codpago; $factura->codproveedor = $this->albaran->codproveedor; $factura->codserie = $this->albaran->codserie; $factura->irpf = $this->albaran->irpf; $factura->neto = $this->albaran->neto; $factura->nombre = $this->albaran->nombre; $factura->numproveedor = $this->albaran->numproveedor; $factura->observaciones = $this->albaran->observaciones; $factura->total = $this->albaran->total; $factura->totalirpf = $this->albaran->totalirpf; $factura->totaliva = $this->albaran->totaliva; $factura->totalrecargo = $this->albaran->totalrecargo; $factura->codagente = $this->albaran->codagente; /// asignamos el ejercicio que corresponde a la fecha elegida $eje0 = $this->ejercicio->get_by_fecha($_POST['facturar']); if ($eje0) { $factura->codejercicio = $eje0->codejercicio; $factura->set_fecha_hora($_POST['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; } } $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; foreach ($this->albaran->get_lineas() as $l) { $linea = new linea_factura_proveedor(); $linea->cantidad = $l->cantidad; $linea->codimpuesto = $l->codimpuesto; $linea->descripcion = $l->descripcion; $linea->dtopor = $l->dtopor; $linea->idalbaran = $l->idalbaran; $linea->idfactura = $factura->idfactura; $linea->irpf = $l->irpf; $linea->iva = $l->iva; $linea->pvpsindto = $l->pvpsindto; $linea->pvptotal = $l->pvptotal; $linea->pvpunitario = $l->pvpunitario; $linea->recargo = $l->recargo; $linea->referencia = $l->referencia; if (!$linea->save()) { $continuar = FALSE; $this->new_error_msg("¡Imposible guardar la línea el artículo " . $linea->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_proveedor($albaranes) { $continuar = TRUE; $factura = new factura_proveedor(); $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->numproveedor = $albaranes[0]->numproveedor; $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 actualizados del proveedor $proveedor = $this->proveedor->get($albaranes[0]->codproveedor); if ($proveedor) { $factura->cifnif = $proveedor->cifnif; $factura->codproveedor = $proveedor->codproveedor; $factura->nombre = $proveedor->razonsocial; } /// 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; 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_proveedor(); $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_proveedor($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!"); } } } } }
private function nueva_factura_proveedor() { $continuar = TRUE; $proveedor = $this->proveedor->get($_POST['proveedor']); if (!$proveedor) { $this->new_error_msg('Proveedor 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 o está cerrado.'); $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_proveedor(); 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->codejercicio = $ejercicio->codejercicio; $factura->codserie = $serie->codserie; $factura->set_fecha_hora($_POST['fecha'], $_POST['hora']); $factura->codproveedor = $proveedor->codproveedor; $factura->nombre = $_POST['nombre']; $factura->cifnif = $_POST['cifnif']; $factura->codalmacen = $almacen->codalmacen; $factura->codpago = $forma_pago->codpago; $factura->coddivisa = $divisa->coddivisa; $factura->tasaconv = $divisa->tasaconv_compra; if ($_POST['tasaconv'] != '') { $factura->tasaconv = floatval($_POST['tasaconv']); } $factura->codagente = $this->agente->codagente; $factura->numproveedor = $_POST['numproveedor']; $factura->observaciones = $_POST['observaciones']; if ($forma_pago->genrecibos == 'Pagados') { $factura->pagada = TRUE; } $regularizacion = new regularizacion_iva(); 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()) { $art0 = new articulo(); $n = floatval($_POST['numlineas']); for ($i = 0; $i < $n; $i++) { if (isset($_POST['referencia_' . $i])) { $linea = new linea_factura_proveedor(); $linea->idfactura = $factura->idfactura; $linea->descripcion = $_POST['desc_' . $i]; if (!$serie->siniva and $proveedor->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) { if (isset($_POST['costemedio'])) { if ($articulo->costemedio == 0) { $articulo->costemedio = $linea->pvptotal / $linea->cantidad; } else { $articulo->costemedio = $articulo->get_costemedio(); if ($articulo->costemedio == 0) { $articulo->costemedio = $linea->pvptotal / $linea->cantidad; } } $this->actualizar_precio_proveedor($factura->codproveedor, $linea); } if (isset($_POST['stock'])) { $articulo->sum_stock($factura->codalmacen, $linea->cantidad); } else { if (isset($_POST['costemedio'])) { $articulo->save(); } } } $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; if ($linea->irpf > $factura->irpf) { $factura->irpf = $linea->irpf; } } 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 el controlador y la vista (" . $factura->total . " frente a " . $_POST['atotal'] . "). 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 Proveedor ' . $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($albaranes) { $continuar = TRUE; $factura = new factura_proveedor(); $factura->codagente = $this->user->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->numproveedor = $albaranes[0]->numproveedor; $factura->observaciones = $albaranes[0]->observaciones; /// obtenemos los datos actualizados del proveedor $proveedor = $this->proveedor->get($albaranes[0]->codproveedor); if ($proveedor) { $factura->cifnif = $proveedor->cifnif; $factura->codproveedor = $proveedor->codproveedor; $factura->nombre = $proveedor->razonsocial; } /// 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 el ejercicio que corresponde a la fecha elegida $eje0 = $this->ejercicio->get_by_fecha($_POST['fecha']); if ($eje0) { $factura->codejercicio = $eje0->codejercicio; $factura->set_fecha_hora($_POST['fecha'], $factura->hora); } /// 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; } } $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 ' . $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 ' . FS_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_proveedor(); $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($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!"); } } } } }