/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new InventarioPersonal(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); //La migración // $todosProductos = ProductoInventario::model()->findAll("cantidad > 0"); // foreach ($todosProductos as $todos_productos) // { // $productoDetalle = new ProductoInventarioDetalle; // $productoDetalle->producto_inventario_id = $todos_productos->id; // $productoDetalle->lote = "---"; // $productoDetalle->cantidad_compra = $todos_productos->cantidad; // $productoDetalle->existencia = $todos_productos->cantidad; // $productoDetalle->save(); // } if (isset($_POST['InventarioPersonal'])) { $model->attributes = $_POST['InventarioPersonal']; $model->comentario = $_POST['InventarioPersonal']['comentario']; if ($model->save()) { //Los detalles de la Compra for ($i = 0; $i <= $_POST['variable']; $i++) { if (isset($_POST['producto_' . $i])) { $detalleC = new InventarioPersonalDetalle(); $detalleC->inventario_personal_id = $model->personal_id; $detalleC->producto_id = $_POST['producto_' . $i]; $detalleC->cantidad = $_POST['cantidad_' . $i]; $detalleC->lote = $_POST['lote_' . $i]; $detalleC->save(); //Reducir inventario general $elProducto = ProductoInventario::model()->findByPk($_POST['id_producto_' . $i]); $elProducto->cantidad = $elProducto->cantidad - $_POST['cantidad_' . $i]; $elProducto->save(); //Reducir inventario por lote $elProducto = ProductoInventarioDetalle::model()->findByPk($_POST['producto_' . $i]); $elProducto->existencia = $elProducto->existencia - $_POST['cantidad_' . $i]; $elProducto->save(); } } $this->redirect(array('view', 'id' => $model->personal_id)); } } $this->render('create', array('model' => $model)); }