<?php defined('BASEPATH') OR exit('No direct script access allowed');

class CustomerModel extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('ServicesModel', 'services_model');
        $this->load->model('ShopModel', 'shop_model');
        date_default_timezone_set('Asia/Hong_Kong');
    }
    //new added customer
     function getAllCustomer() {
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        // $this->db->from('mas_rbac_users');
        $this->db->where('customer_group_id',1);
        $this->db->order_by('code', 'DESC');
        $query = $this->db->get();

        return $query->result();
    }
    function getAllCustomerOrderByid() {
         $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        // $this->db->from('mas_rbac_users');
        $this->db->where('customer_group_id',1);
        $this->db->order_by('id', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }
    function getCustomer($id){
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        $this->db->where('delete_time', NULL);    
        $this->db->where('id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerLivingArea($id){
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_customers_living_area_'.strtolower($shopsrow->shortcode));
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerPromotionCode($id){
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_customers_promotion_code_'.strtolower($shopsrow->shortcode));
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerSkinImprove($id){
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_customers_skin_improve_'.strtolower($shopsrow->shortcode));
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    
    function customercode_exists($code){
        // $this->db->from('adminUsers');
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('delete_time', NULL); 
        // $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        $query = $this->db->get('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function customercode_existsbyid($code,$id){
        // $this->db->from('adminUsers');
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('delete_time', NULL);
        $this->db->where('id !=', $id); 
        // $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        $query = $this->db->get('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function insertCustomer($dataori){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,

        //shop_id use to record different table
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));

        
        $data = array(
            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'chi_name' => $dataori['chi_name'] ,
            'eng_firstname' => $dataori['eng_firstname'] ,
            'eng_lastname' => $dataori['eng_lastname'] ,
            'gender' => $dataori['gender'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'dob' => $dataori['dob'] ,
            'mobile' => $dataori['mobile'] ,
            'join_date' => $dataori['join_date'] ,
            'status' => 1 ,
            'display' => 1 ,
            'referred_mobile' => $dataori['referred_mobile'],
            'remark' =>$dataori['remark'] ,
            'first_registered_tenant_no' =>$dataori['first_registered_tenant_no'] ,
            'concurrent_registered_tenant_no' =>$dataori['concurrent_registered_tenant_no'] ,
            'created_by'=>$this->session->userdata('userid'),
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         
        
         // $query=$this->db->insert('mas_rbac_users', $data);
         $query=$this->db->insert('mas_rbac_users_'.strtolower($shopsrow->shortcode), $data);


         $id = $this->db->insert_id();

         //add living area
          $data = array(
            'customer_id' => $id ,
            'district_id' => $dataori['district_id'] ,
            'area_id' => $dataori['area_id'] ,
            'zone' => $dataori['zone'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'address' => $dataori['address'] ,
            'status' => 1 ,
            'display' => 1 ,
            // 'mobile' => $dataori['mobile'] ,
            // 'join_date' => $dataori['join_date'] ,
            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('mas_customers_living_area_'.strtolower($shopsrow->shortcode), $data);

         //end add living area

         $skin_improve_data="";
         if(!empty($dataori['skin_improve'])){
            $skin_improve_data="[".implode(',',$dataori['skin_improve'])."]";
         }
         //add skin improve
         $data = array(
            'customer_id' => $id ,
            'skin_improve_ids' =>  $skin_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('mas_customers_skin_improve_'.strtolower($shopsrow->shortcode), $data);
         //end add skin improve


         //add promotion code
         if(!empty($dataori['promotion_code'])){
              $data = array(
            'customer_id' => $id ,
            'promotion_code' =>  $dataori['promotion_code'] ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,          
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('mas_customers_promotion_code_'.strtolower($shopsrow->shortcode), $data);

         }//if(!empty($dataori['promotion_code'])){
         //end add promotion code


         if($dataori['need_purchase_service']==true){


         //add customer service
         //find category_id
         $category_id="";
         $category_id=$this->services_model->getCategoryid($dataori['service_id']);
         //end find category_id


         $data = array(
            'category_id' => $category_id ,
            'service_id' => $dataori['service_id'] ,
            'user_id' => $id ,
            'purchased_date' => $dataori['purchased_date'] ,
            'purchased_qty' => $dataori['purchased_qty'] ,
            'available_qty' => $dataori['available_qty'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'status' => 1 ,
            'remark' => $dataori['remark2'] ,
            'display' => 1 ,
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         // $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('mas_service_user_'.strtolower($shopsrow->shortcode), $data);
         //end add customer service
         }//if($dataori['need_purchase_service']==1){

         return $query;
    }
    function updateCustomer($dataori){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,

        //shop_id use to record different table
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));

        
        $data = array(
            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'chi_name' => $dataori['chi_name'] ,
            'eng_firstname' => $dataori['eng_firstname'] ,
            'eng_lastname' => $dataori['eng_lastname'] ,
            'gender' => $dataori['gender'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'dob' => $dataori['dob'] ,
            'mobile' => $dataori['mobile'] ,
            'join_date' => $dataori['join_date'] ,
            'status' => 1 ,
            'display' => 1 ,
            'referred_mobile' => $dataori['referred_mobile'],
            'remark' =>$dataori['remark'] ,
            'first_registered_tenant_no'=>$dataori['first_registered_tenant_no'],
                'concurrent_registered_tenant_no'=>$dataori['concurrent_registered_tenant_no'],
             'updated_by'=>$this->session->userdata('userid'),   
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         
        
         // $query=$this->db->insert('mas_rbac_users', $data);
         $this->db->where('id', $dataori['id']);
         $query=$this->db->update('mas_rbac_users_'.strtolower($shopsrow->shortcode), $data);


        

         //add living area
          $data = array(
            // 'customer_id' => $id ,
            'district_id' => $dataori['district_id'] ,
            'area_id' => $dataori['area_id'] ,
            'zone' => $dataori['zone'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'address' => $dataori['address'] ,
            'status' => 1 ,
            'display' => 1 ,
            // 'mobile' => $dataori['mobile'] ,
            // 'join_date' => $dataori['join_date'] ,
            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('mas_customers_living_area_'.strtolower($shopsrow->shortcode), $data);

         //end add living area

         $skin_improve_data="";
         if(!empty($dataori['skin_improve'])){
            $skin_improve_data="[".implode(',',$dataori['skin_improve'])."]";
         }
         //add skin improve
         $data = array(
            // 'customer_id' => $id ,
            'skin_improve_ids' =>  $skin_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('mas_customers_skin_improve_'.strtolower($shopsrow->shortcode), $data);
         //end add skin improve


         //add promotion code
         if(!empty($dataori['promotion_code'])){
              $data = array(
            // 'customer_id' => $id ,
            'promotion_code' =>  $dataori['promotion_code'] ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,          
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('mas_customers_promotion_code_'.strtolower($shopsrow->shortcode), $data);

         }//if(!empty($dataori['promotion_code'])){
         //end add promotion code


         // if($dataori['need_purchase_service']==true){


         // //add customer service
         // //find category_id
         // $category_id="";
         // $category_id=$this->services_model->getCategoryid($dataori['service_id']);
         // //end find category_id


         // $data = array(
         //    'category_id' => $category_id ,
         //    'service_id' => $dataori['service_id'] ,
         //    'user_id' => $id ,
         //    'purchased_date' => $dataori['purchased_date'] ,
         //    'purchased_qty' => $dataori['purchased_qty'] ,
         //    'available_qty' => $dataori['available_qty'] ,
         //    // 'fullName' => $dataori['inputadminname'] ,
         //    'status' => 1 ,
         //    'remark' => $dataori['remark2'] ,
         //    'display' => 1 ,
         //    // 'updated_by' => 1 , //session user id
         //    // 'created_by' => 1 , //session user id

         // );
         // date_default_timezone_set('UTC');
         // $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         // $query=$this->db->insert('mas_service_user_'.strtolower($shopsrow->shortcode), $data);
         // //end add customer service
         // }//if($dataori['need_purchase_service']==1){

         return $query;
    }//end function update
    function updateStatus($data){

         $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));



            $data2 = array(
            'status' => $data['status'] ,
         );
        date_default_timezone_set('UTC');
        
        $this->db->set('update_time', time(), FALSE);

        $this->db->where('id', $data['id']);
 
        $query=$this->db->update('mas_rbac_users_'.strtolower($shopsrow->shortcode), $data2);
        return $query;
    }
     function getCustomerService($id){

        $this->db->select('u.*, s.code, s.category_id, s.name, s.work, s.price,s.no_credit_rebate');
        $this->db->from('mas_service_user u, mas_services s');
        $this->db->where('u.service_id = s.id');
        $this->db->where('user_id',$id);
        // $this->db->where('r.permissions = u.permissions');
        $query = $this->db->get();

        // $this->db->from('mas_service_user');
        // $this->db->where('user_id',$id);
        // $this->db->order_by('service_id', 'ASC');
        // $query = $this->db->get();

        return $query->result();

     }
    function getCustomerServiceById($shopcode,$id){
        $this->db->select('a.*,c.name as servicecode,b.name as servicename');
        
         $this->db->from('mas_service_user_'.strtolower($shopcode).' a');
         $this->db->join('mas_services b','b.id=a.service_id');
         $this->db->join('mas_categories c','c.id=b.category_id');
        $this->db->where('user_id', $id);
        $query = $this->db->get();

        return $query->result();
    }//function getCustomerServiceById($shopcode,$id){
    function getCustomerData($id){
         $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
        // $this->db->from('mas_rbac_users');
        $this->db->where('id',$id);
        // $this->db->order_by('id', 'ASC');
        $row = $this->db->get()->row();
            
        return $row;
    }
    //end new added customer
    function getAllRoom($id) {
        $this->db->from('mas_room');
        $this->db->where('shop_id',$id);
        $query = $this->db->get();
        return $query->result();
    }

     function room_exists($shop_id,$name){
        // $this->db->from('adminUsers');
        $this->db->where('LOWER(name)', $name);
        $this->db->where('shop_id', $shop_id);
        $query = $this->db->get('mas_room');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function insertAdmin($data){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,
        $data = array(
            'shop_id' => $data['shop_id'] ,
            'name' => $data['name'] ,
            'capacity' => $data['capacity'] ,
            'description' => $data['description'] ,
            // 'fullName' => $data['inputadminname'] ,
            'remark' => $data['remark'] ,
            


         );
        date_default_timezone_set('UTC');
        
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         $query=$this->db->insert('mas_room', $data);
         return $query;
    }
    
    function getBannerAll()
    {
        
        $this->db->from('frontendUsers');
        // $this->db->where(array('show' => 1));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }


    function checking(){

    	$userid = $this->session->userdata('userid');
    	$username = $this->session->userdata('username');
    	$fullName = $this->session->userdata('fullName');
    	$logged_in = $this->session->userdata('logged_in');
    	$secret = $this->session->userdata('secret');
    	$usertype = $this->session->userdata('usertype');

    	$passLogin=false;
    	if($usertype=="frontenduser"){
    		if($logged_in==false){
    			return false;
    		}
    		if(empty($userid)){
    			return false;
    		}
    		if(empty($secret)){
    			return false;
    		}
    		$this->db->where('id',$userid);
        	$this->db->where('secret',$secret);
        	if($this->db->get('frontendUsers')->row_array()){
        		$passLogin=true;
        		return true;
        	}	
        	else{
        		return false;
        	}

    	}//if($usertype=="frontenduser"){
    	if($usertype=="backenduser"){
    	    if($logged_in==false){
    			return false;
    		}
    		if(empty($userid)){
    			return false;
    		}
    		if(empty($secret)){
    			return false;
    		}
    		$this->db->where('id',$userid);
        	$this->db->where('secret',$secret);
        	if($this->db->get('adminUsers')->row_array()){
        		$passLogin=true;
        		return true;
        	}	
        	else{
        		return false;
        	}
    	
    	}
    }
 }