public function userTripDetail()
 {
     $id = Request::segment(3);
     $owner_id = Session::get('user_id');
     $request = Requests::find($id);
     if ($request->owner_id == $owner_id) {
         $locations = WalkLocation::where('request_id', $id)->orderBy('id')->get();
         $start = WalkLocation::where('request_id', $id)->orderBy('id')->first();
         $end = WalkLocation::where('request_id', $id)->orderBy('id', 'desc')->first();
         $map = "https://maps-api-ssl.google.com/maps/api/staticmap?size=249x249&style=feature:landscape|visibility:off&style=feature:poi|visibility:off&style=feature:transit|visibility:off&style=feature:road.highway|element:geometry|lightness:39&style=feature:road.local|element:geometry|gamma:1.45&style=feature:road|element:labels|gamma:1.22&style=feature:administrative|visibility:off&style=feature:administrative.locality|visibility:on&style=feature:landscape.natural|visibility:on&scale=2&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-start@2x.png|{$start->latitude},{$start->longitude}&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-finish@2x.png|{$end->latitude},{$end->longitude}&path=color:0x2dbae4ff|weight:4";
         foreach ($locations as $location) {
             $map .= "|{$location->latitude},{$location->longitude}";
         }
         $start_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$start->latitude},{$start->longitude}"), TRUE);
         $start_address = $start_location['results'][0]['formatted_address'];
         $end_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$end->latitude},{$end->longitude}"), TRUE);
         $end_address = $end_location['results'][0]['formatted_address'];
         $walker = Walker::find($request->confirmed_walker);
         $walker_review = WalkerReview::where('request_id', $id)->first();
         if ($walker_review) {
             $rating = round($walker_review->rating);
         } else {
             $rating = 0;
         }
         return View::make('web.userTripDetail')->with('title', 'My Trips')->with('request', $request)->with('start_address', $start_address)->with('end_address', $end_address)->with('start', $start)->with('end', $end)->with('map_url', $map)->with('walker', $walker)->with('rating', $rating);
     } else {
         echo "false";
     }
 }
 public function get_completed_requests()
 {
     $token = Input::get('token');
     $owner_id = Input::get('id');
     $from = Input::get('from_date');
     // 2015-03-11 07:45:01
     $to_date = Input::get('to_date') . ' 23:59:59';
     //2015-03-11 07:45:01
     $validator = Validator::make(array('token' => $token, 'owner_id' => $owner_id), array('token' => 'required', 'owner_id' => 'required|integer'));
     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 ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) {
             // check for token validity
             if (is_token_active($owner_data->token_expiry) || $is_admin) {
                 // Do necessary operations
                 if ($from != "" && $to_date != "") {
                     $request_data = DB::table('request')->where('request.owner_id', $owner_id)->where('is_completed', 1)->where('is_cancelled', 0)->whereBetween('request_start_time', array($from, $to_date))->leftJoin('walker', 'request.confirmed_walker', '=', 'walker.id')->leftJoin('walker_services', 'walker.id', '=', 'walker_services.provider_id')->leftJoin('walker_type', 'walker_type.id', '=', 'walker_services.type')->leftJoin('request_services', 'request_services.request_id', '=', 'request.id')->select('request.*', 'request.request_start_time', 'request.promo_code', 'walker.first_name', 'walker.id as walker_id', 'walker.last_name', 'walker.phone', 'walker.email', 'walker.picture', 'walker.bio', 'walker.rate', 'walker_type.name as type', 'walker_type.icon', 'request.distance', 'request.time', 'request_services.base_price as req_base_price', 'request_services.distance_cost as req_dis_cost', 'request_services.time_cost as req_time_cost', 'request_services.type as req_typ', 'request.total')->get();
                 } else {
                     $request_data = DB::table('request')->where('request.owner_id', $owner_id)->where('is_completed', 1)->where('is_cancelled', 0)->leftJoin('walker', 'request.confirmed_walker', '=', 'walker.id')->leftJoin('walker_services', 'walker.id', '=', 'walker_services.provider_id')->leftJoin('walker_type', 'walker_type.id', '=', 'walker_services.type')->leftJoin('request_services', 'request_services.request_id', '=', 'request.id')->select('request.*', 'request.request_start_time', 'request.promo_code', 'walker.first_name', 'walker.id as walker_id', 'walker.last_name', 'walker.phone', 'walker.email', 'walker.picture', 'walker.bio', 'walker.rate', 'walker_type.name as type', 'walker_type.icon', 'request.distance', 'request.time', 'request_services.base_price as req_base_price', 'request_services.distance_cost as req_dis_cost', 'request_services.time_cost as req_time_cost', 'request_services.type as req_typ', 'request.total')->get();
                 }
                 $requests = array();
                 $settings = Settings::where('key', 'default_distance_unit')->first();
                 $unit = $settings->value;
                 if ($unit == 0) {
                     $unit_set = 'kms';
                 } elseif ($unit == 1) {
                     $unit_set = 'miles';
                 }
                 /* $currency_selected = Keywords::find(5); */
                 foreach ($request_data as $data) {
                     $request_typ = ProviderType::where('id', '=', $data->req_typ)->first();
                     /* $setbase_price = Settings::where('key', 'base_price')->first();
                        $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first();
                        $settime_price = Settings::where('key', 'price_per_unit_time')->first(); */
                     $setbase_distance = $request_typ->base_distance;
                     $setbase_price = $request_typ->base_price;
                     $setdistance_price = $request_typ->price_per_unit_distance;
                     $settime_price = $request_typ->price_per_unit_time;
                     $locations = WalkLocation::where('request_id', $data->id)->orderBy('id')->get();
                     $start = $end = $map = "";
                     $id = $data->id;
                     if (count($locations) >= 1) {
                         $start = WalkLocation::where('request_id', $id)->orderBy('id')->first();
                         $end = WalkLocation::where('request_id', $id)->orderBy('id', 'desc')->first();
                         $map = "https://maps-api-ssl.google.com/maps/api/staticmap?size=249x249&scale=2&markers=shadow:true|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-start@2x.png|{$start->latitude},{$start->longitude}&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-finish@2x.png|{$end->latitude},{$end->longitude}&path=color:0x2dbae4ff|weight:4";
                         foreach ($locations as $location) {
                             $map .= "|{$location->latitude},{$location->longitude}";
                         }
                         /* $map.="&key=" . Config::get('app.gcm_browser_key'); */
                     }
                     $request['start_lat'] = "";
                     if (isset($start->latitude)) {
                         $request['start_lat'] = $start->latitude;
                     }
                     $request['start_long'] = "";
                     if (isset($start->longitude)) {
                         $request['start_long'] = $start->longitude;
                     }
                     $request['end_lat'] = "";
                     if (isset($end->latitude)) {
                         $request['end_lat'] = $end->latitude;
                     }
                     $request['end_long'] = "";
                     if (isset($end->longitude)) {
                         $request['end_long'] = $end->longitude;
                     }
                     $request['map_url'] = $map;
                     $walker = Walker::where('id', $data->walker_id)->first();
                     if ($walker != NULL) {
                         $user_timezone = $walker->timezone;
                     } else {
                         $user_timezone = 'UTC';
                     }
                     $default_timezone = Config::get('app.timezone');
                     $date_time = get_user_time($default_timezone, $user_timezone, $data->request_start_time);
                     $dist = number_format($data->distance, 2, '.', '');
                     $request['id'] = $data->id;
                     $request['date'] = $date_time;
                     $request['distance'] = (string) $dist;
                     $request['unit'] = $unit_set;
                     $request['time'] = $data->time;
                     $discount = 0;
                     if ($data->promo_code != "") {
                         if ($data->promo_code != "") {
                             $promo_code = PromoCodes::where('id', $data->promo_code)->first();
                             if ($promo_code) {
                                 $promo_value = $promo_code->value;
                                 $promo_type = $promo_code->type;
                                 if ($promo_type == 1) {
                                     // Percent Discount
                                     $discount = $data->total * $promo_value / 100;
                                 } elseif ($promo_type == 2) {
                                     // Absolute Discount
                                     $discount = $promo_value;
                                 }
                             }
                         }
                     }
                     $request['promo_discount'] = currency_converted($discount);
                     $is_multiple_service = Settings::where('key', 'allow_multiple_service')->first();
                     if ($is_multiple_service->value == 0) {
                         if ($data->req_base_price) {
                             $request['base_price'] = currency_converted($data->req_base_price);
                         } else {
                             /* $request['base_price'] = currency_converted($setbase_price->value); */
                             $request['base_price'] = currency_converted($setbase_price);
                         }
                         if ($data->req_dis_cost) {
                             $request['distance_cost'] = currency_converted($data->req_dis_cost);
                         } else {
                             /* $request['distance_cost'] = currency_converted($setdistance_price->value * $data->distance); */
                             if ($data->distance <= $setbase_distance) {
                                 $request['distance_cost'] = 0;
                             } else {
                                 $request['distance_cost'] = currency_converted($setdistance_price->value * ($data->distance - $setbase_distance));
                             }
                         }
                         if ($data->req_time_cost) {
                             $request['time_cost'] = currency_converted($data->req_time_cost);
                         } else {
                             /* $request['time_cost'] = currency_converted($settime_price->value * $data->time); */
                             $request['time_cost'] = currency_converted($settime_price * $data->time);
                         }
                         $request['setbase_distance'] = $setbase_distance;
                         $request['total'] = currency_converted($data->total);
                         $request['actual_total'] = currency_converted($data->total + $data->ledger_payment + $discount);
                         $request['type'] = $data->type;
                         $request['type_icon'] = $data->icon;
                         $rserv = RequestServices::where('request_id', $data->id)->get();
                         $typs = array();
                         $typi = array();
                         $typp = array();
                         $total_price = 0;
                         foreach ($rserv as $typ) {
                             $typ1 = ProviderType::where('id', $typ->type)->first();
                             $typ_price = ProviderServices::where('provider_id', $data->confirmed_walker)->where('type', $typ->type)->first();
                             if ($typ_price->base_price > 0) {
                                 $typp1 = 0.0;
                                 $typp1 = $typ_price->base_price;
                             } elseif ($typ_price->price_per_unit_distance > 0) {
                                 $typp1 = 0.0;
                                 foreach ($rserv as $key) {
                                     $typp1 = $typp1 + $key->distance_cost;
                                 }
                             } else {
                                 $typp1 = 0.0;
                             }
                             $typs['name'] = $typ1->name;
                             $typs['price'] = currency_converted($typp1);
                             $total_price = $total_price + $typp1;
                             array_push($typi, $typs);
                         }
                         $request['type'] = $typi;
                     } else {
                         $rserv = RequestServices::where('request_id', $data->id)->get();
                         $typs = array();
                         $typi = array();
                         $typp = array();
                         $total_price = 0;
                         foreach ($rserv as $typ) {
                             $typ1 = ProviderType::where('id', $typ->type)->first();
                             $typ_price = ProviderServices::where('provider_id', $data->confirmed_walker)->where('type', $typ->type)->first();
                             if ($typ_price->base_price > 0) {
                                 $typp1 = 0.0;
                                 $typp1 = $typ_price->base_price;
                             } elseif ($typ_price->price_per_unit_distance > 0) {
                                 $typp1 = 0.0;
                                 foreach ($rserv as $key) {
                                     $typp1 = $typp1 + $key->distance_cost;
                                 }
                             } else {
                                 $typp1 = 0.0;
                             }
                             $typs['name'] = $typ1->name;
                             $typs['price'] = currency_converted($typp1);
                             $total_price = $total_price + $typp1;
                             array_push($typi, $typs);
                         }
                         $request['type'] = $typi;
                         $base_price = 0;
                         $distance_cost = 0;
                         $time_cost = 0;
                         foreach ($rserv as $key) {
                             $base_price = $base_price + $key->base_price;
                             $distance_cost = $distance_cost + $key->distance_cost;
                             $time_cost = $time_cost + $key->time_cost;
                         }
                         $request['base_price'] = currency_converted($base_price);
                         $request['distance_cost'] = currency_converted($distance_cost);
                         $request['time_cost'] = currency_converted($time_cost);
                         $request['total'] = currency_converted($total_price);
                     }
                     $rate = WalkerReview::where('request_id', $data->id)->where('walker_id', $data->confirmed_walker)->first();
                     if ($rate != NULL) {
                         $request['walker']['rating'] = $rate->rating;
                     } else {
                         $request['walker']['rating'] = '0.0';
                     }
                     /* $request['currency'] = $currency_selected->keyword; */
                     $request['base_price'] = currency_converted($data->req_base_price);
                     $request['distance_cost'] = currency_converted($data->req_dis_cost);
                     $request['time_cost'] = currency_converted($data->req_time_cost);
                     $request['total'] = currency_converted($data->total - $data->ledger_payment - $data->promo_payment);
                     $request['main_total'] = currency_converted($data->total);
                     $request['referral_bonus'] = currency_converted($data->ledger_payment);
                     $request['promo_bonus'] = currency_converted($data->promo_payment);
                     $request['payment_type'] = $data->payment_mode;
                     $request['is_paid'] = $data->is_paid;
                     $request['promo_id'] = $data->promo_id;
                     $request['promo_code'] = $data->promo_code;
                     $request['currency'] = Config::get('app.generic_keywords.Currency');
                     $request['walker']['first_name'] = $data->first_name;
                     $request['walker']['last_name'] = $data->last_name;
                     $request['walker']['phone'] = $data->phone;
                     $request['walker']['email'] = $data->email;
                     $request['walker']['picture'] = $data->picture;
                     $request['walker']['bio'] = $data->bio;
                     $request['walker']['type'] = $data->type;
                     /* $request['walker']['rating'] = $data->rate; */
                     array_push($requests, $request);
                 }
                 $response_array = array('success' => true, 'requests' => $requests);
                 $response_code = 200;
             } else {
                 $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
                 $response_code = 200;
             }
         } else {
             if ($is_admin) {
                 /* $var = Keywords::where('id', 2)->first();
                    $response_array = array('success' => false, 'error' => '' . $var->keyword . ' ID not Found', 'error_code' => 410); */
                 $response_array = array('success' => false, 'error' => '' . Config::get('app.generic_keywords.User') . ' 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;
 }
 public function delete_review()
 {
     $id = Request::segment(4);
     $walker = WalkerReview::where('id', $id)->delete();
     return Redirect::to("/admin/reviews");
 }
 public function request_walk_completed_old123()
 {
     if (Request::isMethod('post')) {
         $request_id = Input::get('request_id');
         $token = Input::get('token');
         $walker_id = Input::get('id');
         $latitude = Input::get('latitude');
         $longitude = Input::get('longitude');
         $distance = Input::get('distance');
         $time = Input::get('time');
         if (Input::has('bearing')) {
             $angle = Input::get('bearing');
         }
         Log::info('distance input = ' . print_r($distance, true));
         Log::info('time input = ' . print_r($time, true));
         $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id, 'latitude' => $latitude, 'longitude' => $longitude, 'distance' => $distance), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer', 'latitude' => 'required', 'longitude' => 'required', 'distance' => 'required'));
         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 {
             $cash_card_user = "";
             $payment_type = "";
             $walker_payment_remaining = 0;
             $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) {
                     $providertype = ProviderType::where('id', $walker_data->type)->first();
                     // Do necessary operations
                     if ($request = Requests::find($request_id)) {
                         $time = $request->time;
                         if ($request->confirmed_walker == $walker_id) {
                             if ($request->is_started == 1) {
                                 $settings = Settings::where('key', 'default_charging_method_for_users')->first();
                                 $pricing_type = $settings->value;
                                 $settings = Settings::where('key', 'default_distance_unit')->first();
                                 $unit = $settings->value;
                                 Log::info('distance = ' . print_r($distance, true));
                                 $reqserv = RequestServices::where('request_id', $request_id)->get();
                                 $actual_total = 0;
                                 $price_per_unit_distance = 0;
                                 $price_per_unit_time = 0;
                                 $base_price = 0;
                                 foreach ($reqserv as $rse) {
                                     Log::info('type = ' . print_r($rse->type, true));
                                     $protype = ProviderType::where('id', $rse->type)->first();
                                     $pt = ProviderServices::where('provider_id', $walker_id)->where('type', $rse->type)->first();
                                     if ($pt->base_price == 0) {
                                         /* $setbase_price = Settings::where('key', 'base_price')->first();
                                            $base_price = $setbase_price->value; */
                                         $base_price = $providertype->base_price;
                                         $rse->base_price = $base_price;
                                     } else {
                                         $base_price = $pt->base_price;
                                         $rse->base_price = $base_price;
                                     }
                                     $is_multiple_service = Settings::where('key', 'allow_multiple_service')->first();
                                     if ($is_multiple_service->value == 0) {
                                         if ($pt->price_per_unit_distance == 0) {
                                             /* $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first();
                                                $price_per_unit_distance = $setdistance_price->value * $distance;
                                                $rse->distance_cost = $price_per_unit_distance; */
                                             if ($distance <= $providertype->base_distance) {
                                                 $price_per_unit_distance = 0;
                                             } else {
                                                 $price_per_unit_distance = $providertype->price_per_unit_distance * ($distance - $providertype->base_distance);
                                             }
                                             $rse->distance_cost = $price_per_unit_distance;
                                         } else {
                                             if ($distance <= $providertype->base_distance) {
                                                 $price_per_unit_distance = 0;
                                             } else {
                                                 $price_per_unit_distance = $pt->price_per_unit_distance * ($distance - $providertype->base_distance);
                                             }
                                             $rse->distance_cost = $price_per_unit_distance;
                                         }
                                         if ($pt->price_per_unit_time == 0) {
                                             /* $settime_price = Settings::where('key', 'price_per_unit_time')->first();
                                                $price_per_unit_time = $settime_price->value * $time; */
                                             $price_per_unit_time = $providertype->price_per_unit_time * $time;
                                             $rse->time_cost = $price_per_unit_time;
                                         } else {
                                             $price_per_unit_time = $pt->price_per_unit_time * $time;
                                             $rse->time_cost = $price_per_unit_time;
                                         }
                                     }
                                     Log::info('total price = ' . print_r($base_price + $price_per_unit_distance + $price_per_unit_time, true));
                                     $rse->total = $base_price + $price_per_unit_distance + $price_per_unit_time;
                                     $rse->save();
                                     $actual_total = $actual_total + $base_price + $price_per_unit_distance + $price_per_unit_time;
                                     Log::info('total_price = ' . print_r($actual_total, true));
                                 }
                                 pay_fail:
                                 $rs = RequestServices::where('request_id', $request_id)->get();
                                 $total = 0;
                                 foreach ($rs as $key) {
                                     Log::info('total = ' . print_r($key->total, true));
                                     $total = $total + $key->total;
                                 }
                                 $request = Requests::find($request_id);
                                 $request->is_completed = 1;
                                 $request->distance = $distance;
                                 $request->time = $time;
                                 $request->security_key = NULL;
                                 $request->total = $total;
                                 $owner_data = Owner::where('id', $request->owner_id)->first();
                                 /* GET REFERRAL & PROMO INFO */
                                 $prom_act = $prom_for_card = $prom_for_cash = $ref_act = $ref_for_card = $ref_for_cash = $ref_total = $promo_total = 0;
                                 $settings = Settings::where('key', 'promotional_code_activation')->first();
                                 $prom_act = $settings->value;
                                 $settings = Settings::where('key', 'referral_code_activation')->first();
                                 $ref_act = $settings->value;
                                 /* GET REFERRAL & PROMO INFO END */
                                 $cash_card_user = $request->payment_mode;
                                 if ($request->payment_mode == 0) {
                                     $walker_payment_remaining = $total;
                                     if ($prom_act) {
                                         $settings = Settings::where('key', 'get_promotional_profit_on_card_payment')->first();
                                         $prom_for_card = $settings->value;
                                         if ($prom_for_card) {
                                             if ($total > 0) {
                                                 if ($pcode = PromoCodes::where('id', $request->promo_id)->first()) {
                                                     if ($pcode->type == 1) {
                                                         $promo_total = $total * ($pcode->value / 100);
                                                         $total = $total - $promo_total;
                                                         if ($total <= 0) {
                                                             $total = 0;
                                                         }
                                                     } else {
                                                         $promo_total = $pcode->value;
                                                         $total = $total - $promo_total;
                                                         if ($total <= 0) {
                                                             $total = 0;
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                     if ($ref_act) {
                                         $settings = Settings::where('key', 'get_referral_profit_on_card_payment')->first();
                                         $ref_for_card = $settings->value;
                                         if ($ref_for_card) {
                                             // charge client
                                             $ledger = Ledger::where('owner_id', $request->owner_id)->first();
                                             if ($ledger) {
                                                 $balance = $ledger->amount_earned - $ledger->amount_spent;
                                                 Log::info('ledger balance = ' . print_r($balance, true));
                                                 if ($balance > 0) {
                                                     if ($total > 0) {
                                                         if ($total > $balance) {
                                                             $ref_total = $balance;
                                                             $ledger_temp = Ledger::find($ledger->id);
                                                             $ledger_temp->amount_spent = $ledger_temp->amount_spent + $balance;
                                                             $ledger_temp->save();
                                                             $total = $total - $balance;
                                                         } else {
                                                             $ref_total = $total;
                                                             $ledger_temp = Ledger::find($ledger->id);
                                                             $ledger_temp->amount_spent = $ledger_temp->amount_spent + $total;
                                                             $ledger_temp->save();
                                                             $total = 0;
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 } else {
                                     if ($request->payment_mode == 1) {
                                         $walker_payment_remaining = 0;
                                         if ($prom_act) {
                                             $settings = Settings::where('key', 'get_promotional_profit_on_cash_payment')->first();
                                             $prom_for_cash = $settings->value;
                                             if ($prom_for_cash) {
                                                 if ($total > 0) {
                                                     if ($pcode = PromoCodes::where('id', $request->promo_id)->first()) {
                                                         if ($pcode->type == 1) {
                                                             $promo_total = $total * ($pcode->value / 100);
                                                             $total = $total - $promo_total;
                                                             if ($total <= 0) {
                                                                 $total = 0;
                                                             }
                                                         } else {
                                                             $promo_total = $pcode->value;
                                                             $total = $total - $promo_total;
                                                             if ($total <= 0) {
                                                                 $total = 0;
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         if ($ref_act) {
                                             $settings = Settings::where('key', 'get_referral_profit_on_cash_payment')->first();
                                             $ref_for_cash = $settings->value;
                                             if ($ref_for_cash) {
                                                 // charge client
                                                 $ledger = Ledger::where('owner_id', $request->owner_id)->first();
                                                 if ($ledger) {
                                                     $balance = $ledger->amount_earned - $ledger->amount_spent;
                                                     Log::info('ledger balance = ' . print_r($balance, true));
                                                     if ($balance > 0) {
                                                         if ($total > 0) {
                                                             if ($total > $balance) {
                                                                 $ref_total = $balance;
                                                                 $ledger_temp = Ledger::find($ledger->id);
                                                                 $ledger_temp->amount_spent = $ledger_temp->amount_spent + $balance;
                                                                 $ledger_temp->save();
                                                                 $total = $total - $balance;
                                                             } else {
                                                                 $ref_total = $total;
                                                                 $ledger_temp = Ledger::find($ledger->id);
                                                                 $ledger_temp->amount_spent = $ledger_temp->amount_spent + $total;
                                                                 $ledger_temp->save();
                                                                 $total = 0;
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 /* $promo_discount = 0;
                                                                       if ($pcode = PromoCodes::where('id', $request->promo_code)->where('type', 1)->first()) {
                                                                       $discount = ($pcode->value) / 100;
                                                                       $promo_discount = $total * $discount;
                                                                       $total = $total - $promo_discount;
                                                                       if ($total < 0) {
                                                                       $total = 0;
                                                                       }
                                                                       }
                                 
                                                                       $request->total = $total; */
                                 Log::info('final total = ' . print_r($total, true));
                                 $cod_sett = Settings::where('key', 'cod')->first();
                                 $allow_cod = $cod_sett->value;
                                 if ($request->payment_mode == 1 and $allow_cod == 1) {
                                     $request->is_paid = 1;
                                     $payment_type = 'Payment By cash';
                                     Log::info('allow_cod');
                                 } else {
                                     if ($request->payment_mode == 2) {
                                         // paypal
                                         $payment_type = 'Payment By paypal payment';
                                         Log::info('paypal payment');
                                     } else {
                                         Log::info('normal payment. Stored cards');
                                         if ($total == 0) {
                                             $request->is_paid = 1;
                                         } else {
                                             $payment_data = Payment::where('owner_id', $request->owner_id)->where('is_default', 1)->first();
                                             if (!$payment_data) {
                                                 $payment_data = Payment::where('owner_id', $request->owner_id)->first();
                                             }
                                             if ($payment_data) {
                                                 $customer_id = $payment_data->customer_id;
                                                 $setransfer = Settings::where('key', 'transfer')->first();
                                                 $transfer_allow = $setransfer->value;
                                                 if (Config::get('app.default_payment') == 'stripe') {
                                                     //dd($customer_id);
                                                     Stripe::setApiKey(Config::get('app.stripe_secret_key'));
                                                     try {
                                                         $charge = Stripe_Charge::create(array("amount" => floor($total) * 100, "currency" => "usd", "customer" => $customer_id));
                                                         if ($charge->paid) {
                                                             $request->is_paid = 1;
                                                             //$payment_type = "Creditcard Card Payment Successfully";
                                                         } else {
                                                             $request->is_paid = 0;
                                                             $payment_type = "Creditcard Card Payment Fail";
                                                             $ledger = Ledger::where('owner_id', $request->owner_id)->first();
                                                             if ($ledger) {
                                                                 $ledger_temp = Ledger::find($ledger->id);
                                                                 $ledger_temp->amount_spent = $ledger_temp->amount_spent + $ref_total;
                                                                 $ledger_temp->save();
                                                             }
                                                             $change_to_cash = Requests::find($request_id);
                                                             $change_to_cash->payment_mode = 1;
                                                             $change_to_cash->save();
                                                             /* Client Side Push */
                                                             $title = 'Your card is declined, please pay cash to driver for your trip.';
                                                             $response_array = array('success' => true, 'message' => $title);
                                                             $message = $response_array;
                                                             send_notifications($request->owner_id, "owner", $title, $message);
                                                             /* Client Side Push END */
                                                             /* Driver Side Push */
                                                             $title = 'Please collect cash from client for your trip.';
                                                             $response_array = array('success' => true, 'message' => $title);
                                                             $message = $response_array;
                                                             send_notifications($walker_id, "walker", $title, $message);
                                                             /* Driver Side Push END */
                                                             goto pay_fail;
                                                         }
                                                     } catch (Stripe_InvalidRequestError $e) {
                                                         $request->is_paid = 0;
                                                         // Invalid parameters were supplied to Stripe's API
                                                         $ownr = Owner::find($request->owner_id);
                                                         $ownr->debt = $total;
                                                         $ownr->save();
                                                         $response_array = array('error' => $e->getMessage());
                                                         $response_code = 200;
                                                         $response = Response::json($response_array, $response_code);
                                                         return $response;
                                                     }
                                                     $settng = Settings::where('key', 'service_fee')->first();
                                                     if ($transfer_allow == 1 && $walker_data->merchant_id != "" && Config::get('app.generic_keywords.Currency') == '$') {
                                                         $transfer = Stripe_Transfer::create(array("amount" => floor($total - $settng->value * $total / 100) * 100, "currency" => "usd", "recipient" => $walker_data->merchant_id));
                                                         $request->transfer_amount = floor($total - $settng->value * $total / 100);
                                                     }
                                                 } else {
                                                     try {
                                                         Braintree_Configuration::environment(Config::get('app.braintree_environment'));
                                                         Braintree_Configuration::merchantId(Config::get('app.braintree_merchant_id'));
                                                         Braintree_Configuration::publicKey(Config::get('app.braintree_public_key'));
                                                         Braintree_Configuration::privateKey(Config::get('app.braintree_private_key'));
                                                         if ($transfer_allow == 1) {
                                                             $sevisett = Settings::where('key', 'service_fee')->first();
                                                             $service_fee = $sevisett->value * $total / 100;
                                                             $result = Braintree_Transaction::sale(array('amount' => $total - $service_fee, 'paymentMethodNonce' => $customer_id, 'merchantAccountId' => $walker_data->merchant_id, 'options' => array('submitForSettlement' => true, 'holdInEscrow' => true), 'serviceFeeAmount' => $service_fee));
                                                         } else {
                                                             $result = Braintree_Transaction::sale(array('amount' => $total, 'paymentMethodNonce' => $customer_id));
                                                         }
                                                         if ($result->success) {
                                                             $request->is_paid = 1;
                                                             $payment_type = "Creditcard Card Payment Successfully";
                                                         } else {
                                                             $request->is_paid = 0;
                                                             $payment_type = "Creditcard Card Payment Fail";
                                                             $ledger = Ledger::where('owner_id', $request->owner_id)->first();
                                                             if ($ledger) {
                                                                 $ledger_temp = Ledger::find($ledger->id);
                                                                 $ledger_temp->amount_spent = $ledger_temp->amount_spent + $ref_total;
                                                                 $ledger_temp->save();
                                                             }
                                                             $change_to_cash = Requests::find($request_id);
                                                             $change_to_cash->payment_mode = 1;
                                                             $change_to_cash->save();
                                                             /* Client Side Push */
                                                             $title = 'Your card is declined, please pay cash to driver for your trip.';
                                                             $response_array = array('success' => true, 'message' => $title);
                                                             $message = $response_array;
                                                             send_notifications($request->owner_id, "owner", $title, $message);
                                                             /* Client Side Push END */
                                                             /* Driver Side Push */
                                                             $title = 'Please collect cash from client for your trip.';
                                                             $response_array = array('success' => true, 'message' => $title);
                                                             $message = $response_array;
                                                             send_notifications($walker_id, "walker", $title, $message);
                                                             /* Driver Side Push END */
                                                             goto pay_fail;
                                                         }
                                                     } catch (Exception $e) {
                                                         $response_array = array('success' => false, 'error' => $e, 'error_code' => 405);
                                                         $response_code = 200;
                                                         $response = Response::json($response_array, $response_code);
                                                         return $response;
                                                     }
                                                 }
                                                 $request->card_payment = $total;
                                                 $request->ledger_payment = $request->total - $total;
                                             }
                                         }
                                     }
                                 }
                                 $request->card_payment = $total;
                                 $request->ledger_payment = $ref_total;
                                 $request->promo_payment = $promo_total;
                                 //$request->payment_mode = $cash_card_user;
                                 $request->save();
                                 if ($request->is_paid == 1) {
                                     $owner = Owner::find($request->owner_id);
                                     $settings = Settings::where('key', 'sms_request_unanswered')->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);
                                 }
                                 $walker = Walker::find($walker_id);
                                 $walker->is_available = 1;
                                 $location = get_location($latitude, $longitude);
                                 $latitude = $location['lat'];
                                 $longitude = $location['long'];
                                 if (!isset($angle)) {
                                     $angle = get_angle($walker->latitude, $walker->longitude, $latitude, $longitude);
                                 }
                                 $walker->old_latitude = $walker->latitude;
                                 $walker->old_longitude = $walker->longitude;
                                 $walker->latitude = $latitude;
                                 $walker->longitude = $longitude;
                                 $walker->bearing = $angle;
                                 $walker->save();
                                 Log::info('distance walk location = ' . print_r($distance, true));
                                 $walk_location = new WalkLocation();
                                 $walk_location->latitude = $latitude;
                                 $walk_location->longitude = $longitude;
                                 $walk_location->request_id = $request_id;
                                 $walk_location->distance = $distance;
                                 $walk_location->bearing = $angle;
                                 $walk_location->save();
                                 // Send Notification
                                 $walker = Walker::find($request->confirmed_walker);
                                 $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(); */
                                 $requestserv = RequestServices::where('request_id', $request->id)->first();
                                 $bill = array();
                                 /* $currency_selected = Keywords::find(5); */
                                 if ($request->is_completed == 1) {
                                     $settings = Settings::where('key', 'default_distance_unit')->first();
                                     $unit = $settings->value;
                                     //    $bill['payment_mode'] = $request->payment_mode;
                                     // $bill['payment_mode'] = "";
                                     $bill['distance'] = (string) $distance;
                                     if ($unit == 0) {
                                         $unit_set = 'kms';
                                     } elseif ($unit == 1) {
                                         $unit_set = 'miles';
                                     }
                                     $bill['unit'] = $unit_set;
                                     $bill['time'] = floatval(sprintf2($request->time, 2));
                                     if ($requestserv->base_price != 0) {
                                         $bill['base_price'] = currency_converted($requestserv->base_price);
                                         $bill['distance_cost'] = currency_converted($requestserv->distance_cost);
                                         $bill['time_cost'] = currency_converted(floatval(sprintf2($requestserv->time_cost, 2)));
                                     } else {
                                         /* $setbase_price = Settings::where('key', 'base_price')->first();
                                            $bill['base_price'] = currency_converted($setbase_price->value); */
                                         $bill['base_price'] = currency_converted($providertype->base_price);
                                         /* $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first();
                                            $bill['distance_cost'] = currency_converted($setdistance_price->value); */
                                         $bill['distance_cost'] = currency_converted($providertype->price_per_unit_distance);
                                         /* $settime_price = Settings::where('key', 'price_per_unit_time')->first();
                                            $bill['time_cost'] = currency_converted(floatval(sprintf2($settime_price->value, 2))); */
                                         $bill['time_cost'] = currency_converted(floatval(sprintf2($providertype->price_per_unit_time, 2)));
                                     }
                                     $admins = Admin::first();
                                     $bill['walker']['email'] = $walker->email;
                                     $bill['admin']['email'] = $admins->username;
                                     if ($request->transfer_amount != 0) {
                                         $bill['walker']['amount'] = currency_converted($request->total - $request->transfer_amount);
                                         $bill['admin']['amount'] = currency_converted($request->transfer_amount);
                                     } else {
                                         $bill['walker']['amount'] = currency_converted($request->transfer_amount);
                                         $bill['admin']['amount'] = currency_converted($request->total - $request->transfer_amount);
                                     }
                                     /* $bill['currency'] = $currency_selected->keyword; */
                                     $bill['currency'] = Config::get('app.generic_keywords.Currency');
                                     $bill['actual_total'] = currency_converted($actual_total);
                                     $bill['total'] = currency_converted($request->total);
                                     $bill['is_paid'] = $request->is_paid;
                                     $bill['promo_discount'] = currency_converted($promo_total);
                                     $bill['main_total'] = currency_converted($request->total);
                                     $bill['total'] = currency_converted($request->total - $request->ledger_payment - $request->promo_payment);
                                     $bill['referral_bonus'] = currency_converted($request->ledger_payment);
                                     $bill['promo_bonus'] = currency_converted($request->promo_payment);
                                     //  $bill['payment_type'] = "";
                                     //  $bill['payment_type'] = $request->"payment_mode;
                                     $bill['is_paid'] = $request->is_paid;
                                     $chagre = array();
                                     $settings = Settings::where('key', 'default_distance_unit')->first();
                                     $unit = $settings->value;
                                     if ($unit == 0) {
                                         $unit_set = 'kms';
                                     } elseif ($unit == 1) {
                                         $unit_set = 'miles';
                                     }
                                     $chagre['unit'] = $unit_set;
                                     $requestserv = RequestServices::where('request_id', $request->id)->first();
                                     if ($requestserv->base_price != 0) {
                                         $chagre['base_price'] = $requestserv->base_price;
                                         $chagre['distance_price'] = $requestserv->distance_cost;
                                         $chagre['price_per_unit_time'] = $requestserv->time_cost;
                                     } else {
                                         $setbase_price = Settings::where('key', 'base_price')->first();
                                         $chagre['base_price'] = $setbase_price->value;
                                         $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first();
                                         $chagre['distance_price'] = $setdistance_price->value;
                                         $settime_price = Settings::where('key', 'price_per_unit_time')->first();
                                         $chagre['price_per_unit_time'] = $settime_price->value;
                                     }
                                     $chagre['total'] = $request->total;
                                     $chagre['is_paid'] = $request->is_paid;
                                     //$message = $response_array;
                                 }
                                 $rservc = RequestServices::where('request_id', $request->id)->get();
                                 $typs = array();
                                 $typi = array();
                                 $typp = array();
                                 foreach ($rservc as $typ) {
                                     $typ1 = ProviderType::where('id', $typ->type)->first();
                                     $typ_price = ProviderServices::where('provider_id', $request->confirmed_walker)->where('type', $typ->type)->first();
                                     if ($typ_price->base_price > 0) {
                                         $typp1 = 0.0;
                                         $typp1 = $typ_price->base_price;
                                     } elseif ($typ_price->price_per_unit_distance > 0) {
                                         $typp1 = 0.0;
                                         foreach ($rservc as $key) {
                                             $typp1 = $typp1 + $key->distance_cost;
                                         }
                                     } else {
                                         $typp1 = 0.0;
                                     }
                                     $typs['name'] = $typ1->name;
                                     // $typs['icon']=$typ1->icon;
                                     $typs['price'] = $typp1;
                                     array_push($typi, $typs);
                                 }
                                 $bill['type'] = $typi;
                                 $rserv = RequestServices::where('request_id', $request_id)->get();
                                 $typs = array();
                                 foreach ($rserv as $typ) {
                                     $typ1 = ProviderType::where('id', $typ->type)->first();
                                     array_push($typs, $typ1->name);
                                 }
                                 $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, 'payment_mode' => "", 'bill' => $bill, 'payment_option' => "", 'is_paid' => $request->is_paid);
                                 $owner_data1 = array();
                                 $owner_data1['name'] = $owner_data->first_name . " " . $owner_data->last_name;
                                 $owner_data1['picture'] = $owner_data->picture;
                                 $owner_data1['phone'] = $owner_data->phone;
                                 $owner_data1['address'] = $owner_data->address;
                                 $owner_data1['bio'] = $owner_data->bio;
                                 $owner_data1['latitude'] = $owner_data->latitude;
                                 $owner_data1['longitude'] = $owner_data->longitude;
                                 $owner_data1['owner_dist_lat'] = $request->D_latitude;
                                 $owner_data1['owner_dist_long'] = $request->D_longitude;
                                 $owner_data1['dest_latitude'] = $request->D_latitude;
                                 $owner_data1['dest_longitude'] = $request->D_longitude;
                                 //$owner_data1['payment_type'] = $request->payment_mode;
                                 // $owner_data1['payment_type'] = "";
                                 $owner_data1['rating'] = $owner_data->rate;
                                 $owner_data1['num_rating'] = $owner_data->rate_count;
                                 $title = "Trip Completed";
                                 $dog1 = array();
                                 if ($dog = Dog::find($owner_data->dog_id)) {
                                     $dog1['name'] = $dog->name;
                                     $dog1['age'] = $dog->age;
                                     $dog1['breed'] = $dog->breed;
                                     $dog1['likes'] = $dog->likes;
                                     $dog1['picture'] = $dog->image_url;
                                 }
                                 $cards = "";
                                 /* $cards['none'] = ""; */
                                 $cardlist = Payment::where('owner_id', $owner_data->id)->where('is_default', 1)->first();
                                 if (count($cardlist) >= 1) {
                                     $cards = array();
                                     $default = $cardlist->is_default;
                                     if ($default == 1) {
                                         $cards['is_default_text'] = "default";
                                     } else {
                                         $cards['is_default_text'] = "not_default";
                                     }
                                     $cards['card_id'] = $cardlist->id;
                                     $cards['owner_id'] = $cardlist->owner_id;
                                     $cards['customer_id'] = $cardlist->customer_id;
                                     $cards['last_four'] = $cardlist->last_four;
                                     $cards['card_token'] = $cardlist->card_token;
                                     $cards['card_type'] = $cardlist->card_type;
                                     $cards['is_default'] = $default;
                                 }
                                 $chagre = array();
                                 $settings = Settings::where('key', 'default_distance_unit')->first();
                                 $unit = $settings->value;
                                 if ($unit == 0) {
                                     $unit_set = 'kms';
                                 } elseif ($unit == 1) {
                                     $unit_set = 'miles';
                                 }
                                 $chagre['unit'] = $unit_set;
                                 $requestserv = RequestServices::where('request_id', $request->id)->first();
                                 if ($requestserv->base_price != 0) {
                                     $chagre['base_price'] = currency_converted($requestserv->base_price);
                                     $chagre['distance_price'] = currency_converted($requestserv->distance_cost);
                                     $chagre['price_per_unit_time'] = currency_converted($requestserv->time_cost);
                                 } else {
                                     /* $setbase_price = Settings::where('key', 'base_price')->first();
                                        $chagre['base_price'] = currency_converted($setbase_price->value); */
                                     $chagre['base_price'] = currency_converted($providertype->base_price);
                                     /* $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first();
                                        $chagre['distance_price'] = currency_converted($setdistance_price->value); */
                                     $chagre['distance_price'] = currency_converted($providertype->price_per_unit_distance);
                                     /* $settime_price = Settings::where('key', 'price_per_unit_time')->first();
                                        $chagre['price_per_unit_time'] = currency_converted($settime_price->value); */
                                     $chagre['price_per_unit_time'] = currency_converted($providertype->price_per_unit_time);
                                 }
                                 $chagre['total'] = currency_converted($request->total);
                                 $chagre['is_paid'] = $request->is_paid;
                                 /* $var = Keywords::where('id', 4)->first(); */
                                 $title = 'Your ' . Config::get('app.generic_keywords.Trip') . ' is completed';
                                 $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_completes_job')->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);
                                 $pattern = str_replace('%amount%', $request->total, $pattern);
                                 sms_notification($request->owner_id, 'owner', $pattern);
                                 $id = $request->id;
                                 // send email
                                 /* $settings = Settings::where('key', 'email_request_finished')->first();
                                    $pattern = $settings->value;
                                    $pattern = str_replace('%id%', $request->id, $pattern);
                                    $pattern = str_replace('%url%', web_url() . "/admin/request/map/" . $request->id, $pattern);
                                    $subject = "Request Completed";
                                    email_notification(2, 'admin', $pattern, $subject); */
                                 // $settings = Settings::where('key','email_invoice_generated_user')->first();
                                 // $pattern = $settings->value;
                                 // $pattern = str_replace('%id%', $request->id, $pattern);
                                 // $pattern = str_replace('%amount%', $request->total, $pattern);
                                 $email_data = array();
                                 $email_data['name'] = $owner->first_name;
                                 $email_data['emailType'] = 'user';
                                 $email_data['base_price'] = $bill['base_price'];
                                 $email_data['distance'] = $bill['distance'];
                                 $email_data['time'] = $bill['time'];
                                 $email_data['unit'] = $bill['unit'];
                                 $email_data['total'] = $bill['total'];
                                 //   $email_data['payment_mode'] = $bill['payment_mode'];
                                 $email_data['actual_total'] = currency_converted($actual_total);
                                 $email_data['is_paid'] = $request->is_paid;
                                 $email_data['promo_discount'] = currency_converted($promo_total);
                                 $request_services = RequestServices::where('request_id', $request->id)->first();
                                 $locations = WalkLocation::where('request_id', $request->id)->orderBy('id')->get();
                                 $start = WalkLocation::where('request_id', $request->id)->orderBy('id')->first();
                                 $end = WalkLocation::where('request_id', $request->id)->orderBy('id', 'desc')->first();
                                 $map = "https://maps-api-ssl.google.com/maps/api/staticmap?size=249x249&style=feature:landscape|visibility:off&style=feature:poi|visibility:off&style=feature:transit|visibility:off&style=feature:road.highway|element:geometry|lightness:39&style=feature:road.local|element:geometry|gamma:1.45&style=feature:road|element:labels|gamma:1.22&style=feature:administrative|visibility:off&style=feature:administrative.locality|visibility:on&style=feature:landscape.natural|visibility:on&scale=2&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-start@2x.png|{$start->latitude},{$start->longitude}&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-finish@2x.png|{$end->latitude},{$end->longitude}&path=color:0x2dbae4ff|weight:4";
                                 foreach ($locations as $location) {
                                     $map .= "|{$location->latitude},{$location->longitude}";
                                 }
                                 $start_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$start->latitude},{$start->longitude}"), TRUE);
                                 $start_address = "Address not found";
                                 if (isset($start_location['results'][0]['formatted_address'])) {
                                     $start_address = $start_location['results'][0]['formatted_address'];
                                 }
                                 $end_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$end->latitude},{$end->longitude}"), TRUE);
                                 $end_address = "Address not found";
                                 if (isset($end_location['results'][0]['formatted_address'])) {
                                     $end_address = $end_location['results'][0]['formatted_address'];
                                 }
                                 $email_data['start_location'] = $start_location;
                                 $email_data['end_location'] = $end_location;
                                 $walker = Walker::find($request->confirmed_walker);
                                 $walker_review = WalkerReview::where('request_id', $id)->first();
                                 if ($walker_review) {
                                     $rating = round($walker_review->rating);
                                 } else {
                                     $rating = 0;
                                 }
                                 $email_data['map'] = $map;
                                 $settings = Settings::where('key', 'admin_email_address')->first();
                                 $admin_email = $settings->value;
                                 $requestserv = RequestServices::where('request_id', $request->id)->orderBy('id', 'DESC')->first();
                                 $get_type_name = ProviderType::where('id', $requestserv->type)->first();
                                 $detail = array('admin_eamil' => $admin_email, 'request' => $request, 'start_address' => $start_address, 'end_address' => $end_address, 'start' => $start, 'end' => $end, 'map_url' => $map, 'walker' => $walker, 'rating', $rating, 'base_price' => $requestserv->base_price, 'price_per_time' => $price_per_unit_time, 'price_per_dist' => $price_per_unit_distance, 'ref_bonus' => $request->ledger_payment, 'promo_bonus' => "", 'dist_cost' => $requestserv->distance_cost, 'time_cost' => $requestserv->time_cost, 'type_name' => ucwords($get_type_name->name));
                                 //send email to owner
                                 /* $subject = "Invoice Generated";
                                    send_email($request->owner_id, 'owner', $email_data, $subject, 'invoice'); */
                                 $subject = "Invoice Generated";
                                 email_notification($request->owner_id, 'owner', $detail, $subject, 'invoice');
                                 $subject = "Request Completed";
                                 email_notification(1, 'admin', $detail, $subject, 'invoice');
                                 //send email to walker
                                 /* $subject = "Invoice Generated";
                                    $email_data['emailType'] = 'walker';
                                    send_email($request->confirmed_walker, 'walker', $email_data, $subject, 'invoice'); */
                                 $subject = "Invoice Generated";
                                 email_notification($request->confirmed_walker, 'walker', $detail, $subject, 'invoice');
                                 if ($request->is_paid == 1) {
                                     // send email
                                     /* $settings = Settings::where('key', 'email_payment_charged')->first();
                                                                               $pattern = $settings->value;
                                     
                                                                               $pattern = str_replace('%id%', $request->id, $pattern);
                                                                               $pattern = str_replace('%url%', web_url() . "/admin/request/map/" . $request->id, $pattern);
                                     
                                                                               $subject = "Payment Charged";
                                                                               email_notification(1, 'admin', $pattern, $subject); */
                                     $settings = Settings::where('key', 'admin_email_address')->first();
                                     $admin_email = $settings->value;
                                     $pattern = array('admin_eamil' => $admin_email, 'name' => 'Administrator', 'amount' => $request->total, 'req_id' => $request_id, 'web_url' => web_url());
                                     $subject = "Payment Done With " . $request_id . "";
                                     email_notification(1, 'admin', $pattern, $subject, 'pay_charged', null);
                                 } else {
                                     // send email
                                     /* $pattern = "Payment Failed for the request id " . $request->id . ".";
                                     
                                                                               $subject = "Payment Failed";
                                                                               email_notification(1, 'admin', $pattern, $subject); */
                                 }
                                 $settings = Settings::where('key', 'default_distance_unit')->first();
                                 $unit = $settings->value;
                                 if ($unit == 0) {
                                     $unit_set = 'kms';
                                 } elseif ($unit == 1) {
                                     $unit_set = 'miles';
                                 }
                                 $distance = DB::table('walk_location')->where('request_id', $request_id)->max('distance');
                                 $end_time = DB::table('walk_location')->where('request_id', $request_id)->max('created_at');
                                 $request_data_1 = array('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_started' => $request->is_started, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_dog_rated' => $request->is_dog_rated, 'is_cancelled' => $request->is_cancelled, 'is_walker_rated' => $request->is_walker_rated, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'accepted_time' => $request->request_start_time, 'distance' => (string) convert($distance, $unit), 'unit' => $unit_set, 'end_time' => $end_time, 'owner' => $owner_data1, 'dog' => $dog1, 'bill' => $bill, 'card_details' => $cards, 'charge_details' => $chagre, 'payment_option' => $request->is_paid);
                                 $response_array = array('success' => true, 'total' => currency_converted($total), 'error' => $payment_type, 'currency' => Config::get('app.generic_keywords.Currency'), 'is_paid' => $request->is_paid, '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, 'payment_mode' => "", 'bill' => $bill, 'owner' => $owner_data1, 'payment_option' => $request->is_paid, 'request' => $request_data_1);
                                 $response_code = 200;
                             } else {
                                 $response_array = array('success' => false, 'error' => 'Service not yet started', 'error_code' => 413);
                                 $response_code = 200;
                             }
                         } else {
                             /* $var = Keywords::where('id', 1)->first();
                                $response_array = array('success' => false, 'error' => 'Service ID doesnot matches with ' . $var->keyword . ' ID', 'error_code' => 407); */
                             $response_array = array('success' => false, 'error' => 'Service ID doesnot matches with ' . Config::get('app.generic_keywords.Provider') . ' ID', 'error_code' => 407);
                             $response_code = 200;
                         }
                     } else {
                         $response_array = array('success' => false, 'error' => 'Service ID Not Found', 'error_code' => 408);
                         $response_code = 200;
                     }
                 } else {
                     $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
                     $response_code = 200;
                 }
             } else {
                 if ($is_admin) {
                     /* $var = Keywords::where('id', 1)->first();
                        $response_array = array('success' => false, 'error' => '' . $var->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;
 }