From 53d065a97b26e91710aff362e3f41b11a65b93ec Mon Sep 17 00:00:00 2001
From: jansa <jansa@techware.in>
Date: Fri, 6 Sep 2019 11:50:23 +0530
Subject: [PATCH] apis

---
 application/controllers/Webservice.php  | 520 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 application/helpers/general_helper.php  |  14 ++++++++++++++
 application/models/Webservice_model.php | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 846 insertions(+), 21 deletions(-)

diff --git a/application/controllers/Webservice.php b/application/controllers/Webservice.php
index 5febde8..10bf572 100644
--- a/application/controllers/Webservice.php
+++ b/application/controllers/Webservice.php
@@ -57,14 +57,6 @@
 				$err = 1;
 				$msg = 'Provide valid Country';
 			}
-			else if(!isset($postData['user_region']) || empty($postData['user_region'])){
-				$err = 1;
-				$msg = 'Provide valid Region';
-			}
-			else if(!isset($postData['user_city']) || empty($postData['user_city'])){
-				$err = 1;
-				$msg = 'Provide valid City';
-			}
 			else if(!isset($postData['user_area']) || empty($postData['user_area'])){
 				$err = 1;
 				$msg = 'Provide valid Area';
@@ -148,6 +140,10 @@
 				$err = 1;
 				$msg = 'Device Id is Required';
 			}
+			else if(!isset($postData['user_type']) || empty($postData['user_type'])){
+				$err = 1;
+				$msg = 'User Type is Required';
+			}
 
 			if($err == 1){
 				$respArr['message'] = $msg;
@@ -157,8 +153,514 @@
 			echo json_encode($respArr);exit;
 		}
 		//Forgot Password
+		public function update_profile(){
+			header('Content-type: application/json');
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			$err = 0;
+			$msg = '';
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$err = 1;
+				$msg = 'User Id is Required';
+			} 
+			else if(!isset($postData['user_name']) || empty($postData['user_name'])){
+				$err = 1;
+				$msg = 'Provide a valid Username';
+			} 
+			else if(!isset($postData['user_password']) || empty($postData['user_password'] = md5($postData['user_password']))){
+				$err = 1;
+				$msg = 'Provide a Password';
+			}
+			else if(!isset($postData['user_email']) || empty($postData['user_email'])){
+				$err = 1;
+				$msg = 'Provide valid Email';
+			}
+			else if(!isset($postData['user_phonecode']) || empty($postData['user_phonecode'])){
+				$err = 1;
+				$msg = 'Provide Phone Code';
+			}
+			else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
+				$err = 1;
+				$msg = 'Provide valid Phone Number';
+			}
+			else if(!isset($postData['user_country']) || empty($postData['user_country'])){
+				$err = 1;
+				$msg = 'Provide valid Country';
+			}
+			else if(!isset($postData['user_area']) || empty($postData['user_area'])){
+				$err = 1;
+				$msg = 'Provide valid Area';
+			}
+			else if(!isset($postData['user_address']) || empty($postData['user_address'])){
+				$err = 1;
+				$msg = 'Provide an Address';
+			}
+			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
+				$err = 1;
+				$msg = 'Provide User Location';
+			}
+			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
+				$err = 1;
+				$msg = 'Provide User Location';
+			}
+			if($err == 1){
+				$respArr['message'] = $msg;
+				echo json_encode($respArr);exit;
+			}
+			$custResp = $this->Webservice_model->updateCustomer($postData);
+			if(empty($custResp)){
+				echo json_encode($respArr);exit;
+			}
+			if($custResp == '1'){
+				$respArr['status'] = 'success';
+				$respArr['message'] = 'Success';
+				echo json_encode($respArr);exit;
+			} else if($custResp == '2'){
+				$respArr['status'] = 'error';
+				$respArr['message'] = 'Email Address already in use';
+				echo json_encode($respArr);exit;
+			} else if($custResp == '3'){
+				$respArr['status'] = 'error';
+				$respArr['message'] = 'Phone already in use';
+				echo json_encode($respArr);exit;
+			}else{
+				$respArr['status'] = 'error';
+				$respArr['message'] = 'Something went wrong..Try Again Later';
+				echo json_encode($respArr);exit;
+			}
+			echo json_encode($respArr); exit;
+		}
+
+		public function upload_profilePic(){
+			header('Content-type: application/json');
+			$postData = $_POST;
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}
+			if(isset($_FILES) && !empty($_FILES) && isset($_FILES['user_profilepic']) && !empty($_FILES['user_profilepic'])){
+				$config = set_upload_service("assets/uploads/customer");
+				$this->load->library('upload');
+				$config['file_name'] = time()."_".$_FILES['user_profilepic']['name'];
+				$this->upload->initialize($config);
+				if($this->upload->do_upload('user_profilepic')){
+					$upload_data = $this->upload->data();
+					$postData['profile_photo'] = $config['upload_path']."/".$upload_data['file_name'];
+				}
+			}
+			$custResp = $this->Webservice_model->upload_profilePic($postData);
+			if($custResp == '1'){
+				$respArr = array('status'=>'success','message'=>'Profile Pic Updated Successfully');
+			}
+			echo json_encode($respArr); exit;
+		}
+
 		public function user_forgetpsw(){
-			
+			header('Content-type: application/json');
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
+				$respArr['message'] = 'Phone Number is Required';
+				echo json_encode($respArr);exit;
+			}else if(!isset($postData['user_countrycode']) || empty($postData['user_countrycode'])){
+				$respArr['message'] = 'Country Code is Required';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->user_forgetpsw($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function user_otp(){
+			header('Content-type: application/json');
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}else if(!isset($postData['otp']) || empty($postData['otp'])){
+				$respArr['message'] = 'OTP is Required';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->user_otp($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function user_otp_resend(){
+			header('Content-type: application/json');
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
+				$respArr['message'] = 'Phone Number is Required';
+				echo json_encode($respArr);exit;
+			}else if(!isset($postData['user_countrycode']) || empty($postData['user_countrycode'])){
+				$respArr['message'] = 'Country Code is Required';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->user_forgetpsw($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function get_carVin(){
+			header('Content-type: application/json');
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'0','message'=>'Something went wrong.');
+			if(empty($postData) || !isset($postData['user_id']) || empty($postData['user_id']) || !isset($postData['car_vinNumber']) || empty($postData['car_vinNumber'])){
+				echo json_encode($respArr);exit;
+			}
+			$settings = getSettings();
+			$url = "https://specifications.vinaudit.com/v3/specifications?format=json&key=".urlencode($settings['vin_audit_api'])."&vin=".urlencode($postData['car_vinNumber']);
+			if(!empty($url)){
+				$vehData=file_get_contents($url);
+				if(empty($vehData) || empty($vehData = json_decode($vehData,true))){
+					echo json_encode($return_arr);exit;
+				}
+				if(!isset($vehData['success']) || empty($vehData['success']) || $vehData['success'] == false || ((!isset($vehData['attributes']) || empty($vehData['attributes'])) && 
+                	(!isset($vehData['selections']) || empty($vehData['selections'])))){
+					$return_arr['status'] = 2;
+					$return_arr['message'] = 'No Data Found.';
+					echo json_encode($return_arr);exit;
+				}
+				$vehDetails['vehicle'] = $vehData['attributes']['year'].' '.
+                                         	 $vehData['attributes']['make'].' '.
+                                         	 $vehData['attributes']['model'];
+                $vehDetails['year']    = $vehData['attributes']['year'];
+                $vehDetails['make']    = $vehData['attributes']['make'];
+                $vehDetails['model']   = $vehData['attributes']['model'];
+				$vehDetails['mileage'] = (isset($postData['mileage']) && !empty($postData['mileage']))?$postData['mileage']:'';
+				$car_id = $this->Webservice_model->addVehicle($vehDetails,$postData);
+				if($car_id){
+					$return_arr['status'] = '1';
+					$return_arr['data'] = $vehDetails;
+				}
+			}
+			echo json_encode($return_arr);exit;
+		}
+
+		public function people_most_purchased(){
+			header('Content-type: application/json');
+			$respArr = $this->Webservice_model->people_most_purchased();
+			echo json_encode($respArr); exit;
+		}
+
+		public function products_for_user(){
+			header('Content-type: application/json');
+			$postData = $_GET;
+			$respArr = $this->Webservice_model->products_for_user($postData['user_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function user_bookedService(){
+			header('Content-type: application/json');
+			$postData = $_GET;
+			$respArr = $this->Webservice_model->user_bookedService($postData['user_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function add_UserCar(){
+			$postData = $_POST;
+			$respArr = array('status'=>'error','message'=>'Something went wrong1.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			$err = 0;
+			$msg = '';
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$err = 1;
+				$msg = 'User Id is Required';
+			} 
+			else if(!isset($postData['car_VinNumber']) || empty($postData['car_VinNumber'])){
+				$err = 1;
+				$msg = 'Provide a car Vin Number';
+			} 
+			else if(!isset($postData['car_brand']) || empty($postData['car_brand'])){
+				$err = 1;
+				$msg = 'Provide a Car Brand';
+			}
+			else if(!isset($postData['car_model']) || empty($postData['car_model'])){
+				$err = 1;
+				$msg = 'Provide a Car Model';
+			}
+			else if(!isset($postData['car_year']) || empty($postData['car_year'])){
+				$err = 1;
+				$msg = 'Provide s Car Year';
+			}
+			else if(!isset($postData['car_milege']) || empty($postData['car_milege'])){
+				$err = 1;
+				$msg = 'Provide Car Mileage';
+			}
+			if($err == 1){
+				$respArr['message'] = $msg;
+				echo json_encode($respArr);exit;
+			}
+			$postData['vehicle_image'] = '';
+			if(isset($_FILES)&& !empty($_FILES)&& isset($_FILES['car_image'])&& !empty($_FILES['car_image'])){
+				$config = set_upload_service("assets/uploads/vehicle");
+				$this->load->library('upload');
+				$config['file_name'] = time()."_".$_FILES['car_image']['name'];
+				$this->upload->initialize($config);
+				if($this->upload->do_upload('car_image')){
+					$upload_data = $this->upload->data();
+					$postData['vehicle_image'] = $config['upload_path']."/".$upload_data['file_name'];
+				}
+			}
+			$respArr = $this->Webservice_model->add_UserCar($postData);
+			echo json_encode($respArr);exit;
+		}
+
+		public function get_mainservicelist(){
+			header('Content-type: application/json');
+			$respArr = $this->Webservice_model->get_MainServices();
+			echo json_encode($respArr); exit;
+		}
+
+		public function get_subservicelist(){
+			header('Content-type: application/json');
+			$postData = $_GET;
+			if(!isset($postData['service_id']) || empty($postData['service_id'])){
+				$respArr['status'] = "error";
+				$respArr['message'] = 'Provide Car Mileage';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->get_subServices($postData['service_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function get_carDetails(){
+			header('Content-type: application/json');
+			$postData = $_GET;
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['status'] = "error";
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->get_carDetails($postData['user_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function get_userProfile(){
+			header('Content-type: application/json');
+			$postData = $_GET;
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['status'] = "error";
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->get_userProfile($postData['user_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function road_assistanceList(){
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
+				$respArr['message'] = 'User Latitude is Required';
+				echo json_encode($respArr); exit;
+			} 
+			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
+				$respArr['message'] = 'User Longitude is Required';
+				echo json_encode($respArr); exit;
+			} 
+			$respArr = $this->Webservice_model->road_assistanceList($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function road_assistanceDetail(){
+			$postData = $_GET;
+
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			if(!isset($postData['roadassistant_id']) || empty($postData['roadassistant_id'])){
+				$respArr['message'] = 'Road Assistant Id is Required';
+				echo json_encode($respArr); exit;
+			}
+			$respArr = $this->Webservice_model->road_assistanceDetail($postData['roadassistant_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function book_roadassistance(){
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			$err = 0;
+			$msg = '';
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$err = 1;
+				$msg = 'User Id is Required';
+			} 
+			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
+				$err = 1;
+				$msg = 'Provide User Longitude';
+			} 
+			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
+				$err = 1;
+				$msg = 'Provide User Latitude';
+			}
+			else if(!isset($postData['roadassistance_id']) || empty($postData['roadassistance_id'])){
+				$err = 1;
+				$msg = 'Provide Road Assistant Id';
+			}
+			else if(!isset($postData['user_carId']) || empty($postData['user_carId'])){
+				$err = 1;
+				$msg = 'Provide User Car Id';
+			}
+			else if(!isset($postData['date']) || empty($postData['date'])){
+				$err = 1;
+				$msg = 'Provide Booking Date';
+			}
+			if($err == 1){
+				$respArr['message'] = $msg;
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->book_roadassistance($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function road_assistanceDetail_history(){
+			$postData = $_GET;
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(!isset($postData['roadassistant_id']) || empty($postData['roadassistant_id'])){
+				$respArr['message'] = 'Road Assistant Id is Required';
+				echo json_encode($respArr); exit;
+			}
+			$respArr = $this->Webservice_model->road_assistanceDetail_history($postData['roadassistant_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function status_roadassistancerequest(){
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			if(!isset($postData['roadservice_requestId']) || empty($postData['roadservice_requestId'])){
+				$respArr['message'] = 'Road Assistant Id is Required';
+				echo json_encode($respArr); exit;
+			}else if(!isset($postData['status'])){
+				$respArr['message'] = 'Status is Required';
+				echo json_encode($respArr); exit;
+			}
+			$respArr = $this->Webservice_model->status_roadassistancerequest($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function road_assistancerequestlist(){
+			$postData = $_GET;
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$respArr['message'] = 'User Id is Required';
+				echo json_encode($respArr); exit;
+			}
+			$respArr = $this->Webservice_model->road_assistancerequestlist($postData['user_id']);
+			echo json_encode($respArr); exit;
+		}
+
+		public function write_review(){
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			$err = 0;
+			$msg = '';
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$err = 1;
+				$msg = 'User Id is Required';
+			}
+			else if(!isset($postData['review_rate']) || empty($postData['review_rate'])){
+				$err = 1;
+				$msg = 'Rating is Required';
+			}
+			else if(!isset($postData['reviewing_type']) || empty($postData['reviewing_type'])){
+				$err = 1;
+				$msg = 'Reviewing Type is Required';
+			}
+			else if(!isset($postData['review_caption']) || empty($postData['review_caption'])){
+				$err = 1;
+				$msg = 'Review Caption is Required';
+			}
+			else if(!isset($postData['review_description']) || empty($postData['review_description'])){
+				$err = 1;
+				$msg = 'Review Description is Required';
+			}
+			else if(!isset($postData['booking_id']) || empty($postData['booking_id'])){
+				$err = 1;
+				$msg = 'Booking Id is Required';
+			}
+			if($err == 1){
+				$respArr['message'] = $msg;
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->write_review($postData);
+			echo json_encode($respArr); exit;
+		}
+
+		public function user_requestService(){
+			$post = file_get_contents("php://input");
+			$postData = json_decode($post, true);
+			$respArr = array('status'=>'error','message'=>'Something went wrong.');
+			if(empty($postData)){
+				echo json_encode($respArr);exit;
+			}
+			$err = 0;
+			$msg = '';
+			if(!isset($postData['user_id']) || empty($postData['user_id'])){
+				$err = 1;
+				$msg = 'User Id is Required';
+			}
+			else if(!isset($postData['subservice_id']) || empty($postData['subservice_id'])){
+				$err = 1;
+				$msg = 'Sub Service Id is Required';
+			}
+			else if(!isset($postData['booking_date']) || empty($postData['booking_date'])){
+				$err = 1;
+				$msg = 'Booking Date is Required';
+			}
+			else if(!isset($postData['car_id']) || empty($postData['car_id'])){
+				$err = 1;
+				$msg = 'Car Id is Required';
+			}
+			if($err == 1){
+				$respArr['message'] = $msg;
+				echo json_encode($respArr);exit;
+			}
+			$respArr = $this->Webservice_model->user_requestService($postData);
+			echo json_encode($respArr); exit;
 		}
 
 	}
diff --git a/application/helpers/general_helper.php b/application/helpers/general_helper.php
index 17f0db2..68fff87 100644
--- a/application/helpers/general_helper.php
+++ b/application/helpers/general_helper.php
@@ -13,4 +13,18 @@
 		echo '<br>';
 	}
 
+	function set_upload_service($path){
+	    $config = array();
+	    $config['upload_path'] = $path;
+	    $config['allowed_types'] = '*';
+	    $config['overwrite'] = FALSE;
+	    return $config;
+	}
+
+	function getSettings(){
+		$CI = & get_instance();
+		$settings = $CI->db->get('settings');
+		return (!empty($settings))?$settings->row_array():'';
+	}
+
 ?>
\ No newline at end of file
diff --git a/application/models/Webservice_model.php b/application/models/Webservice_model.php
index 375bcd9..b57d95a 100644
--- a/application/models/Webservice_model.php
+++ b/application/models/Webservice_model.php
@@ -9,8 +9,8 @@ class Webservice_model extends CI_Model {
         if(empty($postData))
             return 0;
         if(isset($postData['user_email']) && !empty($postData['user_email'])){
-            $this->db->join('customer AS CUST','CUST.id = USR.user_id');
-            $emailChk = $this->db->get_where('users AS USR',array('CUST.email_id'=>$postData['user_email'], 'USR.status !='=>'2','USR.user_type'=>'1'));
+           // $this->db->join('customer AS CUST','CUST.cust_id = USR.profile_id');
+            $emailChk = $this->db->get_where('users AS USR',array('USR.username'=>$postData['user_email'], 'USR.status !='=>'2','USR.user_type'=>'1'));
             if(!empty($emailChk) && $emailChk->num_rows() > 0){
                 return 2;
             }
@@ -28,18 +28,18 @@ class Webservice_model extends CI_Model {
                             'phone_no'=>$postData['user_phonenumber'],
                             'device_id'=>$postData['device_id'],
                             'address'=>$postData['user_address'],
-                            'country'=>$postData['user_country'],
-                            'region'=>$postData['user_region'],
-                            'city'=>$postData['user_city'],
+                            // 'country'=>$postData['user_country'],
+                            // 'region'=>$postData['user_region'],
+                            // 'city'=>$postData['user_city'],
                             'area'=>$postData['user_area'],
                             'lat'=>$postData['user_lat'],
                             'lng'=>$postData['user_lng'],
-                            'password'=>$postData['user_password'],
+                            //'password'=>$postData['user_password'],
                         );
         $status = $this->db->insert('customer',$insert_array);
         $last_id = $this->db->insert_id();
 
-        $status = $this->db->insert('users',array('user_type'=>'1','user_id'=>$last_id,'username'=>$postData['user_email'],'phone_no'=>$postData['user_phonenumber'],'password'=>$postData['user_password'],'status'=>'1'));
+        $status = $this->db->insert('users',array('user_type'=>'1','profile_id'=>$last_id,'username'=>$postData['user_email'],'phone_no'=>$postData['user_phonenumber'],'password'=>$postData['user_password'],'status'=>'1'));
         return ($status)?1:0;
     }
 
@@ -48,15 +48,15 @@ class Webservice_model extends CI_Model {
         if(empty($userLogData)){
             return $respArr;
         }
-        $this->db->join('customer AS CUST','CUST.id = USR.user_id');
-        $result = $this->db->get_where('users AS USR',array('CUST.email_id'=>$userLogData['user_email'], 'USR.status'=>'1','USR.user_type'=>'1'));
+        //$this->db->join('customer AS CUST','CUST.cust_id = USR.profile_id');
+        $result = $this->db->get_where('users AS USR',array('USR.username'=>$userLogData['user_email'], 'USR.status'=>'1'));
 
         if(empty($result) || $result->num_rows() < 1 || empty($custData = $result->row())){
             $respArr['message'] = "Invalid Email Address";
             return $respArr;
         }
-        $this->db->join('customer AS CUST','CUST.id = USR.user_id');
-        $result = $this->db->get_where('users AS USR',array('CUST.email_id'=>$userLogData['user_email'], 'USR.status'=>'1','USR.user_type'=>'1','USR.password'=>$userLogData['user_password']));
+        //$this->db->join('customer AS CUST','CUST.cust_id = USR.profile_id');
+        $result = $this->db->get_where('users AS USR',array('USR.username'=>$userLogData['user_email'], 'USR.status'=>'1','USR.user_type'=>'1','USR.password'=>$userLogData['user_password']));
 
         $respArr['message'] = "Invalid Password";
 
@@ -77,7 +77,7 @@ class Webservice_model extends CI_Model {
         if($result['status'] == 'error'){
             return $result;
         }
-        if($this->db->update('customer',array('lat'=>$postData['user_lat'],'lng'=>$postData['user_lng'],'device_id'=>$postData['device_id']),array('id'=>$result['data']->user_id))){
+        if($this->db->update('customer',array('lat'=>$postData['user_lat'],'lng'=>$postData['user_lng'],'device_id'=>$postData['device_id']),array('cust_id'=>$result['data']->profile_id))){
             $respArr['status'] = "success";
             $respArr['message'] = "success";
             $respArr['data'] = $result['data'];
@@ -85,5 +85,314 @@ class Webservice_model extends CI_Model {
         return $respArr;
     }
 
+    public function updateCustomer($postData){
+         if(empty($postData))
+            return 0;
+        if(isset($postData['user_email']) && !empty($postData['user_email'])){
+            //$this->db->join('customer AS CUST','CUST.cust_id = USR.profile_id');
+            $emailChk = $this->db->get_where('users AS USR',array('USR.username'=>$postData['user_email'], 'USR.status !='=>'2','USR.user_type'=>'1','USR.profile_id !='=>$postData['user_id']));
+            if(!empty($emailChk) && $emailChk->num_rows() > 0){
+                return 2;
+            }
+        }
+        if(isset($postData['user_phonenumber']) && !empty($postData['user_phonenumber'])){
+            $phoneChk = $this->db->get_where('users',array('phone_no'=>$postData['user_phonenumber'],'status !='=>'2','profile_id !='=>$postData['user_id']));
+            if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
+                return 3;
+            }
+        }
+        $insert_array = array(
+                            'name'=>$postData['user_name'],
+                            'email_id'=>$postData['user_email'],
+                            'code'=>$postData['user_phonecode'],
+                            'phone_no'=>$postData['user_phonenumber'],
+                            'address'=>$postData['user_address'],
+                            'area'=>$postData['user_area'],
+                            'lat'=>$postData['user_lat'],
+                            'lng'=>$postData['user_lng']
+                        );
+        if($this->db->update('customer',$insert_array,array('cust_id'=>$postData['user_id']))){
+            $status = $this->db->update('users',array('user_type'=>'1','username'=>$postData['user_email'],'phone_no'=>$postData['user_phonenumber'],'status'=>'1'),array('profile_id'=>$postData['user_id']));
+            return ($status)?1:0;
+        }
+    }
+
+    public function upload_profilePic($postData = array()){
+        if($this->db->update('customer',array('profile_photo'=>$postData['profile_photo']),array('cust_id'=>$postData['user_id']))){
+            return 1;
+        }
+    }
+
+    public function add_UserCar($postData = array()){
+        $respArr = array('status'=>'error');
+        $vehicle = $postData['car_year'].' '.$postData['car_brand'].' '.$postData['car_model'];
+        if($this->db->insert('vehicle_model',array('cust_id'=>$postData['user_id'],'vin'=>$postData['car_VinNumber'],'make'=>$postData['car_brand'],'model'=>$postData['car_model'],'year'=>$postData['car_year'],'vehicle_name'=>$vehicle,'mileage'=>$postData['car_milege'],'vehicle_image'=>$postData['vehicle_image']))){
+            $respArr['status'] = "success";
+            $respArr['message'] = "Car Added Successfully";
+        }
+        return $respArr;
+    }
+
+    public function user_forgetpsw($postData = array()){
+        $respArr = array('status'=>'error');
+        $this->db->join("customer","customer.cust_id = users.profile_id");
+        $phoneChk = $this->db->get_where('users',array('users.phone_no'=>$postData['user_phonenumber'],'status !='=>'2','users.user_type'=>'1','customer.code'=>$postData['user_countrycode']));
+        if(empty($phoneChk) || $phoneChk->num_rows() < 0 || empty($phoneChk = $phoneChk->row_array())){
+            $respArr['message'] = "Phone Number Doesnot Exist";
+            return $respArr;
+        }
+        $otp = rand(1000,9999);
+        if($this->db->update("users",array("otp"=>$otp),array('profile_id'=>$postData['user_id'],'user_type'=>'1'))){
+            $respArr['status'] = "success";
+            $respArr['data'] = $otp;
+        }
+        return $respArr;
+    }
+
+    public function user_otp($postData = array()){
+        $respArr = array('status'=>'error','message'=>'OTP Doesnot Match');
+        $otpChk = $this->db->get_where('users',array('profile_id'=>$postData['user_id'],'user_type'=>'1'));
+        if(empty($otpChk) || empty($otpChk = $otpChk->row_array())){
+            $respArr['message'] = "User Doesnot Exist";
+            return $respArr;
+        }
+        if($postData['otp'] == $otpChk['otp']){
+            $respArr['status'] = "success";
+            $respArr['message'] = "success";
+        }
+        return $respArr;
+    }
+
+    public function people_most_purchased(){
+        $respArr = array('status'=>'error');
+        $sql =  $this->db->query("SELECT count(PTS.id) AS count,PTS.id,PTS.unique_name,PTS.part_number FROM pos_parts AS PTS LEFT JOIN booking_parts AS BKPTS ON BKPTS.pos_part_id = PTS.id WHERE PTS.status='1' GROUP BY PTS.id ORDER BY count DESC");
+        if(!empty($sql) && !empty($data=$sql->result_array())){
+            $respArr['status'] = "success";
+            $respArr['data'] = $data;
+        }
+        return $respArr;
+    }
+
+    public function products_for_user($userId =''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT * FROM vehicle_model WHERE cust_id=$userId");
+        if(!empty($sql) && !empty($data=$sql->result_array())){
+            $name = '';
+            foreach ($data as $key => $value) {
+                $eCond = ($key != count($data)-1)?' OR ':'';
+                $name .= " unique_name LIKE '%".$value['vehicle_name']."%' ".$eCond;
+            }
+            $res = $this->db->query("SELECT unique_name,id,part_number pos_parts FROM pos_parts WHERE $name GROUP BY unique_name");
+            if(!empty($res) && !empty($prdtData=$res->result_array())){
+                $respArr['data'] = $prdtData;
+            }else{
+                $result = $this->people_most_purchased();    
+                $respArr['data'] = $result['data'];
+            }
+        }else{
+            $result = $this->people_most_purchased();
+            $respArr['data'] = $result['data'];
+        }
+        $respArr['status'] = "success";
+        return $respArr;
+    }
+
+    public function user_bookedService($userId = ''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT GRBK.booking_id,GRBK.booking_date,GROUP_CONCAT(SBSRVCE.sub_services SEPARATOR ', ') AS subservices FROM garage_booking AS GRBK LEFT JOIN sub_services AS SBSRVCE ON find_in_set(SBSRVCE.id,GRBK.subservices_ids) WHERE GRBK.cust_id=$userId AND GRBK.status=1");
+        if(empty($sql) || empty($data=$sql->result_array())){
+            $respArr['message'] = "No Bookings";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $data;
+        return $respArr;
+    }
+
+    public function get_MainServices(){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT * FROM services WHERE status=1");
+        if(empty($sql) || empty($data=$sql->result_array())){
+            $respArr['message'] = "No Service";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $data;
+        return $respArr;    
+    }
+
+    public function get_subServices($serviceId = ''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT * FROM sub_services WHERE service_id=$serviceId AND status=1");
+        if(empty($sql) || empty($data=$sql->result_array())){
+            $respArr['message'] = "No SubService";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $data;
+        return $respArr;
+    }
+
+    public function get_userProfile($userId = ''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT customer.*,users.username,users.password,users.phone_no FROM customer JOIN users ON (users.profile_id = customer.cust_id AND users.user_type=1) WHERE customer.cust_id =$userId");
+        if(empty($sql) || empty($data=$sql->result_array())){
+            $respArr['message'] = "No Users Exist";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $data;
+        return $respArr;
+    }
+
+    public function get_carDetails($userId = ''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT * FROM vehicle_model WHERE cust_id =$userId");
+        if(empty($sql) || empty($data=$sql->result_array())){
+            $respArr['message'] = "No Cars Added";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $data;
+        return $respArr;
+    }
+
+    public function road_assistanceList($postData = array()){
+        $respArr = array('status'=>'error');
+        $current_lat = $postData['user_lat'];
+        $current_lng = $postData['user_lng'];
+        $sql = "SELECT RDAST.road_assist_id,RDAST.road_assist_code,RDAST.name,RDAST.profile_photo,
+                 3956*2*ASIN(SQRT(POWER(SIN(($current_lat-RDAST.lat)*pi()/180/2),2)+
+                 COS($current_lat*pi()/180 )*COS(RDAST.lat*pi()/180)*
+                 POWER(SIN(($current_lng-RDAST.lng)*pi()/180/2),2) )) AS distance
+            FROM road_assist AS RDAST
+            INNER JOIN users AS USR ON (RDAST.road_assist_id=USR.profile_id AND USR.user_type=5)
+            WHERE USR.status='1'
+            HAVING distance<10 ";
+        $res = $this->db->query($sql);
+        if(empty($res) || empty($result = $res->result_array())){
+            $respArr['message'] = "No Mechanics Near to You";
+            return $respArr;
+        }
+        foreach ($result as $key => $value) {
+          $result[$key]['distance'] = round((float)$value['distance'],2);
+          $this->db->select("COUNT(rating.id) AS count, round(AVG(rate),2) AS rating ");
+          $this->db->join('road_booking','road_booking.id = rating.booking_id AND rating.type=5');
+          $roadRvw = $this->db->get_where('rating',array('road_booking.road_assist_id'=>$value['road_assist_id']));
+          if(!empty($roadRvw) && !empty($roadRvw = $roadRvw->row_array())){
+            $result[$key]['total_review'] = (empty($roadRvw['count']))?'0':$roadRvw['count'];
+            $result[$key]['avg_rate'] = (empty($roadRvw['rating']))?'0':$roadRvw['rating'];
+          }
+        }
+        if(!empty($result)){
+            $respArr['status'] = "success";
+            $respArr['data'] = $result;
+        }
+        return $respArr;
+    }
+
+    public function road_assistanceDetail($road_assist_id=''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT RDAST.road_assist_id,RDAST.road_assist_code,RDAST.name,RDAST.profile_photo,RDAST.phone_no,RDAST.email_id,RDAST.address FROM road_assist AS RDAST WHERE road_assist_id=$road_assist_id");
+        if(empty($sql) || empty($result = $sql->row_array())){
+            $respArr['message'] = "No Road Assist";
+            return $respArr;
+        }
+        $this->db->select("rating.id,rating.rate,rating.caption,rating.description");
+        $this->db->join('road_booking','road_booking.id = rating.booking_id AND rating.type=5');
+        $roadRvw = $this->db->get_where('rating',array('road_booking.road_assist_id'=>$road_assist_id));
+        if(!empty($roadRvw) && !empty($roadRvwroadRvw = $roadRvw->result_array())){
+            $result['review'] = (empty($roadRvw))?[]:$roadRvwroadRvw;
+        }
+        $respArr['status'] = "success";
+        $respArr['data '] = $result;
+        return $respArr;
+    }
+
+    public function book_roadassistance($postData = array()){
+        $respArr = array('status'=>'error');
+        $insert_array = array(
+                            'cust_id'=>$postData['user_id'],
+                            'vehicle_id'=>$postData['user_carId'],
+                            'date'=>$postData['date'],
+                            'requirement'=>(isset($postData['message']) && !empty($postData['message']))?$postData['message']:'',
+                            'lat'=>$postData['user_lat'],
+                            'lng'=>$postData['user_lng'],
+                            'road_assist_id'=>$postData['roadassistance_id'],
+                            'status'=>'1'
+                        );
+        if($this->db->insert("road_booking",$insert_array)){
+            $respArr['status'] = "success";
+            $respArr['message'] = "Road Booking Inserted Successfully";
+        }
+        return $respArr;
+    }
+
+    public function road_assistanceDetail_history($road_assist_id=''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT CUST.name,CUST.cust_id,CUST.phone_no,RDBK.id AS roadservicerequestid,RDBK.lat,RDBK.lng,VM.make,VM.model FROM road_booking AS RDBK INNER JOIN customer AS CUST ON (CUST.cust_id = RDBK.cust_id) INNER JOIN vehicle_model AS VM ON (VM.id = RDBK.vehicle_id) WHERE RDBK.road_assist_id =$road_assist_id");
+        if(empty($sql) || empty($result = $sql->result_array())){
+            $respArr['message'] = "No data Found";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $result;
+        return $respArr;
+    }
+
+    public function status_roadassistancerequest($postData = array()){
+        $respArr = array('status'=>'error');
+        if($this->db->update("road_booking",array('status'=>$postData['status']),array('id'=>$postData['roadservice_requestId']))){
+            $respArr['status'] = "success";
+        }
+        return $respArr;
+    }
+
+    public function road_assistancerequestlist($userId =''){
+        $respArr = array('status'=>'error');
+        $sql = $this->db->query("SELECT CUST.name,RDBK.road_assist_id,CUST.phone_no,RDBK.id AS roadservicerequestid,RDBK.status,RDBK.date FROM road_booking AS RDBK INNER JOIN customer AS CUST ON (CUST.cust_id = RDBK.cust_id) WHERE RDBK.cust_id =$userId");
+        if(empty($sql) || empty($result = $sql->result_array())){
+            $respArr['message'] = "No data Found";
+            return $respArr;
+        }
+        $respArr['status'] = "success";
+        $respArr['data'] = $result;
+        return $respArr; 
+    }
+
+    public function write_review($postData = array()){
+        $respArr = array('status'=>'error');
+        $insert_array = array(
+                            'user_id'=>$postData['user_id'],
+                            'type'=>$postData['reviewing_type'],
+                            'booking_id'=>$postData['booking_id'],
+                            'rate'=>$postData['review_rate'],
+                            'caption'=>$postData['review_caption'],
+                            'description'=>$postData['review_description']
+                        );
+        if($this->db->insert("rating",$insert_array)){
+            $respArr['status'] = "success";
+            $respArr['message'] = "Reviewed Successfully";
+        }
+        return $respArr;
+    }
+
+    public function user_requestService($postData = array()){
+        $respArr = array('status'=>'error');
+        // $squence = str_pad(rand(1111,9999),4,0,STR_PAD_LEFT);'booking_id'=>'BKGID'.date('ymd').$squence
+        $insert_array = array(
+                            'cust_id'=>$postData['user_id'],
+                            'subservice_ids'=>implode(',',$postData['subservice_id']),
+                            'booking_date'=>$postData['booking_date'],
+                            'vehicle_id'=>$postData['car_id'],
+                            'requirement'=>(isset($postData['message']) && !empty($postData['message']))?$postData['message']:''
+                        );
+        if($this->db->insert("garage_request",$insert_array)){
+            $respArr['status'] = "success";
+            $respArr['message'] = "Request Inserted Successfully";
+        }
+        return $respArr;
+    }
+
 }
 ?>
--
libgit2 0.27.1