public function createReport()
 {
     $to = Input::get('to');
     $from = Input::get('from');
     if ($from == null || $to == null) {
         $todayFrom = date('Y-m-d', strtotime('-30 day')) . ' 00:00:00';
         $todayTo = date('Y-m-d') . ' 23:59:59';
     } else {
         $todayFrom = $from . ' 00:00:00';
         $todayTo = $to . ' 23:59:59';
     }
     $results = [];
     try {
         $trips = DailyTrips::where('departure_date_time', '>', $todayFrom)->where('departure_date_time', '<', $todayTo)->orderBy('departure_date_time')->get();
         $totalTripCount = count($trips);
         $totalTripCost = 0;
         $totalTripDistance = 0;
         $totalTripTime = 0;
         foreach ($trips as $trip) {
             $totalTripCost += $trip->trip_cost;
             $totalTripDistance += $trip->arrival_km - $trip->departure_km;
             $totalTripTime += strtotime($trip->arrival_date_time) - strtotime($trip->departure_date_time);
         }
         $totalTripTime = date('H:i:s', $totalTripTime);
         $totalFuel = FuelFillUp::where('date_and_time', '>', $todayFrom)->where('date_and_time', '<', $todayTo)->orderBy('date_and_time')->get();
         $totalFuelCost = 0;
         $totalFuelAmount = 0;
         foreach ($totalFuel as $Fuel) {
             $totalFuelCost += $Fuel->cost;
             $totalFuelAmount += $Fuel->amount;
         }
         $totalPayments = 0;
         $totalOther = 0;
         $payments = Payment::where('created_at', '>', $todayFrom)->where('created_at', '<', $todayTo)->orderBy('created_at')->get();
         foreach ($payments as $payment) {
             $totalPayments += $payment->amount;
             $totalOther += $payment->other;
         }
         $report = ['totalTripCounts' => $totalTripCount, 'totalTripCost' => $totalTripCost, 'totalTripkm' => $totalTripDistance, 'totalTripTime' => $totalTripTime, 'totalFuelCost' => round($totalFuelCost, 2), 'totalFuelAmount' => round($totalFuelAmount, 2), 'totalPayments' => $totalPayments, 'totalOther' => $totalOther];
         array_push($results, $report);
         /*
         $queries = DB::getQueryLog();
         $last_query = end($queries);
         */
     } catch (Exception $ex) {
         \Log::error(__METHOD__ . ' | error :' . print_r($ex, 1));
     }
     //\Log::info(__METHOD__.' | =====> $results : '.print_r($results,1 ));
     return json_encode($results);
 }
 public function saveFuelFillUp()
 {
     $post = Input::all();
     $dateAndTime = $post['date_and_time'];
     $cost = $post['cost'];
     $amount = $post['amount'];
     $pricePerLiter = $cost / $amount;
     try {
         $FuelFillUp = new FuelFillUp();
         $FuelFillUp->user_id = Session::get('user_id');
         $FuelFillUp->car_id = Session::get('car_id');
         $FuelFillUp->date_and_time = $dateAndTime;
         $FuelFillUp->cost = $cost;
         $FuelFillUp->amount = $amount;
         $FuelFillUp->price_per_liter = $pricePerLiter;
         $FuelFillUp->save();
         $result = array('success' => true, 'message' => 'Fuel Fill-up saved');
     } catch (Exception $ex) {
         \Log::error(__METHOD__ . ' | error : ' . print_r($ex, 1));
         $result = array('success' => false, 'message' => 'an error occurred');
     }
     return $result;
 }