Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.99 KB | None | 0 0
  1. public function updateMasterStatus($borrowerid, $idloan, $status)
  2.     {
  3.  
  4.         $data['getFundingLoanByLoanId'] = $this->Loan_model->getFundingLoanByLoanId($idloan);
  5.         $data['getMasterLoan'] = $this->Loan_model->getDetailLoan($idloan, $borrowerid);
  6.         $realization = $data['getFundingLoanByLoanId']->total;
  7.         $interest_borrower = $data['getMasterLoan']->interest_borrower;
  8.         $termLoan = getTermLoan($idloan);
  9.  
  10.         $dataUpdate = array(
  11.             'status' => $status,
  12.             'modified_date' => date("Y-m-d H:i:s"),
  13.             'modified_by' => $_SESSION['email'],
  14.             'realization_amount' => $data['getFundingLoanByLoanId']->total,
  15.             'installment' => ($realization / $termLoan) + ($realization * $interest_borrower),
  16.             'base_installment' => $realization / $termLoan
  17.         );
  18.  
  19.         $dataUpdateBorrowerDetail = array(
  20.             'collateral_status' => 'yes'
  21.         );
  22.  
  23.         $data['update'] = $this->Loan_model->updateStatusLoan($idloan, $dataUpdate);
  24.         $data['update_borrower'] = $this->User_model->updateAccountStatusBorrower($dataUpdateBorrowerDetail, $borrowerid);
  25.  
  26.         $messages = array();
  27.         $badges = array();
  28.  
  29.         //jika aktif, insert table payment schedule
  30.         $installment = getInstallment($idloan);
  31.         $paymentDate = date("Y-m-d H:i:s");
  32.  
  33.         // Create timestamp for transaction references + unique random generated number
  34.         $t = microtime(true);
  35.         $micro = sprintf("%06d",($t - floor($t)) * 1000000);
  36.         $d = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
  37.  
  38.         $date_ref = $d->format("YmdHisu");
  39.         $transaction_ref = substr($date_ref, 0, -3).mt_rand(100, 999);
  40.  
  41.         if ($status == 'disbursed') {
  42.  
  43.  
  44.  
  45.             $amountDisbursed = $data['getFundingLoanByLoanId']->total - getConfiguration('ADMIN_FEE') - (getConfiguration('PROVISI_FEE') * $data['getFundingLoanByLoanId']->total);
  46.  
  47.             // Preparing insert data
  48.             $data_insert_borrower = array(
  49.                 'loan_id' => $idloan,
  50.                 'borrower_id' => $borrowerid,
  51.                 'ref' => 'disburse',
  52.                 'tag' => '',
  53.                 'credit' => abs($amountDisbursed),
  54.                 'debit' => 0,
  55.                 'status' => 'pending',
  56.                 'payment_schedule_id' => '',
  57.                 'create_date' => date("Y-m-d h:i:s")
  58.             );
  59.  
  60.             $data_insert_company = array(
  61.                 'loan_id' => $idloan,
  62.                 'ref' => 'disburse',
  63.                 'tag' => '',
  64.                 'credit' => abs($amountDisbursed),
  65.                 'debit' => 0,
  66.                 'status' => 'pending',
  67.                 'create_date' => date("Y-m-d h:i:s")
  68.             );
  69.  
  70.             $success = 0;
  71.             // Try this :
  72.             // Cek bank yang dituju apakah BNI atau yang lain
  73.             $borrower_doc = $this->User_model->getDocByBorrowerId($borrowerid);
  74.             // Jika kode bank adalah BNI maka gunakan API inhouse
  75.             if($borrower_doc[0]->bank_code == '009'){
  76.  
  77.                 // First of all, cross check account
  78.                 $inhouseInquiryParams = array(
  79.                     'client_id' => getConfiguration('BNI_CLIENT_ID'),
  80.                     'accountNo' => $borrower_doc[0]->bank_account_number
  81.                 );
  82.  
  83.                 $inquiry_result = $this->_getInhouseInquiry($inhouseInquiryParams);
  84.                 if($inquiry_result->responseCode == '001'){
  85.                     // Do Payment from escrow to borrower bank
  86.                     // yyyyMMddHHmmssSSSXXX
  87.                     $paymentParams = array(
  88.                         'client_id' => getConfiguration('BNI_CLIENT_ID'),
  89.                         'customer_ref_no' => $transaction_ref,
  90.                         'debit_account_no' => $borrower_doc[0]->bank_account_number,
  91.                         'credit_account_no' => getConfiguration('ESCROW_ACCOUNT'),
  92.                         'amount' => abs($amountDisbursed),
  93.                         'valued_date' => $date_ref
  94.                     );
  95.  
  96.                     $this->db->trans_start();
  97.                     $trans_company = $this->Loan_model->saveTransactionCompany($data_insert_company);
  98.                     $trans_borrower = $this->Loan_model->saveTransactionBorrower($data_insert_borrower);
  99.                    
  100.                     $this->db->trans_complete();
  101.                     if ($this->db->trans_status() === FALSE){
  102.                             $this->db->trans_rollback();
  103.                             array_push($messages, 'Gagal memproses transaksi.');
  104.                             array_push($badges, 1);
  105.                             $this->session->set_flashdata('message', $messages);
  106.                             $this->session->set_flashdata('notif_status', $badges);
  107.                             redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  108.                             exit(1);
  109.                     }
  110.                     else {
  111.                         $this->db->trans_commit();
  112.                     }    
  113.  
  114.                     $payment_result = $this->_doPayment($paymentParams);
  115.                     if($payment_result->responseCode == '001'){
  116.                         // Insert this payment reference to transaction table
  117.                         $customer_ref = $payment_result->customerReference;
  118.                         $success = 1;
  119.                     } else {
  120.                         // Payment unsuccessfull
  121.                         $error_message = $payment_result->errorMessage;
  122.                         array_push($messages, 'Error. '.$error_message);
  123.                         array_push($badges, 0);
  124.                         $this->session->set_flashdata('message', $messages);
  125.                         $this->session->set_flashdata('notif_status', $badges);
  126.                         redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  127.                     }
  128.                 } else {
  129.                     // Account / Rekening is not valid
  130.                     $error_message = $inquiry_result->errorMessage;
  131.                     array_push($messages, 'Error. '.$error_message);
  132.                     array_push($badges, 0);
  133.                     $this->session->set_flashdata('message', $messages);
  134.                     $this->session->set_flashdata('notif_status', $badges);
  135.                     redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  136.                 }
  137.  
  138.             } else {
  139.                 // Check others rekening first. is according to defined in system?
  140.                 $interbankInquiryParams = array(
  141.                     'customer_ref_no' => $transaction_ref,  
  142.                     'account_num' => getConfiguration('ESCROW_ACCOUNT'),  
  143.                     'dest_bank_code' => $borrower_doc[0]->bank_code,            
  144.                     'dest_account_num' => $borrower_doc[0]->bank_account_number
  145.                 );
  146.  
  147.                 $inquiry_result = $this->_getInterbankInquiry($interbankInquiryParams);
  148.                
  149.                 if($inquiry_result->responseCode == '001'){
  150.                     $interbankPaymentParams = array(
  151.                         'customerReferenceNumber' => $transaction_ref,  
  152.                         'amount' => abs($amountDisbursed),
  153.                         'valuedDate' => $date_ref,  
  154.                         'destinationAccountNum' => $inquiry_result->destinationAccountNum,  
  155.                         'destinationAccountName' => $inquiry_result->destinationAccountName,  
  156.                         'destinationBankCode' => $borrower_doc[0]->bank_code,  
  157.                         'destinationBankName' => $inquiry_result->destinationBankName,  
  158.                         'accountNum' => getConfiguration('ESCROW_ACCOUNT'),  
  159.                         'retrievalReffNum' => $inquiry_result->retrievalReffNum
  160.                     );
  161.  
  162.                     $this->db->trans_start();
  163.                     $trans_company = $this->Loan_model->saveTransactionCompany($data_insert_company);
  164.                     $trans_borrower = $this->Loan_model->saveTransactionBorrower($data_insert_borrower);
  165.                    
  166.                     $this->db->trans_complete();
  167.                     if ($this->db->trans_status() === FALSE){
  168.                             $this->db->trans_rollback();
  169.                             array_push($messages, 'Gagal memproses transaksi.');
  170.                             array_push($badges, 1);
  171.                             $this->session->set_flashdata('message', $messages);
  172.                             $this->session->set_flashdata('notif_status', $badges);
  173.                             redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  174.                             exit(1);
  175.                     }
  176.                     else {
  177.                         $this->db->trans_commit();
  178.                     }
  179.                
  180.                     $payment_result = $this->_getInterbankPayment($interbankPaymentParams);
  181.                     if($payment_result->responseCode == '001'){
  182.                         // Insert this payment reference to transaction table and credit to borrower
  183.                         $customer_ref = $payment_result->customerReffNum;
  184.                         $success = 1;
  185.  
  186.                     } else {
  187.                         // Payment unsuccessfull
  188.                         $error_message = $payment_result->errorMessage;
  189.                         array_push($messages, 'Error. '.$error_message);
  190.                         array_push($badges, 0);
  191.                         $this->session->set_flashdata('message', $messages);
  192.                         $this->session->set_flashdata('notif_status', $badges);
  193.                         redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  194.                     }
  195.                 } else {
  196.                     // Account / Rekening is not valid
  197.                     $error_message = $inquiry_result->errorMessage;
  198.                     array_push($messages, 'Error. '.$error_message);
  199.                     array_push($badges, 0);
  200.                     $this->session->set_flashdata('message', $messages);
  201.                     $this->session->set_flashdata('notif_status', $badges);
  202.                     redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  203.                 }
  204.  
  205.             }
  206.  
  207.             if($success == 1){
  208.                 // Get the insert id
  209.                 $trans_borrower_id = $trans_borrower[1];
  210.                 $trans_company_id = $trans_company[1];
  211.                 // Update to borrower and company transactions
  212.                 $data_company = array(
  213.                     'ref' => 'disburse',
  214.                     'credit' => abs($amountDisbursed),
  215.                     'debit' => 0,
  216.                     'status' => 'success',
  217.                     'bni_customer_ref' => $customer_ref
  218.                 );
  219.  
  220.                 $data_borrower = array(
  221.                     'ref' => 'disburse',
  222.                     'credit' => abs($amountDisbursed),
  223.                     'debit' => 0,
  224.                     'status' => 'success',
  225.                     'bni_customer_ref' => $customer_ref
  226.                 );
  227.  
  228.                 $this->Loan_model->updateTransaction($trans_borrower_id, $data_borrower, 'borrower');
  229.                 $this->Loan_model->updateTransaction($trans_company_id, $data_company, 'company');
  230.  
  231.                 // Write Log
  232.                 $dataLog = array(
  233.                     'loan_id' => $idloan,
  234.                     'borrower_id' => $borrowerid,
  235.                     'investor_id' => '',
  236.                     'message' => 'Berhasil. Pencairan dana sebesar Rp.'.abs($amountDisbursed),
  237.                     'created_date' => date("Y-m-d h:i:s")
  238.                 );
  239.  
  240.                 $log = $this->Support_model->writeLogLoan($dataLog);
  241.  
  242.                 $message = $payment_result->responseMessage;
  243.                 array_push($messages, 'Success. '.$message);
  244.                 array_push($badges, 1);
  245.                 $this->session->set_flashdata('message', $messages);
  246.                 $this->session->set_flashdata('notif_status', $badges);
  247.             }
  248.  
  249.             for ($i = 1; $i <= $termLoan; $i++) {
  250.                 $schedule_date = date('Y-m-d H:i:s', strtotime('+' . $i . ' month', strtotime($paymentDate)));
  251.  
  252.                 $dataSchedule = array(
  253.                     'loan_id' => $idloan,
  254.                     'installment' => $installment,
  255.                     'installment_date' => $schedule_date,
  256.                     'month' => $i,
  257.                     'created_date' => date("Y-m-d H:i:s")
  258.                 );
  259.  
  260.                 $data['save_schedule'] = $this->Loan_model->saveScheduleLoan($dataSchedule);
  261.             }
  262.  
  263.             redirect(base_url() . "loan_manager/disburseLoan/" . $borrowerid . "/" . $idloan, "refresh");
  264.             // redirect(base_url() . "loan_manager/loanmasteredit/" . $borrowerid . "/" . $idloan, "refresh");
  265.  
  266.         }
  267.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement