public function providerTripChangeState()
 {
     $date = date("Y-m-d H:i:s");
     $time_limit = date("Y-m-d H:i:s", strtotime($date) - 3 * 60 * 60);
     $walker_id = Session::get('walker_id');
     $state = $request_id = Request::segment(4);
     $current_request = Requests::where('confirmed_walker', $walker_id)->where('is_cancelled', 0)->where('is_dog_rated', 0)->where('created_at', '>', $time_limit)->orderBy('created_at', 'desc')->where(function ($query) {
         $query->where('status', 0)->orWhere(function ($query_inner) {
             $query_inner->where('status', 1)->where('is_dog_rated', 0);
         });
     })->first();
     if ($current_request && $state) {
         if ($state == 2) {
             $current_request->is_walker_started = 1;
             $owner = Owner::find($current_request->owner_id);
             $walker = Walker::find($walker_id);
             $location = get_location($owner->latitude, $owner->longitude);
             $latitude = $location['lat'];
             $longitude = $location['long'];
             $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();
             $walk_location = new WalkLocation();
             $walk_location->request_id = $current_request->id;
             $walk_location->latitude = $latitude;
             $walk_location->longitude = $longitude;
             $walk_location->distance = 0;
             $walk_location->save();
         }
         if ($state == 3) {
             $current_request->is_walker_arrived = 1;
         }
         if ($state == 4) {
             $current_request->is_started = 1;
         }
         if ($state == 6) {
             $rating = 0;
             if (Input::has('rating')) {
                 $rating = Input::get('rating');
             }
             $current_request->is_dog_rated = 1;
             $current_request->save();
             $review_dog = new DogReview();
             $review_dog->walker_id = $current_request->confirmed_walker;
             $review_dog->comment = Input::get('review');
             $review_dog->rating = $rating;
             $review_dog->owner_id = $current_request->owner_id;
             $review_dog->request_id = $current_request->id;
             $review_dog->save();
             if ($rating) {
                 if ($owner = Owner::find($current_request->owner_id)) {
                     $old_rate = $owner->rate;
                     $old_rate_count = $owner->rate_count;
                     $new_rate_counter = $owner->rate_count + 1;
                     $new_rate = ($owner->rate * $owner->rate_count + $rating) / $new_rate_counter;
                     $owner->rate_count = $new_rate_counter;
                     $owner->rate = $new_rate;
                     $owner->save();
                 }
             }
             $message = "You has successfully rated the owner.";
             $type = "success";
             return Redirect::to('/provider/trips')->with('message', $message)->with('type', $type);
         }
         if ($state == 5) {
             $request_services = RequestServices::where('request_id', $current_request->id)->first();
             $request_typ = ProviderType::where('id', '=', $request_services->req_typ)->first();
             $address = urlencode(Input::get('address'));
             $end_address = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}"), TRUE);
             $end_location = $end_address['results'][0]['geometry'];
             $latitude = $end_location['location']['lat'];
             $longitude = $end_location['location']['lng'];
             $location = get_location($latitude, $longitude);
             $latitude = $location['lat'];
             $longitude = $location['long'];
             $request_id = $current_request->id;
             $walk_location_last = WalkLocation::where('request_id', $request_id)->orderBy('created_at', 'desc')->first();
             if ($walk_location_last) {
                 $distance_old = $walk_location_last->distance;
                 $distance_new = distanceGeoPoints($walk_location_last->latitude, $walk_location_last->longitude, $latitude, $longitude);
                 $distance = $distance_old + $distance_new;
                 $settings = Settings::where('key', 'default_distance_unit')->first();
                 //$unit = $settings->value;
                 $distance = $distance;
             } else {
                 $distance = 0;
             }
             $walker = Walker::find($walker_id);
             $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();
             $walk_location = new WalkLocation();
             $walk_location->request_id = $request_id;
             $walk_location->latitude = $latitude;
             $walk_location->longitude = $longitude;
             $walk_location->distance = $distance;
             $walk_location->save();
             Walker::where('id', '=', $walker_id)->update(array('is_available' => 1));
             // Calculate Rerquest Stats
             $time = 0;
             $time_query = "SELECT TIMESTAMPDIFF(SECOND,MIN(created_at),MAX(created_at)) as diff\n\t\t\t\tFROM walk_location where request_id = {$current_request->id}\n\t\t\t\tGROUP BY request_id limit 1 ";
             $time_data = DB::select(DB::raw($time_query));
             foreach ($time_data as $time_diff) {
                 $time = $time_diff->diff;
             }
             $time = $time / 60;
             /* TIME CALCULATION REDIRECTED */
             $time = 0;
             /* TIME CALCULATION REDIRECTED END */
             $walker_data = Walker::find($current_request->confirmed_walker);
             $provider_type = ProviderServices::where('type', $walker_data->type)->where('provider_id', $walker_id)->first();
             if ($provider_type == NULL) {
                 /* $settings = Settings::where('key', 'price_per_unit_distance')->first();
                    $price_per_unit_distance = $settings->value;
                    $settings = Settings::where('key', 'price_per_unit_time')->first();
                    $price_per_unit_time = $settings->value;
                    $settings = Settings::where('key', 'base_price')->first();
                    $base_price = $settings->value; */
                 $setbase_distance = $request_typ->base_distance;
                 $base_price = $request_typ->base_price;
                 $price_per_unit_distance = $request_typ->price_per_unit_distance;
                 $price_per_unit_time = $request_typ->price_per_unit_time;
             } else {
                 // $setbase_distance = $request_typ->base_distance;
                 $setbase_distance = "";
                 $provider_type = ProviderServices::where('type', $walker_data->type)->where('provider_id', $walker_id)->first();
                 $base_price = $provider_type->base_price;
                 $price_per_unit_distance = $provider_type->price_per_unit_distance;
                 $price_per_unit_time = $provider_type->price_per_unit_time;
             }
             $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;
             $distance = convert($distance);
             if ($pricing_type == 1) {
                 if ($distance <= $setbase_distance) {
                     $distance_cost = 0;
                 } else {
                     $distance_cost = $price_per_unit_distance * ($distance - $setbase_distance);
                 }
                 $time_cost = $price_per_unit_time * $time;
                 $total = $base_price + $distance_cost + $time_cost;
             } else {
                 $distance_cost = 0;
                 $time_cost = 0;
                 $total = $base_price;
             }
             $current_request->is_completed = 1;
             $current_request->distance = $distance;
             $current_request->time = $time;
             $request_services->base_price = $base_price;
             $request_services->distance_cost = $distance_cost;
             $request_services->time_cost = $time_cost;
             $request_services->total = $total;
             $current_request->total = $total;
             $request_services->save();
             // charge client
             // charge client
             $ledger = Ledger::where('owner_id', $current_request->owner_id)->first();
             if ($ledger) {
                 $balance = $ledger->amount_earned - $ledger->amount_spent;
                 if ($balance > 0) {
                     if ($total > $balance) {
                         $ledger_temp = Ledger::find($ledger->id);
                         $ledger_temp->amount_spent = $ledger_temp->amount_spent + $balance;
                         $ledger_temp->save();
                         $total = $total - $balance;
                     } else {
                         $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', $current_request->promo_code)->where('type', 1)->first()) {
                 $discount = $pcode->value / 100;
                 $promo_discount = $total * $discount;
                 $total = $total - $promo_discount;
                 if ($total < 0) {
                     $total = 0;
                 }
             }
             $current_request->total = $total;
             $current_request->save();
             $cod_sett = Settings::where('key', 'cod')->first();
             $allow_cod = $cod_sett->value;
             if ($current_request->payment_mode == 1 and $allow_cod == 1) {
                 // Pay by Cash
                 $current_request->is_paid = 1;
                 Log::info('allow_cod');
             } elseif ($current_request->payment_mode == 2) {
                 // paypal
                 Log::info('paypal payment');
             } else {
                 Log::info('normal payment. Stored cards');
                 // stored cards
                 if ($total == 0) {
                     $current_request->is_paid = 1;
                 } else {
                     $payment_data = Payment::where('owner_id', $current_request->owner_id)->where('is_default', 1)->first();
                     if (!$payment_data) {
                         $payment_data = Payment::where('owner_id', $current_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" => ceil($total * 100), "currency" => "usd", "customer" => $customer_id));
                                 Log::info($charge);
                             } catch (Stripe_InvalidRequestError $e) {
                                 // Invalid parameters were supplied to Stripe's API
                                 $ownr = Owner::find($current_request->owner_id);
                                 $ownr->debt = $total;
                                 $ownr->save();
                                 $message = array('error' => $e->getMessage());
                                 $type = "success";
                                 Log::info($message);
                                 return Redirect::to('/provider/tripinprogress')->with('message', $message)->with('type', $type);
                             }
                             $current_request->is_paid = 1;
                             $settng = Settings::where('key', 'service_fee')->first();
                             $settng_mode = Settings::where('key', 'payment_mode')->first();
                             if ($settng_mode->value == 2 and $transfer_allow == 1) {
                                 $transfer = Stripe_Transfer::create(array("amount" => ($total - $settng->value * $total / 100) * 100, "currency" => "usd", "recipient" => $walker_data->merchant_id));
                                 $current_request->transfer_amount = $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 ($settng_mode->value == 2 and $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;
                                 } else {
                                     $request->is_paid = 0;
                                 }
                             } catch (Exception $e) {
                                 $message = "Something went wrong in the payment. Please try again.";
                                 $type = "success";
                                 return Redirect::to('/provider/tripinprogress')->with('message', $message)->with('type', $type);
                             }
                         }
                         $current_request->card_payment = $total;
                         $current_request->ledger_payment = $current_request->total - $total;
                     }
                 }
             }
             $current_request->save();
         }
         $current_request->save();
     }
     return Redirect::to('/provider/tripinprogress');
 }
 public function searchpromo()
 {
     $valu = $_GET['valu'];
     $type = $_GET['type'];
     $success = Input::get('success');
     Session::put('valu', $valu);
     Session::put('type', $type);
     if ($type == 'promo_id') {
         $promo_codes = PromoCodes::where('id', $valu)->paginate(10);
     } elseif ($type == 'promo_name') {
         $promo_codes = PromoCodes::where('coupon_code', 'like', '%' . $valu . '%')->paginate(10);
     } elseif ($type == 'promo_type') {
         if ($valu == '%') {
             $promo_codes = PromoCodes::where('type', 1)->paginate(10);
         } elseif ($val = '$') {
             $promo_codes = PromoCodes::where('type', 2)->paginate(10);
         }
     } elseif ($type == 'promo_state') {
         if ($valu == 'active' || $valu == 'Active') {
             $promo_codes = PromoCodes::where('state', 1)->paginate(10);
         } elseif ($val = 'Deactivated' || ($val = 'deactivated')) {
             $promo_codes = PromoCodes::where('state', 2)->paginate(10);
         }
     }
     $title = ucwords(trans('customize.promo_codes') . " | Search Result");
     /* 'Promo Codes | Search Result' */
     return View::make('list_promo_codes')->with('title', $title)->with('page', 'promo_code')->with('success', $success)->with('promo_codes', $promo_codes);
 }
 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 check_promo_code()
 {
     $promo_code = Input::get('promo_code');
     $token = Input::get('token');
     $owner_id = Input::get('id');
     $validator = Validator::make(array('promo_code' => $promo_code, 'token' => $token, 'owner_id' => $owner_id), array('promo_code' => 'required', '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) {
                 $settings = Settings::where('key', 'promotional_code_activation')->first();
                 $prom_act = $settings->value;
                 if ($prom_act) {
                     // check promo code
                     $check_code = PromoCodes::where('coupon_code', $promo_code)->where('uses', '>', 0)->first();
                     if ($check_code != NULL) {
                         if ($check_code->state == 1 && date('Y-m-d H:i:s', strtotime($check_code->expiry)) > date('Y-m-d H:i:s') && date('Y-m-d H:i:s', strtotime($check_code->start_date)) <= date('Y-m-d H:i:s')) {
                             if ($check_code->type == 1) {
                                 $discount = $check_code->value . " %";
                             } elseif ($check_code->type == 2) {
                                 $discount = "\$ " . $check_code->value;
                             }
                             $response_array = array('success' => true, 'discount' => $discount);
                         } else {
                             $response_array = array('success' => false, 'error' => 'Invalid Promo Code', 'error_code' => 418);
                         }
                     } else {
                         $response_array = array('success' => false, 'error' => 'Invalid Promo Code', 'error_code' => 419);
                     }
                 } else {
                     $response_array = array('success' => false, 'error' => 'Promotion feature is not active.', 'error_code' => 419);
                 }
             } else {
                 $response_array = array('success' => false, 'error' => 'Request ID Not Found', 'error_code' => 408);
             }
         } else {
             $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
         }
         $response_code = 200;
     }
     $response = Response::json($response_array, $response_code);
     return $response;
 }
 public function saveUserRequestTrip()
 {
     Session::forget('skipReview');
     $latitude = Input::get('latitude');
     $longitude = Input::get('longitude');
     $d_latitude = Input::get('d_latitude');
     $d_longitude = Input::get('d_longitude');
     $type = Input::get('type');
     $owner_id = Session::get('user_id');
     $payment_type = Input::get('payment_type');
     if ($type == "") {
         $message = "Please Select the Service Type";
         $type = "warning";
         return Redirect::route('userrequestTrip')->with('message', $message)->with('type', $type);
     } else {
         if (Input::has('promo_code')) {
             $promo_code = Input::get('promo_code');
             $code_check = PromoCodes::where('coupon_code', $promo_code)->first();
             if ($code_check == NULL) {
                 $message = "Invalid Promo Code";
                 $type = "error";
                 return Redirect::route('userrequestTrip')->with('message', $message)->with('type', $type);
             } else {
                 if ($code_check->state == 1 && date('Y-m-d H:i:s', strtotime($code_check->expiry)) > date('Y-m-d H:i:s')) {
                     $code_id = $code_check->id;
                 } else {
                     $message = "Invalid Promo Code";
                     $type = "error";
                     return Redirect::route('userrequestTrip')->with('message', $message)->with('type', $type);
                 }
             }
         } else {
             $code_id = NULL;
         }
         $owner_data = Owner::find($owner_id);
         $settings = Settings::where('key', 'default_search_radius')->first();
         $distance = $settings->value;
         if (Input::has('type')) {
             $type = Input::get('type');
             if (!$type) {
                 // choose default type
                 $provider_type = ProviderType::where('is_default', 1)->first();
                 if (!$provider_type) {
                     $type = 1;
                 } else {
                     $type = $provider_type->id;
                 }
             }
         }
         $typequery = "SELECT distinct provider_id from walker_services where type IN({$type})";
         $typewalkers = DB::select(DB::raw($typequery));
         /* $var = Keywords::where('id', 1)->first(); */
         if (empty($typewalkers)) {
             /* $message = "No " . $var->keyword . " found matching the service type."; */
             $message = "No " . Config::get('app.generic_keywords.Provider') . " found matching the service type.";
         } else {
             Log::info('typewalkers = ' . print_r($typewalkers, true));
             foreach ($typewalkers as $key) {
                 $types[] = $key->provider_id;
             }
             $typestring = implode(",", $types);
             Log::info('typestring = ' . print_r($typestring, true));
             if ($typestring == '') {
                 /* $message = "No " . $var->keyword . " found matching the service type."; */
                 $message = "No " . Config::get('app.generic_keywords.Provider') . " found matching the service type.";
             }
             $settings = Settings::where('key', 'default_search_radius')->first();
             $distance = $settings->value;
             $settings = Settings::where('key', 'default_distance_unit')->first();
             $unit = $settings->value;
             if ($unit == 0) {
                 $multiply = 1.609344;
             } elseif ($unit == 1) {
                 $multiply = 1;
             }
             $query = "SELECT walker.*, ROUND(" . $multiply . " * 3956 * acos( cos( radians('{$latitude}') ) * cos( radians(latitude) ) * cos( radians(longitude) - radians('{$longitude}') ) + sin( radians('{$latitude}') ) * sin( radians(latitude) ) ) ,8) as distance from walker where is_available = 1 and is_active = 1 and is_approved = 1 and ROUND((" . $multiply . " * 3956 * acos( cos( radians('{$latitude}') ) * cos( radians(latitude) ) * cos( radians(longitude) - radians('{$longitude}') ) + sin( radians('{$latitude}') ) * sin( radians(latitude) ) ) ) ,8) <= {$distance} and walker.deleted_at IS NULL and walker.id IN({$typestring}) order by distance";
             $walkers = DB::select(DB::raw($query));
             $walker_list = array();
             $owner = Owner::find($owner_id);
             $owner->latitude = $latitude;
             $owner->longitude = $longitude;
             $owner->save();
             $user_timezone = $owner->timezone;
             $default_timezone = Config::get('app.timezone');
             $offset = $this->get_timezone_offset($default_timezone, $user_timezone);
             $request = new Requests();
             $request->owner_id = $owner_id;
             if ($d_longitude != '' && $d_latitude != '') {
                 $request->D_latitude = $d_latitude;
                 $request->D_longitude = $d_longitude;
             }
             $request->request_start_time = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")) + $offset);
             $request->save();
             $request_service = new RequestServices();
             $request_service->type = $type;
             $request_service->request_id = $request->id;
             $request_service->save();
             $i = 0;
             $first_walker_id = 0;
             foreach ($walkers as $walker) {
                 $request_meta = new RequestMeta();
                 $request_meta->request_id = $request->id;
                 $request_meta->walker_id = $walker->id;
                 if ($i == 0) {
                     $first_walker_id = $walker->id;
                     $i++;
                 }
                 $request_meta->save();
             }
             $req = Requests::find($request->id);
             $req->current_walker = $first_walker_id;
             $req->confirmed_walker = 0;
             $req->payment_mode = $payment_type;
             $req->promo_code = $code_id;
             $req->save();
             $settings = Settings::where('key', 'provider_timeout')->first();
             $time_left = $settings->value;
             /* $var = Keywords::where('id', 1)->first();
             
                               $message = "Your Request is successful. Please wait while we are finding a nearest " . $var->keyword . " for you."; */
             $message = "Your Request is successful. Please wait while we are finding a nearest " . Config::get('app.generic_keywords.Provider') . " for you.";
             $type = "success";
         }
         return Redirect::to('/user/request-trip')->with('message', $message)->with('type', $type);
         // Send Notification
         $walker = Walker::find($first_walker_id);
         if ($walker) {
             $msg_array = array();
             $msg_array['unique_id'] = 1;
             $msg_array['request_id'] = $request->id;
             $msg_array['time_left_to_respond'] = $time_left;
             $owner = Owner::find($owner_id);
             $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;
             /* $request_data['owner']['rating'] = DB::table('review_dog')->where('owner_id', '=', $owner->id)->avg('rating') ? : 0; */
             $request_data['owner']['rating'] = $owner->rate;
             /* $request_data['owner']['num_rating'] = DB::table('review_dog')->where('owner_id', '=', $owner->id)->count(); */
             $request_data['owner']['num_rating'] = $owner->rate_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 = json_encode($msg_array);
             send_notifications($first_walker_id, "walker", $title, $message);
         }
         // Send SMS
         $settings = Settings::where('key', 'sms_request_created')->first();
         $pattern = $settings->value;
         $pattern = str_replace('%user%', $owner_data->first_name . " " . $owner_data->last_name, $pattern);
         $pattern = str_replace('%id%', $request->id, $pattern);
         $pattern = str_replace('%user_mobile%', $owner_data->phone, $pattern);
         sms_notification(1, 'admin', $pattern);
         // send email
         /* $settings = Settings::where('key', 'email_new_request')->first();
            $pattern = $settings->value;
            $pattern = str_replace('%id%', $request->id, $pattern);
            $pattern = str_replace('%url%', web_url() . "/admin/request/map/" . $request->id, $pattern);
            $subject = "New Request Created";
            email_notification(1, 'admin', $pattern, $subject); */
         $settings = Settings::where('key', 'admin_email_address')->first();
         $admin_email = $settings->value;
         $follow_url = web_url() . "/user/signin";
         $pattern = array('admin_eamil' => $admin_email, 'trip_id' => $request->id, 'follow_url' => $follow_url);
         $subject = "Ride Booking Request";
         email_notification(1, 'admin', $pattern, $subject, 'new_request', null);
         return Redirect::to('/user/request-trip')->with('message', $message)->with('type', $type);
     }
 }
 ?>
                 </td>
                 <td>
                     <?php 
 echo sprintf2($walk->ledger_payment, 2);
 ?>
                 </td>
                 <td>
                     <?php 
 echo sprintf2($walk->card_payment, 2);
 ?>
                 </td>
                 <td>
                     <?php 
 if ($walk->promo_id !== NULL) {
     $promo = PromoCodes::where('id', $walk->promo_id)->first();
     if ($promo) {
         if ($promo->type == 2) {
             echo sprintf2($promo->value, 2);
         } elseif ($promo->type == 1) {
             echo sprintf2($promo->value * $walk->total / 100, 2);
         } else {
             echo "<span class='badge bg-red'>" . Config::get('app.blank_fiend_val') . "</span>";
         }
     } else {
         echo "<span class='badge bg-red'>" . Config::get('app.blank_fiend_val') . "</span>";
     }
 } else {
     echo "<span class='badge bg-red'>" . Config::get('app.blank_fiend_val') . "</span>";
 }
 ?>