<?php
class Master_Settings_Model extends CI_Model {
    public function __construct(){
        parent::__construct();
        $this->master_settings = TBL_SETTINGS_MASTER;
    }
    
    public function list_all() {
        $resource = $this->db->get($this->master_settings);
        return $resource->result();
    }
    
    public function get_data($ids) {
        $this->db->select('id');
        if (!empty($ids)) {
            $this->db->where_not_in('id', $ids);
        }
        $resource = $this->db->get($this->master_settings);
        return $resource->result();
    }
    
    public function list_active() {
        $this->db->select('id, master_name, controller_name');
        $this->db->where('status', 1);
        $this->db->order_by('seq_number');
        return $this->db->get($this->master_settings)->result();
    }
    
    public function controller_name_str() {
        $this->db->select('GROUP_CONCAT(controller_name SEPARATOR ",") as controller_name', FALSE);
        $this->db->where('status', 1);
        return $this->db->get($this->master_settings)->row()->controller_name;
    }
    
    public function permissions_by_controller($controller_name_arr) {
        $this->db->select('id, master_name, controller_name, menu_name, menu_icon');
        if(!empty($controller_name_arr))
            $this->db->where_in('controller_name', $controller_name_arr);
        
        $this->db->where('status', 1);
        $this->db->order_by('seq_number');
        return $this->db->get($this->master_settings)->result_array();
    }
    
    public function update($data,$id){
        $this->db->where('id', $id);
        $this->db->update($this->master_settings, $data);
        return $this->db->affected_rows();
    }
    
    function get_avail_payment_types() {
        $where = array(
            'self.status !=' => 2,
        );
        
        $this->db->group_by('pay.pay_type_id');
        $this->db->order_by('self.menu_id');
        $this->db->select('self.*, COUNT(pay.id) AS paid_cnt', FALSE);
        $this->db->join(TBL_PAY . ' AS pay', 'self.id = pay.pay_type_id AND pay.status != 2', 'left');
        $q = $this->db->get_where( TBL_PAY_TYPE . ' AS self', $where );
        $r = $q->result_array();
        //echo "<pre> Data: "; print_r($r); die;
        return $r;
        
    }
}
?>
