<?php

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

class Bankrec_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    /**
     * This funtion takes id as a parameter and will fetch the record.
     * If id is not provided, then it will fetch all the records form the table.
     * @param int $id
     * @return mixed
     */
    public function get($id = null) {

        $userdata = $this->customlib->getUserData();
        $role_id = $userdata["role_id"];
        $carray = array();

            $this->db->select()->from('bankrec');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
            if ($id != null) {
                $grouplist = $query->row_array();
            } else {
                $grouplist = $query->result_array();
            }
            // echo $this->db->last_query();
            // exit();
        return $grouplist;
    }

      //we will use the updating INFO FO
      public function update($id)  
      {  
            $this->db->select()->from('bankrec');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
			
         return $query;  
      }

      public function select()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('bankrec');  
         return $query;  
      }  
	  
	  public function select_where_cleared($cashbook)  
      { 

		$active = 1;
	    $queryme = " SELECT MAX(id) as maxid FROM bankrec";
		$query1d = $this->db->query($queryme);															 
		$result = $query1d->row();
		$id = $result->maxid;
		$query = " SELECT * FROM bankrec WHERE cashbook = '$cashbook' and id='$id'";
		
		$query = $this->db->query($query);
				
		return $query;			
		  
      } 
	  
      //we will use the updating INFO FO
      public function update_bankrec_yrperiod($id)  
      {  
            $this->db->select()->from('bankrec_yrperiod');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
			
         return $query;  
      }	  
	  public function select_report()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('bankrec');  
         return $query;  
      }	  
	  
	  public function select_bankrec_yrperiod()  
      {  
	  	$query = " SELECT * from bankrec_yrperiod ORDER BY year ASC";
		
		$query = $this->db->query($query);
				
		return $query;
         //data is retrive from this query  
         $query = $this->db->get('bankrec_yrperiod');  
         return $query;  
      }
    /**
     * This function will delete the record based on the id
     * @param $id
     */
    public function remove($id) {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->delete('bankrec_yrperiod'); //class record delete.

       // $this->db->where('group_id', $id);
       // $this->db->delete('uniform_group'); //class_sections record delete.

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
        return TRUE;
    }

    /**
     * This function will take the post data passed from the controller
     * If id is present, then it will do an update
     * else an insert. One function doing both add and edit.
     * @param $data
     */
    public function add($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('bankrec', $data);
        } else {
            $this->db->insert('bankrec', $data);
        }
    }      
	public function add_bankrec_yrperiod($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('bankrec_yrperiod', $data);
        } else {
            $this->db->insert('bankrec_yrperiod', $data);
        }
    }   

    function check_data_exists($data) {
        $this->db->where('bankrec', $data);

        $query = $this->db->get('bankrec');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

    public function class_exists($str) {

        $class = $this->security->xss_clean($str);
        $res = $this->check_data_exists($class);

        if ($res) {
            $pre_class_id = $this->input->post('pre_class_id');
            if (isset($pre_class_id)) {
                if ($res->id == $pre_class_id) {
                    return TRUE;
                }
            }
            $this->form_validation->set_message('class_exists', 'Record already exists');
            return FALSE;
        } else {
            return TRUE;
        }
    }
//CODE FOR UPDATING CASHBOOK TABLE

 public function update_cashbook_table($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('cashbook', $data);
        } else {
            $this->db->insert('cashbook', $data);
        }	 
    }
	
// CODE FOR SEARCHING BY DATE

 public function bankrec_search_bydate($cashbook_account, $find) {
		$this->db->select('*');
		$this->db->from('cashbook');
		$this->db->where('cashbook = ',  $cashbook_account);
		$this->db->where('date = ', $find);
		$query = $this->db->get();

        return $query;
    }
	
	// CODE FOR SEARCHING BY REFERENCE
 public function bankrec_search_all($cashbook_account, $period,$year) {
	 
		$query = " SELECT `id`,`date`,`cashbook`, `description`, `account`, `module`, `receipt`, `currency`, `debit`, `credit`, `bankrec`, 
        MONTH (date) AS month
        FROM cashbook
		WHERE cashbook = '$cashbook_account' and bankrec='0' and MONTH(date) = '$period' and YEAR(date)='$year'";
		
		$query = $this->db->query($query);
				
		return $query;									
    }	// CODE FOR SEARCHING BY REFERENCE

 public function bankrec_search_by_reference($cashbook_account, $period,$year, $find) {
	 
		$query = "
		SELECT `id`,`date`,`cashbook`, `description`, `account`, `module`, `receipt`, `currency`, `debit`, `credit`, `bankrec`, 
        MONTH (date) AS month
        FROM cashbook
		WHERE cashbook = '$cashbook_account' and bankrec='0' and receipt = '$find' and MONTH(date) = '$period' and YEAR(date)='$year'";
		
		$query = $this->db->query($query);
				
		return $query;									
    }	
	
	// CODE FOR SEARCHING BY PAYMENTS

 public function bankrec_search_by_payments($cashbook_account, $period,$year, $find) {

		$query = "
		SELECT `id`,`date`,`cashbook`, `description`, `account`, `module`, `receipt`, `currency`, `debit`, `credit`, `bankrec`, 
        MONTH (date) AS month
        FROM cashbook
		WHERE cashbook = '$cashbook_account' and bankrec ='0' and credit = '$find' and MONTH(date) = '$period' and YEAR(date)='$year'";
		
		$query = $this->db->query($query);
		return $query;		
    } 	
	
	
	// CODE FOR SEARCHING BY PAYMENTS
	
	public function bankrec_search_by_deposits($cashbook_account, $period,$year, $find) {
	 
		$query = "
		SELECT `id`,`date`,`cashbook`, `description`, `account`, `module`, `receipt`, `currency`, `debit`, `credit`, `bankrec`, 
        MONTH (date) AS month
        FROM cashbook
		WHERE cashbook = '$cashbook_account' and bankrec ='0' and debit = '$find' and MONTH(date) = '$period' and YEAR(date)='$year'";
		
		$query = $this->db->query($query);
		return $query;
    }
	
	
}
