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');
 }
 /**
  * Display a listing of the resource.
  *
  *
  * @return Response
  */
 public function index()
 {
     echo distanceGeoPoints(22, 50.0000001, 22, 50.000001);
 }
                    //echo "<pre>".var_dump($zip)."</pre>\n";
                    ?>

<!-- //////// record from ID: <?php 
                    echo get_the_ID();
                    ?>
  ///////// -->
<div class='mls-entry'>

		<?php 
                    the_title('<h1 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h1>');
                    //get_template_part( 'content', get_post_format() );
                    $posts_found++;
                    $last_id = get_the_ID();
                    echo "<p><span class='fields'>" . $my_fields['street'] . "<br>" . $my_fields['city'] . ", " . $my_fields['stateprovince'] . ", " . $my_fields["country"] . " " . $my_fields["postalcode"] . "</span><p><br>";
                    echo "<p><span class='distance'><b>Distance:</b> " . intval(distanceGeoPoints($my_coordinates['latitude'], $my_coordinates['longitude'], $zip['lat'], $zip['long'])) . " miles.</span></p><br>\n";
                    echo '<li><span class="map-list-sendmail "><a href="/?mls=mail&mls_id=' . get_the_ID() . '">Send Claim to Lawyer</a></span><br>';
                    echo '<li><span class="map-list-directions "><a href="">Get directions</a></span><hr>';
                    ?>


</div><!-- mls entry -->

        <?php 
                    if ($my_query_flag == 'mls_name' || $my_query_flag == 'country' || $my_query_flag == 'city' || $my_query_flag == 'state' || $my_query_flag == 'zip' || $my_query_flag == 'geosearch') {
                        ?>


<script>
my_id[<?php 
                        echo $posts_found - 1;
Example #4
0
#!/usr/bin/php
<?php 
include 'ingress_lib.php';
$drawtool = json_decode($argv[1]);
foreach ($drawtool as $poly) {
    # print_r($poly);
    if ($poly->type == "polygon") {
        # $p0 = array();
        $p0 = new stdClass();
        $p1 = new stdClass();
        $p2 = new stdClass();
        $l1 = new stdClass();
        $l2 = new stdClass();
        $dist1 = distanceGeoPoints($poly->latLngs[0], $poly->latLngs[1]);
        $dist2 = distanceGeoPoints($poly->latLngs[1], $poly->latLngs[2]);
        $dist3 = distanceGeoPoints($poly->latLngs[2], $poly->latLngs[0]);
        $area = areaFromLength($dist1, $dist2, $dist3);
        print "area: {$area}\n";
    }
}
 public function walk_location()
 {
     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');
         $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id, 'latitude' => $latitude, 'longitude' => $longitude), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer', 'latitude' => 'required', 'longitude' => '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 {
             $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) {
                     // Do necessary operations
                     if ($request = Requests::find($request_id)) {
                         if ($request->confirmed_walker == $walker_id) {
                             if ($request->is_started == 1) {
                                 $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;
                                 }
                                 $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();
                                 $response_array = array('success' => true, 'distance' => $distance);
                                 $response_code = 200;
                             } else {
                                 $response_array = array('success' => false, 'error' => 'Walk not yet started', 'error_code' => 414);
                                 $response_code = 200;
                             }
                         } else {
                             $response_array = array('success' => false, 'error' => 'Request ID doesnot matches with Walker ID', 'error_code' => 407);
                             $response_code = 200;
                         }
                     } else {
                         $response_array = array('success' => false, 'error' => 'Walk 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) {
                     $response_array = array('success' => false, 'error' => 'Walker 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 walk_location()
 {
     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');
         if (Input::has('bearing')) {
             $angle = Input::get('bearing');
         }
         $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id, 'latitude' => $latitude, 'longitude' => $longitude), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer', 'latitude' => 'required', 'longitude' => '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 {
             $unit = $unit_set = -1;
             $settings = Settings::where('key', 'default_distance_unit')->first();
             $unit = $settings->value;
             if ($unit == 0) {
                 $unit_set = 'kms';
             } elseif ($unit == 1) {
                 $unit_set = 'miles';
             }
             $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) {
                     // Do necessary operations
                     if ($request = Requests::find($request_id)) {
                         if ($request->confirmed_walker == $walker_id) {
                             if ($request->is_started == 1) {
                                 $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;
                                     if ($unit == 0) {
                                         $unit_set = 'kms';
                                     } elseif ($unit == 1) {
                                         $unit_set = 'miles';
                                     }
                                     $distancecon = convert($distance, $unit);
                                 } else {
                                     $distance = 0;
                                 }
                                 $walker = Walker::find($walker_id);
                                 $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();
                                 /* GET SECOND LAST ENTY FOR TIME */
                                 $loc1 = WalkLocation::where('request_id', $request->id)->orderBy('id', 'desc')->first();
                                 /* GET SECOND LAST ENTY FOR TIME END */
                                 if ($request->is_completed != 1) {
                                     $walk_location = new WalkLocation();
                                     $walk_location->request_id = $request_id;
                                     $walk_location->latitude = $latitude;
                                     $walk_location->longitude = $longitude;
                                     $walk_location->distance = $distance;
                                     $walk_location->bearing = $angle;
                                     $walk_location->save();
                                 }
                                 $one_minut_old_time = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")) - 60);
                                 /* $loc1 = WalkLocation::where('request_id', $request->id)->first(); */
                                 /* print $loc1; */
                                 $loc2 = WalkLocation::where('request_id', $request->id)->orderBy('id', 'desc')->first();
                                 if ($loc1) {
                                     $time1 = strtotime($loc2->created_at);
                                     $time2 = strtotime($loc1->created_at);
                                     /* echo $difference = intval(($time1 - $time2) / 60); */
                                     $difference = ($time1 - $time2) / 60;
                                     $loc1min = WalkLocation::where('request_id', $request->id)->where('created_at', '<=', $one_minut_old_time)->orderBy('id', 'desc')->first();
                                     $distence = distanceGeoPoints($loc1min->latitude, $loc1min->longitude, $latitude, $longitude);
                                     if ($request->is_completed != 1) {
                                         if ($distence <= 50) {
                                             $request->time = $request->time + $difference;
                                         } else {
                                             $request->time = $request->time;
                                         }
                                     }
                                 } else {
                                     $request->time = 0;
                                 }
                                 $request->save();
                                 $response_array = array('success' => true, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'payment_type' => $request->payment_mode, 'is_cancelled' => $request->is_cancelled, 'distance' => $distancecon, 'unit' => $unit_set, 'time' => $difference);
                                 $response_code = 200;
                             } else {
                                 $walker = Walker::find($walker_id);
                                 $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();
                                 $response_array = array('success' => false, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'payment_type' => $owner->payment_mode, 'is_cancelled' => $request->is_cancelled, 'unit' => $unit_set, 'error' => 'Service not yet started', 'error_code' => 414);
                                 $response_code = 200;
                             }
                         } else {
                             /* $var = Keywords::where('id', 1)->first();
                                $response_array = array('success' => false, 'error' => 'Request ID doesnot matches with ' . $var->keyword . ' ID', 'error_code' => 407); */
                             $response_array = array('success' => false, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'payment_type' => $owner->payment_mode, 'is_cancelled' => $request->is_cancelled, 'unit' => $unit_set, 'error' => 'Request 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;
 }
 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('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);
             $walk_location = new WalkLocation();
             $walk_location->request_id = $current_request->id;
             $walk_location->latitude = $owner->latitude;
             $walk_location->longitude = $owner->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) {
             $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 = Input::get('rating');
             $review_dog->owner_id = $current_request->owner_id;
             $review_dog->request_id = $current_request->id;
             $review_dog->save();
             $message = "You has successfully rated the owner.";
             $type = "success";
             return Redirect::to('/provider/trips')->with('message', $message)->with('type', $type);
         }
         if ($state == 5) {
             $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'];
             $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;
             }
             $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;
             $walker_data = Walker::find($current_request->confirmed_walker);
             if (!$walker_data->type) {
                 $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;
             } else {
                 $provider_type = ProviderType::find($walker_data->type);
                 $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, $unit);
             if ($pricing_type == 1) {
                 $distance_cost = $price_per_unit_distance * $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;
             $current_request->base_price = $base_price;
             $current_request->distance_cost = $distance_cost;
             $current_request->time_cost = $time_cost;
             $current_request->total = $total;
             // 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;
                     }
                 }
             }
             if ($total == 0) {
                 $current_request->is_paid = 1;
             } else {
                 /*
                 $payment_data = Payment::where('owner_id',$current_request->owner_id)->first();
                 
                 if( $payment_data )
                 {
                 	$customer_id = $payment_data->customer_id;
                 	try{
                 		if(Config::get('app.default_payment') == 'stripe')
                 		{
                 			Stripe::setApiKey(Config::get('app.stripe_secret_key'));
                 			Stripe_Charge::create(array(
                 					  "amount"   => $total * 100, 
                 					  "currency" => "usd",
                 					  "customer" => $customer_id)
                 					);
                 			$request->is_paid = 1;
                 		}
                 		else{
                 
                 			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'));
                 
                 			$result = Braintree_Transaction::sale(array(
                 			  'amount' => round($total,2),
                 			  'paymentMethodNonce' => $customer_id
                 			));
                 			
                 			if ($result->success) {
                 				$request->is_paid = 1;
                 			}
                 			else{
                 				$request->is_paid = 0;
                 			}
                 		}
                 		
                 	}
                 	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;
                 	}
                 
                 }
                 */
             }
             $current_request->card_payment = $total;
             $current_request->ledger_payment = $current_request->total - $total;
             $current_request->save();
         }
         $current_request->save();
     }
     return Redirect::to('/provider/tripinprogress');
 }