<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Transactions_mdl extends CI_Model {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('email');
		$config['protocol'] = 'sendmail';
        $config['mailpath'] = '/usr/sbin/sendmail';
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $this->email->initialize($config);
	}
	
	public function update_bef_image_entry(){
		
		if(isset($_FILES['befImg']['name']) && $_FILES['befImg']['name']!=''){
		
			$bef_iban_no = $this->input->post('h_bef_iban_no');
		
			$fil_exp_befImg = explode(".", $_FILES['befImg']['name']);
			$fileExt = strtolower(end($fil_exp_befImg));
			$fileName = time().".".$fileExt;				
			
			$path = './assets/upload/bef_images/';
			$config['file_name'] = $fileName;
			$config['upload_path'] = $path;
			$config['allowed_types'] = '*';
			$this->load->library('upload');
			$this->upload->initialize($config);
			$this->upload->do_upload('befImg');				
			$errors = $this->upload->display_errors();
			if(isset($errors) && $errors!=''){
				$this->session->set_flashdata('error', $errors);
			}else{				
				$this->db->where('bef_iban_no',$bef_iban_no); 
				$this->db->update('transfer_money', array("bef_image"=>$fileName));
				
				if(isset($bef_iban_no) && $bef_iban_no!=''){
					$old_upload_doc = trim($this->input->post('old_upload_doc'));
					if(isset($old_upload_doc) && $old_upload_doc!=''){
 						$chkFileExist = FCPATH.'assets/upload/bef_images/'.$old_upload_doc;
						if(file_exists($chkFileExist)){
							unlink($path.$old_upload_doc);
						}
					}
				}
				
				$this->session->set_flashdata('success', 'Image has been uploaded successfully.');
			}
		}
	}
	
	public function admin_transactions_beneficiary_listing(){
		$this->db->where('bef_iban_no != ', '');
		$this->db->group_by('bef_iban_no');
		$query = $this->db->get('transfer_money');
		return $query->result();
	}
	
	public function admin_transactions_vectra_beneficiary_listing(){
		$this->db->where('beneficiary_account_no != ', '');
		$this->db->group_by('beneficiary_account_no');
		$query = $this->db->get('vectra_transfer_money');
		return $query->result();
	}
	
	public function user_accounts_data(){
		$this->db->where('is_deleted', '0');
		$query = $this->db->get('users');
		return $query->result();
	}
	
	public function update_sts_entry(){
		$tid = $this->input->post('h_tid');
		$action_sts = $this->input->post('pstatus');
 		$this->db->where('tid',$tid);
		$this->db->update('transfer_money',array("action_sts"=>$action_sts));
		$this->session->set_flashdata('success', 'Status has been changed successfully.');
		return 'success';	
	}
	
	public function update_vectra_sts_entry(){
		$id = $this->input->post('h_id');
		$action_sts = $this->input->post('pstatus');
 		$this->db->where('id',$id);
		$this->db->update('vectra_transfer_money',array("action_sts"=>$action_sts));
		$this->session->set_flashdata('success', 'Status has been changed successfully.');
		return 'success';	
	}
	
	public function admin_transactions_listing(){
		$this->db->select('t.*, sfromu.customer_name as sender_user_name, ubnk.account_cur');
		$this->db->from('transfer_money as t');
		$this->db->order_by('t.tid', 'desc');
		$this->db->join('users as sfromu', 'sfromu.user_id = t.sent_by', 'INNER');
		$this->db->join('user_bank_accounts as ubnk', 'ubnk.account_id = t.account_id', 'INNER');
		$query = $this->db->get();
		return $query->result();
		
		/*$this->db->order_by('tid', 'desc');
		$query = $this->db->get('transfer_money');
		return $query->result();*/
	}
	
	public function admin_transactions_details($tid){
		$this->db->select('t.*, sfromu.customer_name, ubnk.*');
		$this->db->from('transfer_money as t');
		$this->db->where('t.tid', $tid);
		$this->db->order_by('t.tid', 'desc');
		$this->db->join('users as sfromu', 'sfromu.user_id = t.sent_by', 'INNER');
		$this->db->join('user_bank_accounts as ubnk', 'ubnk.account_id = t.account_id', 'INNER');
		$query = $this->db->get();
		return $query->row();
	}
	
	public function delete_trans($tid){		
		$this->db->where('tid',$tid);
		$this->db->delete('transfer_money');		
 		$this->session->set_flashdata('success', 'Transaction has been deleted successfully.');		
	}
	
	public function delete_vectra($id){		
		$this->db->where('id',$id);
		$this->db->delete('vectra_transfer_money');		
 		$this->session->set_flashdata('success', 'Transaction has been deleted successfully.');		
	}
	
	
	public function admin_vectra_transactions_listing(){
		$this->db->select('vt.*, u.customer_name as sender_user_name, ubnk.account_no as sender_acct_no, ubnk.account_cur');
		$this->db->from('vectra_transfer_money as vt');
		$this->db->order_by('vt.id', 'desc');
		$this->db->join('users as u', 'u.user_id = vt.sent_by', 'INNER');
		$this->db->join('user_bank_accounts as ubnk', 'ubnk.account_id = vt.from_user_account_id', 'INNER');
		$query = $this->db->get();
		return $query->result();
	}
	
	public function update_vectra_bef_image_entry(){
		
		if(isset($_FILES['befImg']['name']) && $_FILES['befImg']['name']!=''){
		
			$beneficiary_account_no = $this->input->post('h_beneficiary_account_no');
		
			$fil_exp_befImg = explode(".", $_FILES['befImg']['name']);
			$fileExt = strtolower(end($fil_exp_befImg));
			$fileName = time().".".$fileExt;				
			
			$path = './assets/upload/bef_images/';
			$config['file_name'] = $fileName;
			$config['upload_path'] = $path;
			$config['allowed_types'] = '*';
			$this->load->library('upload');
			$this->upload->initialize($config);
			$this->upload->do_upload('befImg');				
			$errors = $this->upload->display_errors();
			if(isset($errors) && $errors!=''){
				$this->session->set_flashdata('error', $errors);
			}else{				
				$this->db->where('beneficiary_account_no',$beneficiary_account_no); 
				$this->db->update('vectra_transfer_money', array("bef_image"=>$fileName));
				
				if(isset($bef_iban_no) && $bef_iban_no!=''){
					$old_upload_doc = trim($this->input->post('old_upload_doc'));
					if(isset($old_upload_doc) && $old_upload_doc!=''){
 						$chkFileExist = FCPATH.'assets/upload/bef_images/'.$old_upload_doc;
						if(file_exists($chkFileExist)){
							unlink($path.$old_upload_doc);
						}
					}
				}
				
				$this->session->set_flashdata('success', 'Image has been uploaded successfully.');
			}
		}
	}
	
	public function vectra_debit_entry($txn_id){
		
		$user_id = $this->input->post('user_id');
		$accountFrom = $this->input->post('accountFrom');
		$accountTo = $this->input->post('accountTo');
		
		if($accountTo=='other'){
			$beneficiary_account_no = $this->input->post('enterAcctNo');
			$beneficiary_account_name = $this->input->post('enterAcctName');
			$bef_image = '';
		}else{
			$accountToArr = explode('|',$accountTo);
			$beneficiary_account_no = $accountToArr[0];
			$beneficiary_account_name = $accountToArr[1];
			$bef_image = $accountToArr[2];
		}
		
		if($beneficiary_account_no=='' || $beneficiary_account_name==''){
			return 'error|Oops, enter the beneficiary name and account number.';
		}else{
		
			$this->db->where('account_id',$accountFrom);
			$query_accts = $this->db->get('user_bank_accounts');
			$act_details = $query_accts->row();
			$account_no = $act_details->account_no;
			$account_status = $act_details->status;
			
			if($account_status==0){
			
				$available_balance = $act_details->available_balance;
				
 				$transfer_amt = $this->input->post('sendAmt');
				$transOccurDate = 0;
				if(isset($_POST['transOccurDate']) && $_POST['transOccurDate']!=''){
					$transOccurDateArr = explode('/',$_POST['transOccurDate']);
					$transOccurDate = strtotime($transOccurDateArr[2].'-'.$transOccurDateArr[0].'-'.$transOccurDateArr[1]); 
				}			
  				
					if($available_balance>=$transfer_amt){
						
						$sent_date = strtotime(date('Y-m-d'));
						$sent_time = time();
								
						$this->db->insert('vectra_transfer_money', array("sent_by"=>$user_id, "from_user_account_id"=>$accountFrom, "beneficiary_account_no"=>$beneficiary_account_no, "beneficiary_account_name"=>$beneficiary_account_name, "bef_image"=>$bef_image, "trans_amt"=>$transfer_amt, "trans_occur_date"=>$transOccurDate, "sent_date"=>$sent_date, "sent_time"=>$sent_time));
						$tid=$this->db->insert_id();	
						$random_txn_id=$txn_id.$tid; 
						
						$this->db->where('id',$tid);
						$this->db->update('vectra_transfer_money',array("txn_id"=>$random_txn_id));
						
						$remain_bal = $available_balance-$transfer_amt;
						$this->db->where('account_id',$accountFrom);
						$this->db->update('user_bank_accounts',array("available_balance"=>$remain_bal));
 						$this->session->set_flashdata('success', 'Debited successfully.');								
						return 'success|'.$random_txn_id;								
					
					}else{
						return 'error|Oops, your account has no available balance to transfer money.';
					}
				
				 
			
			}else{
				return 'error|Oops, selected from account has been deactivated.';
			}
		
		}
	}
	
	public function debit_entry($txn_id){
		
		$user_id = $this->input->post('user_id');
		$accountTo = $this->input->post('accountTo');
		
		if($accountTo=='other'){
		
			$bef_account_name = $this->input->post('uf_account_name');
			$bef_iban_no = $this->input->post('uf_iban_no');
			$bef_bank_btc = $this->input->post('uf_bank_btc');
			$bef_bank_name_address = $this->input->post('uf_bank_name_address');
			$bef_bank_address = $this->input->post('uf_bank_address');
			$bef_image = '';
			
		}else{
			
			$accToArr = explode('|',$accountTo);
			$accTo = $accToArr[0];
			$bef_image = $accToArr[2];
			
			$this->db->where('bef_iban_no',$accTo);
			$qry_benf = $this->db->get('transfer_money');
			$bef_details = $qry_benf->row();
			
			$bef_account_name = $bef_details->bef_account_name;
			$bef_iban_no = $bef_details->bef_iban_no;
			$bef_bank_btc = $bef_details->bef_bank_btc;
			$bef_bank_name_address = $bef_details->bef_bank_name_address;
			$bef_bank_address = $bef_details->bef_bank_address;
			
		}
		
 		$account_id = $this->input->post('accountFrom');
		$this->db->where('account_id',$account_id);
		$query_accts = $this->db->get('user_bank_accounts');
		$act_details = $query_accts->row();
		$account_no = $act_details->account_no;
 		$account_status = $act_details->status;
		
		if($account_status==0){
		
			$available_balance = $act_details->available_balance;
			$transfer_code = $act_details->transfer_code;
			$db_vat_code = $act_details->vat_code;
			$db_IMF_code = $act_details->imf_code;
			
			$transfer_amt = $this->input->post('uf_transfer_amt');
			$transactions_fees = $this->input->post('h_transactions_fees');
			$transfer_money = $transfer_amt+$transactions_fees;
			
			if($available_balance>=$transfer_money){
								
 							
							$transfer_remark = $this->input->post('uf_transfer_remark');
							
							$payment_due_date = 0;
							if(isset($_POST['uf_paymentDue']) && $_POST['uf_paymentDue']!=''){
								$payment_due_dateArr = explode('/',$_POST['uf_paymentDue']);
								$payment_due_date = strtotime($payment_due_dateArr[2].'-'.$payment_due_dateArr[0].'-'.$payment_due_dateArr[1]); 
							}
							
							$txn_date = strtotime(date('Y-m-d'));
							$sent_date = time();
							$data=array("txn_date"=>$txn_date, "sent_by"=>$user_id, "account_id"=>$account_id, "total_trans_amt"=>$transfer_money, "trans_fees"=>$transactions_fees, "trans_amt_without_fee"=>$transfer_amt, "bef_account_name"=>$bef_account_name, "bef_iban_no"=>$bef_iban_no, "bef_bank_btc"=>$bef_bank_btc, "bef_bank_name_address"=>$bef_bank_name_address, "bef_bank_address"=>$bef_bank_address, "bef_image"=>$bef_image, "transfer_remark"=>$transfer_remark, "payment_due_date"=>$payment_due_date, "sent_date"=>$txn_date, "sent_time"=>$sent_date, "transfer_remark"=>$transfer_remark);			
							$this->db->insert('transfer_money',$data);
							$tid=$this->db->insert_id();	
							$random_txn_id=$txn_id.$tid;
							//$random_vat_code=$vat_code_one.$tid.$vat_code_two;
							$this->db->where('tid',$tid);
							$this->db->update('transfer_money',array("txn_id"=>$random_txn_id));
							
							$remain_bal = $available_balance-$transfer_money;
							
 							 
							$this->db->where('user_id',$user_id);
							$this->db->where('account_id',$account_id);
							$this->db->update('user_bank_accounts',array("available_balance"=>$remain_bal));
							
							return 'success|'.$random_txn_id; 
							
						 
					
				 
				 
			
			}else{
				return 'error|Oops, your account has no available balance to transfer money.';
			}
		
		}else{
			return 'error|Oops, your account has been deactivated.';
		}
		
	}
	
}