/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new FeesMasterTransaction();
     $fees_details = new FeesDetailsTable();
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation(array($model, $fees_details));
     if (isset($_POST['FeesDetailsTable'])) {
         //$model->attributes=$_POST['FeesMasterTransaction'];
         $fees_details->attributes = $_POST['FeesDetailsTable'];
         $fees_details->fees_details_created_by = Yii::app()->user->id;
         $fees_details->fees_details_created_date = new CDbExpression('NOW()');
         $fees_details->save();
         if (isset($_REQUEST['id'])) {
             $model->fees_master_id = $_REQUEST['id'];
         }
         $model->fees_desc_id = $fees_details->fees_details_id;
         if ($model->save()) {
             $this->redirect(array('feesMaster/view', 'id' => $model->fees_master_id));
         }
     }
     $this->render('create', array('model' => $model, 'fees_details' => $fees_details));
 }
 public function actionGenerateMultipleFees()
 {
     $model = new FeesMaster();
     $model->scenario = 'GenerateMultipleFees';
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation($model);
     $fees_master = new FeesMasterTransaction();
     $fees_details = new FeesDetailsTable();
     if (isset($_POST['FeesMaster']['fees_branch_id']) && isset($_POST['FeesMaster']['fees_quota_id']) && !empty($_POST['amounts']) && !empty($_POST['FeesMaster']['fees_academic_term_name_id'])) {
         $f = 0;
         $org_id = Yii::app()->user->getState('org_id');
         $checkboxbranchvalue = $_POST['FeesMaster']['fees_branch_id'];
         $checkboxquotavalue = $_POST['FeesMaster']['fees_quota_id'];
         $model->attributes = $_POST['FeesMaster'];
         $branchvalue = array();
         $quotavalue = array();
         $sem_array = AcademicTerm::model()->findByPk($model->fees_academic_term_name_id);
         foreach ($checkboxbranchvalue as $b) {
             foreach ($checkboxquotavalue as $q) {
                 $check_existing_record = FeesMaster::model()->findByAttributes(array('fees_quota_id' => $q, 'fees_branch_id' => $b, 'fees_academic_term_name_id' => $model->fees_academic_term_name_id, 'fees_organization_id' => $org_id));
                 if ($check_existing_record) {
                     foreach ($_POST['amounts'] as $index => $value) {
                         if ($value != null) {
                             $feesmastertransaction = Yii::app()->db->createCommand()->select('fees_desc_id')->from('fees_master_transaction fm')->join('fees_details_table fd', 'fd.fees_details_id=fm.fees_desc_id')->where('fees_master_id=' . $check_existing_record->fees_master_id . ' and fd.fees_details_name =' . $index)->queryRow();
                             //$fees_table = FeesDetailsTable::model()->findByAttributes(array('fees_details_name'=>$index,'fees_details_id' => $feesmastertransaction['fees_desc_id']));
                             if ($feesmastertransaction) {
                                 FeesDetailsTable::model()->updateAll(array('fees_details_amount' => $value), 'fees_details_id=' . $feesmastertransaction['fees_desc_id']);
                             } else {
                                 $fees_details->fees_details_name = $index;
                                 $fees_details->fees_details_amount = $value;
                                 $fees_details->fees_details_created_by = Yii::app()->user->id;
                                 $fees_details->fees_details_created_date = new CDbExpression('NOW()');
                                 $fees_master->fees_master_id = $check_existing_record->fees_master_id;
                                 $fees_details->setIsNewRecord(true);
                                 $fees_details->fees_details_id = null;
                                 $fees_details->save(false);
                                 $fees_master->fees_desc_id = $fees_details->fees_details_id;
                                 $fees_master->setIsNewRecord(true);
                                 $fees_master->fees_id = null;
                                 $fees_master->save(false);
                             }
                         }
                     }
                 } else {
                     $model->fees_branch_id = (int) $b;
                     $model->fees_academic_term_id = $sem_array->academic_term_period_id;
                     $model->fees_academic_term_name_id = $model->fees_academic_term_name_id;
                     $model->fees_quota_id = (int) $q;
                     $model->fees_master_name = Branch::model()->findByPk($b)->branch_alias . '-' . AcademicTerm::model()->findByPk($model->fees_academic_term_name_id)->academic_term_name . '-' . Quota::model()->findByPk($q)->quota_name . '-' . AcademicTermPeriod::model()->findByPk($sem_array->academic_term_period_id)->academic_term_period;
                     $model->fees_master_created_by = Yii::app()->user->id;
                     $model->fees_master_created_date = new CDbExpression('NOW()');
                     $model->fees_organization_id = $org_id;
                     $model->setIsNewRecord(true);
                     $model->fees_master_id = null;
                     $model->save();
                     foreach ($_POST['amounts'] as $index => $value) {
                         if ($value != null) {
                             $fees_details->fees_details_name = $index;
                             $fees_details->fees_details_amount = $value;
                             $fees_details->fees_details_created_by = Yii::app()->user->id;
                             $fees_details->fees_details_created_date = new CDbExpression('NOW()');
                             $fees_master->fees_master_id = $model->fees_master_id;
                             $fees_details->setIsNewRecord(true);
                             $fees_details->fees_details_id = null;
                             $fees_details->save(false);
                             $fees_master->fees_desc_id = $fees_details->fees_details_id;
                             $fees_master->setIsNewRecord(true);
                             $fees_master->fees_id = null;
                             $fees_master->save(false);
                         }
                     }
                 }
             }
         }
         $this->redirect(array('admin'));
     } else {
         $this->render('MultipleFeesGenerateForm', array('model' => $model));
     }
 }