public function updateTranLoanById($data)
 {
     try {
         $db = $this->getAdapter();
         $db->beginTransaction();
         $session_user = new Zend_Session_Namespace('auth');
         $user_id = $session_user->user_id;
         $is_allinterest = 0;
         if (!empty($data['all_interest'])) {
             $is_allinterest = $data['all_interest'];
         }
         $arr = array('sender_id' => $data['sender'], 'invoice' => $data['invoice_no'], 'loan_date' => $data['loan_date'], 'user_id' => $user_id, 'status' => 1, 'is_allinterest' => $is_allinterest);
         $where = $db->quoteInto('b_id=?', $data['tran_id']);
         $this->update($arr, $where);
         $db_tran = new Application_Model_DbTable_DbCustomerLoan();
         $rows = $db_tran->getBorrowDetail($data['tran_id']);
         $dbc = new Application_Model_DbTable_DbCapital();
         $this->_name = 'cs_capital_detail';
         foreach ($rows as $key => $rs) {
             $row = $dbc->DetechCapitalExist($user_id, $rs['currency_type'], null);
             if (!empty($row)) {
                 $arr = array('amount' => $row['amount'] + $rs['loan_amount']);
                 $dbc->updateCurrentBalanceById($row['id'], $arr);
                 $where = ' tran_id = ' . $rs['id'] . ' AND tran_type = 5 ';
                 $this->delete($where);
                 //for delete capital detail
             }
         }
         $this->_name = 'cs_borrow_detail';
         $where = 'br_id = ' . $data['tran_id'];
         $this->delete($where);
         $process = 0;
         $amount = 0;
         for ($i = 1; $i < 4; $i++) {
             //for add capital detail and update current capital by staff
             if (!empty($data['borrow_dollar']) and $i == 1) {
                 $process = 1;
                 $curr_type = 1;
                 $amount = $data['borrow_dollar'] + ($data['debt_dollar'] - $data['old_dollar']);
                 if ($data['sender'] != $data['old_sender']) {
                     $amount = $data['borrow_dollar'] + $data['debt_dollar'];
                 }
                 $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_dollar'];
             } elseif (!empty($data['borrow_bath']) and $i == 2) {
                 $process = 1;
                 $curr_type = 2;
                 $amount = $data['borrow_bath'] + ($data['debt_bath'] - $data['old_bath']);
                 if ($data['sender'] != $data['old_sender']) {
                     $amount = $data['borrow_bath'] + $data['debt_bath'];
                 }
                 $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_bath'];
             } elseif (!empty($data['borrow_riel']) and $i == 3) {
                 $process = 1;
                 $curr_type = 3;
                 $amount = $data['borrow_riel'] + ($data['debt_khmer'] - $data['old_riel']);
                 if ($data['sender'] != $data['old_sender']) {
                     $amount = $data['borrow_riel'] + $data['debt_khmer'];
                 }
                 $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_riel'];
             }
             if ($process == 1) {
                 //with loan tran_type = 5
                 $arr = array('br_id' => $data['tran_id'], 'currency_type' => $curr_type, 'loan_amount' => $amount, 'laon_rate' => $interest_rate, 'BD' => $data['BD'], 'RD' => $data['RD'], 'RB' => $data['RB'], 'status' => 1);
                 $l_id = $this->insert($arr);
                 $arr = array('tran_id' => $l_id, 'tran_type' => 5, 'curr_type' => $curr_type, 'amount' => -$amount, 'user_id' => $user_id);
                 $dbc->addMoneyToCapitalDetail($arr);
                 $rs = $dbc->DetechCapitalExist($session_user->user_id, $curr_type, null);
                 if (!empty($rs)) {
                     //update old user
                     $arr = array('amount' => $rs['amount'] - $amount);
                     $dbc->updateCurrentBalanceById($rs['id'], $arr);
                 } else {
                     $date = date("Y-m-d H:i:s");
                     $arr = array('amount' => -$amount, 'currencyType' => $curr_type, 'userid' => $user_id, 'statusDate' => $date);
                     $dbc->AddCurrentBalanceById($arr);
                 }
             }
             $process = 0;
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
     }
 }
 public function editedAction()
 {
     $tr_id = $this->getRequest()->getParam('tr_id');
     $tr_id = empty($tr_id) ? 0 : $tr_id;
     $db = new Application_Model_DbTable_DbCustomerLoan();
     if ($this->getRequest()->isPost()) {
         try {
             $data = $this->getRequest()->getPost();
             $db->updateTranLoanById($data);
             Application_Form_FrmMessage::Sucessfull('ការកែប្រែប្រតិបត្តិការនេះ​​ជោគ​ជ័យ', self::REDIRECT_URL);
         } catch (Exception $e) {
             echo $e->getMessage();
             exit;
             Application_Form_FrmMessage::Sucessfull('ការកែប្រែប្រតិបត្តិការនេះបរាជ័យ', self::REDIRECT_URL);
         }
     }
     $rs = $db->getTranLaonById($tr_id);
     if (empty($rs)) {
         Application_Form_FrmMessage::Sucessfull('មិនអាចទាញយកទិន្នន័យមកកែប្រែបានទេ ', self::REDIRECT_URL . '/index/index');
     }
     $this->view->rs = $rs;
     $result = array();
     $db_tran = new Application_Model_DbTable_DbCustomerLoan();
     $rows = $db_tran->getBorrowDetail($tr_id);
     $result = array('amount_dollar' => '', 'rate_dollar' => '', 'amount_bath' => '', 'rate_bath' => '', 'amount_riel' => '', 'rate_riel' => '');
     foreach ($rows as $key => $rs) {
         if ($rs['currency_type'] == 1) {
             $result['amount_dollar'] = $rs['loan_amount'];
             $result['rate_dollar'] = $rs['laon_rate'];
         } elseif ($rs['currency_type'] == 2) {
             $result['amount_bath'] = $rs['loan_amount'];
             $result['rate_bath'] = $rs['laon_rate'];
         } else {
             $result['amount_riel'] = $rs['loan_amount'];
             $result['rate_riel'] = $rs['laon_rate'];
         }
     }
     $this->view->row = $result;
     // 		print_r($result);
     $session_user = new Zend_Session_Namespace('auth');
     $this->view->user_name = $session_user->last_name . ' ' . $session_user->first_name;
     $db_keycode = new Application_Model_DbTable_DbKeycode();
     $this->view->keycode = $db_keycode->getKeyCodeMiniInv();
     $sender = new Application_Model_DbTable_DbSender();
     $_sender = $sender->getAllSenderKbank(2);
     $this->view->sender = $_sender;
     $db_rate = new Application_Model_DbTable_DbRate();
     $this->view->rate = $db_rate->getCurrentRate();
     $cur = new Application_Model_DbTable_DbCurrencies();
     $this->view->currency = $cur->getCurrencyList();
 }