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

 	public function getPos_responseData($requestId =''){

 		$sql = $this->db->query("SELECT PSREQ.id,PSREQ.posReq_id,CUST.name as cust_name,CUST.phone_no,CUST.area,PSPTS.unique_name,VHM.make,VHM.model,VHM.year,VHM.vehicle_name,VHM.mileage FROM pos_request AS PSREQ INNER JOIN customer AS CUST ON CUST.cust_id = PSREQ.cust_id INNER JOIN pos_parts AS PSPTS ON PSPTS.id = PSREQ.part_ids INNER JOIN vehicle_model AS VHM ON VHM.id = PSREQ.vehicle_id WHERE PSREQ.posReq_id='".$requestId."'");
        if(empty($sql) || empty($result = $sql->row_array())){
            return false;
        }
        return $result;
 	}

    public function get_all_Pos_response($reqId = '',$respId='',$type=''){
        $where = '';
        if(isset($reqId) && !empty($reqId)){
            $where .= "PSRSP.req_id=$reqId";
        }
        if(isset($respId) && !empty($respId)){
            $where .= "PSRSP.pos_resp_code='".$respId."'";
        }
        if(isset($type) && $type == 3){
            $where .= " AND PSRSP.status='2'";
        }
        $sql = $this->db->query("SELECT POS.pos_code,POS.name,POS.phone_no,PSRSP.cost,PSRSP.margin,PSRSP.total,PSRSP.id AS ps_rsp_id,PSRSP.pos_resp_code,PSRSP.status,STNGS.admin_charge,STNGS.VAT FROM pos_response AS PSRSP INNER JOIN pos AS POS ON POS.pos_id = PSRSP.pos_id INNER JOIN settings AS STNGS WHERE $where");
        if(empty($sql)){
            return false;
        }
        if(isset($respId) && !empty($respId)){
            return $sql->row_array();    
        }
        return $sql->result_array();
    }

    public function updatePosMargin($postData = array()){
        $settings = getSettings();
        $total = ($postData['total'] * $settings['VAT'])/100;
        if($this->db->update('pos_response',array('margin'=>$postData['margin'],'total'=>$total),array('id'=>$postData['respId']))){
            return true;
        }
    }

    public function approve_quote($postData = ''){
        $selected = implode(',', $postData['selectedIds']);
        if($this->db->query("UPDATE pos_response SET status=2 WHERE id IN($selected)")){
            
            $posResp = $this->get_all_Pos_response($postData['req_id'],'',3);

            $cust_data = $this->db->query("SELECT CUST.name,CUST.email_id FROM pos_request AS PSREQ INNER JOIN customer AS CUST ON CUST.cust_id = PSREQ.cust_id WHERE PSREQ.id=".$postData['req_id'])->row_array() ;
            $to =isset($cust_data['email_id']) && !empty($cust_data['email_id'])?$cust_data['email_id']:'';
            $sender_email = "hello@access.sa";
            $data['name']=$cust_data['name'];
            $data['email']=$cust_data['email_id'];
            $data['postData']=$posResp;
            $mail_head = "Your POS Request Best Offers";
            $mail_status = send_custom_email($sender_email,$mail_head,$to,'Pos Request Details', $this->load->view('template/mail_template/pos_quotes', $data,TRUE),'');
            $this->db->update('pos_request',array('status'=>'4'),array('id'=>$postData['req_id']));
            return true;            
        }
    }




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

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

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



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

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


}
?>