<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class membership_card_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->member_topup = TBL_BILL_MEMBER_TOPUP;
        $this->service = TBL_SERVICE;
        $this->service_menu = TBL_SERVICE_MENU;
        $this->stars = TBL_STAR;
        $this->payment_type = TBL_PAY_TYPE;
        $this->member_card = TBL_BILL_MEMBER_CARD;
        $this->card_members = TBL_BILL_CARD_MEMBERS;
        $this->payment = TBL_PAY;
        $this->user = TBL_USERS;
        $this->card_topup = TBL_BILL_CARD_TOPUP;
    }
    
    public function list_all() {
        /* $select = 'mr.id, mr.name1, mr.registered_date, mr.contact_no, mt.membership_card_no, s.name as service';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = mr.service_id');
        $this->db->join($this->member_topup . ' as mt','mt.member_register_id = mr.id'); */
        #$this->db->where('mc.status !=',2);
        $this->db->order_by('mc.receipt_no','desc');
        return $this->db->get($this->member_card . ' as mc')->result();
    }
    
    public function star_lists() {
        $this->db->where('status',1);
        return $this->db->get($this->stars)->result();
    }
    
    public function get_service_type($type= ''){
        $this->db->where('s.menu_name',$type);
        $this->db->where('s.status',1);
        return $this->db->get($this->service . ' as s')->result();
    }
    
    public function insert($data){
        $this->db->insert($this->member_card, $data);
        return $this->db->insert_id();
    }
    
    public function insert_reg_mem($data){
        $this->db->insert($this->card_members, $data);
        return $this->db->insert_id();
    }
    
    public function insert_topup($data){
        $this->db->insert($this->card_topup, $data);
        return $this->db->insert_id();
    }

    public function update($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->member_card,$data);
        return $this->db->affected_rows();
    }
    
    public function update_topup($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->member_topup,$data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $select = 'mr.*, st1.name as star_name, st1.tamil_name as star_tamil_name, mr.status AS status';
        $this->db->select($select,FALSE);
        $this->db->join($this->stars . ' as st1','st1.id = mr.star','left');
        /* $this->db->join($this->service . ' as s','s.id = mr.service_id','left');
        $this->db->join($this->stars . ' as st2','st2.id = mr.star2','left');
        $this->db->join($this->member_topup . ' as mt','mt.member_register_id = mr.id'); */
        $this->db->where('mr.id',$id);
        return $this->db->get($this->member_card . ' as mr')->row();
    }
    
    public function get_members_by_id($id) {
        $this->db->select('cm.*, st.name as star_name, st.tamil_name as star_tamil_name');
        $this->db->join($this->stars . ' as st','st.id = cm.star','left');
        $this->db->where('cm.mem_reg_id',$id);
        $this->db->where('cm.status',1);
        return $this->db->get($this->card_members . ' as cm')->result();
    }

    public function get_membership_card_by_id($id) {
        $this->db->where('member_register_id',$id);
        return $this->db->get($this->member_topup)->row();
    }
    
    public function membership_card_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($this->member_card)->num_rows();
    }
    
    public function payment_mode_lists() {
        $this->db->where('status',1);
        $this->db->where('menu_id',0);
        return $this->db->get($this->payment_type)->result();
    }
    
    public function insert_payment($data){
        $this->db->insert($this->payment,$data);
        return $this->db->insert_id();
    }
    
    public function get_t_card_details($card_no) {
        $this->db->select('mc.*, st.name as star_name, st.tamil_name as tamil_star',FALSE);
        $this->db->join($this->stars . ' as st','st.id = mc.star','left');
        $this->db->where('mc.card_no',$card_no);
        $this->db->where('mc.status',1);
        return $this->db->get($this->member_card . ' as mc')->row();
    }
    
    public function check_valid_pwd($user_id) {
        $this->db->where('id',$user_id);
        return $this->db->get($this->user)->row();
    }
    
    public function update_mem_reg($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->card_members,$data);
    }
    
    public function get_paid_type($menu,$type,$id) {
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('pay_type_id',$id);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function get_paid_type_by_id($menu,$type) {
        $this->db->select('p.pay_amount, pt.img_url, pt.name',FALSE);
        $this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id');
        $this->db->where('p.pay_ref_type',$menu);
        $this->db->where('p.pay_ref_id',$type);
        $this->db->where('p.status',1);
        return $this->db->get($this->payment . ' as p')->result();
    }
    
    public function update_mem_topup($data,$id) {
        $this->db->where('mem_id',$id);
        $this->db->update($this->card_topup,$data);
        return $this->db->affected_rows();
    }
    
    public function update_mem_reg_by_mem_id($data,$id) {
        $this->db->where('mem_reg_id',$id);
        $this->db->update($this->card_members,$data);
        return $this->db->affected_rows();
    }
    
    public function update_paid_type($data,$type_id,$id) {
        $this->db->where('pay_ref_type',$type_id);
        $this->db->where('pay_ref_id',$id);
        $this->db->update($this->payment,$data);
        return $this->db->affected_rows();
    }
    
    public function service_type_exists() {
        //$this->db->where('status !=',2);
        $this->db->order_by('receipt_no','desc');
        return $this->db->get($this->member_card);
    }
    
    public function check_contact_email_exists($field,$id,$val) {
        if($field == 'Contact No')
            $this->db->where('contact_no',$val);
        
        if($field == 'Email')
            $this->db->where('email',$val);
        
        #$this->db->where('status !=',2);
        if($this->db->get($this->member_card)->num_rows() > 0)
            return 1;
        
        else
            return 0;
    }
    
    public function total_paid_amount($menu,$type) {
        $this->db->select('SUM(pay_amount) as paidAmount');
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function get_last_entry() {
        $this->db->where('status',1);
        $this->db->order_by('id','desc');
        $this->db->limit(1);
        return $this->db->get($this->card_topup);
    }
}
?>