<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_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 chk_user_availability_sts($user_encrypt_id){	
 		$this->db->where('user_encrypt_id', $user_encrypt_id);
		$query = $this->db->get('users');
		return $query->num_rows();	
	}
	
	public function setup_account_entry(){
		$user_encrypt_id = $this->input->post('h_user_encrypt_id');
		$login_id = $this->input->post('login_id');
		$password = $this->input->post('password');
		if(isset($user_encrypt_id) && $user_encrypt_id!='' && isset($login_id) && $login_id!='' && isset($password) && $password!=''){			
			$this->db->where('is_deleted', 0);
			$this->db->where('user_encrypt_id != ', $user_encrypt_id);
			$this->db->where('login_id', $login_id);
			$query = $this->db->get('users');
			$num_rows=$query->num_rows();
			if($num_rows==0){ 			
				$data_arr=array("login_id"=>$login_id, "password"=>md5($password), "password_v"=>$password, "status"=>'0', "last_modification_date"=>time());
				$this->db->where('user_encrypt_id',$user_encrypt_id);
				$this->db->update('users',$data_arr);
				$this->session->set_flashdata('success', 'Your account has been created successfully.');
				return 'success';				
			}else{
				return 'Oops <strong>'.$login_id.'</strong> user id already exists.';
			}
		}else{
			return "Please provide all required field data.";
		}
	}
	
	public function transaction_details($txn_id){	
		$this->db->select('tm.*, acc.account_cur, acc.account_no, acc.available_balance');
		$this->db->from('transfer_money AS tm');
 		$this->db->where('tm.txn_id', $txn_id);
		$this->db->join('user_bank_accounts as acc', 'acc.account_id = tm.account_id', 'INNER');
		$query = $this->db->get();		
 		return $query->row();
	}
	
	public function transfer_history_listing($user_id){	
		$this->db->select('tm.*, acc.account_cur, acc.account_no');
		$this->db->from('transfer_money AS tm');
 		$this->db->where('tm.sent_by', $user_id);
		$this->db->join('user_bank_accounts as acc', 'acc.account_id = tm.account_id', 'INNER');
		$this->db->order_by('tm.tid', 'desc');
		$this->db->limit(10);
		$query = $this->db->get();		
 		return $query->result();
	}
	
	
	public function vectra_history_listing($user_id){	
		$this->db->select('tm.*, acc.account_cur, acc.account_no');
		$this->db->from('vectra_transfer_money AS tm');
 		$this->db->where('tm.sent_by', $user_id);
		$this->db->join('user_bank_accounts as acc', 'acc.account_id = tm.from_user_account_id', 'INNER');
		$this->db->order_by('tm.id', 'desc');
		$this->db->limit(7);
		$query = $this->db->get();		
 		return $query->result();
	}
	
	public function transfer_money_entry($user_id,$txn_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');
			
		}else{
			
			$accToArr = explode('|',$accountTo);
			$accTo = $accToArr[0];
			
			$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('uf_select_account');
		$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_pin = $this->input->post('uf_transfer_pin');
				$transfer_code_field_sts = $this->input->post('h_transfer_code_field_sts');
				
				if(($transfer_pin==$transfer_code && $transfer_code_field_sts==1) || ($transfer_code_field_sts==0)){
				
					$vat_code = $this->input->post('uf_vat_code');
					$vat_code_field_sts = $this->input->post('h_vat_code_field_sts');
					
					if(($vat_code==$db_vat_code && $vat_code_field_sts==1) || ($vat_code_field_sts==0)){
					
						$IMF_code = $this->input->post('uf_imf_code');
						$IMF_code_field_sts = $this->input->post('h_IMF_code_field_sts');
						
						if(($IMF_code==$db_IMF_code && $IMF_code_field_sts==1) || ($IMF_code_field_sts==0)){
					
							//$reference = $this->input->post('uf_reference');
 							
							$transfer_remark = $this->input->post('uf_transfer_remark');
							$payment_due_date = strtotime($this->input->post('uf_paymentDue'));
							$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, "transfer_remark"=>$transfer_remark, "payment_due_date"=>$payment_due_date, "transfer_pin"=>$transfer_pin, "sent_date"=>$txn_date, "sent_time"=>$sent_date);			
							$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;
							
							$user_name = $this->input->post('h_user_name');
							$user_email = $this->input->post('h_user_email');
							$subject = 'Payment Received';
							
							$message = '<div style="background-color: #f1f3f4; box-shadow: 0 0 10px #ccc; padding:40px;"><div style="font-size:15px; padding:5px 20px;background-color: #ffffff;"><p>Dear <strong>'.$user_name.',</strong></p>';
							$message.='<p style="line-height:25px;"><strong>'.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['csign'].' '.$transfer_money.' '.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['name'].'</strong> was successfully transfered to <strong>'.$bef_account_name.'</strong>.<br>'.date('d F Y',$sent_date).' at '.date('h:i A',$sent_date).'<br>Transaction ID - '.$random_txn_id.'<br>Payment Status - <strong>Completed</strong>';
							$message.='<hr><h4 style="margin-top:10px;">Your Transaction Details-</h4>';
							$message.='<table style="width:100%;">';
							$message.='<tr><td style="width:30%;padding:3px;">Account Number</td><td style="width:70%;padding:3px;">'.$account_no.'</td></tr>';
							$message.='<tr><td style="padding:3px;">Amount</td><td style="padding:3px;">'.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['name'].' '.number_format($transfer_money,2).'</td></tr>';
							
							if(isset($transactions_fees) && $transactions_fees!='' && $transactions_fees>0){$transactions_fees_f = $transactions_fees.' '.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['name'];}else{$transactions_fees_f = 'Free';}
							$message.='<tr><td style="padding:3px;">Transaction Charge</td><td style="padding:3px;">'.$transactions_fees_f.'</td></tr>';
							
							$message.='<tr><td style="padding:3px;">Receiver Name</td><td style="padding:3px;">'.$bef_account_name.'</td></tr>';
							$message.='<tr><td style="padding:3px;">Transaction Narration</td><td style="padding:3px;">'.$transfer_remark.'</td></tr>';
							$message.='<tr><td style="padding:3px;">Transaction ID</td><td style="padding:3px;">'.$random_txn_id.'</td></tr>';
							$message.='<tr><td style="padding:3px;">Transaction Date</td><td style="padding:3px;">'.date('M d, Y',$sent_date).' '.date('h:i:s A',$sent_date).'</td></tr>';
							$message.='<tr><td style="padding:3px;">Available Balance</td><td style="padding:3px;">'.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['csign'].' '.$remain_bal.' '.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['name'].'</td></tr>';
							$message.='</table></div></div>';
							send_mail($user_email,$message,$user_name,'',$subject);
							 
							$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));
							
							echo 'success|'.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['csign'].' '.$transfer_money.' '.$this->config->item('amt_currencies_array_config')[$act_details->account_cur]['name'].'##'.$random_txn_id; 
							
						}else{
							echo "error|Oops, your IMF code didn't matched.";
						}
					
					}else{
						echo "error|Oops, your vat code didn't matched.";
					}
				}else{
					echo "error|Oops, your transfer code didn't matched.";
				}
			
			}else{
				echo 'error|Oops, your account has no available balance to transfer money.';
			}
		
		}else{
			echo 'error|Oops, your account has been deactivated.';
		}
	}
		
	function check_login($auth_code){	
		$login_id = trim($this->input->post('login_id'));
		$password = $this->input->post('password');
 		if(isset($login_id) && $login_id!='' && isset($password) && $password!=''){
  			$where="login_id='".$login_id."'";
			$this->db->where($where);
			$this->db->where('password',md5($password));
			$this->db->order_by('user_id', 'desc');
			$query = $this->db->get('users');
			$count = $query->num_rows();			
			if($count>0){
				$row = $query->row();
				if($row->status==0){
 					$active_step = $this->input->post('active_step');
					if($active_step==1){ 
 						echo 'success';
					}else{
						$pet_name = $this->input->post('pet_name');
						if($pet_name==$row->pet_name){
							$sess_prefix = $this->config->item('sess_prefix');
							$session_data = array($sess_prefix.'sess_user_id'=> $row->user_id,$sess_prefix.'sess_customer_name'=> $row->customer_name,'logged_in'=>TRUE);							
							$this->session->set_userdata($session_data);
							echo 'success';
						}else{
							echo 'Sorry, you entered the wrong pet name.';
						}
					}				
 					
 				}else{
					echo 'Oops, your profile is not active. Contact Support';
				}
			}else{
				echo 'Sorry, you entered the wrong username and/or password.';
			}
		}else{			
			echo 'please provide proper username and password of required fields.';
		}
	}
	
	public function user_details($user_id){
		$this->db->where('user_id',$user_id);
		$query = $this->db->get('users');
		return $query->row();
	}
	
	public function change_acct_det($account_id){
		$this->db->where('account_id',$account_id);
		$query = $this->db->get('user_bank_accounts');
		return $query->row();
	}
	
	public function user_accounts_listing($user_id){
		//$this->db->where('status', '0');
		$this->db->where('is_deleted', '0');
		$this->db->where('user_id', $user_id);
		$this->db->order_by('account_id', 'asc');
		$query = $this->db->get('user_bank_accounts');
		return $query->result();
	}
	
	public function change_username($user_id){	
		$username = trim($this->input->post('up_txt_username'));
		if(isset($username) && $username!=''){ 						
			$this->db->where('user_id',$user_id); 
			$this->db->update('users',array("name"=>$username));
			$sess_prefix = $this->config->item('sess_prefix');						
			$this->session->set_userdata($sess_prefix.'sess_user_name',$username);				
			return 'success';	
		}else{
			return 'please provide data of required fields.';
		}
	}
	
	public function change_your_password($user_id){	
		$this->db->where('user_id', $user_id);
		$query_1 = $this->db->get('users');
		$row_user = $query_1->row();
 		$h_old_password = $row_user->password_v;
		$current_password = trim($this->input->post('current_password'));
		$password = trim($this->input->post('confirm_password'));		
		if(isset($h_old_password) && $h_old_password!='' && isset($current_password) && $current_password!='' && isset($password) && $password!=''){			
			if($h_old_password==$current_password){ 						
				$this->db->where('user_id',$user_id); 
				$this->db->update('users',array("password"=>md5($password),"password_v"=>$password));				
				return 'success';	
			}else{
				return 'Sorry, please enter your current password correct.';
			}		
		}else{			
			return 'please provide data of required fields.';
		}		
	}
	
	public function update_user_information($user_encrypt_id){
		$customer_name = trim($this->input->post('customer_name'));
		$gender = trim($this->input->post('gender'));
		$date_of_birth_arr = explode('-',$this->input->post('date_of_birth'));	
		$mm = $date_of_birth_arr[0];
		$dd = $date_of_birth_arr[1];
		$yy = $date_of_birth_arr[2];
		$date_of_birth = strtotime($yy.'-'.$mm.'-'.$dd);
		$mobile_number = $this->input->post('mobile_number');	
		$address = trim($this->input->post('address'));
		$email = $this->input->post('email');
 		$data_arr=array("customer_name"=>$customer_name, "gender"=>$gender, "email"=>$email, "date_of_birth"=>$date_of_birth, "address"=>$address, "mobile_number"=>$mobile_number, "last_modification_date"=>time());
		$this->db->where('user_encrypt_id',$user_encrypt_id);
		$this->db->update('users',$data_arr);
		$sess_prefix = $this->config->item('sess_prefix');							
		$this->session->set_userdata($sess_prefix.'sess_customer_name',$customer_name);
		$this->session->set_flashdata('success', '<strong>Success </strong> Your profile information has been update successfully.');
	}
	
	
	
	public function victra_transfer_money_entry($user_id,$txn_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');
		}else{
			$accountToArr = explode('|',$accountTo);
			$beneficiary_account_no = $accountToArr[0];
			$beneficiary_account_name = $accountToArr[1];
		}
		
		if($beneficiary_account_no=='' || $beneficiary_account_name==''){
			echo '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_code = $act_details->transfer_code;
				
				$transfer_pin = $this->input->post('transPin');
				$transfer_amt = $this->input->post('sendAmt');
				$transOccurDate = strtotime($this->input->post('transOccurDate')); 
				
				if($transfer_pin==$transfer_code){
				
				
					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, "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));
 														
						echo 'success|'.$random_txn_id;								
					
					}else{
						echo 'error|Oops, your account has no available balance to transfer money.';
					}
				
				}else{
					echo "error|Oops, your transfer pin didn't matched.";
				}
			
			}else{
				echo 'error|Oops, selected from account has been deactivated.';
			}
		
		}
	}
	
	public function my_bank_accounts_listing($user_id){
		$this->db->where('status', '0');
		$this->db->where('is_deleted', '0');
		$this->db->where('user_id', $user_id);
		$this->db->order_by('account_id', 'asc');
		$query = $this->db->get('user_bank_accounts');
		return $query->result_array();
	}
	
	public function unique_sent_account_no($user_id){
		$this->db->select('beneficiary_account_no, beneficiary_account_name, bef_image');
		$this->db->where('sent_by', $user_id);
		$this->db->group_by('beneficiary_account_no');
		$query = $this->db->get('vectra_transfer_money');
		return $query->result_array();
	}
	
	public function my_unique_beneficiary_accounts($user_id){
		$this->db->select('bef_iban_no, bef_account_name, bef_image');
		$this->db->where('bef_iban_no != ', '');
		if(isset($user_id) && $user_id!='' && $user_id>0){
			$this->db->where('sent_by', $user_id);
		}
		$this->db->group_by('bef_iban_no');
		$query = $this->db->get('transfer_money');
		return $query->result_array();
	}
	
	
}