private function nuevo_servicio_cliente() { $continuar = TRUE; $cliente = $this->cliente->get($_POST['cliente']); $almacen = $this->almacen->get($_POST['almacen']); $eje0 = new ejercicio(); $ejercicio = $eje0->get_by_fecha($_POST['fecha']); $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($forma_pago->codpago); } else { $this->new_error_msg('Forma de pago no encontrada.'); $continuar = FALSE; } $divisa = $this->divisa->get($_POST['divisa']); $servicio = new servicio_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="' . $servicio->url() . '">Servicios</a> para ver si el servicio se ha guardado correctamente.'); $continuar = FALSE; } if ($continuar) { $servicio->fecha = $_POST['fecha']; $servicio->codalmacen = $almacen->codalmacen; $servicio->codejercicio = $ejercicio->codejercicio; $servicio->codserie = $serie->codserie; $servicio->codpago = $forma_pago->codpago; $servicio->coddivisa = $divisa->coddivisa; $servicio->tasaconv = $divisa->tasaconv; if ($_POST['tasaconv'] != '') { $servicio->tasaconv = floatval($_POST['tasaconv']); } $servicio->codagente = $this->agente->codagente; $servicio->observaciones = $_POST['observaciones']; if (isset($_POST['numero2'])) { $servicio->numero2 = $_POST['numero2']; } $servicio->porcomision = $this->agente->porcomision; $servicio->codcliente = $cliente->codcliente; $servicio->cifnif = $cliente->cifnif; $servicio->nombrecliente = $cliente->razonsocial; $servicio->ciudad = $_POST['ciudad']; $servicio->codpais = $_POST['codpais']; $servicio->codpostal = $_POST['codpostal']; $servicio->direccion = $_POST['direccion']; $servicio->provincia = $_POST['provincia']; $servicio->prioridad = intval($_POST['prioridad']); $servicio->idestado = $_POST['estado']; if (isset($_POST['material'])) { $servicio->material = $_POST['material']; } if (isset($_POST['material_estado'])) { $servicio->material_estado = $_POST['material_estado']; } if (isset($_POST['accesorios'])) { $servicio->accesorios = $_POST['accesorios']; } if (isset($_POST['descripcion'])) { $servicio->descripcion = $_POST['descripcion']; } if (isset($_POST['solucion'])) { $servicio->solucion = $_POST['solucion']; } $servicio->fechainicio = Date('d-m-Y H:i'); if (isset($_POST['fechainicio'])) { $servicio->fechainicio = $_POST['fechainicio']; } if (isset($_POST['fechafin'])) { $servicio->fechafin = $_POST['fechafin']; } else { $servicio->fechafin = date('Y-m-d H:i', strtotime($servicio->fechainicio . '+ ' . $this->setup['cal_intervalo'] . 'minutes')); } if (isset($_POST['garantia'])) { $servicio->garantia = $_POST['garantia']; } if ($servicio->save()) { $art0 = new articulo(); $n = floatval($_POST['numlineas']); for ($i = 0; $i <= $n; $i++) { if (isset($_POST['referencia_' . $i])) { $linea = new linea_servicio_cliente(); $linea->idservicio = $servicio->idservicio; $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()) { $servicio->neto += $linea->pvptotal; $servicio->totaliva += $linea->pvptotal * $linea->iva / 100; $servicio->totalirpf += $linea->pvptotal * $linea->irpf / 100; $servicio->totalrecargo += $linea->pvptotal * $linea->recargo / 100; if ($linea->irpf > $servicio->irpf) { $servicio->irpf = $linea->irpf; } } else { $this->new_error_msg("¡Imposible guardar la linea con referencia: " . $linea->referencia); $continuar = FALSE; } } } if ($continuar) { /// redondeamos $servicio->neto = round($servicio->neto, FS_NF0); $servicio->totaliva = round($servicio->totaliva, FS_NF0); $servicio->totalirpf = round($servicio->totalirpf, FS_NF0); $servicio->totalrecargo = round($servicio->totalrecargo, FS_NF0); $servicio->total = $servicio->neto + $servicio->totaliva - $servicio->totalirpf + $servicio->totalrecargo; if (abs(floatval($_POST['atotal']) - $servicio->total) >= 0.02) { $this->new_error_msg("El total difiere entre el controlador y la vista (" . $servicio->total . " frente a " . $_POST['atotal'] . "). Debes informar del error."); $servicio->delete(); } else { if ($servicio->save()) { $this->new_message("<a href='" . $servicio->url() . "'>" . ucfirst(FS_SERVICIO) . "</a> guardado correctamente."); $this->new_change(ucfirst(FS_SERVICIO) . " a Cliente " . $servicio->codigo, $servicio->url(), TRUE); header('Location: ' . $servicio->url()); } else { $this->new_error_msg("¡Imposible actualizar el <a href='" . $servicio->url() . "'>" . FS_SERVICIO . "</a>!"); } } } else { if ($servicio->delete()) { $this->new_message(ucfirst(FS_SERVICIO) . " eliminado correctamente."); } else { $this->new_error_msg("¡Imposible eliminar el <a href='" . $servicio->url() . "'>" . FS_SERVICIO . "</a>!"); } } } else { $this->new_error_msg("¡Imposible guardar el " . FS_SERVICIO . "!"); } } }
private function modificar() { $this->servicio->observaciones = $_POST['observaciones']; $this->servicio->numero2 = $_POST['numero2']; $this->servicio->estado = $_POST['estado']; if (isset($_POST['material'])) { $this->servicio->material = $_POST['material']; } if (isset($_POST['material_estado'])) { $this->servicio->material_estado = $_POST['material_estado']; } if (isset($_POST['accesorios'])) { $this->servicio->accesorios = $_POST['accesorios']; } if (isset($_POST['descripcion'])) { $this->servicio->descripcion = $_POST['descripcion']; } if (isset($_POST['solucion'])) { $this->servicio->solucion = $_POST['solucion']; } if (isset($_POST['fechainicio'])) { $this->servicio->fechainicio = $_POST['fechainicio']; } if (isset($_POST['fechafin'])) { $this->servicio->fechafin = $_POST['fechafin']; } if (isset($_POST['garantia'])) { $this->servicio->garantia = $_POST['garantia']; } else { $this->servicio->garantia = FALSE; } if (isset($_POST['codmarca'])) { $this->servicio->codmarca = $_POST['codmarca']; } if (isset($_POST['codmodelo'])) { $this->servicio->codmodelo = $_POST['codmodelo']; } if (isset($_POST['codtipo'])) { $this->servicio->codtipo = $_POST['codtipo']; } $this->servicio->prioridad = $_POST['prioridad']; if ($this->servicio->editable()) { /// obtenemos los datos del ejercicio para acotar la fecha $eje0 = $this->ejercicio->get($this->servicio->codejercicio); if ($eje0) { $this->servicio->fecha = $eje0->get_best_fecha($_POST['fecha'], TRUE); $this->servicio->hora = $_POST['hora']; } else { $this->new_error_msg('No se encuentra el ejercicio asociado al ' . FS_SERVICIO); } /// ¿cambiamos el cliente? if ($_POST['cliente'] != $this->servicio->codcliente) { $cliente = $this->cliente->get($_POST['cliente']); if ($cliente) { foreach ($cliente->get_direcciones() as $d) { if ($d->domfacturacion) { $this->servicio->codcliente = $cliente->codcliente; $this->servicio->cifnif = $cliente->cifnif; $this->servicio->nombrecliente = $cliente->razonsocial; $this->servicio->apartado = $d->apartado; $this->servicio->ciudad = $d->ciudad; $this->servicio->coddir = $d->id; $this->servicio->codpais = $d->codpais; $this->servicio->codpostal = $d->codpostal; $this->servicio->direccion = $d->direccion; $this->servicio->provincia = $d->provincia; break; } } } else { die('No se ha encontrado el cliente.'); } } else { $this->servicio->codpais = $_POST['codpais']; $this->servicio->provincia = $_POST['provincia']; $this->servicio->ciudad = $_POST['ciudad']; $this->servicio->codpostal = $_POST['codpostal']; $this->servicio->direccion = $_POST['direccion']; $cliente = $this->cliente->get($this->servicio->codcliente); } $serie = $this->serie->get($this->servicio->codserie); /// ¿cambiamos la serie? if ($_POST['serie'] != $this->servicio->codserie) { $serie2 = $this->serie->get($_POST['serie']); if ($serie2) { $this->servicio->codserie = $serie2->codserie; $this->servicio->irpf = $serie2->irpf; $this->servicio->new_codigo(); $serie = $serie2; } } $this->servicio->codpago = $_POST['forma_pago']; /// ¿Cambiamos la divisa? if ($_POST['divisa'] != $this->servicio->coddivisa) { $divisa = $this->divisa->get($_POST['divisa']); if ($divisa) { $this->servicio->coddivisa = $divisa->coddivisa; $this->servicio->tasaconv = $divisa->tasaconv; } } else { if ($_POST['tasaconv'] != '') { $this->servicio->tasaconv = floatval($_POST['tasaconv']); } } if (isset($_POST['numlineas'])) { $numlineas = intval($_POST['numlineas']); $this->servicio->neto = 0; $this->servicio->totaliva = 0; $this->servicio->totalirpf = 0; $this->servicio->totalrecargo = 0; $lineas = $this->servicio->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()) { $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; $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 = $this->servicio->irpf; if (!$serie->siniva and $cliente->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->servicio->neto += $value->pvptotal; $this->servicio->totaliva += $value->pvptotal * $value->iva / 100; $this->servicio->totalirpf += $value->pvptotal * $value->irpf / 100; $this->servicio->totalrecargo += $value->pvptotal * $value->recargo / 100; } 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_servicio_cliente(); $linea->idservicio = $this->servicio->idservicio; $linea->descripcion = $_POST['desc_' . $num]; if (!$serie->siniva and $cliente->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()) { $this->servicio->neto += $linea->pvptotal; $this->servicio->totaliva += $linea->pvptotal * $linea->iva / 100; $this->servicio->totalirpf += $linea->pvptotal * $linea->irpf / 100; $this->servicio->totalrecargo += $linea->pvptotal * $linea->recargo / 100; } else { $this->new_error_msg("¡Imposible guardar la línea del artículo " . $linea->referencia . "!"); } } } } /// redondeamos $this->servicio->neto = round($this->servicio->neto, FS_NF0); $this->servicio->totaliva = round($this->servicio->totaliva, FS_NF0); $this->servicio->totalirpf = round($this->servicio->totalirpf, FS_NF0); $this->servicio->totalrecargo = round($this->servicio->totalrecargo, FS_NF0); $this->servicio->total = $this->servicio->neto + $this->servicio->totaliva - $this->servicio->totalirpf + $this->servicio->totalrecargo; if (abs(floatval($_POST['atotal']) - $this->servicio->total) >= 0.02) { $this->new_error_msg("El total difiere entre el controlador y la vista (" . $this->servicio->total . " frente a " . $_POST['atotal'] . "). Debes informar del error."); } } } if ($this->servicio->save()) { $this->new_message(ucfirst(FS_SERVICIO) . " modificado correctamente."); $this->new_change(ucfirst(FS_SERVICIO) . ' Cliente ' . $this->servicio->codigo, $this->servicio->url()); } else { $this->new_error_msg("¡Imposible modificar el " . FS_SERVICIO . "!"); } if ($this->servicio->idestado != $_POST['estado']) { /// si tiene el mismo estado no tiene que hacer nada sino tiene que añadir un detalle $this->servicio->idestado = $_POST['estado']; $this->agrega_detalle_estado($_POST['estado']); foreach ($this->estado->all() as $est) { if ($est->id == $this->servicio->idestado) { if ($est->albaran) { $this->generar_albaran(); } break; } } $this->servicio->save(); } }