예제 #1
0
 /**
  * Performs the work of inserting or updating the row in the database.
  *
  * If the object is new, it inserts it; otherwise an update is performed.
  * All related objects are also updated in this method.
  *
  * @param PropelPDO $con
  * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
  * @throws PropelException
  * @see        save()
  */
 protected function doSave(PropelPDO $con)
 {
     $affectedRows = 0;
     // initialize var to track total num of affected rows
     if (!$this->alreadyInSave) {
         $this->alreadyInSave = true;
         // We call the save method on the following object(s) if they
         // were passed to this object by their corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aLugarinventario !== null) {
             if ($this->aLugarinventario->isModified() || $this->aLugarinventario->isNew()) {
                 $affectedRows += $this->aLugarinventario->save($con);
             }
             $this->setLugarinventario($this->aLugarinventario);
         }
         if ($this->aTraspaso !== null) {
             if ($this->aTraspaso->isModified() || $this->aTraspaso->isNew()) {
                 $affectedRows += $this->aTraspaso->save($con);
             }
             $this->setTraspaso($this->aTraspaso);
         }
         if ($this->isNew() || $this->isModified()) {
             // persist changes
             if ($this->isNew()) {
                 $this->doInsert($con);
             } else {
                 $this->doUpdate($con);
             }
             $affectedRows += 1;
             $this->resetModified();
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
예제 #2
0
 public function editarAction()
 {
     //Cachamos el valor desde nuestro params
     $id = (int) $this->params()->fromRoute('id');
     //Verificamos que el Id articulo que se quiere modificar exista
     if (!\TraspasoQuery::create()->filterByIdinventariolugar($id)->exists()) {
         $id = 0;
     }
     //Si es incorrecto redireccionavos al action nuevo
     if (!$id) {
         return $this->redirect()->toRoute('almacen-transferencias', array('action' => 'nuevo'));
     }
     $request = $this->getRequest();
     $entity = \TraspasoQuery::create()->findOneByIdinventariolugar($id);
     $entity_array = $entity->toArray();
     $entity_array['detalles'] = array();
     $traspaso_detalles = $entity->getTraspasodetalless();
     foreach ($traspaso_detalles as $detalle) {
         $tmp['costo'] = $detalle->getLugarinventario()->getOrdencompradetalle()->getArticulovariante()->getArticulovarianteCosto();
         $tmp['idarticulovariante'] = $detalle->getLugarinventario()->getOrdencompradetalle()->getIdarticulovariante();
         $tmp['idlugarinventario'] = $detalle->getIdlugarinventario();
         $tmp['cantidad'] = (int) $detalle->getTraspasoCantidad();
         $tmp['descripcion'] = '';
         $idarticulovariante = $detalle->getLugarinventario()->getOrdencompradetalle()->getIdarticulovariante();
         $articulovariante = \ArticulovarianteQuery::create()->findPk($idarticulovariante);
         $articulovariantevalor = \ArticulovariantevalorQuery::create()->filterByIdarticulovariante($idarticulovariante)->find();
         $tmp['descripcion'] .= $articulovariante->getArticulo()->getArticuloNombre() . ' ';
         $propiedadCount = 0;
         foreach ($articulovariantevalor as $key => $value) {
             $propiedadCount++;
             $tmp['descripcion'] .= \PropiedadQuery::create()->findOneByIdpropiedad($value->getIdpropiedad())->getPropiedadNombre();
             //Propiedad
             $tmp['descripcion'] .= ':' . \PropiedadvalorQuery::create()->findOneByIdpropiedadvalor($value->getIdpropiedadvalor())->getPropiedadvalorNombre();
             //PropiedadValor
             if ($propiedadCount < $articulovariantevalor->count()) {
                 $tmp['descripcion'] .= ' - ';
             }
         }
         $tmp['caducidad'] = !is_null($detalle->getLugarinventario()->getOrdencompradetalle()->getOrdencompradetalleCaducidad('m/Y')) ? $detalle->getLugarinventario()->getOrdencompradetalle()->getOrdencompradetalleCaducidad('m/Y') : 'N/D';
         array_push($entity_array['detalles'], $tmp);
     }
     //Instanciamos nuestro lugares
     $lugaresCollection = \LugarQuery::create()->find();
     $lugarArray = array();
     foreach ($lugaresCollection as $lugar) {
         $lugarArray[] = array('value' => $lugar->getIdLugar(), 'name' => $lugar->getLugarNombre());
     }
     if ($request->isPost()) {
         //Si hicieron POST
         $post_data = $request->getPost();
         //echo '<pre>';var_dump($entity_array); echo '<pre>';exit();
         //Eliminamos nuestra anterior transferencia
         $traspaso = \TraspasoQuery::create()->findOneByIdinventariolugar($id);
         $traspaso_idorden = $traspaso->getIdOrdencompra();
         $traspaso_detalles = $traspaso->getTraspasodetalless();
         //Eliminamos los detalles y los regresamos a almacen general
         foreach ($traspaso_detalles as $detalle) {
             //Instanciamos nuestro el lugar inventario del detalle
             $lugar_inventario = $detalle->getLugarinventario();
             $current_stock = $lugar_inventario->getLugarinventarioCantidad();
             $new_stock = $current_stock + $detalle->getTraspasoCantidad();
             $lugar_inventario->setLugarinventarioCantidad($new_stock);
             $lugar_inventario->save();
         }
         //Eliminamos el traspaso y la orden
         $traspaso->delete();
         \OrdencompraQuery::create()->findPk($traspaso_idorden)->delete();
         /*
          * Creamos la nueva
          */
         $traspaso_fecha = \DateTime::createFromFormat('d/m/Y', $post_data['traspaso_fecha']);
         //Si existen items en el traspaso
         if (isset($post_data["traspaso_detalles"])) {
             //Creamos una nueva orden donde vamos a registrar el traspaso
             $orden = new \Ordencompra();
             $orden->setIdproveedor(1)->setOrdencompraNofactura($traspaso_fecha->getTimestamp())->setOrdencompraFecha($traspaso_fecha->format('Y-m-d h:m:s'))->setOrdencompraImporte(0.0)->setOrdencompraStatus('inventario')->setOrdencompraFechaapagar($traspaso_fecha->format('Y-m-d'))->save();
             //Generamos el traspaso
             $traspaso = new \Traspaso();
             $traspaso->setIdinventariolugar($entity_array['Idinventariolugar']);
             $traspaso_fecha = \DateTime::createFromFormat('d/m/Y', $post_data['traspaso_fecha']);
             $traspaso->setTraspasoFecha($traspaso_fecha->format('Y-m-d h:m:s'));
             $traspaso->setIdordencompra($orden->getIdordencompra());
             $traspaso->setTraspasoStatus('recibido');
             $traspaso->setIdlugarremitente($post_data['traspaso_idlugarorigen']);
             $traspaso->setIdlugardestinatario($post_data["traspaso_idlugardestinatario"]);
             $traspaso->save();
             $idtraspaso = $traspaso->getIdinventariolugar();
             //Se guardo con exito la orden
             if (!$orden->isPrimaryKeyNull()) {
                 $idorden = $orden->getIdordencompra();
                 //Modificamos el idorden de nuestro traspaso
                 //Insertamos los orden items
                 foreach ($post_data["traspaso_detalles"] as $detalle) {
                     //Agregamos los detalles del traspaso
                     $traspaso_detalle = new \Traspasodetalles();
                     $traspaso_detalle->setIdtraspaso($idtraspaso)->setIdlugarinventario($detalle["idlugarinventario"])->setTraspasoCantidad($detalle["traspaso_cantidad"])->save();
                     //Agregamos el order item a la orden
                     $orden_detalle = new \Ordencompradetalle();
                     $orden_detalle->setIdordencompra($idorden)->setIdarticulovariante($detalle["idarticulovariante"])->setOrdencompradetalleCantidad($detalle["traspaso_cantidad"])->setOrdencompradetalleCosto(0.0)->setOrdencompradetallePrecio(0.0)->setOrdencompradetalleImporte(0.0);
                     if ($detalle['caducidad'] != 'N/D') {
                         $caducidad = \DateTime::createFromFormat('m/Y', $detalle['caducidad']);
                         $orden_detalle->setOrdencompradetalleCaducidad($caducidad->format('Y-m-d'));
                     }
                     $orden_detalle->save();
                     //Insertamos en nuestro almacen destinatario
                     $lugar_inventario = new \Lugarinventario();
                     $lugar_inventario->setIdlugar($post_data["traspaso_idlugardestinatario"])->setIdordencompradetalle($orden_detalle->getIdordencompradetalle())->setLugarinventarioCantidad($orden_detalle->getOrdencompradetalleCantidad())->save();
                     //Restamos del lugarinventario remitente (almacen general)
                     $lugar_inventario = \LugarinventarioQuery::create()->findPk($detalle["idlugarinventario"]);
                     $current_stock = $lugar_inventario->getLugarinventarioCantidad();
                     $new_stock = $current_stock - $orden_detalle->getOrdencompradetalleCantidad();
                     $lugar_inventario->setLugarinventarioCantidad($new_stock)->save();
                 }
             }
         }
         //Agregamos un mensaje
         $this->flashMessenger()->addMessage('Transferencia modificada exitosamente!');
         return $this->getResponse()->setContent(\Zend\Json\Json::encode(array('response' => true)));
     }
     return new ViewModel(array('id' => $id, 'transferencia' => $entity_array, 'lugares' => $lugarArray));
 }