public function __construct($l = FALSE)
 {
     parent::__construct('lineasalbaranesprov');
     if (!isset(self::$albaranes)) {
         self::$albaranes = array();
     }
     if ($l) {
         $this->idlinea = $this->intval($l['idlinea']);
         $this->idalbaran = $this->intval($l['idalbaran']);
         $this->referencia = $l['referencia'];
         $this->descripcion = $l['descripcion'];
         $this->cantidad = floatval($l['cantidad']);
         $this->dtopor = floatval($l['dtopor']);
         $this->dtolineal = floatval($l['dtolineal']);
         $this->codimpuesto = $l['codimpuesto'];
         $this->iva = floatval($l['iva']);
         $this->pvptotal = floatval($l['pvptotal']);
         $this->pvpsindto = floatval($l['pvpsindto']);
         $this->pvpunitario = floatval($l['pvpunitario']);
         $this->irpf = floatval($l['irpf']);
         $this->recargo = floatval($l['recargo']);
     } else {
         $this->idlinea = NULL;
         $this->idalbaran = NULL;
         $this->referencia = '';
         $this->descripcion = '';
         $this->cantidad = 0;
         $this->dtopor = 0;
         $this->dtolineal = 0;
         $this->codimpuesto = NULL;
         $this->iva = 0;
         $this->pvptotal = 0;
         $this->pvpsindto = 0;
         $this->pvpunitario = 0;
         $this->irpf = 0;
         $this->recargo = 0;
     }
 }
 public function buscar_lineas()
 {
     /// cambiamos la plantilla HTML
     $this->template = 'ajax/compras_lineas_albaranes';
     $this->buscar_lineas = $_POST['buscar_lineas'];
     $linea = new linea_albaran_proveedor();
     if (isset($_POST['codproveedor'])) {
         $this->lineas = $linea->search_from_proveedor($_POST['codproveedor'], $this->buscar_lineas);
     } else {
         $this->lineas = $linea->search($this->buscar_lineas);
     }
 }
 public function get_lineas()
 {
     $linea = new linea_albaran_proveedor();
     return $linea->all_from_albaran($this->idalbaran);
 }
예제 #4
0
 private function modificar()
 {
     $error = FALSE;
     $this->albaran->numproveedor = $_POST['numproveedor'];
     $this->albaran->observaciones = $_POST['observaciones'];
     $this->albaran->nombre = $_POST['nombre'];
     $this->albaran->cifnif = $_POST['cifnif'];
     if ($this->albaran->ptefactura) {
         $eje0 = $this->ejercicio->get_by_fecha($_POST['fecha'], FALSE);
         if (!$eje0) {
             $this->new_error_msg('Ningún ejercicio encontrado.');
         } else {
             $this->albaran->fecha = $_POST['fecha'];
             $this->albaran->hora = $_POST['hora'];
         }
         /// ¿Cambiamos el proveedor?
         if ($_POST['proveedor'] != $this->albaran->codproveedor) {
             $proveedor = $this->proveedor->get($_POST['proveedor']);
             if ($proveedor) {
                 $this->albaran->codproveedor = $proveedor->codproveedor;
                 $this->albaran->nombre = $proveedor->razonsocial;
                 $this->albaran->cifnif = $proveedor->cifnif;
             } else {
                 die('No se ha encontrado el proveedor');
             }
         } else {
             $proveedor = $this->proveedor->get($this->albaran->codproveedor);
         }
         $serie = $this->serie->get($this->albaran->codserie);
         /// ¿cambiamos la serie?
         if ($_POST['serie'] != $this->albaran->codserie) {
             $serie2 = $this->serie->get($_POST['serie']);
             if ($serie2) {
                 $this->albaran->codserie = $serie2->codserie;
                 $this->albaran->new_codigo();
                 $serie = $serie2;
             }
         }
         $this->albaran->codpago = $_POST['forma_pago'];
         /// ¿Cambiamos la divisa?
         if ($_POST['divisa'] != $this->albaran->coddivisa) {
             $divisa = $this->divisa->get($_POST['divisa']);
             if ($divisa) {
                 $this->albaran->coddivisa = $divisa->coddivisa;
                 $this->albaran->tasaconv = $divisa->tasaconv_compra;
             }
         } else {
             if ($_POST['tasaconv'] != '') {
                 $this->albaran->tasaconv = floatval($_POST['tasaconv']);
             }
         }
         if (isset($_POST['numlineas'])) {
             $numlineas = intval($_POST['numlineas']);
             $this->albaran->neto = 0;
             $this->albaran->totaliva = 0;
             $this->albaran->totalirpf = 0;
             $this->albaran->totalrecargo = 0;
             $this->albaran->irpf = 0;
             $lineas = $this->albaran->get_lineas();
             $articulo = new articulo();
             /// eliminamos las líneas que no encontremos en el $_POST
             foreach ($lineas as $l) {
                 $encontrada = FALSE;
                 for ($num = 0; $num <= $numlineas; $num++) {
                     if (isset($_POST['idlinea_' . $num])) {
                         if ($l->idlinea == intval($_POST['idlinea_' . $num])) {
                             $encontrada = TRUE;
                             break;
                         }
                     }
                 }
                 if (!$encontrada) {
                     if ($l->delete()) {
                         /// actualizamos el stock
                         $art0 = $articulo->get($l->referencia);
                         if ($art0) {
                             $art0->sum_stock($this->albaran->codalmacen, 0 - $l->cantidad);
                         }
                     } else {
                         $this->new_error_msg("¡Imposible eliminar la línea del artículo " . $l->referencia . "!");
                     }
                 }
             }
             /// modificamos y/o añadimos las demás líneas
             for ($num = 0; $num <= $numlineas; $num++) {
                 $encontrada = FALSE;
                 if (isset($_POST['idlinea_' . $num])) {
                     foreach ($lineas as $k => $value) {
                         /// modificamos la línea
                         if ($value->idlinea == intval($_POST['idlinea_' . $num])) {
                             $encontrada = TRUE;
                             $cantidad_old = $value->cantidad;
                             $lineas[$k]->cantidad = floatval($_POST['cantidad_' . $num]);
                             $lineas[$k]->pvpunitario = floatval($_POST['pvp_' . $num]);
                             $lineas[$k]->dtopor = floatval($_POST['dto_' . $num]);
                             $lineas[$k]->pvpsindto = $value->cantidad * $value->pvpunitario;
                             $lineas[$k]->pvptotal = $value->cantidad * $value->pvpunitario * (100 - $value->dtopor) / 100;
                             $lineas[$k]->descripcion = $_POST['desc_' . $num];
                             $lineas[$k]->codimpuesto = NULL;
                             $lineas[$k]->iva = 0;
                             $lineas[$k]->recargo = 0;
                             $lineas[$k]->irpf = floatval($_POST['irpf_' . $num]);
                             if (!$serie->siniva and $proveedor->regimeniva != 'Exento') {
                                 $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]);
                                 if ($imp0) {
                                     $lineas[$k]->codimpuesto = $imp0->codimpuesto;
                                 }
                                 $lineas[$k]->iva = floatval($_POST['iva_' . $num]);
                                 $lineas[$k]->recargo = floatval($_POST['recargo_' . $num]);
                             }
                             if ($lineas[$k]->save()) {
                                 $this->albaran->neto += $value->pvptotal;
                                 $this->albaran->totaliva += $value->pvptotal * $value->iva / 100;
                                 $this->albaran->totalirpf += $value->pvptotal * $value->irpf / 100;
                                 $this->albaran->totalrecargo += $value->pvptotal * $value->recargo / 100;
                                 if ($value->irpf > $this->albaran->irpf) {
                                     $this->albaran->irpf = $value->irpf;
                                 }
                                 if ($lineas[$k]->cantidad != $cantidad_old) {
                                     /// actualizamos el stock
                                     $art0 = $articulo->get($value->referencia);
                                     if ($art0) {
                                         $art0->sum_stock($this->albaran->codalmacen, $lineas[$k]->cantidad - $cantidad_old);
                                     }
                                 }
                             } else {
                                 $this->new_error_msg("¡Imposible modificar la línea del artículo " . $value->referencia . "!");
                             }
                             break;
                         }
                     }
                     /// añadimos la línea
                     if (!$encontrada and intval($_POST['idlinea_' . $num]) == -1 and isset($_POST['referencia_' . $num])) {
                         $linea = new linea_albaran_proveedor();
                         $linea->idalbaran = $this->albaran->idalbaran;
                         $linea->descripcion = $_POST['desc_' . $num];
                         if (!$serie->siniva and $proveedor->regimeniva != 'Exento') {
                             $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]);
                             if ($imp0) {
                                 $linea->codimpuesto = $imp0->codimpuesto;
                             }
                             $linea->iva = floatval($_POST['iva_' . $num]);
                             $linea->recargo = floatval($_POST['recargo_' . $num]);
                         }
                         $linea->irpf = floatval($_POST['irpf_' . $num]);
                         $linea->cantidad = floatval($_POST['cantidad_' . $num]);
                         $linea->pvpunitario = floatval($_POST['pvp_' . $num]);
                         $linea->dtopor = floatval($_POST['dto_' . $num]);
                         $linea->pvpsindto = $linea->cantidad * $linea->pvpunitario;
                         $linea->pvptotal = $linea->cantidad * $linea->pvpunitario * (100 - $linea->dtopor) / 100;
                         $art0 = $articulo->get($_POST['referencia_' . $num]);
                         if ($art0) {
                             $linea->referencia = $art0->referencia;
                         }
                         if ($linea->save()) {
                             if ($art0) {
                                 /// actualizamos el stock
                                 $art0->sum_stock($this->albaran->codalmacen, $linea->cantidad);
                             }
                             $this->albaran->neto += $linea->pvptotal;
                             $this->albaran->totaliva += $linea->pvptotal * $linea->iva / 100;
                             $this->albaran->totalirpf += $linea->pvptotal * $linea->irpf / 100;
                             $this->albaran->totalrecargo += $linea->pvptotal * $linea->recargo / 100;
                             if ($linea->irpf > $this->albaran->irpf) {
                                 $this->albaran->irpf = $linea->irpf;
                             }
                         } else {
                             $this->new_error_msg("¡Imposible guardar la línea del artículo " . $linea->referencia . "!");
                         }
                     }
                 }
             }
             /// redondeamos
             $this->albaran->neto = round($this->albaran->neto, FS_NF0);
             $this->albaran->totaliva = round($this->albaran->totaliva, FS_NF0);
             $this->albaran->totalirpf = round($this->albaran->totalirpf, FS_NF0);
             $this->albaran->totalrecargo = round($this->albaran->totalrecargo, FS_NF0);
             $this->albaran->total = $this->albaran->neto + $this->albaran->totaliva - $this->albaran->totalirpf + $this->albaran->totalrecargo;
             if (abs(floatval($_POST['atotal']) - $this->albaran->total) >= 0.02) {
                 $this->new_error_msg("El total difiere entre el controlador y la vista (" . $this->albaran->total . " frente a " . $_POST['atotal'] . "). Debes informar del error.");
             }
         }
     }
     if ($this->albaran->save()) {
         if (!$error) {
             $this->new_message(ucfirst(FS_ALBARAN) . " modificado correctamente.");
         }
         $this->new_change(ucfirst(FS_ALBARAN) . ' Proveedor ' . $this->albaran->codigo, $this->albaran->url());
     } else {
         $this->new_error_msg("¡Imposible modificar el " . FS_ALBARAN . "!");
     }
 }
 private function generar_albaran()
 {
     $albaran = new albaran_proveedor();
     $albaran->cifnif = $this->pedido->cifnif;
     $albaran->codagente = $this->pedido->codagente;
     $albaran->codalmacen = $this->pedido->codalmacen;
     $albaran->codproveedor = $this->pedido->codproveedor;
     $albaran->coddivisa = $this->pedido->coddivisa;
     $albaran->tasaconv = $this->pedido->tasaconv;
     $albaran->codpago = $this->pedido->codpago;
     $albaran->codserie = $this->pedido->codserie;
     $albaran->neto = $this->pedido->neto;
     $albaran->nombre = $this->pedido->nombre;
     $albaran->observaciones = $this->pedido->observaciones;
     $albaran->total = $this->pedido->total;
     $albaran->totaliva = $this->pedido->totaliva;
     $albaran->numproveedor = $this->pedido->numproveedor;
     $albaran->irpf = $this->pedido->irpf;
     $albaran->totalirpf = $this->pedido->totalirpf;
     $albaran->totalrecargo = $this->pedido->totalrecargo;
     /**
      * Obtenemos el ejercicio para la fecha seleccionada.
      */
     $eje0 = $this->ejercicio->get_by_fecha($_POST['aprobar'], FALSE);
     if ($eje0) {
         $albaran->fecha = $_POST['aprobar'];
         $albaran->codejercicio = $eje0->codejercicio;
     }
     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 ($albaran->save()) {
                 $continuar = TRUE;
                 $art0 = new articulo();
                 foreach ($this->pedido->get_lineas() as $l) {
                     $n = new linea_albaran_proveedor();
                     $n->idlineapedido = $l->idlinea;
                     $n->idpedido = $l->idpedido;
                     $n->idalbaran = $albaran->idalbaran;
                     $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()) {
                         /// añadimos al stock
                         if (!is_null($n->referencia)) {
                             $articulo = $art0->get($n->referencia);
                             if ($articulo) {
                                 $articulo->sum_stock($albaran->codalmacen, $l->cantidad, isset($_POST['costemedio']));
                             }
                         }
                     } else {
                         $continuar = FALSE;
                         $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! ");
                         break;
                     }
                 }
                 if ($continuar) {
                     $this->pedido->idalbaran = $albaran->idalbaran;
                     $this->pedido->editable = FALSE;
                     if ($this->pedido->save()) {
                         $this->new_message("<a href='" . $albaran->url() . "'>" . ucfirst(FS_ALBARAN) . '</a> generado correctamente.');
                     } else {
                         $this->new_error_msg("¡Imposible vincular el " . FS_PEDIDO . " con el nuevo " . FS_ALBARAN . "!");
                         if ($albaran->delete()) {
                             $this->new_error_msg("El " . FS_ALBARAN . " se ha borrado.");
                         } else {
                             $this->new_error_msg("¡Imposible borrar el " . FS_ALBARAN . "!");
                         }
                     }
                 } else {
                     if ($albaran->delete()) {
                         $this->new_error_msg("El " . FS_ALBARAN . " se ha borrado.");
                     } else {
                         $this->new_error_msg("¡Imposible borrar el " . FS_ALBARAN . "!");
                     }
                 }
             } else {
                 $this->new_error_msg("¡Imposible guardar el " . FS_ALBARAN . "!");
             }
         }
     }
 }
예제 #6
0
 private function nuevo_albaran_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'], FALSE);
     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;
     }
     $albaran = new albaran_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="' . $albaran->url() . '">' . FS_ALBARANES . '</a>
            para ver si el ' . FS_ALBARAN . ' se ha guardado correctamente.');
         $continuar = FALSE;
     }
     if ($continuar) {
         $albaran->fecha = $_POST['fecha'];
         $albaran->hora = $_POST['hora'];
         $albaran->codproveedor = $proveedor->codproveedor;
         $albaran->nombre = $_POST['nombre'];
         $albaran->cifnif = $_POST['cifnif'];
         $albaran->codalmacen = $almacen->codalmacen;
         $albaran->codejercicio = $ejercicio->codejercicio;
         $albaran->codserie = $serie->codserie;
         $albaran->codpago = $forma_pago->codpago;
         $albaran->coddivisa = $divisa->coddivisa;
         $albaran->tasaconv = $divisa->tasaconv_compra;
         if ($_POST['tasaconv'] != '') {
             $albaran->tasaconv = floatval($_POST['tasaconv']);
         }
         $albaran->codagente = $this->agente->codagente;
         $albaran->numproveedor = $_POST['numproveedor'];
         $albaran->observaciones = $_POST['observaciones'];
         if ($albaran->save()) {
             $art0 = new articulo();
             $n = floatval($_POST['numlineas']);
             for ($i = 0; $i < $n; $i++) {
                 if (isset($_POST['referencia_' . $i])) {
                     $linea = new linea_albaran_proveedor();
                     $linea->idalbaran = $albaran->idalbaran;
                     $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['stock'])) {
                                 $articulo->sum_stock($albaran->codalmacen, $linea->cantidad, isset($_POST['costemedio']));
                             } else {
                                 if (isset($_POST['costemedio'])) {
                                     $articulo->stockfis += $linea->cantidad;
                                     $articulo->costemedio = $articulo->get_costemedio();
                                     $articulo->stockfis -= $linea->cantidad;
                                     $articulo->save();
                                 }
                             }
                             if (isset($_POST['costemedio'])) {
                                 $this->actualizar_precio_proveedor($albaran->codproveedor, $linea);
                             }
                         }
                         $albaran->neto += $linea->pvptotal;
                         $albaran->totaliva += $linea->pvptotal * $linea->iva / 100;
                         $albaran->totalirpf += $linea->pvptotal * $linea->irpf / 100;
                         $albaran->totalrecargo += $linea->pvptotal * $linea->recargo / 100;
                         if ($linea->irpf > $albaran->irpf) {
                             $albaran->irpf = $linea->irpf;
                         }
                     } else {
                         $this->new_error_msg("¡Imposible guardar la linea con referencia: " . $linea->referencia);
                         $continuar = FALSE;
                     }
                 }
             }
             if ($continuar) {
                 /// redondeamos
                 $albaran->neto = round($albaran->neto, FS_NF0);
                 $albaran->totaliva = round($albaran->totaliva, FS_NF0);
                 $albaran->totalirpf = round($albaran->totalirpf, FS_NF0);
                 $albaran->totalrecargo = round($albaran->totalrecargo, FS_NF0);
                 $albaran->total = $albaran->neto + $albaran->totaliva - $albaran->totalirpf + $albaran->totalrecargo;
                 if (abs(floatval($_POST['atotal']) - $albaran->total) >= 0.02) {
                     $this->new_error_msg("El total difiere entre la vista y el controlador (" . $_POST['atotal'] . " frente a " . $albaran->total . "). Debes informar del error.");
                     $albaran->delete();
                 } else {
                     if ($albaran->save()) {
                         $this->new_message("<a href='" . $albaran->url() . "'>" . ucfirst(FS_ALBARAN) . "</a> guardado correctamente.");
                         $this->new_change(ucfirst(FS_ALBARAN) . ' Proveedor ' . $albaran->codigo, $albaran->url(), TRUE);
                         if ($_POST['redir'] == 'TRUE') {
                             header('Location: ' . $albaran->url());
                         }
                     } else {
                         $this->new_error_msg("¡Imposible actualizar el <a href='" . $albaran->url() . "'>" . FS_ALBARAN . "</a>!");
                     }
                 }
             } else {
                 if ($albaran->delete()) {
                     $this->new_message(FS_ALBARAN . " eliminado correctamente.");
                 } else {
                     $this->new_error_msg("¡Imposible eliminar el <a href='" . $albaran->url() . "'>" . FS_ALBARAN . "</a>!");
                 }
             }
         } else {
             $this->new_error_msg("¡Imposible guardar el " . FS_ALBARAN . "!");
         }
     }
 }
예제 #7
0
 public function get_lineas_albaran_prov($offset = 0, $limit = FS_ITEM_LIMIT)
 {
     $linea = new linea_albaran_proveedor();
     return $linea->all_from_articulo($this->referencia, $offset, $limit);
 }
 private function modificar()
 {
     $inventario = new inventario();
     $error = FALSE;
     $this->albaran->numproveedor = $_POST['numproveedor'];
     $this->albaran->observaciones = $_POST['observaciones'];
     $this->albaran->cai = $_POST['cai'];
     $this->albaran->caivence = $_POST['caivence'];
     if ($this->albaran->ptefactura) {
         $eje0 = $this->ejercicio->get_by_fecha($_POST['fecha']);
         if ($eje0) {
             if ($eje0->codejercicio == $this->albaran->codejercicio) {
                 $continuar = TRUE;
             } else {
                 $continuar = FALSE;
             }
         } else {
             $continuar = FALSE;
         }
         if ($continuar == TRUE) {
             $this->albaran->fecha = $_POST['fecha'];
             $this->albaran->hora = $_POST['hora'];
         } else {
             $error = TRUE;
             $this->new_error_msg('La fecha seleccionada está fuere del rando del ejercicio ' . $this->albaran->codejercicio . '. Si deseas asignar la fecha ' . $_POST['fecha'] . ' pulsa <a href="' . $this->url() . '&forze_fecha=' . $_POST['fecha'] . '">aquí</a>' . ' y se asignará un nuevo código y un nuevo número al ' . FS_ALBARAN . '.');
         }
         /// ¿Cambiamos el proveedor?
         if ($_POST['proveedor'] != $this->albaran->codproveedor) {
             $proveedor = $this->proveedor->get($_POST['proveedor']);
             if ($proveedor) {
                 $this->albaran->codproveedor = $proveedor->codproveedor;
                 $this->albaran->nombre = $proveedor->razonsocial;
                 $this->albaran->cifnif = $proveedor->cifnif;
             } else {
                 die('No se ha encontrado el proveedor');
             }
         } else {
             $proveedor = $this->proveedor->get($this->albaran->codproveedor);
         }
         $serie = $this->serie->get($this->albaran->codserie);
         /// ¿cambiamos la serie?
         if ($_POST['serie'] != $this->albaran->codserie) {
             $serie2 = $this->serie->get($_POST['serie']);
             if ($serie2) {
                 $this->albaran->codserie = $serie2->codserie;
                 $this->albaran->irpf = $serie2->irpf;
                 $this->albaran->new_codigo();
                 $serie = $serie2;
             }
         }
         $this->albaran->codpago = $_POST['forma_pago'];
         /// ¿Cambiamos la divisa?
         if ($_POST['divisa'] != $this->albaran->coddivisa) {
             $divisa = $this->divisa->get($_POST['divisa']);
             if ($divisa) {
                 $this->albaran->coddivisa = $divisa->coddivisa;
                 $this->albaran->tasaconv = $divisa->tasaconv_compra;
             }
         } else {
             if ($_POST['tasaconv'] != '') {
                 $this->albaran->tasaconv = floatval($_POST['tasaconv']);
             }
         }
         if (isset($_POST['numlineas'])) {
             $numlineas = intval($_POST['numlineas']);
             $this->albaran->neto = 0;
             $this->albaran->totaliva = 0;
             $this->albaran->totalirpf = 0;
             $this->albaran->totalrecargo = 0;
             $lineas = $this->albaran->get_lineas();
             $articulo = new articulo();
             /// eliminamos las líneas que no encontremos en el $_POST
             foreach ($lineas as $l) {
                 $encontrada = FALSE;
                 for ($num = 0; $num <= $numlineas; $num++) {
                     if (isset($_POST['idlinea_' . $num])) {
                         if ($l->idlinea == intval($_POST['idlinea_' . $num])) {
                             $encontrada = TRUE;
                             break;
                         }
                     }
                 }
                 if (!$encontrada) {
                     if ($l->delete()) {
                         /// actualizamos el stock
                         $art0 = $articulo->get($l->referencia);
                         if ($art0) {
                             $art0->sum_stock($this->albaran->codalmacen, 0 - $l->cantidad);
                         }
                         if ($art0) {
                             $inventario->inventario_agregar($this->albaran->codalmacen, $l->referencia, 0 - $l->cantidad, $l->pvpunitario);
                         }
                     } else {
                         $this->new_error_msg("¡Imposible eliminar la línea del artículo " . $l->referencia . "!");
                     }
                 }
             }
             /// modificamos y/o añadimos las demás líneas
             for ($num = 0; $num < $numlineas; $num++) {
                 $encontrada = FALSE;
                 if (isset($_POST['idlinea_' . $num])) {
                     foreach ($lineas as $k => $value) {
                         /// modificamos la línea
                         if ($value->idlinea == intval($_POST['idlinea_' . $num])) {
                             $encontrada = TRUE;
                             $cantidad_old = $value->cantidad;
                             $lineas[$k]->cantidad = floatval($_POST['cantidad_' . $num]);
                             $lineas[$k]->pvpunitario = floatval($_POST['pvp_' . $num]);
                             $lineas[$k]->dtopor = floatval($_POST['dto_' . $num]);
                             $lineas[$k]->pvpsindto = $value->cantidad * $value->pvpunitario;
                             $lineas[$k]->pvptotal = $value->cantidad * $value->pvpunitario * (100 - $value->dtopor) / 100;
                             $lineas[$k]->descripcion = $_POST['desc_' . $num];
                             if (isset($_POST['subcuenta_' . $num])) {
                                 $postot = strlen($_POST['subcuenta_' . $num]);
                                 $poscad = strpos($_POST['subcuenta_' . $num], '/');
                                 $posid = strpos($_POST['subcuenta_' . $num], '%');
                                 $subcuencod = substr($_POST['subcuenta_' . $num], 0, $poscad);
                                 $subcuendes = substr($_POST['subcuenta_' . $num], $poscad + 1, $posid - $postot);
                                 $idsubcuen = substr($_POST['subcuenta_' . $num], $posid + 1);
                                 $lineas[$k]->codsubcuenta = $subcuencod;
                                 $lineas[$k]->idsubcuenta = $idsubcuen;
                                 $lineas[$k]->subcuentadesc = $subcuendes;
                             }
                             $lineas[$k]->codimpuesto = NULL;
                             $lineas[$k]->iva = 0;
                             $lineas[$k]->recargo = 0;
                             $lineas[$k]->irpf = floatval($_POST['irpf_' . $num]);
                             if (!$serie->siniva and $proveedor->regimeniva != 'Exento') {
                                 $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]);
                                 if ($imp0) {
                                     $lineas[$k]->codimpuesto = $imp0->codimpuesto;
                                 }
                                 $lineas[$k]->iva = floatval($_POST['iva_' . $num]);
                                 $lineas[$k]->recargo = floatval($_POST['recargo_' . $num]);
                             }
                             if ($lineas[$k]->save()) {
                                 $this->albaran->neto += $value->pvptotal;
                                 $this->albaran->totaliva += $value->pvptotal * $value->iva / 100;
                                 $this->albaran->totalirpf += $value->pvptotal * $value->irpf / 100;
                                 $this->albaran->totalrecargo += $value->pvptotal * $value->recargo / 100;
                                 if ($lineas[$k]->cantidad != $cantidad_old) {
                                     /// actualizamos el stock
                                     $art0 = $articulo->get($value->referencia);
                                     if ($art0) {
                                         $art0->sum_stock($this->albaran->codalmacen, $lineas[$k]->cantidad - $cantidad_old);
                                     }
                                     if ($art0) {
                                         $inventario->inventario_agregar($this->albaran->codalmacen, $lineas[$k]->referencia, $lineas[$k]->cantidad - $cantidad_old, $lineas[$k]->pvpunitario);
                                     }
                                 }
                             } else {
                                 $this->new_error_msg("¡Imposible modificar la línea del artículo " . $value->referencia . "!");
                             }
                             break;
                         }
                     }
                     /// añadimos la línea
                     if (!$encontrada and intval($_POST['idlinea_' . $num]) == -1 and isset($_POST['referencia_' . $num])) {
                         $linea = new linea_albaran_proveedor();
                         $linea->idalbaran = $this->albaran->idalbaran;
                         $linea->descripcion = $_POST['desc_' . $num];
                         /*                     if( !$serie->siniva AND $proveedor->regimeniva != 'Exento' )
                                              {
                                                 $imp0 = $this->impuesto->get_by_iva($_POST['iva_'.$num]);
                                                 if($imp0)
                                                    $linea->codimpuesto = $imp0->codimpuesto;
                                                 
                                                 $linea->iva = floatval($_POST['iva_'.$num]);
                                                 $linea->recargo = floatval($_POST['recargo_'.$num]);
                                              }
                          */
                         $linea->irpf = floatval($_POST['irpf_' . $num]);
                         $linea->cantidad = floatval($_POST['cantidad_' . $num]);
                         $linea->pvpunitario = floatval($_POST['pvp_' . $num]);
                         $linea->dtopor = floatval($_POST['dto_' . $num]);
                         $linea->pvpsindto = $linea->cantidad * $linea->pvpunitario;
                         $linea->pvptotal = $linea->cantidad * $linea->pvpunitario * (100 - $linea->dtopor) / 100;
                         if ($_POST['subcuenta_' . $num] == '/%') {
                             $linea->codsubcuenta = NULL;
                             $linea->idsubcuenta = NULL;
                             $linea->subcuentadesc = NULL;
                         } else {
                             $postot = strlen($_POST['subcuenta_' . $num]);
                             $poscad = strpos($_POST['subcuenta_' . $num], '/');
                             $posid = strpos($_POST['subcuenta_' . $num], '%');
                             $subcuencod = substr($_POST['subcuenta_' . $num], 0, $poscad);
                             $subcuendes = substr($_POST['subcuenta_' . $num], $poscad + 1, $posid - $postot);
                             $idsubcuen = substr($_POST['subcuenta_' . $num], $posid + 1);
                             $linea->codsubcuenta = $subcuencod;
                             $linea->idsubcuenta = $idsubcuen;
                             $linea->subcuentadesc = $subcuendes;
                         }
                         $art0 = $articulo->get($_POST['referencia_' . $num]);
                         if ($art0) {
                             $linea->referencia = $art0->referencia;
                         }
                         if ($linea->save()) {
                             if ($art0) {
                                 /// actualizamos el stock
                                 $art0->sum_stock($this->albaran->codalmacen, $linea->cantidad);
                                 if ($art0) {
                                     $inventario->inventario_agregar($this->albaran->codalmacen, $linea->referencia, $linea->cantidad, $linea->pvpunitario);
                                 }
                             }
                             $this->albaran->neto += $linea->pvptotal;
                             $this->albaran->totaliva += $linea->pvptotal * $linea->iva / 100;
                             $this->albaran->totalirpf += $linea->pvptotal * $linea->irpf / 100;
                             $this->albaran->totalrecargo += $linea->pvptotal * $linea->recargo / 100;
                         } else {
                             $this->new_error_msg("¡Imposible guardar la línea del artículo " . $linea->referencia . "!");
                         }
                     }
                 }
             }
             /// redondeamos
             $this->albaran->neto = round($this->albaran->neto, FS_NF0);
             $this->albaran->totaliva = round($this->albaran->totaliva, FS_NF0);
             $this->albaran->totalirpf = round($this->albaran->totalirpf, FS_NF0);
             $this->albaran->totalrecargo = round($this->albaran->totalrecargo, FS_NF0);
             $this->albaran->total = $this->albaran->neto + $this->albaran->totaliva - $this->albaran->totalirpf + $this->albaran->totalrecargo;
             if (abs(floatval($_POST['atotal']) - $this->albaran->total) >= 0.02) {
                 $this->new_error_msg("El total difiere entre el controlador y la vista (" . $this->albaran->total . " frente a " . $_POST['atotal'] . "). Debes informar del error.");
             }
         }
     }
     if ($this->albaran->save()) {
         if (!$error) {
             $this->new_message(ucfirst(FS_ALBARAN) . " modificado correctamente.");
         }
         $this->new_change(ucfirst(FS_ALBARAN) . ' Proveedor ' . $this->albaran->codigo, $this->albaran->url());
     } else {
         $this->new_error_msg("¡Imposible modificar el " . FS_ALBARAN . "!");
     }
 }
예제 #9
0
 private function generar_albaran()
 {
     $albaran = new albaran_proveedor();
     $albaran->cifnif = $this->pedido->cifnif;
     $albaran->codagente = $this->pedido->codagente;
     $albaran->codalmacen = $this->pedido->codalmacen;
     $albaran->codproveedor = $this->pedido->codproveedor;
     $albaran->coddivisa = $this->pedido->coddivisa;
     $albaran->tasaconv = $this->pedido->tasaconv;
     $albaran->codpago = $this->pedido->codpago;
     $albaran->codserie = $this->pedido->codserie;
     $albaran->neto = $this->pedido->neto;
     $albaran->nombre = $this->pedido->nombre;
     $albaran->observaciones = $this->pedido->observaciones;
     $albaran->total = $this->pedido->total;
     $albaran->totaliva = $this->pedido->totaliva;
     $albaran->numproveedor = $this->pedido->numproveedor;
     $albaran->irpf = $this->pedido->irpf;
     $albaran->totalirpf = $this->pedido->totalirpf;
     $albaran->totalrecargo = $this->pedido->totalrecargo;
     /**
      * Obtenemos el ejercicio para la fecha de hoy (puede que
      * no sea el mismo ejercicio que el del pedido, por ejemplo
      * si hemos cambiado de año)
      */
     $eje0 = $this->ejercicio->get_by_fecha($albaran->fecha);
     $albaran->codejercicio = $eje0->codejercicio;
     $regularizacion = new regularizacion_iva();
     if (!$eje0->abierto()) {
         $this->new_error_msg("El ejercicio está cerrado.");
     } else {
         if ($regularizacion->get_fecha_inside($albaran->fecha)) {
             $this->new_error_msg("El IVA de ese periodo ya ha sido regularizado. No se pueden añadir más " . FS_ALBARANES . " en esa fecha.");
         } else {
             if ($albaran->save()) {
                 $continuar = TRUE;
                 $art0 = new articulo();
                 foreach ($this->pedido->get_lineas() as $l) {
                     $n = new linea_albaran_proveedor();
                     $n->idlineapedido = $l->idlinea;
                     $n->idpedido = $l->idpedido;
                     $n->idalbaran = $albaran->idalbaran;
                     $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()) {
                         /// añadimos al stock
                         if (!is_null($n->referencia)) {
                             $articulo = $art0->get($n->referencia);
                             if ($articulo) {
                                 $articulo->sum_stock($albaran->codalmacen, $l->cantidad);
                             }
                         }
                     } else {
                         $continuar = FALSE;
                         $this->new_error_msg("¡Imposible guardar la línea el artículo " . $n->referencia . "! ");
                         break;
                     }
                 }
                 if ($continuar) {
                     $this->pedido->idalbaran = $albaran->idalbaran;
                     $this->pedido->editable = FALSE;
                     if ($this->pedido->save()) {
                         $this->new_message("<a href='" . $albaran->url() . "'>" . ucfirst(FS_ALBARAN) . '</a> generado correctamente.');
                     } else {
                         $this->new_error_msg("¡Imposible vincular el " . FS_PEDIDO . " con el nuevo " . FS_ALBARAN . "!");
                         if ($albaran->delete()) {
                             $this->new_error_msg("El " . FS_ALBARAN . " se ha borrado.");
                         } else {
                             $this->new_error_msg("¡Imposible borrar el " . FS_ALBARAN . "!");
                         }
                     }
                 } else {
                     if ($albaran->delete()) {
                         $this->new_error_msg("El " . FS_ALBARAN . " se ha borrado.");
                     } else {
                         $this->new_error_msg("¡Imposible borrar el " . FS_ALBARAN . "!");
                     }
                 }
             } else {
                 $this->new_error_msg("¡Imposible guardar el " . FS_ALBARAN . "!");
             }
         }
     }
 }
 private function agrupar_pedidos()
 {
     $continuar = TRUE;
     $albaran = new albaran_proveedor();
     $albaran_rellenado = FALSE;
     $art0 = new articulo();
     $num = 0;
     foreach ($this->resultados as $ped) {
         foreach ($ped->get_lineas() as $lin) {
             if (!isset($_POST['idl_' . $num]) or !$continuar) {
             } else {
                 if ($lin->idlinea == intval($_POST['idl_' . $num])) {
                     if (!$albaran_rellenado) {
                         $albaran->codagente = $this->user->codagente;
                         $albaran->codalmacen = $ped->codalmacen;
                         $albaran->coddivisa = $ped->coddivisa;
                         $albaran->tasaconv = $ped->tasaconv;
                         $albaran->codejercicio = $ped->codejercicio;
                         $albaran->codpago = $ped->codpago;
                         $albaran->codserie = $ped->codserie;
                         $albaran->irpf = $ped->irpf;
                         $albaran->nombre = $this->proveedor->nombre;
                         if (!$albaran->save()) {
                             $continuar = FALSE;
                             $this->new_error_msg('Error al agrupar el pedido.');
                         }
                         $albaran_rellenado = TRUE;
                     }
                     $linea = new linea_albaran_proveedor();
                     $linea->idalbaran = $albaran->idalbaran;
                     $linea->idpedido = $ped->idpedido;
                     $linea->idlineapedido = $lin->idlinea;
                     $linea->referencia = $lin->referencia;
                     $linea->descripcion = $lin->descripcion;
                     $linea->cantidad = floatval($_POST['cantidad_' . $num]);
                     $linea->pvpunitario = $lin->pvpunitario;
                     $linea->codimpuesto = $lin->codimpuesto;
                     $linea->dtopor = $lin->dtopor;
                     $linea->irpf = $lin->irpf;
                     $linea->iva = $lin->iva;
                     $linea->recargo = $lin->recargo;
                     $linea->pvpsindto = $linea->pvpunitario * $linea->cantidad;
                     $linea->pvptotal = $linea->pvpunitario * $linea->cantidad * (100 - $linea->dtopor) / 100;
                     if ($linea->save()) {
                         /// añadimos al stock
                         $articulo = $art0->get($linea->referencia);
                         if ($articulo) {
                             $articulo->sum_stock($albaran->codalmacen, $linea->cantidad, TRUE);
                         }
                         $albaran->neto += $linea->pvptotal;
                         $albaran->totaliva += $linea->pvptotal * $linea->iva / 100;
                         $albaran->totalirpf += $linea->pvptotal * $linea->irpf / 100;
                         $albaran->totalrecargo += $linea->pvptotal * $linea->recargo / 100;
                     } else {
                         $this->new_error_msg("¡Imposible guardar la linea con referencia: " . $linea->referencia);
                         $continuar = FALSE;
                     }
                 }
             }
             $num++;
         }
         if (isset($_POST['aprobado'])) {
             if (in_array($ped->idpedido, $_POST['aprobado'])) {
                 $ped->editable = FALSE;
                 $ped->idalbaran = $albaran->idalbaran;
                 $ped->save();
             }
         }
     }
     if ($continuar) {
         /// redondeamos
         $albaran->neto = round($albaran->neto, FS_NF0);
         $albaran->totaliva = round($albaran->totaliva, FS_NF0);
         $albaran->totalirpf = round($albaran->totalirpf, FS_NF0);
         $albaran->totalrecargo = round($albaran->totalrecargo, FS_NF0);
         $albaran->total = $albaran->neto + $albaran->totaliva - $albaran->totalirpf + $albaran->totalrecargo;
         if ($albaran->save()) {
             $this->new_message('<a href="' . $albaran->url() . '">' . ucfirst(FS_ALBARAN) . '</a> generado correctamente.');
         } else {
             $this->new_error_msg('Error al generar el ' . FS_ALBARAN);
             $albaran->delete();
         }
     } else {
         if (!is_null($albaran->idalbaran)) {
             $albaran->delete();
         }
     }
 }