<?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);
	  //echo $this->db->last_query();exit;
	    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($pos_id='',$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']);
    }
	$where = '';
    $where .= "find_in_set('".$pos_id."',pos_request.pos_ids) AND pos_request.status IN ('2','4','5')";
	$this->db->order_by('pos_request.id','desc');
	$this->db->select("pos_request.*,customer.name as customer_name,customer.phone_no as customer_phone,pos_parts.unique_name");
	$this->db->from('pos_request');
    $this->db->join('customer', 'customer.cust_id = pos_request.cust_id');
    $this->db->join("pos_parts","pos_parts.id=pos_request.part_ids");
    $this->db->where($where);
    $this->db->group_by('pos_request.id');
	$res = $this->db->get();         
	return $res->result();
}

public function getPosData($reqId =''){
    $this->db->select("pos_request.*,customer_address.name,customer_address.phone_no,customer_address.house_no,customer_address.area,customer_address.country,customer_address.district,customer_address.city,customer.name as customer_name,customer.phone_no as customer_phone,pos_parts.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","pos_parts.id=pos_request.part_ids","left",false);
    $this->db->join("pos","find_in_set(pos.pos_id,pos_request.pos_ids)<> 0","left",false);
    $this->db->where('pos_request.id',$reqId);
    $res = $this->db->get();    
    return $res->row();
}

public function getSendQuoteDetails($reqId){
    $this->db->select("pos_response.*,customer.name as customer_name,pos_parts.unique_name,customer_address.phone_no,customer_address.area,pos_request.posReq_id,vehicle_model.make,vehicle_model.model,vehicle_model.year,vehicle_model.vehicle_name,vehicle_model.mileage,vehicle_model.vehicle_image");
    $this->db->from('pos_response');
    $this->db->join('pos_request','pos_request.id = pos_response.req_id');
    $this->db->join('customer', 'customer.cust_id = pos_request.cust_id');
    $this->db->join('vehicle_model', 'vehicle_model.id = pos_request.vehicle_id','left');
    $this->db->join('customer_address', 'customer_address.ID = pos_request.address');
    $this->db->join("pos_parts","pos_parts.id=pos_response.pos_parts");
    $this->db->where("pos_response.pos_id",$this->session->userdata('pos_id'));
    $this->db->where("pos_response.req_id",$reqId);
    $res = $this->db->get();
    return $res->row();
}


}
?>