/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new HojaGastos(); $superTotal = 0; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['HojaGastos'])) { $datosCita = Citas::model()->findByPk($_GET['idCita']); $model->attributes = $_POST['HojaGastos']; $model->cita_id = $datosCita->id; $model->paciente_id = $datosCita->paciente_id; $model->observaciones = $_POST['HojaGastos']['observaciones']; $model->fecha = date("Y-m-d H:i:s"); $model->personal_id = Yii::app()->user->usuarioId; if ($model->save()) { //Los detalles de la Hoja de Gastos for ($i = 0; $i <= $_POST['variable']; $i++) { if (isset($_POST['producto_' . $i])) { $detalleC = new HojaGastosDetalle(); $detalleC->hoja_gastos_id = $model->id; $detalleC->producto_id = $_POST['elid_' . $i]; $detalleC->cantidad = $_POST['cantidad_' . $i]; $detalleC->save(); //Agregar a relación de Hoja de gstos $elCosto = ProductoInventario::model()->findByPk($_POST['producto_' . $i]); //Reducir inventario e inventario detalle $elProducto = InventarioPersonalDetalle::model()->find('id =' . $_POST['producto_' . $i] . " and inventario_personal_id =" . Yii::app()->user->usuarioId); $elProducto->cantidad = $elProducto->cantidad - $_POST['cantidad_' . $i]; $elProducto->save(); $elProductoDetalle = ProductoInventarioDetalle::model()->find('producto_inventario_id = ' . $elProducto->producto_id . ' and lote = "' . $_POST['lote_' . $i] . '"'); $elProductoDetalle->existencia = $elProductoDetalle->existencia - $_POST['cantidad_' . $i]; if ($elProductoDetalle->save()) { $elProducto = ProductoInventario::model()->findByPk($elProductoDetalle->producto_inventario_id); $elProducto->cantidad = $elProducto->cantidad - $_POST['cantidad_' . $i]; $elProducto->save(); } $superTotal = $superTotal + $elProducto->costo_iva; } } } $relacion = new RelacionHojaGastos(); $relacion->hoja_gastos_id = $model->id; $relacion->paciente_id = $model->paciente_id; $relacion->n_identificacion = $datosCita->n_identificacion; $relacion->hoja = "Hoja de Gastos"; $relacion->asistencial_id = $datosCita->personal_id; $relacion->cita_id = $datosCita->id; $relacion->linea_servicio_id = $datosCita->linea_servicio_id; $relacion->fecha = date("Y-m-d"); $relacion->fecha_hora = date("Y-m-d H:i:s"); $relacion->costo = $superTotal; $relacion->personal_id = Yii::app()->user->usuarioId; $relacion->save(); $this->redirect(array('view', 'id' => $model->id)); } $this->render('create', array('model' => $model)); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return RelacionHojaGastos the loaded model * @throws CHttpException */ public function loadModel($id) { $model = RelacionHojaGastos::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }