public function provideravailabilitySubmit()
 {
     $id = Request::segment(4);
     $proavis = $_POST['proavis'];
     $proavie = $_POST['proavie'];
     $length = $_POST['length'];
     Log::info('Start end time Array Length = ' . print_r($length, true));
     DB::delete("delete from provider_availability where provider_id = '" . $id . "';");
     for ($l = 0; $l < $length; $l++) {
         $pv = new ProviderAvail();
         $pv->provider_id = $id;
         $pv->start = $proavis[$l];
         $pv->end = $proavie[$l];
         $pv->save();
     }
     Log::info('providers availability start = ' . print_r($proavis, true));
     Log::info('providers availability end = ' . print_r($proavie, true));
     return Response::json(array('success' => true));
 }
 public function provideravailability()
 {
     if (Session::has('walker_id')) {
         $pavail = ProviderAvail::where('provider_id', Session::get('walker_id'))->get();
         $prvi = array();
         foreach ($pavail as $pv) {
             $prv = array();
             $prv['title'] = 'available';
             $prv['start'] = date('Y-m-d', strtotime($pv->start)) . "T" . date('H:i:s', strtotime($pv->start));
             $prv['end'] = date('Y-m-d', strtotime($pv->end)) . "T" . date('H:i:s', strtotime($pv->end));
             array_push($prvi, $prv);
         }
         $pvjson = json_encode($prvi);
         Log::info('Provider availability json = ' . print_r($pvjson, true));
         return View::make('web.provideravailability')->with('pvjson', $pvjson)->with('title', 'Calendar')->with('page', 'yo');
     }
 }
 public function respond_request()
 {
     $token = Input::get('token');
     $walker_id = Input::get('id');
     $request_id = Input::get('request_id');
     $accepted = Input::get('accepted');
     $date_time = Input::get('datetime');
     $validator = Validator::make(array('token' => $token, 'walker_id' => $walker_id, 'request_id' => $request_id, 'accepted' => $accepted), array('token' => 'required', 'walker_id' => 'required|integer', 'accepted' => 'required|integer', 'request_id' => 'required|integer'));
     /* $driver = Keywords::where('id', 1)->first(); */
     if ($validator->fails()) {
         $error_messages = $validator->messages()->all();
         $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages);
         $response_code = 200;
     } else {
         $is_admin = $this->isAdmin($token);
         if ($walker_data = $this->getWalkerData($walker_id, $token, $is_admin)) {
             // check for token validity
             if (is_token_active($walker_data->token_expiry) || $is_admin) {
                 // Retrive and validate the Request
                 if ($request = Requests::find($request_id)) {
                     if ($request->current_walker == $walker_id) {
                         if ($accepted == 1) {
                             if ($request->later == 1) {
                                 // request ended
                                 Requests::where('id', '=', $request_id)->update(array('confirmed_walker' => $walker_id, 'status' => 1));
                             } else {
                                 Requests::where('id', '=', $request_id)->update(array('confirmed_walker' => $walker_id, 'status' => 1, 'request_start_time' => date('Y-m-d H:i:s')));
                             }
                             // confirm walker
                             RequestMeta::where('request_id', '=', $request_id)->where('walker_id', '=', $walker_id)->update(array('status' => 1));
                             // Update Walker availability
                             Walker::where('id', '=', $walker_id)->update(array('is_available' => 0));
                             // remove other schedule_meta
                             RequestMeta::where('request_id', '=', $request_id)->where('status', '=', 0)->delete();
                             // Send Notification
                             $walker = Walker::find($walker_id);
                             $walker_data = array();
                             $walker_data['first_name'] = $walker->first_name;
                             $walker_data['last_name'] = $walker->last_name;
                             $walker_data['phone'] = $walker->phone;
                             $walker_data['bio'] = $walker->bio;
                             $walker_data['picture'] = $walker->picture;
                             $walker_data['latitude'] = $walker->latitude;
                             $walker_data['longitude'] = $walker->longitude;
                             $walker_data['type'] = $walker->type;
                             $walker_data['rating'] = $walker->rate;
                             $walker_data['num_rating'] = $walker->rate_count;
                             $walker_data['car_model'] = $walker->car_model;
                             $walker_data['car_number'] = $walker->car_number;
                             /* $walker_data['rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->avg('rating') ? : 0;
                                $walker_data['num_rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->count(); */
                             $settings = Settings::where('key', 'default_distance_unit')->first();
                             $unit = $settings->value;
                             if ($unit == 0) {
                                 $unit_set = 'kms';
                             } elseif ($unit == 1) {
                                 $unit_set = 'miles';
                             }
                             $bill = array();
                             if ($request->is_completed == 1) {
                                 $bill['distance'] = (string) convert($request->distance, $unit);
                                 $bill['unit'] = $unit_set;
                                 $bill['time'] = $request->time;
                                 $bill['base_price'] = $request->base_price;
                                 $bill['distance_cost'] = $request->distance_cost;
                                 $bill['time_cost'] = $request->time_cost;
                                 $bill['total'] = $request->total;
                                 $bill['is_paid'] = $request->is_paid;
                             }
                             /* $setting = Settings::where('key', 'allow_calendar')->first();
                             
                                                               if ($request->later == 1 && $setting->value == 1) { */
                             if ($request->later == 1) {
                                 $date_time = $request->request_start_time;
                                 $datewant = new DateTime($date_time);
                                 $datetime = $datewant->format('Y-m-d H:i:s');
                                 $end_time = $datewant->add(new DateInterval('P0Y0M0DT2H0M0S'))->format('Y-m-d H:i:s');
                                 $provavail = ProviderAvail::where('provider_id', $walker_id)->where('start', '<=', $datetime)->where('end', '>=', $end_time)->first();
                                 $starttime = $provavail->start;
                                 $endtime = $provavail->end;
                                 $provavail->delete();
                                 if ($starttime == $datetime) {
                                     $provavail1 = new ProviderAvail();
                                     $provavail1->provider_id = $walker_id;
                                     $provavail1->start = $end_time;
                                     $provavail1->end = $endtime;
                                     $provavail1->save();
                                 } elseif ($endtime == $end_time) {
                                     $provavail1 = new ProviderAvail();
                                     $provavail1->provider_id = $walker_id;
                                     $provavail1->start = $starttime;
                                     $provavail1->end = $datetime;
                                     $provavail1->save();
                                 } else {
                                     $provavail1 = new ProviderAvail();
                                     $provavail1->provider_id = $walker_id;
                                     $provavail1->start = $starttime;
                                     $provavail1->end = $datetime;
                                     $provavail1->save();
                                     $provavail2 = new ProviderAvail();
                                     $provavail2->provider_id = $walker_id;
                                     $provavail2->start = $end_time;
                                     $provavail2->end = $endtime;
                                     $provavail2->save();
                                 }
                             }
                             $response_array = array('success' => true, 'request_id' => $request_id, 'status' => $request->status, 'confirmed_walker' => $request->confirmed_walker, 'is_walker_started' => $request->is_walker_started, 'is_walker_arrived' => $request->is_walker_arrived, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_walker_rated' => $request->is_walker_rated, 'walker' => $walker_data, 'bill' => $bill);
                             /* $driver = Keywords::where('id', 1)->first(); */
                             /* $trip = Keywords::where('id', 4)->first(); */
                             /* $title = '' . $driver->keyword . ' has accepted the ' . $trip->keyword; */
                             $title = '' . Config::get('app.generic_keywords.Provider') . ' has accepted the ' . Config::get('app.generic_keywords.Trip');
                             $message = $response_array;
                             send_notifications($request->owner_id, "owner", $title, $message);
                             // Send SMS
                             $owner = Owner::find($request->owner_id);
                             $settings = Settings::where('key', 'sms_when_provider_accepts')->first();
                             $pattern = $settings->value;
                             $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern);
                             $pattern = str_replace('%driver%', $walker->first_name . " " . $walker->last_name, $pattern);
                             $pattern = str_replace('%driver_mobile%', $walker->phone, $pattern);
                             sms_notification($request->owner_id, 'owner', $pattern);
                             // Send SMS
                             $owner = Owner::find($request->owner_id);
                             $src_address = get_address($owner->latitude, $owner->longitude);
                             $pattern = Config::get('app.generic_keywords.User') . " Pickup Address : " . $src_address;
                             sms_notification($walker_id, 'walker', $pattern);
                             // Send SMS
                             $settings = Settings::where('key', 'sms_request_completed')->first();
                             $pattern = $settings->value;
                             $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern);
                             $pattern = str_replace('%id%', $request->id, $pattern);
                             $pattern = str_replace('%user_mobile%', $owner->phone, $pattern);
                             sms_notification(1, 'admin', $pattern);
                             //email to client for accept the request
                             $settings = Settings::where('key', 'admin_email_address')->first();
                             $admin_email = $settings->value;
                             $pattern = array('admin_eamil' => $admin_email, 'client_name' => ucwords($owner->first_name . " " . $owner->last_name), 'web_url' => web_url(), 'driver_name' => ucwords($walker->first_name . " " . $walker->last_name), 'driver_contact' => $walker->phone, 'driver_car_model' => $walker->car_model, 'driver_licence' => $walker->car_number);
                             $subject = "Get Ready For Ride";
                             email_notification($owner->id, 'owner', $pattern, $subject, 'accept_request', null);
                         } else {
                             $time = date("Y-m-d H:i:s");
                             $query = "SELECT id,owner_id,current_walker,TIMESTAMPDIFF(SECOND,request_start_time, '{$time}') as diff from request where id = '{$request_id}'";
                             $results = DB::select(DB::raw($query));
                             $settings = Settings::where('key', 'provider_timeout')->first();
                             $timeout = $settings->value;
                             // Archiving Old Walker
                             RequestMeta::where('request_id', '=', $request_id)->where('walker_id', '=', $walker_id)->update(array('status' => 3));
                             $request_meta = RequestMeta::where('request_id', '=', $request_id)->where('status', '=', 0)->orderBy('created_at')->first();
                             // update request
                             if (isset($request_meta->walker_id)) {
                                 Requests::where('id', '=', $request_id)->update(array('current_walker' => $request_meta->walker_id, 'request_start_time' => date("Y-m-d H:i:s")));
                                 // Send Notification
                                 $walker = Walker::find($request_meta->walker_id);
                                 $settings = Settings::where('key', 'provider_timeout')->first();
                                 $time_left = $settings->value;
                                 $owner = Owner::find($request->owner_id);
                                 $msg_array = array();
                                 $msg_array['unique_id'] = 1;
                                 $msg_array['request_id'] = $request->id;
                                 $msg_array['id'] = $request_meta->walker_id;
                                 if ($walker) {
                                     $msg_array['token'] = $walker->token;
                                 }
                                 $msg_array['time_left_to_respond'] = $time_left;
                                 $msg_array['payment_mode'] = $request->payment_mode;
                                 $msg_array['payment_type'] = $request->payment_mode;
                                 $msg_array['time_left_to_respond'] = $timeout;
                                 $msg_array['client_profile'] = array();
                                 $msg_array['client_profile']['name'] = $owner->first_name . " " . $owner->last_name;
                                 $msg_array['client_profile']['picture'] = $owner->picture;
                                 $msg_array['client_profile']['bio'] = $owner->bio;
                                 $msg_array['client_profile']['address'] = $owner->address;
                                 $msg_array['client_profile']['phone'] = $owner->phone;
                                 $request_data = array();
                                 $request_data['owner'] = array();
                                 $request_data['owner']['name'] = $owner->first_name . " " . $owner->last_name;
                                 $request_data['owner']['picture'] = $owner->picture;
                                 $request_data['owner']['phone'] = $owner->phone;
                                 $request_data['owner']['address'] = $owner->address;
                                 $request_data['owner']['latitude'] = $owner->latitude;
                                 $request_data['owner']['longitude'] = $owner->longitude;
                                 if ($request->d_latitude != NULL) {
                                     $request_data['owner']['d_latitude'] = $request->D_latitude;
                                     $request_data['owner']['d_longitude'] = $request->D_longitude;
                                 }
                                 $request_data['owner']['owner_dist_lat'] = $request->D_latitude;
                                 $request_data['owner']['owner_dist_long'] = $request->D_longitude;
                                 $request_data['owner']['dest_latitude'] = $request->D_latitude;
                                 $request_data['owner']['dest_longitude'] = $request->D_longitude;
                                 $request_data['owner']['payment_type'] = $request->payment_mode;
                                 $request_data['owner']['rating'] = $owner->rate;
                                 $request_data['owner']['num_rating'] = $owner->rate_count;
                                 /* $request_data['owner']['rating'] = DB::table('review_dog')->where('owner_id', '=', $owner->id)->avg('rating') ? : 0;
                                    $request_data['owner']['num_rating'] = DB::table('review_dog')->where('owner_id', '=', $owner->id)->count(); */
                                 $request_data['dog'] = array();
                                 if ($dog = Dog::find($owner->dog_id)) {
                                     $request_data['dog']['name'] = $dog->name;
                                     $request_data['dog']['age'] = $dog->age;
                                     $request_data['dog']['breed'] = $dog->breed;
                                     $request_data['dog']['likes'] = $dog->likes;
                                     $request_data['dog']['picture'] = $dog->image_url;
                                 }
                                 $msg_array['request_data'] = $request_data;
                                 $title = "New Request";
                                 $message = $msg_array;
                                 send_notifications($request_meta->walker_id, "walker", $title, $message);
                             } else {
                                 // request ended
                                 Requests::where('id', '=', $request_id)->update(array('current_walker' => 0, 'status' => 1));
                                 /* $driver = Keywords::where('id', 1)->first(); */
                                 $owne = Owner::where('id', $request->owner_id)->first();
                                 /* $driver_keyword = $driver->keyword; */
                                 $driver_keyword = Config::get('app.generic_keywords.Provider');
                                 $owner_data_id = $owne->id;
                                 send_notifications($owner_data_id, "owner", 'No ' . $driver_keyword . ' Found', 'No ' . $driver_keyword . ' are available right now in your area. Kindly try after sometime.');
                             }
                         }
                         $response_array = array('success' => true);
                         $response_code = 200;
                     } else {
                         /* $response_array = array('success' => false, 'error' => 'Request ID does not matches' . $driver->keyword . ' ID', 'error_code' => 472); */
                         $response_array = array('success' => false, 'error' => 'Request ID does not matches' . Config::get('app.generic_keywords.Provider') . ' ID', 'error_code' => 472);
                         $response_code = 200;
                     }
                 } else {
                     $response_array = array('success' => false, 'error' => 'Request ID Not Found', 'error_code' => 405);
                     $response_code = 200;
                 }
             } else {
                 $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
                 $response_code = 200;
             }
         } else {
             if ($is_admin) {
                 /* $response_array = array('success' => false, 'error' => '' . $driver->keyword . ' ID not Found', 'error_code' => 410); */
                 $response_array = array('success' => false, 'error' => '' . Config::get('app.generic_keywords.Provider') . ' ID not Found', 'error_code' => 410);
             } else {
                 $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406);
             }
             $response_code = 200;
         }
     }
     $response = Response::json($response_array, $response_code);
     return $response;
 }