/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new ClienteFiador();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['ClienteFiador'])) {
         $model->attributes = $_POST['ClienteFiador'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Cliente'])) {
         $model->attributes = $_POST['Cliente'];
         $model->direccion_alternativa = $_POST['Cliente']['direccion_alternativa'];
         $usuario = Usuario::model()->findByPk($model->usuario_id);
         $cliente_fiador = null;
         $fiador = null;
         if ($usuario != null) {
             $usuario->nombre = $_POST['Cliente']['nombre'];
             $usuario->apellido = $_POST['Cliente']['apellido'];
             $usuario->email = $_POST['Cliente']['email'];
             $ok = false;
             if (isset($_POST['Cliente']['fiador_rut'])) {
                 $cliente_fiador = $model->clienteFiadors;
                 if ($cliente_fiador != null) {
                     $fiador = $cliente_fiador->fiador;
                 }
                 if (isset($_POST['Cliente']['fiador_rut'])) {
                     if ($fiador == null) {
                         $fiador = new Fiador();
                     }
                     $fiador->rut = $_POST['Cliente']['fiador_rut'];
                     $fiador->rut = Tools::removeDots($fiador->rut);
                     $fiador->nombre = $_POST['Cliente']['fiador_nombre'];
                     $fiador->apellido = $_POST['Cliente']['fiador_apellido'];
                     $fiador->email = $_POST['Cliente']['fiador_email'];
                     $fiador->telefono = $_POST['Cliente']['fiador_telefono'];
                     $fiador->direccion = $_POST['Cliente']['fiador_direccion'];
                     if ($fiador->validate()) {
                         if ($fiador->save()) {
                             $ok = true;
                         }
                     } else {
                         echo "<br>";
                         echo CHtml::errorSummary($fiador);
                     }
                 }
             }
             if ($model->validate()) {
                 if ($usuario->validate()) {
                     if ($usuario->save()) {
                         if ($model->save()) {
                             if ($ok) {
                                 if ($cliente_fiador == null) {
                                     $cliente_fiador = new ClienteFiador();
                                 }
                                 $cliente_fiador->cliente_id = $model->id;
                                 $cliente_fiador->fiador_id = $fiador->id;
                                 $cliente_fiador->save();
                             }
                             $this->redirect(array('view', 'id' => $model->id));
                         }
                     }
                 } else {
                     echo "<br>";
                     echo CHtml::errorSummary($usuario);
                 }
             } else {
                 echo "<br>";
                 echo CHtml::errorSummary($model);
             }
         }
     }
     $this->render('update', array('model' => $model));
 }