<?php

/* 
 * Author       : Jai K
 * Purpose      : Model for Expenses
 * Created On   : 2017-01-25 18:40
 */

class 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('et.type_name, ex.*',FALSE);
        $this->db->join(TBL_EXP_TYPE . ' as et','et.id = ex.exp_type_id','left');
        $this->db->where('ex.status !=',2);
        return $this->db->get(TBL_EXPENSES . ' as ex')->result_array();
    }
    
    function get_ex_by_id($id) {
        $this->db->select('et.type_name, ex.*',FALSE);
        $this->db->join(TBL_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_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];
        
        //echo "<pre>Data: "; print_r( $post ); die;
        $d = explode('-', $post['exp_date']);
        
        if( $post['exp_type'] == 'add' ) :
            $exp_type = $post['exp_type_add'];
        else :
            $this->db->select('exp_type');
            $this->db->limit(1);
            $q = $this->db->get_where( TBL_EXPENSES, array('exp_type_slug' => $post['exp_type'] ) );
            $exp_type = $q->row_array()['exp_type'];
        endif;
        
        if( $post['exp_sub'] == 'add' ) :
            $exp_sub = $post['exp_sub_add'];
        else :
            $this->db->select('exp_sub');
            $this->db->limit(1);
            $q = $this->db->get_where( TBL_EXPENSES, array('exp_sub_slug' => $post['exp_sub'] ) );
            $exp_sub = $q->row_array()['exp_sub'];
        endif;
        
        $exp_type_name = $this->get_by_id($post['exp_type']);
        $args = array(
            'exp_type'        => $exp_type_name->type_name,
            'exp_type_id'     => $post['exp_type'],
            'exp_sub'         => $exp_sub, 
            'exp_name'        => $post['exp_name'],
            'exp_date'        => $d[2].'-'.$d[1].'-'.$d[0], 
            'exp_amount'      => $post['exp_amount'],
            'exp_desc'        => $post['exp_desc'],
            'exp_slug'        => get_var_name( $post['exp_name'], '_' ),  
            'status'          => $post['status'],
        );
        
        $args['exp_type_slug'] = get_var_name( $args['exp_type'], '_' );
        $args['exp_sub_slug'] = get_var_name( $args['exp_sub'], '_' );
        
        if($exp_type_name->type_name == 'Banking Deposits') {
            $args['mode'] = $post['mode'];
            if($args['mode'] == 2) {
                $args['cheque_no'] = $post['cheque_no'];
                $args['bank_name'] = $post['cheque_bank'];
                $args['cheque_date'] = date('Y-m-d',strtotime($post['cheque_date']));
            } else {
                $args['cheque_no'] = $args['bank_name'] = $args['cheque_date'] = '';
            }
        } else {
            $args['mode'] = $args['cheque_no'] = $args['bank_name'] = $args['cheque_date'] = '';
        }
        
        if( $is_add ) :
            // Insert 
            $args['exp_rec_no'] = generate_rec_no(TBL_EXPENSES); 
            $args['created_on'] = $date_now;
            $args['created_by'] = $session_data['user_id'];

            $this->db->insert( TBL_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_EXPENSES, $args, array( 'id' => $db_id ) );

        endif;
        
        
    }
    
    function ajax(){
        $post = $this->input->post();
        $return = array();
        
        switch( $post['act'] ) :
            case 'get_sub' :
                $whr = array(
                    'status' => '1',
                );
                
                if( !empty( $post['val'] ) ) :
                    $whr['exp_type_id'] = $post['val'];
                endif;
                
                $return = $this->get(TBL_EXPENSES, $whr, array('select' => 'DISTINCT(exp_sub), exp_sub_slug', 'order_by' => 'exp_sub') );
            break;    
        endswitch;
        
        return $return;
    }
    
    function delete($id) {
        $args = array(
            'status'    => 2,
        );
        
        $this->db->update( TBL_EXPENSES, $args, array('id' => $id) );
        
    }
    
    public function list_all_types() {
        $this->db->where('status !=',2);
        return $this->db->get(TBL_EXP_TYPE)->result();
    }
    
    public  function insert($data){  
        $this->db->insert(TBL_EXP_TYPE, $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_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_EXP_TYPE)->row();
   }
   
   public function update($data, $id){
       $this->db->where('id', $id);
       $this->db->update(TBL_EXP_TYPE, $data);
       return $this->db->affected_rows();
   }
   
   public function get_types() {
       $this->db->where('status !=',2);
       return $this->db->get(TBL_EXP_TYPE)->result();
   }
    
    
}