<?php 
class Pos_model extends CI_Model {
	public function _consruct(){
		parent::_construct();
 	}

 	public function getPosData($Pos_id='',$view=''){

 		
 		$sql = "SELECT *
			    FROM pos  
			  
			    WHERE pos_id='$Pos_id' Order by pos_id DESC";

	    $PosData = $this->db->query($sql);
	
	    
	    if(isset($PosData)){

	    	return ($PosData->row());
	    }
	    return 0;
 	}

 	public function addPos($Pos_data = array()){

	if(empty($Pos_data))
 			return 0;
          $userNameChk = $this->db->query("SELECT * FROM users 
 									     WHERE  user_type='2' AND username='".$Pos_data['email']."'");
 		if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2; 

 		
 		$emailChk = $this->db->query("SELECT * FROM pos AS CUST 
 									  
								      WHERE  (CUST.email_id='".$Pos_data['email']."')");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) return 2;

 		$phoneChk = $this->db->query("SELECT * FROM pos AS CUST 
 									 
									  WHERE (CUST.phone_no='".$Pos_data['phone']."')");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

    $status = $this->db->insert('pos', 
									 array('name'=>$Pos_data['name'],
										   'email_id'=>$Pos_data['email'],
										   'code'=>$Pos_data['phone_code'],
										   'phone_no'=>$Pos_data['phone'],
										   'altered_no'=>$Pos_data['altered_no'],
										   'location'=>$Pos_data['location'],
										   'lat'=>$Pos_data['lat'],
										   'lng'=>$Pos_data['lng'],
										   'Pos_code'=>$Pos_data['Pos_code'],
										  
										   'address'=>$Pos_data['address'],
										   'working_from'=>$Pos_data['working_from'],
										   'working_to'=>$Pos_data['working_to'],
										   'profile_photo'=>$Pos_data['profile_image']));
		                       $pos_id = $this->db->insert_id();

if (isset($Pos_data['services']) && isset($Pos_data['amount']) && 
	!empty($Pos_data['services']) && !empty($Pos_data['amount'])) {

    foreach ($Pos_data['services'] as $key => $service_id) {
        $amount   = $Pos_data['amount'][$key];
        $services = $Pos_data['services'][$key];

        $this->db->query("INSERT INTO pos_services (service_id, cost, pos_id) VALUES ('$services', '$amount', '$pos_id')");
    } 
}
 		
 

		if(empty($pos_id) && !isset($pos_id)){
 			return 0;
 		}else{

         $status = $this->db->insert('users', array('user_type'=>'3',
										   'profile_id'=>$pos_id,
										   'username'=>$Pos_data['email'],
										   'phone_no'=>$Pos_data['phone'],
										   'password'=>$Pos_data['password']));

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


        if(empty($insertId) && !isset($insertId)){
 			return 0;
 		}else{

   
    $data['status']=1;
 		}


       }
       return $data;	   
 		
 	}




 	function updatePos($Pos_id = '', $Pos_data = array()){

 		
 		if(empty($Pos_id) || empty($Pos_data))
 			return 0;
 		$userIdChk = $this->db->query("SELECT * FROM pos AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.pos_id)
 									   WHERE CUST.pos_id!='".$Pos_id."' AND 
								   			 USR.username='".$Pos_data['email']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0) { return 4; }

 		$emailChk = $this->db->query("SELECT * FROM pos AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.pos_id)
 									  WHERE CUST.pos_id!='".$Pos_id."' AND 
								   			CUST.email_id='".$Pos_data['email']."'");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) { return 2; }

 		$phoneChk = $this->db->query("SELECT * FROM pos AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.pos_id)
 									  WHERE CUST.pos_id!='".$Pos_id."' AND 
								   			CUST.phone_no='".$Pos_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) { return 3; }

 		                 $upMecArr =  array('name'=>$Pos_data['name'],
										   'email_id'=>$Pos_data['email'],
										   'code'=>$Pos_data['phone_code'],
										   'phone_no'=>$Pos_data['phone'],
										   'altered_no'=>$Pos_data['altered_no'],
										   'location'=>$Pos_data['location'],
										   'lat'=>$Pos_data['lat'],
										   'lng'=>$Pos_data['lng'],
										   'Pos_code'=>$Pos_data['Pos_code'],
										   'address'=>$Pos_data['address'],
										   'working_from'=>$Pos_data['working_from'],
										   'working_to'=>$Pos_data['working_to']);
                               
 		$admUpdateArr = array('username'=>$Pos_data['email']);
							  

 		if(isset($Pos_data['profile_image']) && !empty($Pos_data['profile_image'])){
 			$upMecArr['profile_photo'] = $Pos_data['profile_image'];
 			//$admUpdateArr['profile_photo'] = $Pos_data['profile_image'];
 		}
//echo json_encode($upMecArr);exit;
 		$status = $this->db->update('users',$admUpdateArr,array('profile_id'=>$Pos_id,'user_type'=>'1'));
 		//echo $this->db->last_query();exit;
 		if(!$status) { return 0; }

		$status = $this->db->update('pos',$upMecArr,array('pos_id'=>$Pos_id));

 		return $status;
 	}

 	function changeStatus($Pos_id = '', $status = '0'){

 		if(empty($Pos_id)){
 			return 0;
 		}

 		$status = $this->db->update('users',array('status'=>$status),array('profile_id'=>$Pos_id,'user_type'=>'1'));
 		//echo $this->db->last_query();exit;
 		return $status;
 	}



function email_availability_check($email)
    {
if($email!=''){

        $query = "SELECT * FROM pos AS CUST 
 									   INNER JOIN users AS USR ON (USR.user_id=CUST.pos_id)
 									   WHERE CUST.id!='".$Pos_id."' AND 
								   			 USR.username='".$email."'";
        $query=$this->db->query($query,array($email));
        if($query->num_rows()>0)
        {
            return $query->row_array();
        } else
        {
            return false;
        }

        }
    return false;
    }

public function get_all_Pos($filter=null) {
    	if($filter){
    		$this->db->order_by($filter['order_by'], $filter['order_type']);
    		if(!empty($filter['where'])){
    			$this->db->where($filter['where']);
    		}
    		if($filter['length']!=-1)
    			$this->db->limit($filter['length'],$filter['start']);
    	}
    	$this->db->where('users.user_type','3');
    	$this->db->where('users.status!=','2');
    	$this->db->group_by('pos.pos_code');
    	$this->db->select('pos.*,users.status');
    	$this->db->from('pos');
    	$this->db->join('users', 'users.profile_id = pos.pos_id');
        
    	$res = $this->db->get();
    //echo $this->db->last_query();exit;
    //print_r($res->result());exit;
    	return $res->result();
    }

public function getPosServices(){

 		

 		$sql = "SELECT *
			    FROM services WHERE status!='3' Order by id DESC";

	    $PosData = $this->db->query($sql);
	 
	    if(isset($PosData)){

	    	return ($PosData->result());
	    }
	    return 0;
 	}
 	public function getAmenities(){

 		

 		$sql = "SELECT *
			    FROM amenities WHERE status!='3' Order by id DESC";

	    $amenitiesData = $this->db->query($sql);
	 
	    if(isset($amenitiesData)){

	    	return ($amenitiesData->result());
	    }
	    return 0;
 	}

}
?>