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

class User_account_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 admin_users_listing(){
		$this->db->where('is_deleted', '0');
		$this->db->order_by('user_id', 'desc');
		$query = $this->db->get('users');
		return $query->result();
	}
	
	public function user_accounts_listing($user_id){
		$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 user_fulldetails($user_encrypt_id){	
 		$this->db->where('user_encrypt_id', $user_encrypt_id);
		$query = $this->db->get('users');
		return $query->row();		
	}
	
	public function update_user_information($user_encrypt_id){
		$login_id = $this->input->post('login_id');
		$email = trim(strtolower($this->input->post('email')));	
		$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){	
			$password = $this->input->post('edit_password');	
			$customer_name = trim($this->input->post('customer_name'));
			$gender = trim($this->input->post('gender'));
			$date_of_birth = strtotime($this->input->post('date_of_birth'));	
			$mobile_number = $this->input->post('mobile_number');	
			$address = trim($this->input->post('address'));
			$status = $this->input->post('status');
			$pet_name = $this->input->post('pet_name');
			
			$transfer_code_field_sts = $this->input->post('transfer_code_field_sts');
			$transfer_code_user_field_label = $this->input->post('transfer_code_user_field_label');
			$vat_code_field_sts = $this->input->post('vat_code_field_sts');
			$vat_code_user_field_label = $this->input->post('vat_code_user_field_label');
			$imf_code_field_sts = $this->input->post('imf_code_field_sts');
			$imf_code_user_field_label = $this->input->post('imf_code_user_field_label');
			
			if(isset($password) && $password!=''){
				$data_arr=array("customer_name"=>$customer_name, "gender"=>$gender, "pet_name"=>$pet_name, "email"=>$email, "login_id"=>$login_id, "password"=>md5($password), "password_v"=>$password, "date_of_birth"=>$date_of_birth, "address"=>$address, "mobile_number"=>$mobile_number, "status"=>$status, "last_modification_date"=>time()
				, "transfer_code_field_sts"=>$transfer_code_field_sts, "transfer_code_user_field_label"=>$transfer_code_user_field_label, "vat_code_field_sts"=>$vat_code_field_sts, "vat_code_user_field_label"=>$vat_code_user_field_label,"imf_code_field_sts"=>$imf_code_field_sts, "imf_code_user_field_label"=>$imf_code_user_field_label);
			}else{
				$data_arr=array("customer_name"=>$customer_name, "gender"=>$gender, "pet_name"=>$pet_name, "email"=>$email, "login_id"=>$login_id, "date_of_birth"=>$date_of_birth, "address"=>$address, "mobile_number"=>$mobile_number, "status"=>$status, "last_modification_date"=>time()
				, "transfer_code_field_sts"=>$transfer_code_field_sts, "transfer_code_user_field_label"=>$transfer_code_user_field_label, "vat_code_field_sts"=>$vat_code_field_sts, "vat_code_user_field_label"=>$vat_code_user_field_label, "imf_code_field_sts"=>$imf_code_field_sts, "imf_code_user_field_label"=>$imf_code_user_field_label);
			}
			$this->db->where('user_encrypt_id',$user_encrypt_id);
			$this->db->update('users',$data_arr);
			
			$accounts_arr = $this->input->post('accounts_arr');
			if(count($accounts_arr)>0){
				foreach($accounts_arr as $account_id){					
					$account_no = $this->input->post('bank_account_no_'.$account_id);
					$account_cur = $this->input->post('bank_account_cur_'.$account_id);
					$account_category = $this->input->post('bank_account_category_'.$account_id);
					$minimum_balance = $this->input->post('bank_minimum_balance_'.$account_id);
					$transfer_code = $this->input->post('bank_transfer_code_'.$account_id);	
					$vat_code = $this->input->post('bank_vat_code_'.$account_id);	
					$imf_code = $this->input->post('bank_imf_code_'.$account_id);	
					$account_status = $this->input->post('account_status_'.$account_id);					
					$bank_data_arr=array("account_no"=>$account_no, "account_cur"=>$account_cur, "account_category"=>$account_category, "available_balance"=>$minimum_balance, "transfer_code"=>$transfer_code, "vat_code"=>$vat_code, "imf_code"=>$imf_code, "status"=>$account_status, "last_modification_date"=>time());
					$this->db->where('account_id',$account_id);
					$this->db->update('user_bank_accounts',$bank_data_arr);
				}
			}
			
			if(isset($_FILES['upload_doc']['name']) && $_FILES['upload_doc']['name']!=''){
				$path = './assets/upload/photo/';
				$fil_exp = explode(".", $_FILES['upload_doc']['name']);
				$fileExt = end($fil_exp);
				$fileName = time().".".$fileExt;
				$new_file_name=$fileName; 
				$imgresize = first_auto_height_common_ImageResize('200', '150', '150', 'upload_doc', $path, $new_file_name, $fileExt);		
				if(isset($imgresize) && $imgresize=='0'){						  
					
					$old_image_name = trim($this->input->post('old_image_name'));
					if(isset($old_image_name) && $old_image_name!=''){
						$path_sml = $path.'small/';	
						$unlink = unlink($path.$old_image_name);
						$unlink1 = unlink($path_sml.$old_image_name);	
					}
					$this->db->where('user_encrypt_id',$user_encrypt_id);
					$this->db->update('users',array("user_image"=>$new_file_name));	
					
				}else if(isset($imgresize) && $imgresize=='1'){						
					$this->session->set_flashdata('error', '<strong>Oops : </strong> unknown image extension.');
					redirect(base_url().$this->config->item('admin_directory_name').'/user_account/edit/'.$user_encrypt_id);
				}else if(isset($imgresize) && $imgresize=='2'){	
					$this->session->set_flashdata('error', '<strong>Error : </strong> you have exceeded the size limit.');	
					redirect(base_url().$this->config->item('admin_directory_name').'/user_account/edit/'.$user_encrypt_id);
				}
				
			}
			$this->session->set_flashdata('success', 'User "<strong>'.ucfirst($customer_name).'</strong>" information has been update successfully.');
		}else{
			$this->session->set_flashdata('error',  'Oops <strong>'.ucfirst($email).'</strong> user email address already exists.');
			redirect(base_url().$this->config->item('admin_directory_name').'/user_account/edit/'.$user_encrypt_id);
		}
	}
	
	
	
	public function credit_amt_entry($txn_id){		
		$user_id = $this->input->post('h_credit_user_id');
		$txn_date_arr = explode('/',$this->input->post('txt_txn_date'));		
		$txn_date = strtotime($txn_date_arr[2].'-'.$txn_date_arr[0].'-'.$txn_date_arr[1]);		
		$account_info = explode('|',$this->input->post('txt_credit_amt_in_acc'));
 		$account_id = $account_info[0];
 		$available_balance = $account_info[1];
		$account_cur = $account_info[2];
		$account_no = $account_info[3];
 		$credit_amt = $this->input->post('txt_credit_amt');
		$credit_desc = $this->input->post('txt_credit_desc');
		$new_available_balance = $available_balance+$credit_amt;
		$sent_date = strtotime(date('Y-m-d'));
		$txn_time = time();
		$credit_data_arr=array("txn_date"=>$txn_date, "sent_by"=>$user_id, "account_id"=>$account_id, "credit_debit_sts"=>'1', "total_trans_amt"=>$credit_amt, "sent_date"=>$sent_date, "sent_time"=>$txn_time, "transfer_remark"=>$credit_desc);
		$this->db->insert('transfer_money',$credit_data_arr);
		$tid=$this->db->insert_id();	
		$random_txn_id=$txn_id.$tid;
		$this->db->where('tid',$tid);
		$this->db->update('transfer_money',array("txn_id"=>$random_txn_id));
 		
		$this->db->where('account_id',$account_id);
		$this->db->update('user_bank_accounts',array("available_balance"=>$new_available_balance,"last_modification_date"=>time()));
		
/*$user_name = $this->input->post('h_user_name');
$user_email = $this->input->post('h_user_email');
$subject = 'Credit Amount';
$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>'.$credit_amt.' '.$this->config->item('amt_currencies_array_config')[$account_cur]['name'].'</strong> was successfully transfered into your account on '.date('d F Y',$sent_date).' at '.date('h:i A',$txn_time).'<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')[$account_cur]['name'].' '.number_format($credit_amt,2).' (Credit)</td></tr>';
$message.='<tr><td style="padding:3px;">Transaction Narration</td><td style="padding:3px;">'.$credit_desc.'</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',$txn_time).'</td></tr>';
$message.='<tr><td style="padding:3px;">Available Balance</td><td style="padding:3px;">'.$new_available_balance.' '.$this->config->item('amt_currencies_array_config')[$account_cur]['name'].'</td></tr>';
$message.='</table></div></div>';
send_mail($user_email,$message,$user_name,'',$subject);*/
		
		$this->session->set_flashdata('success', 'Amount has been credited successfully.');		
	}
	
	public function acct_entry(){
		$user_id = $this->input->post('h_user_id');
		$account_no = $this->input->post('bank_account_no');
		$this->db->where('is_deleted', 0);
		$this->db->where('account_no', $account_no);
		$query = $this->db->get('user_bank_accounts');
		$num_rows=$query->num_rows();
	 	if($num_rows==0){			
			$account_cur = $this->input->post('bank_account_cur');
			$account_category = $this->input->post('bank_account_category');
			$minimum_balance = $this->input->post('bank_minimum_balance');
			$transfer_code = $this->input->post('bank_transfer_code');
			$vat_code = $this->input->post('bank_vat_code');
			$imf_code = $this->input->post('bank_imf_code');
			$create_date = strtotime(date('Y-m-d'));
			$bank_data_arr=array("user_id"=>$user_id, "account_no"=>$account_no, "account_cur"=>$account_cur, "account_category"=>$account_category, "available_balance"=>$minimum_balance, "opening_acct_bal"=>$minimum_balance, "transfer_code"=>$transfer_code, "vat_code"=>$vat_code, "imf_code"=>$imf_code, "create_date"=>$create_date, "last_modification_date"=>$create_date);			
			$this->db->insert('user_bank_accounts',$bank_data_arr);
 			$this->session->set_flashdata('success', 'Bank account has been created successfully.');			
		}else{
			$this->session->set_flashdata('error',  'Oops <strong>'.ucfirst($bank_account_no).'</strong> bank account already exists.');
		}
	}
	
	public function user_entry(){	 	
		$login_id = $this->input->post('login_id');
		$email = trim(strtolower($this->input->post('email')));	
		$this->db->where('is_deleted', 0);
		$this->db->where('login_id', $login_id);
		$query = $this->db->get('users');
		$num_rows=$query->num_rows();
	 	if($num_rows==0){							
 			$password = $this->input->post('new_password');	
			$pet_name = $this->input->post('pet_name');	
			$customer_name = trim($this->input->post('customer_name'));
			$gender = trim($this->input->post('gender'));
			$date_of_birth = strtotime($this->input->post('date_of_birth'));	
			$mobile_number = $this->input->post('mobile_number');	
			$address = trim($this->input->post('address'));								
			$create_date = strtotime(date('Y-m-d'));
			$created_time = time();										
			$data_arr=array("customer_name"=>$customer_name, "gender"=>$gender, "pet_name"=>$pet_name, "email"=>$email, "login_id"=>$login_id, "password"=>md5($password), "password_v"=>$password, "date_of_birth"=>$date_of_birth, "address"=>$address, "mobile_number"=>$mobile_number, "create_date"=>$create_date, "last_modification_date"=>$create_date, "created_time"=>$created_time);			
			$this->db->insert('users',$data_arr);			
			$user_id=$this->db->insert_id();	
					
			$user_encrypt_id=md5($user_id).$user_id;
			$this->db->where('user_id',$user_id);
			$this->db->update('users',array("user_encrypt_id"=>$user_encrypt_id));
			
			$account_no = $this->input->post('bank_account_no');
			$account_cur = $this->input->post('bank_account_cur');
			$account_category = $this->input->post('bank_account_category');
			$minimum_balance = $this->input->post('bank_minimum_balance');
			$transfer_code = $this->input->post('bank_transfer_code');
			$vat_code = $this->input->post('bank_vat_code');
			$imf_code = $this->input->post('bank_imf_code');
			
			$bank_data_arr=array("user_id"=>$user_id, "account_no"=>$account_no, "account_cur"=>$account_cur, "account_category"=>$account_category, "available_balance"=>$minimum_balance, "opening_acct_bal"=>$minimum_balance, "transfer_code"=>$transfer_code, "vat_code"=>$vat_code, "imf_code"=>$imf_code, "create_date"=>$create_date, "last_modification_date"=>$create_date);			
			$this->db->insert('user_bank_accounts',$bank_data_arr);
 			$this->session->set_flashdata('success', '<strong>'.ucfirst($customer_name).'</strong> user account has been created successfully.');
			redirect(base_url().$this->config->item('admin_directory_name').'/user_account');
		}else{
			$this->session->set_flashdata('error',  'Oops <strong>'.ucfirst($email).'</strong> user email address already exists.');
			redirect(base_url().$this->config->item('admin_directory_name').'/user_account/create');
		}		
	}
	
 	public function delete_account($user_encrypt_id){		
		$this->db->where('user_encrypt_id',$user_encrypt_id);
		$this->db->update('users',array("is_deleted"=>'1', "last_modification_date"=>time()));		
 		$this->session->set_flashdata('success', 'User account has been deleted successfully.');		
	}	 
	
}