Esempio n. 1
0
 /**
  * OK: Guarda los datos de un remito en la bd
  * usado por nuevoAction
  * @return bool
  */
 public function guardarNuevoAction()
 {
     if (!$this->request->isPost()) {
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "index"));
     }
     $planilla_id = $this->request->getPost("remito_planillaId");
     //Controlamos el formulario
     $data = $this->request->getPost();
     $remitoForm = new RemitoForm(null, array('required' => ''));
     if (!$remitoForm->isValid($data, new Remito())) {
         foreach ($remitoForm->getMessages() as $message) {
             $this->flash->error($message);
         }
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
     }
     $clienteForm = new ClienteNewForm(null, array('required' => ''));
     if (!$clienteForm->isValid($data, new Cliente())) {
         foreach ($clienteForm->getMessages() as $message) {
             $this->flash->error($message);
         }
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
     }
     //Controlamos que exista la planilla
     $planilla = Planilla::findFirst(array('planilla_id=:planilla_id: AND planilla_finalizada=0 AND planilla_habilitado=1 AND planilla_armada=1', 'bind' => array('planilla_id' => $planilla_id)));
     if (!$planilla) {
         $this->flash->error('Hubo un problema, la planilla no permite agregar más remitos.');
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
     }
     /*==================== Generar Ultimo Nro de Remito ===============================*/
     $this->db->begin();
     $nuevoRemito = new Remito();
     $ultimoRemito = Remito::findFirst(array("remito_ultima = 1 AND remito_habilitado=1 AND remito_planillaId = :planilla_id:", 'bind' => array('planilla_id' => $planilla_id)));
     if (!$ultimoRemito) {
         $nuevoRemito->setRemitoUltima(1);
         $nuevoRemito->setRemitoNroOrden(1);
     } else {
         $nuevoRemito->setRemitoUltima(1);
         $ultimoRemito->setRemitoUltima(0);
         $nuevoRemito->setRemitoNroOrden($ultimoRemito->getRemitoNroOrden() + 1);
         if (!$ultimoRemito->update()) {
             $this->flash->error('Hubo un problema con la conexion, intenteló nuevamente');
             $this->db->rollback();
             return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
         }
     }
     /*==================== Guardando Datos del Remito ===============================*/
     $nuevoRemito->setRemitoPlanillaId($planilla_id);
     $nuevoRemito->setRemitoFechaCreacion(date('Y-m-d'));
     $nuevoRemito->setRemitoCreadoPor($this->session->get('auth')['usuario_nick']);
     $nuevoRemito->setRemitoHabilitado(1);
     $nuevoRemito->setRemitoFecha($this->request->getPost('remito_fecha'));
     $nuevoRemito->setRemitoNro($this->request->getPost('remito_nro'));
     $nuevoRemito->setRemitoPeriodo(date('m', date(strtotime(date($this->request->getPost("remito_fecha"))))));
     if ($this->request->hasFiles() && $this->request->getUploadedFiles()[0] != NULL) {
         var_dump($this->request->getUploadedFiles());
         $upload = $this->guardarPDF($this->request->getUploadedFiles(), $planilla->getPlanillaClienteId(), $planilla->getPlanillaFecha());
         if (!$upload['success']) {
             $this->flash->error($upload['mensaje']);
             $this->db->rollback();
             return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
         }
         $nuevoRemito->setRemitoPdf($upload['path']);
     }
     $nuevoRemito->setRemitoObservacion($this->request->getPost('remito_observacion'));
     $nuevoRemito->setRemitoConformidad($this->request->getPost('remito_conformidad'));
     $nuevoRemito->setRemitoNoConformidad($this->request->getPost('remito_noConformidad'));
     /* ==================== ==================== ==================== ==================== */
     $nuevoRemito->setRemitoTransporteId($this->request->getPost('remito_transporteId'));
     $nuevoRemito->setRemitoTipoEquipoId($this->request->getPost('remito_tipoEquipoId'));
     $nuevoRemito->setRemitoTipoCargaId($this->request->getPost('remito_tipoCargaId'));
     $nuevoRemito->setRemitoChoferId($this->request->getPost('remito_choferId'));
     /* ==================== ==================== ==================== ==================== */
     $nuevoRemito->setRemitoClienteId($planilla->getPlanillaClienteId());
     $nuevoRemito->setRemitoCentroCostoId($this->request->getPost('centroCosto_id'));
     $nuevoRemito->setRemitoEquipoPozoId($this->request->getPost('equipoPozo_id'));
     $nuevoRemito->setRemitoOperadoraId($this->request->getPost('operadora_id'));
     /* ==================== ==================== ==================== ==================== */
     $nuevoRemito->setRemitoViajeId($this->request->getPost('remito_viajeId'));
     $nuevoRemito->setRemitoConcatenadoId($this->request->getPost('remito_concatenadoId'));
     /* ==================== ==================== ==================== ==================== */
     $tarifa = new Tarifa();
     $tarifa->setTarifaHoraInicial($this->request->getPost("tarifa_horaInicial"));
     $tarifa->setTarifaHoraFinal($this->request->getPost("tarifa_horaFinal"));
     $tarifa->setTarifaHsServicio($this->request->getPost("tarifa_hsServicio"));
     $tarifa->setTarifaHsHidro($this->request->getPost("tarifa_hsHidro"));
     $tarifa->setTarifaHsMalacate($this->request->getPost("tarifa_hsMalacate"));
     $tarifa->setTarifaHsStand($this->request->getPost("tarifa_hsStand"));
     $tarifa->setTarifaKm($this->request->getPost("tarifa_km"));
     if (!$tarifa->save()) {
         foreach ($tarifa->getMessages() as $mensaje) {
             $this->flash->error($mensaje);
         }
         $this->db->rollback();
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
     }
     $nuevoRemito->setRemitoTarifaId($tarifa->getTarifaId());
     /* ==================== ==================== ==================== ==================== */
     $planilla = Planilla::findFirstByPlanilla_id($planilla_id);
     $columnas = Columna::find(array("columna_cabeceraId=:cabecera_id: AND columna_habilitado = 1 AND columna_extra = 1", 'bind' => array('cabecera_id' => $planilla->getPlanillaCabeceraid())));
     if (count($columnas) != 0) {
         foreach ($columnas as $col) {
             $contenidoExtra = new Contenidoextra();
             $contenidoExtra->setContenidoExtraHabilitado(1);
             $contenidoExtra->setContenidoExtraColumnaId($col->getColumnaId());
             echo "<br> COl NOmbre" . $col->getColumnaClave();
             echo "<br> getPOst" . $this->request->getPost($col->getColumnaClave());
             $contenidoExtra->setContenidoExtraDescripcion($this->request->getPost($col->getColumnaClave()));
             if (!$contenidoExtra->save()) {
                 foreach ($contenidoExtra->getMessages() as $mensaje) {
                     $this->flash->error($mensaje);
                 }
                 $this->db->rollback();
                 return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
             }
         }
     }
     /* ==================== ==================== ==================== ==================== */
     if (!$nuevoRemito->save()) {
         foreach ($nuevoRemito->getMessages() as $mensaje) {
             $this->flash->error($mensaje);
         }
         $this->db->rollback();
         return $this->dispatcher->forward(array("controller" => "remito", "action" => "nuevo", "params" => array($planilla_id)));
     }
     $this->db->commit();
     $this->flash->success('Remito creado satisfactoriamente');
     return $this->dispatcher->forward(array("controller" => "remito", "action" => "finaliza", "params" => array($planilla_id)));
 }