public function actionPartialfees()
 {
     if (isset($_POST['FinanceFees']) and isset($_POST['FinanceFees']['fees_paid'])) {
         $model = $this->loadModel($_POST['FinanceFees']['id']);
         $dt = date('Y-m-d');
         $model->saveAttributes(array('date' => $dt));
         $student = Students::model()->findByAttributes(array('id' => $_POST['FinanceFees']['student_id']));
         $collection = FinanceFeeCollections::model()->findByAttributes(array('id' => $_POST['FinanceFees']['fee_collection_id']));
         $check_admission_no = FinanceFeeParticulars::model()->findAllByAttributes(array('finance_fee_category_id' => $collection->fee_category_id, 'admission_no' => $student->admission_no));
         $to_student = "";
         if (count($check_admission_no) > 0) {
             // If any particular is present for this student
             $adm_amount = 0;
             foreach ($check_admission_no as $adm_no) {
                 $adm_amount = $adm_amount + $adm_no->amount;
             }
             $fees = $adm_amount;
         } else {
             // If any particular is present for this student category
             $check_student_category = FinanceFeeParticulars::model()->findAllByAttributes(array('finance_fee_category_id' => $collection->fee_category_id, 'student_category_id' => $student->student_category_id, 'admission_no' => ''));
             if (count($check_student_category) > 0) {
                 $cat_amount = 0;
                 foreach ($check_student_category as $stu_cat) {
                     $cat_amount = $cat_amount + $stu_cat->amount;
                 }
                 $fees = $cat_amount;
             } else {
                 //If no particular is present for this student or student category
                 $check_all = FinanceFeeParticulars::model()->findAllByAttributes(array('finance_fee_category_id' => $collection->fee_category_id, 'student_category_id' => NULL, 'admission_no' => ''));
                 if (count($check_all) > 0) {
                     $all_amount = 0;
                     foreach ($check_all as $all) {
                         $all_amount = $all_amount + $all->amount;
                     }
                     $fees = $all_amount;
                 } else {
                     $fees = 0;
                     // If no particular is found.
                 }
             }
         }
     } elseif (isset($_REQUEST['id'])) {
         $model = $this->loadModel($_REQUEST['id']);
     }
     // Flag to know if we will render the form or try to add
     // new jon.
     $flag = true;
     if (isset($_POST['FinanceFees']) and isset($_POST['FinanceFees']['fees_paid'])) {
         $flag = false;
         $fees_paid = $model->fees_paid + $_POST['FinanceFees']['fees_paid'];
         if ($model->saveAttributes(array('fees_paid' => $fees_paid))) {
             $transaction = new FinanceTransaction();
             $transaction->amount = $_POST['FinanceFees']['fees_paid'];
             $transaction->collection_id = $_POST['FinanceFees']['fee_collection_id'];
             $transaction->student_id = $_POST['FinanceFees']['student_id'];
             $transaction->transaction_date = date('Y-m-d');
             $transaction->save();
             if ($fees == $fees_paid) {
                 $model->saveAttributes(array('is_paid' => 1));
             }
             $sms_settings = SmsSettings::model()->findByAttributes(array('settings_key' => 'FeesEnabled'));
             if ($sms_settings->is_enabled == '1') {
                 // Checking if SMS is enabled.
                 $guardian = Guardians::model()->findByAttributes(array('ward_id' => $_POST['FinanceFees']['student_id']));
                 $student = Students::model()->findByAttributes(array('id' => $_POST['FinanceFees']['student_id']));
                 if (count($guardian) != 0 && $guardian->mobile_phone && $guardian->mobile_phone != "") {
                     $to = $guardian->mobile_phone;
                 } else {
                     if ($student->phone1) {
                         $to = $student->phone1;
                     } else {
                         if ($student->phone2) {
                             $to = $student->phone2;
                         }
                     }
                 }
                 $balance = $fees - $fees_paid > 0 ? $fees - $fees_paid : 0;
                 SmsSettings::model()->sendSmsFees($to, $student->first_name . ' ' . $student->last_name, $fees_paid, $balance);
                 if (defined('EMAIL_ALERT_ADDRESS')) {
                     UserModule::sendMail(constant('EMAIL_ALERT_ADDRESS'), UserModule::t("Student paid fees : {student_name}", array('{student_name}' => $student->first_name . ' ' . $student->last_name)), UserModule::t("Student has paid fees: {student_name} of Rs. {fee_amount}", array('{student_name}' => $old_model->firstname . " " . $old_model->lastname, 'fee_amount' => $fees_paid)));
                 }
             }
             echo CJSON::encode(array('status' => 'success'));
             exit;
         } else {
             echo CJSON::encode(array('status' => 'error'));
             exit;
         }
     }
     if ($flag) {
         Yii::app()->clientScript->scriptMap['jquery.js'] = false;
         $this->renderPartial('partialfees', array('model' => $model), false, true);
     }
 }