<?php
class Temple_expenses_type_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        
    }
    
    function get( $tbl_name, $where = array(), $args = array() ) {
        if (!is_array($where)) {
            $where = array();
        }
        $where['status'] = 1;
        
        if( !empty( $args['order_by'] ) ) :
            $this->db->order_by( $args['order_by'] );
        endif;
        
        if( !empty( $args['select'] ) ) :
            $this->db->select( $args['select'] );
        endif;
        
        $q = $this->db->get_where( $tbl_name, $where );
        return $q->result_array();
            
    }
    
    function list_all() {
        $this->db->select('et.type_name, ex.*',FALSE);
        $this->db->join(TBL_TEMPLE_EXP_TYPE . ' as et','et.id = ex.exp_type_id','left');
        $this->db->where('ex.status !=',2);
        return $this->db->get(TBL_TEMPLE_EXPENSES . ' as ex')->result_array();
    }
    
    function get_ex_by_id($id) {
        $this->db->select('et.type_name, ex.*',FALSE);
        $this->db->join(TBL_TEMPLE_EXP_TYPE . ' as et','et.id = ex.exp_type_id','left');
        $this->db->where('ex.id',$id);
        $this->db->where('ex.status !=',2);
        return $this->db->get(TBL_TEMPLE_EXPENSES . ' as ex')->row();
    }   
    
    function delete($id) {
        $args = array(
            'status'    => 2,
        );
        
        $this->db->update( TBL_TEMPLE_EXPENSES, $args, array('id' => $id) );
        
    }
    
    public function list_all_types() {
        $this->db->where('status !=',2);
        return $this->db->get(TBL_TEMPLE_EXP_TYPE)->result();
    }
    
    public  function insert($data){  
        $this->db->insert(TBL_TEMPLE_EXP_TYPE, $data);  
        return $this->db->insert_id();
    } 
       
   public function get_by_id($id) {
       $this->db->select('*, CASE status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
       $this->db->where('id', $id);
       return $this->db->get(TBL_TEMPLE_EXP_TYPE)->row();
   }
   
   public function update($data, $id){
       $this->db->where('id', $id);
       $this->db->update(TBL_TEMPLE_EXP_TYPE, $data);
       return $this->db->affected_rows();
   }
   
   public function get_types() {
       $this->db->where('status !=',2);
       return $this->db->get(TBL_TEMPLE_EXP_TYPE)->result();
   }
   
   public function type_name_exists($name = '', $id = '') {
        $this->db->where('type_name', trim($name));
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get(TBL_TEMPLE_EXP_TYPE)->num_rows() > 0)
            return 1;
        else 
            return 0;
    }
}