public function saveNewTrip() { if (!Session::get('logged')) { return Redirect::to('/'); } $tripCost = 0; $post = Input::all(); /** * b : base fare * ct : cost per time * cd : cost per distance * If your ride lasted for time T and you rode a distance of D, the total cost is simply b + ct∗T + cd∗D. */ if ($post != null) { try { $startKm = $post['departure_km']; $endKm = $post['arrival_km']; $phone = (int) $post['phone']; $emailAddress = $post['email']; $flatPrice = $post['flat_price']; $dailyPrice = $post['daily_price']; $hourlyPrice = $post['hourly_price']; $base = $post['base']; $perKm = $post['per_km']; $perMin = $post['per_min']; $tripMode = $post['trip_mode']; $tollFee = $post['toll_fee']; $parkingFee = $post['parking_fee']; $tempTripId = $post['temp_trip_id']; if (!empty($tollFee) || !empty($parkingFee)) { if ($tripMode == DailyTrips::FREE_MODE) { $extraCharge = 0; $extraCost = $tollFee + $parkingFee; } else { $extraCharge = $tollFee + $parkingFee; $extraCost = 0; } } else { $extraCost = $extraCharge = 0; } $tripKm = $endKm - $startKm; $startTime = strtotime($post['departure_date_time']); $endTime = strtotime($post['arrival_date_time']); $tripTime = ($endTime - $startTime) / 60; $clientId = $post['client_id']; if (!empty($clientId)) { try { $client = Client::find($clientId); $baseFare = $client->base; $costPerKm = $client->price_per_km; $costPerMin = $client->price_per_min; $currency = $client->currency; $tripCost = $baseFare + $costPerKm * $tripKm + $costPerMin * $tripTime; } catch (Exception $ex) { \Log::error(__METHOD__ . ' | error : ' . print_r($ex, 1)); } } else { if (!empty($base) && !empty($perKm) && !empty($perMin)) { $baseFare = $base; $costPerKm = $perKm; $costPerMin = $perMin; $tripCost = $baseFare + $costPerKm * $tripKm + $costPerMin * $tripTime; } } if (!empty($flatPrice)) { $tripCost = $flatPrice; } if (!empty($hourlyPrice)) { $tripCost = $tripTime * ($hourlyPrice / 60); } if (!empty($daily1Price)) { $tripCost = $tripTime * ($dailyPrice / (24 * 60)); } $tripCost += $extraCharge; $newDailyTrip = new DailyTrips(); $newDailyTrip->user_id = Session::get('user_id'); $newDailyTrip->car_id = Session::get('car_id'); $newDailyTrip->client_id = $clientId; $newDailyTrip->customer_name = $post['customer_name']; $newDailyTrip->toll_fee = $tollFee; $newDailyTrip->parking_fee = $parkingFee; $newDailyTrip->customer_email = $post['email']; $newDailyTrip->customer_phone = $phone; $newDailyTrip->departure_km = $startKm; $newDailyTrip->departure_date_time = $post['departure_date_time']; $newDailyTrip->arrival_km = $endKm; $newDailyTrip->arrival_date_time = $post['arrival_date_time']; $newDailyTrip->departure_address = $post['departure_address']; $newDailyTrip->arrival_address = $post['arrival_address']; $newDailyTrip->trip_cost = $tripCost; $newDailyTrip->extra_cost = $extraCost; $newDailyTrip->extra_charge = $extraCharge; $newDailyTrip->currency = $currency; $newDailyTrip->trip_time = $tripTime; $newDailyTrip->trip_distance = $tripKm; $newDailyTrip->delete_req = null; $newDailyTrip->edit_req = null; $newDailyTrip->save(); if (!empty($email) && strpos($email, '@')) { $messageBody = 'Your trip with Presidential Car cost: ' . $currency . ' ' . round($tripCost, 2); CommunicationController::sendEmail($emailAddress, $messageBody, 'emails/cost', 'Your Trip Receipt'); $email = new Email(); $email->email_address = $emailAddress; $email->message = $messageBody; $email->date_sent = $post['arrival_date_time']; $email->save(); } /* CommunicationController::sendSmsToNumber($phone, $messageBody); $message = new Message(); $message->phone = $phone; $message->cost = $tripCost; $message->message = $messageBody; $message->currency = $currency; $message->date_sent = $post['arrival_date_time']; $message->save(); */ $tempTrip = DailyTripsTemp::find($tempTripId); //->delete(); \Log::info(__METHOD__ . ' | ============= Temp Trip: ' . print_r($tempTrip, 1)); if ($tempTrip instanceof DailyTripsTemp) { $tempTrip->delete(); } \Session::set('temp_trip', ''); $result = array('success' => true, 'message' => 'New trip entered successfully'); } catch (Exception $ex) { \Log::error(__METHOD__ . ' | error :' . print_r($ex, 1)); $result = array('success' => false, 'message' => 'an error occurred'); } return $result; } }