private function saveAllRecords($loginRecord, $detailsRecord, $emailRecord, $timeRecord)
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         if ($timeRecord->save()) {
             $detailsRecord->user_details2time_details = $timeRecord->id;
             if ($detailsRecord->save()) {
                 $detailsRecord->unique_name = $detailsRecord->first_name . "-" . $detailsRecord->id;
                 if (!$detailsRecord->save()) {
                     throw new Exception('error');
                 }
                 //add in wallet 50 rupees for new registration
                 AppCommonWallet::createWalletRecord('credit', 50, 50, 'registration_bonus', null, $detailsRecord->id, date('Y-m-d H:i:s'), '2016-01-01 00:00:00', null, $detailsRecord->id, 'wallet');
                 //***
                 $loginRecord->user_login2user_details = $detailsRecord->id;
                 if ($loginRecord->save()) {
                     $emailRecord->link = $detailsRecord->id;
                     $emailRecord->link_table = "user_details";
                     if ($emailRecord->save()) {
                         $transaction->commit();
                         return true;
                     } else {
                         throw new Exception('error');
                     }
                 } else {
                     throw new Exception('error');
                 }
             } else {
                 throw new Exception('error');
             }
         } else {
             throw new Exception('error');
         }
     } catch (Exception $e) {
         Yii::ankFileSave($e->getMessage());
         Yii::app()->user->setFlash('registration', UserModule::t("Some problem happened. Kindly try again."));
         $transaction->rollback();
         $this->refresh();
     }
 }
 public static function debitAmountFromWallet($amount, $transactionMethodName, $currDate, $transactionMethodRecordUniqueId, $useTableFor, $userDetailsId)
 {
     if ($amount == 0) {
         return true;
     }
     $response = false;
     $amountToDeduct = $amount;
     if (isset($userDetailsId) && $userDetailsId) {
         $walletRecords = AWallet::model()->findAll(array('condition' => 't.is_deleted = "no" AND t.debit_or_credit = "credit" AND t.wallet2user_details = ' . $userDetailsId . ' AND t.use_table_for = "wallet" AND t.expiry_time_of_credited_amount > "' . $currDate . '" AND t.curr_available_amnt_if_credit_row > 0 ', 'order' => 't.expiry_time_of_credited_amount ASC, t.id ASC'));
         $availableAmount = 0;
         if (isset($walletRecords)) {
             foreach ($walletRecords as $key => $value) {
                 $availableAmount += $value->curr_available_amnt_if_credit_row;
             }
         }
         if (isset($walletRecords)) {
             if ($availableAmount >= $amountToDeduct) {
                 foreach ($walletRecords as $key => $value) {
                     if ($amountToDeduct == 0) {
                         break;
                     }
                     if ($value->curr_available_amnt_if_credit_row >= $amountToDeduct) {
                         $value->curr_available_amnt_if_credit_row = $value->curr_available_amnt_if_credit_row - $amountToDeduct;
                         $value->save();
                         //create debit record
                         AppCommonWallet::createWalletRecord('debit', $amountToDeduct, 0, $transactionMethodName, null, $transactionMethodRecordUniqueId, $currDate, $currDate, $value->id, $userDetailsId, $useTableFor);
                         $amountToDeduct = 0;
                     } else {
                         $amountToDeduct = $amountToDeduct - $value->curr_available_amnt_if_credit_row;
                         //create debit record
                         AppCommonWallet::createWalletRecord('debit', $value->curr_available_amnt_if_credit_row, 0, $transactionMethodName, null, $transactionMethodRecordUniqueId, $currDate, $currDate, $value->id, $userDetailsId, $useTableFor);
                         $value->curr_available_amnt_if_credit_row = 0;
                         $value->save();
                     }
                 }
             }
         }
     }
     if ($amountToDeduct == 0) {
         $response = true;
     }
     return $response;
 }