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

 	public function getPos_requestData($Pos_request_id='',$view=''){

 		$cond  = (!empty($view))?" USR.user_type IN ($view) ":" USR.status != '2' ";
 		$cond .= (!empty($Pos_request_id))?" AND CUST.cust_id='$Pos_request_id' ":"";

 		$sql = "SELECT CUST.*,USR.status,USR.password
			    FROM Pos_request AS CUST 
			    INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
			    WHERE $cond AND USR.user_type='1' Order by id DESC";

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

	    	return (empty($Pos_request_id))?$Pos_requestData->result():$Pos_requestData->row();
	    }
	    return 0;
 	}
 
 	function changeStatus($Pos_request_id = '', $status = '0'){

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

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

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

        $query = "SELECT * FROM Pos_request AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$Pos_request_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;
    }

function get_all_Pos_request($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('pos_request.status!=','2');
    	$this->db->order_by('pos_request.id','desc');
       

    	$this->db->select("pos_request.*,customer_address.name,customer_address.phone_no,customer_address.house_no,customer_address.area,customer_address.country,customer_address.city,customer.name as customer_name,customer.phone_no as customer_phone,GROUP_CONCAT(DISTINCT(pos_parts.unique_name)) AS unique_name,GROUP_CONCAT(DISTINCT(pos.name)) AS pos_name");
    	$this->db->from('pos_request');
        $this->db->join('customer', 'customer.cust_id = pos_request.cust_id','left');
        $this->db->join('customer_address', 'customer_address.id = pos_request.address','left');
        $this->db->join("pos_parts","find_in_set(pos_parts.id,pos_request.part_ids)<> 0","left",false);
        $this->db->join("pos","find_in_set(pos.pos_id,pos_request.pos_ids) > 0","left",false);
        $this->db->group_by('pos_request.id');
    	$res = $this->db->get();    
    	return $res->result();
    }

    function getPosData($reqId='',$requestId = ''){
        $this->db->select("pos_request.*,customer_address.name,customer_address.phone_no,customer_address.house_no,customer_address.area,customer_address.country,customer_address.city,customer.name as customer_name,customer.phone_no as customer_phone,GROUP_CONCAT(DISTINCT(pos_parts.unique_name)) AS unique_name,GROUP_CONCAT(DISTINCT(pos.name)) AS pos_name,vehicle_model.make,vehicle_model.model,vehicle_model.year,vehicle_model.vehicle_name,vehicle_model.mileage,vehicle_model.vehicle_image");
        $this->db->from('pos_request');
        $this->db->join('customer', 'customer.cust_id = pos_request.cust_id','left');
        $this->db->join('customer_address', 'customer_address.id = pos_request.address','left');
        $this->db->join('vehicle_model', 'vehicle_model.id = pos_request.vehicle_id','left');
        $this->db->join("pos_parts","find_in_set(pos_parts.id,pos_request.part_ids)<> 0","left",false);
        $this->db->join("pos","find_in_set(pos.pos_id,pos_request.pos_ids)<> 0","left",false);
        if(isset($requestId) && !empty($requestId)){
            $this->db->where('pos_request.posReq_id',$requestId);    
        }
        if(isset($reqId) && !empty($reqId)){    
            $this->db->where('pos_request.id',$reqId);
        }
        $res = $this->db->get();    
        return $res->row();
    }

    public function getAssignPos($requestId = ''){
        $sql = $this->db->query("SELECT DISTINCT POS.pos_id,POS.name,POS.pos_code,POS.phone_no,POS.location 
                        FROM pos_request AS PSREQ 
                        LEFT JOIN pos_response AS PSRSP ON PSREQ.id = PSRSP.req_id 
                        INNER JOIN pos AS POS ON FIND_IN_SET(POS.pos_id, PSREQ.pos_ids) 
                        WHERE PSREQ.posReq_id = '".$requestId."'
                        GROUP BY POS.pos_id");
        if(empty($sql) || empty($result = $sql->result_array())){
            return false;
        }
        return $result;
    }

    public function getPosDetails($requestId = ''){
        $sql = $this->db->query("SELECT POPTS.unique_name FROM pos_request AS PSREQ INNER JOIN pos_parts AS POPTS ON PSREQ.part_ids = POPTS.id WHERE PSREQ.posReq_id='".$requestId."'");
        if(empty($sql) || empty($sql = $sql->row_array())){
            return false;
        }
        $posData = $this->db->query("SELECT POS.pos_id,POS.name,POS.pos_code FROM pos AS POS INNER JOIN pos_parts AS POPTS ON POS.pos_id = POPTS.pos_id WHERE POPTS.unique_name='".$sql['unique_name']."'");
        if(empty($posData) || empty($posData = $posData->result_array())){
            return false;
        }
        return $posData;
        
    }

    public function assign_pos($postData = array()){
        if(!isset($postData)){
            return 0;
        }
        $pos = implode(",",$_POST['pos']);
        $status = $this->db->update('pos_request',array('pos_ids'=>$pos,'status'=>'2'),array('id'=>$_POST['req_id']));
        return $status;
    }

}
?>