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

class BrPortableform2amodel extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('BrservicesModel', 'services_model');
        $this->load->model('BrservicesuserModel', 'servicesUser_model');

        $this->load->model('BrservicesModel', 'services_model');
        date_default_timezone_set('Asia/Hong_Kong');
    }
    //added portableform1amodel function
     function form_exists($custid){
        $this->db->where('customer_id', $custid);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brportableform2a');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }

    function insertForm($dataori){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,

        //shop_id use to record different table
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->trans_begin();
        
        $question_data="";
         if(!empty($dataori['question'])){
            $question_data="[".implode(',',$dataori['question'])."]";
         }

         //if not empty signaturePad
         //save image
         $image_name="";
         // define('UPLOAD_DIR', 'images/');
         if(!empty($dataori['signaturePad'])){

         define('UPLOAD_DIR', './uploads/photoform2a/');
            // $image_parts = explode(";base64,", $_POST['image']);
            $image_parts = explode(";base64,",  $dataori['signaturePad']);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            $image_name =uniqid() . '.png';
            // $file = UPLOAD_DIR . uniqid() . '.png';
            $file = UPLOAD_DIR . $image_name;
            // $file =  $image_name;
            file_put_contents($file, $image_base64);

        }//if(!empty($dataori['signaturePad'])){

         //end if not empty signaturePad



        $data = array(
            'customer_id' => $dataori['currentFormId'] ,
            'question'=>$question_data,
            // 'signaturePad' =>  $dataori['signaturePad'],
            // 'signatureImg' => $image_name,
            'signatureImg' => $image_name,
            'question4_text' => $dataori['question4_text'] ,



         );
         date_default_timezone_set('Asia/Hong_Kong');
         $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('brportableform2a', $data);

          // return $query;
        if ($this->db->trans_status() === FALSE)
        {
                $this->db->trans_rollback();
                // echo "roll back";
                return false;
        }
        else
        {
             // $this->db->trans_rollback();
                $this->db->trans_commit();
                // echo "commit transaction";
                return true;
        }
    }
    function updateForm($dataori){
         $this->db->trans_begin();
        
        $question_data="";
         if(!empty($dataori['question'])){
            $question_data="[".implode(',',$dataori['question'])."]";
         }

         //if not empty signaturePad
         //save image
         $image_name="";
         // define('UPLOAD_DIR', 'images/');
        $updateimage_name="";
        $signauteName=$this->getSignatureName($dataori['currentUserId']);
        $updateimage_name=$signauteName;
        if(!empty($dataori['signaturePad'])){

         //get old signautreImg
           
            if(!empty($signauteName)){
                unlink('./uploads/photoform2a/'.$signauteName);
            } //if(!empty($signauteName)){          

        //and unline the image

         define('UPLOAD_DIR', './uploads/photoform2a/');
            // $image_parts = explode(";base64,", $_POST['image']);
            $image_parts = explode(";base64,",  $dataori['signaturePad']);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            $image_name =uniqid() . '.png';
            // $file = UPLOAD_DIR . uniqid() . '.png';
            $file = UPLOAD_DIR . $image_name;
            // $file =  $image_name;
            file_put_contents($file, $image_base64);
             $updateimage_name=$image_name;
        }//if(!empty($dataori['signaturePad'])){


         //end if not empty signaturePad



        $data = array(
            // 'customer_id' => $dataori['currentFormId'] ,
            'question'=>$question_data,
            // 'signaturePad' =>  $dataori['signaturePad'],
            // 'signatureImg' => $image_name,
            'signatureImg' => $updateimage_name,
            'question4_text' => $dataori['question4_text'] ,



         );
         date_default_timezone_set('Asia/Hong_Kong');



         $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['currentFormId']);
         $query=$this->db->update('brportableform2a', $data);



          // return $query;
        if ($this->db->trans_status() === FALSE)
        {
                $this->db->trans_rollback();
                // echo "roll back";
                return false;
        }
        else
        {
             // $this->db->trans_rollback();
                $this->db->trans_commit();
                // echo "commit transaction";
                return true;
        }
    }
    function deleteForm($dataori){
         $this->db->trans_begin();


         //clean signature image
         $signauteName=$this->getSignatureName($dataori['currentUserId']);
           if(!empty($signauteName)){
                unlink('./uploads/photoform2a/'.$signauteName);
            } //if(!empty($signauteName)){ 
         //end clean signature image


         date_default_timezone_set('Asia/Hong_Kong');
         $this->db->set('delete_time', time(), FALSE);
         $this->db->where('id',$dataori['currentFormId']);
         $query=$this->db->update('brportableform2a');

        if ($this->db->trans_status() === FALSE)
        {
                $this->db->trans_rollback();
                return false;
        }
        else
        {
                $this->db->trans_commit();
                return true;
        }
    }
      function getSignatureName($custid){
        $this->db->from('brportableform2a');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $custid);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret->signatureImg;
    }
    function getFormData($id){
     
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brportableform2a');
        $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 checkSubmited($id){
         $this->db->from('brportableform2a');
        $this->db->where('delete_time', NULL); 
        $this->db->where('customer_id', $id);
        $query = $this->db->get();
        if($query->num_rows() >= 1){
            return true;
        } else {
            return false;
        }
    }
    function getFormIdByCustId($custId){
        $this->db->from('brportableform2a');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $custId);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row()->id;
        return $ret;
    }
    //end added portableform1amodel function

    //added customermodel function
    function getCustomerAll($shopid="",$serachlevel="",$channelSelectID="",$searchmem="")
    {
        
        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }
    function getCustomerAllShop(){
        //for add booking model
        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get();
        return $query->result();
        //end for add booking model
    }

    function customercode_exists($code){
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function customermobile_exists($mobile){
        $this->db->where('mobile', $mobile);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function customermobile_existsbyid($mobile,$id){
        $this->db->where('mobile', $mobile);
        $this->db->where('id!=', $id);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function customercode_existsbyid($code,$id){
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('id!=', $id);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function getCustomerByPhone($mobile){
        $this->db->where('mobile', $mobile);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        $id = $query->row()->id;
        return $id;
    }
    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'));
        $this->db->trans_begin();
        
        $data = array(
            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'display_name' => $dataori['display_name'] ,
            // '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'),
            'level'=>$dataori['level'] ,
            'isTemp'=>$dataori['isTemp'] ,
            'channelSelectID'=>$dataori['channelSelectID'] ,
            'prefer_pic'=>$dataori['prefer_pic'] ,
            'bodyoptionother_text'=>$dataori['bodyoptionother_text'] ,
            'faceoptionother_text'=>$dataori['faceoptionother_text'] ,
            // '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('brcustomer', $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('brcustomer_address', $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('brcustomer_skin_improve', $data);
         //end add skin improve

         //add body improve
        $body_improve_data="";
         if(!empty($dataori['body_improve'])){
            $body_improve_data="[".implode(',',$dataori['body_improve'])."]";
         }

         $data = array(
            'customer_id' => $id ,
            'body_improve_ids' =>  $body_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('brcustomer_body_improve', $data);
         //end body improve

         //add face improve
        $face_improve_data="";
         if(!empty($dataori['face_improve'])){
            $face_improve_data="[".implode(',',$dataori['face_improve'])."]";
         }

         $data = array(
            'customer_id' => $id ,
            'face_improve_ids' =>  $face_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('brcustomer_face_improve', $data);
         //end face 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('brcustomer_promotion_code', $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;
        if ($this->db->trans_status() === FALSE)
        {
                $this->db->trans_rollback();
                // echo "roll back";
                return false;
        }
        else
        {
             // $this->db->trans_rollback();
                $this->db->trans_commit();
                // echo "commit transaction";
                return true;
        }
    }
     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 ,
            'display_name' => $dataori['display_name'] ,
            // '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'],
                'level'=>$dataori['level'],
                'isTemp'=>$dataori['isTemp'],
                'channelSelectID'=>$dataori['channelSelectID'],
             'updated_by'=>$this->session->userdata('userid'),   
             'bodyoptionother_text'=>$dataori['bodyoptionother_text'],   
             'faceoptionother_text'=>$dataori['faceoptionother_text'],   
            // '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('brcustomer', $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('brcustomer_address', $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('brcustomer_skin_improve', $data);
         //end add skin improve

         // body improve
          $body_improve_data="";
         if(!empty($dataori['body_improve'])){
            $body_improve_data="[".implode(',',$dataori['body_improve'])."]";
         }

         $data = array(
            // 'customer_id' => $id ,
            'body_improve_ids' =>  $body_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('brcustomer_body_improve', $data);
         // end add body improve

         // face improve
          $face_improve_data="";
         if(!empty($dataori['face_improve'])){
            $face_improve_data="[".implode(',',$dataori['face_improve'])."]";
         }

         $data = array(
            // 'customer_id' => $id ,
            'face_improve_ids' =>  $face_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('brcustomer_face_improve', $data);
         // end add face 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('brcustomer_promotion_code', $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 getCustomer($id){
        $this->session->userdata('currentshop');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $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('brcustomer_address');
        $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('brcustomer_promotion_code');
        $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('brcustomer_skin_improve');
        $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 getCustomerBodyImprove($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_body_improve');
        $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 getCustomerFaceImprove($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_face_improve');
        $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 updateStatus($data){
        
            $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('brcustomer', $data2);
        return $query;

    }
     function getCustomerService($id){

        $this->db->select('u.*, s.code, s.category_id, s.name, s.work, s.price,s.time,s.no_credit_rebate');
        $this->db->from('brservice_user u, brservices s');
        $this->db->where('u.service_id = s.id');
        $this->db->where('u.user_id',$id);
        $this->db->where('u.delete_time', NULL); 
        // $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 getCustomerIdByMobile($mobile){
        $this->db->select('id');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);    
        $this->db->where('mobile', $mobile);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $id = $query->row()->id;
        return $id;
    }
    function getLastCustomerId($shopid){
        $shopRow=$this->shop_model->getShopbyid($shopid);
        $shortcode=$shopRow->shortcode;

        $this->db->select("*");
        $this->db->from('brcustomer');
        $this->db->like('code', $shortcode);
        $this->db->where('first_registered_tenant_no', $shopid);
         $this->db->order_by('code', 'DESC');    
        $code = $this->db->get()->row()->code;

         $cust_num = substr($code, -4);
         $cust_num = $cust_num+1;

         $printCustnum=str_pad($cust_num, 4, '0', STR_PAD_LEFT);
         $printCust=$shortcode.$printCustnum;

        return $printCust;
    }//function getLastCustomer($shopid){

    function getPreferPic($custid){
        $this->db->select('prefer_pic');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);    
        $this->db->where('id', $custid);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $prefer_pic = $query->row()->prefer_pic;
        return $prefer_pic;
    }//function getPreferPic($custid){

    //end added customermodel function
    function getShopAll()
    {
        
        $this->db->from('bshop');
        // $this->db->where(array('show' => 1));
        $this->db->where(array('deleted' => 0));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }
    function shopcode_exists($code){
        $this->db->where('LOWER(shortcode)', $code);
        $query = $this->db->get('bshop');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function insertShop($inputdata){
           $data = array(
            'shortcode' => $inputdata['shortcode'] ,
            'code' => $inputdata['code'] ,
            'name' => $inputdata['name'] ,
            'status' => 1 ,
            'display' => 0 ,// 0 stand for display, 1 stand for not display
            'deleted' => 0 , //0 stand for not deleted, 1 stand for deleted
            );
           date_default_timezone_set('UTC');
           $this->db->set('update_time', time(), FALSE);
           $this->db->set('create_time', time(), FALSE);
           $query=$this->db->insert('bshop', $data);

           return $query;
    }
    
    function isNewCustomer($id){
        // $today=date("Y-m-d");
        // $timestamp1 = strtotime($today);

        $dateInTwoWeeks = strtotime('+2 weeks');
        $dateBeforeTwoWeeks = strtotime('-2 weeks');


        $this->db->select('code');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);      
        $this->db->where('id', $id);
         $this->db->where('code','');    
        $this->db->group_start();
        $this->db->where('create_time <=',$dateInTwoWeeks);
        $this->db->or_where('create_time >=',$dateBeforeTwoWeeks);
        $this->db->group_end();
        // $this->db->order_by('code');
        $query = $this->db->get();
        $num_row=$query->num_rows();
        // $id = $query->row()->code;
        // return $id;
        if($num_row>0){
            return true;
        }
        else{
            return false;
        }
    }

    function getBannerAll()
    {
        
        $this->db->from('tblFolders');
        // $this->db->where(array('show' => 1));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }

    // function getAdmindata(){
    //     $this->db->select('id,login, fullName, update_time');
    //     $query = $this->db->get('adminUsers');
    //     return $query->result();
    // }
    // function record_count(){
    //     $this->db->select('id');
    //     $this->db->where('level1', '0');
    //     $this->db->where('level2', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();
    // }
    // function count_all_folders(){
    //     $this->db->select('id');
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();    
    // }   
    // function getflodercount($id){
    //     $this->db->select('id');
    //     $this->db->where('level1', $id);
    //     // $this->db->where('level2!=', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();    
    // }    

    // function getfolderdata($perpage,$page){
    //     $this->db->select('id,name,level1, update_time');
    //     $offset=$page* $perpage;
    //     $this->db->where('level1', 0);
    //     $this->db->limit($perpage, $offset);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }


    // function getlevel1data($id)
    // {
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel2data($id)
    // {
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level2', $id);
    //     $this->db->where('level3', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel3data($id){
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level3', $id);
    //     $this->db->where('level4', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel4data($id){
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level4', $id);
    //     $this->db->where('level5', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }

    // //get all folder data for rightend
    // function getallfolderdata(){
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }    
    // function getalllevel1datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level1', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // } 
    // function getalllevel2datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level2', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // } 
    // function getalllevel3datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level3', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }    

    // //end get all folder data for rightend

    // function getAdmindateByid($id){
    //     $this->db->select('id,login, companyName,fullName,tel, update_time');
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }

    
    // function getFolderName($id){
    //     $this->db->select('name');
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     if ($query->num_rows() > 0) {
    //         // return $query->first_row();
    //          $row = $query->row(); 
    //         return $row->name;
    //     }
    //     return "";
    // }

    // function insertFolder($data){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
    //     $data = array(
    //         'name' => $data['inputflodername'] ,
    //         'level1' => '0' ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelFolder($data){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
    //     if($data['addlevel']==1){
    //         $data = array(
    //             'name' => $data['inputflodernamelevel'] ,
    //             'level1' => '0' ,
                
    //          );
    //     }
    //     if($data['addlevel']==2){
    //         $data = array(
    //             'name' => $data['inputflodernamelevel'] ,
    //             'level1' => $data['addlevelid'] ,
                
    //          );
    //     }
        
        
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelOneFolder($name,$level){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => '0' ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
        
    // }
    // function insertLevelTwoFolder($name,$addlevel,$addlevelid){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelThreeFolder($name,$addlevel,$addlevelid,$secondvalues){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
    //         'level2' => $secondvalues ,
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }

    // function insertLevelFourFolder($name,$addlevel,$addlevelid,$thirdvalues){
    //     $secondlevelid=$this->findLevelThreeid($thirdvalues);
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
    //         'level2' => $secondlevelid ,
    //         'level3' => $thirdvalues ,
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelMonthWeekFolder($values,$addlevel,$addlevelid,$secondvalues){
    //     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
    //     $weekarray =  array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5");
    //     foreach($montharray as $month){
    //         if($this->folder_levelThreeexists(strtolower($month),$addlevel,$addlevelid,$secondvalues)==true){
    //              $data = array(
    //             'name' =>  $month ,
    //             'level1' => $addlevelid ,
    //             'level2' => $secondvalues ,
    //          );
    //          $this->db->set('update_time', 'NOW()', FALSE);
    //          $query=$this->db->insert('tblFolders', $data);
    //          $lastinsertid = $this->db->insert_id();
    //          foreach ($weekarray as $week) {

    //              $data = array(
    //             'name' =>  $week ,
    //             'level1' => $addlevelid ,
    //             'level2' => $secondvalues ,
    //             'level3' => $lastinsertid ,
    //              );
    //             $this->db->set('update_time', 'NOW()', FALSE);
    //              $query=$this->db->insert('tblFolders', $data);
    //          }

    //         }//if(folder_levelThreeexists($month,$addlevel,$addlevelid,$secondvalues)==false){
    //     }//foreach($montharray as $month){
    //     return $query;
    // }

    // function updateAdmin($input){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
        
    //     if(empty($input['inputPassword'])){
    //         $data = array(
    //             'login' => $input['inputloginname'] ,
    //             'companyName' => $input['inputcompanynameedit'],
    //             'fullName' => $input['inputfullnameedit'], 
    //             'tel' => $input['inputteledit']  
                
    //          );
    //     }
    //     else{
    //         $data = array(
    //             'login' => $input['inputloginname'] ,
    //             'companyName' => $input['inputcompanynameedit'],
    //             'fullName' => $input['inputfullnameedit'], 
    //             'tel' => $input['inputteledit']  ,
    //             'pwd' => $input['inputPassword'] 
    //          );
    //     }
        
    //      $this->db->set('update_time', 'NOW()', FALSE);

    //      $this->db->where('id', $input['id']);
    //      $query=$this->db->update('tblFolders', $data);
    //      return $query;
    // }

    // function deleteAdmin($id){
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     $row = $query->row(); 

    //     //check level of delete
    //     $deleteLevel=0;
    //     if($row->level4 ==NULL  && $row->level3!=NULL){
    //         $deleteLevel=4;
    //     }
    //     if($row->level3 ==NULL  && $row->level2!=NULL){
    //         $deleteLevel=3;
    //     }
    //     if($row->level2 ==NULL  && $row->level1!=NULL && $row->level1!=0){
    //         $deleteLevel=2;
    //     }
    //     if( $row->level1==0){
    //         $deleteLevel=1;
    //     }

    //     //end check level of delete
    //     if($deleteLevel==4){
    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==3){

    //         $this->db->where('level3 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level3', $id);
    //              $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==2){
    //         $this->db->where('level2 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level2', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==1){
    //         $this->db->where('level1 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level1', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }

        
    //     return $query;
    // }

    // function folder_exists($foldername){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('LOWER(name)', $foldername);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function folder_levelOneexists($foldername){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', '0');
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function folder_levelTwoexists($foldername,$level,$id){
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function folder_levelThreeexists($foldername,$level,$id,$secondllevelid){
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', $secondllevelid);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function folder_levelFourexists($foldername,$level,$id,$thirdllevelid){
    //     $secondlevelid=$this->findLevelThreeid($thirdllevelid);
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', $secondlevelid);
    //     $this->db->where('level3', $thirdllevelid);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }

    // }

    // function folder_levelTwoMonthWeekexists($values,$addlevel,$addlevelid,$secondvalues){
    //     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
    //     foreach($montharray as $month){
    //         $this->db->select("id");
    //         $this->db->where('name', $month);
    //         $this->db->where('level1', $addlevelid);
    //         $this->db->where('level2', NULL);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() == 1){
    //             return false;
    //             //check week    
                

    //             //end check week

    //         } else {

    //         }
    //     }
        
    //     return true;
    // }

    // function checkleveltwoid($secondlevelid){
    //     $this->db->where('id', $secondlevelid);
    //     $this->db->where('level1 !=',0);
    //     $this->db->where('level2 ', NULL);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function checklevelthreeid($thirdllevelid){
    //     $this->db->where('id', $thirdllevelid);
    //     $this->db->where('level2 !=', NULL);
    //     $this->db->where('level3 ', NULL);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function findLevelThreeid($thirdllevelid){
    //     $this->db->select("level2");
    //     $this->db->where('id', $thirdllevelid);
    //     $this->db->from('tblFolders');
    //     $query = $this->db->get();
    //     // $ret = $query->row();
    //     // return $ret->level2;
    //     if ($query->num_rows() > 0)
    //     {
    //         $row = $query->row(); 
    //         return $row->level2;
    //     }
    //     return NULL;
    // }

    // function login_existsbyid($loginname,$id){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(login)', $loginname);
    //     $this->db->where('id!=', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
   

    // function admin_exists($loginname){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(fullName)', $loginname);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function admin_existsbyid($loginname,$id){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(fullName)', $loginname);
    //     $this->db->where('id!=', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }


    // //process login
    // function check(){
    //     $userName =  $this->input->post('userName');
    //     $password =  $this->input->post('password');

    //     $this->db->where('login',$userName);
    //     $this->db->where('pwd',$password);
    //     // $this->db->where('user_active','yes');
    //     return $this->db->get('tblFolders')->row_array();
    // }


    // function processLogin($userName=NULL,$password){



    //     $this->db->select("id,login,fullName");
    //     $whereCondition = $array = array('login' =>$userName,'pwd'=>$password);
    //     $this->db->where($whereCondition);
    //     $this->db->from('tblFolders');
    //     $query = $this->db->get();
    //     return $query;
    // }//function processLogin(){
    // //end process login

    // function buildTopMenuItem()
    // {
    //     // $menu = array();
        
    //     // $result = $this->getSectionAll();
    //     // foreach ($result as $row)
    //     // {
    //     //     $menu[] = array(
    //     //         'name_zh' => $row->cp_name,
    //     //         'name_cn' => $row->sp_name,
    //     //         'name_en' => $row->ep_name,
    //     //         'link' => $row->link
    //     //     );
    //     // }
    //     // return $menu;
        
    // }
}