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

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']);
    }
    $expId = $this->session->userdata['exp_id'];
    $where = '';
    $where .= "pos_booking.status ='2' OR (pos_booking.status ='5' AND pos_booking.carrier_id=".$expId.")";
	$this->db->order_by('pos_booking.id','desc');
	$this->db->select("pos_booking.*,customer.name as customer_name,customer.phone_no as customer_phone,pos_parts.unique_name,pos_request.posReq_id");
	$this->db->from('pos_booking');
    $this->db->join('pos_response', 'pos_response.id = pos_booking.resp_id');
    $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("pos_parts","pos_parts.id=pos_request.part_ids");
    $this->db->where($where);
    $this->db->group_by('pos_booking.id');
	$res = $this->db->get();    
    if(!empty($result = $res->result())){
        return $result;    
    }    

	return false;
}

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.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();
}

public function acceptQuote($bookId='',$status='',$expId=''){
    $respArr = array('status'=>'error');
    $bookData = $this->db->get_where('pos_booking',array('id'=>$bookId))->row_array();
    //pr($this->db->last_query());
    if(empty($bookData)){
        $respArr['error'] = 'Booking does Not Exist';
        return $respArr;
    }

    if($bookData['carrier_id'] == '0' && $bookData['status'] == '2'){
        $this->db->update('pos_booking',array('carrier_id'=>$expId,'status'=>$status),array('id'=>$bookId));
        $respArr['status'] = 'success';
        return $respArr;exit();
    }
}


}
?>