<?php
class Temple_expenses_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('ex.*',FALSE);
        $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('ex.*',FALSE);
        $this->db->where('ex.id',$id);
        $this->db->where('ex.status !=',2);
        return $this->db->get(TBL_TEMPLE_EXPENSES . ' as ex')->row();
    }
	
	function manipulate() {
        $post   		= $this->input->post();
        $date_now 		= date("Y-m-d H:i:s");
        $session_data 	= $this->session->userdata( TBL_PREFIX . 'user_logged_in' );
        $segments	 	= $this->uri->segments;
        $page   		= $segments[2];
        $is_add 		= $page == 'add' || $page == 'insert' ? TRUE : FALSE;
        $db_id  		= $is_add ? '' : $segments[3];
		
        $d 				= explode('-', $post['exp_date']);
		$d2				= explode('-', $post['func_date']);       
       
        $args = array(
            'exp_type_name'   => $post['exp_type_name'],
            'exp_type_id'     => $post['exp_type'],
            'expenses_for'    => $post['exp_for'],
            'exp_subject'     => $post['sub_name'],
			'exp_name'     	  => $post['name'],			
            'exp_date'        => $d[2].'-'.$d[1].'-'.$d[0], 
			'exp_func_date'   => $d2[2].'-'.$d2[1].'-'.$d2[0], 
			'exp_event_name'  => $post['event_name'], 
			'exp_desc'        => $post['exp_desc'],
            'exp_amount'      => $post['exp_amount'],             
            'status'      => 1
        );
        
        if( $is_add ) :
            // Insert            
            $args['created_on'] = $date_now;
            $args['created_by'] = $session_data['user_id'];

            $this->db->insert( TBL_TEMPLE_EXPENSES, $args );
            $db_id = $this->db->insert_id();
            
        elseif( !empty( $db_id ) ):
            // Update
            $args['updated_on'] = $date_now;
            $args['updated_by'] = $session_data['user_id'];

            $this->db->update( TBL_TEMPLE_EXPENSES, $args, array( 'id' => $db_id ) );

        endif;
    }
	
    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_EXPENSES, $data);  
        return $this->db->insert_id();
    } 
    
    public function type_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get(TBL_TEMPLE_EXP_TYPE)->num_rows();
   }
   
   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_EXPENSES)->row();
   }
   
   public function update($data, $id){
       $this->db->where('id', $id);
       $this->db->update(TBL_TEMPLE_EXPENSES, $data);
       return $this->db->affected_rows();
   }
   
   public function get_types() {
       $this->db->where('status',1);
       return $this->db->get(TBL_TEMPLE_EXP_TYPE)->result();
   }    
}