/**
  * check bus stop history,
  * if last visited bus is the last bus stop on its route, then delete record that related to that bus
  */
 public function checkBusStopHistory()
 {
     $busStopHistoryModel = new BusStopHistory();
     $busStopHistory = $busStopHistoryModel->where('plat_nomor', '=', $this->plat_nomor)->orderBy('arrival_history', 'desc')->with('routeOrder')->take(1)->get()->toArray();
     if (sizeof($busStopHistory > 0) && $busStopHistory != null) {
         $busRouteModel = new BusRoute();
         $lastRouteOrder = $busRouteModel->where('rute_id', '=', $busStopHistory[0]['rute_id'])->orderBy('urutan', 'desc')->take(1)->get()->toArray();
         //if bus has reach the last bus stop on its route, then delete related record
         if ($busStopHistory[0]['route_order']['urutan'] == $lastRouteOrder[0]['urutan']) {
             $busStopHistoryModel = new BusStopHistory();
             $busStopHistoryModel->where('plat_nomor', '=', $this->plat_nomor)->delete();
         }
     }
 }
 /**
  * get 10 most recent departure from certain bus stop
  *
  * @param $halte_id
  * @return \Illuminate\Http\JsonResponse
  */
 public function getDepartureHistory($halte_id)
 {
     $busStopHistoryModel = new BusStopHistory();
     try {
         $busStopHistory = $busStopHistoryModel->where('halte_id', '=', $halte_id)->orderBy('arrival_history', 'desc')->take(10)->get()->toArray();
         if (isset($busStopHistory[0])) {
             $response = array();
             $response['code'] = 200;
             $response['data'] = $busStopHistory;
         } else {
             $response['code'] = 400;
             $response['data']['msg'] = 'there is no bus visited this bus stop yet';
         }
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = 'internal error, please try again later or contact administrator';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 /**
  * get remaining bus stop information based on route_id of the bus and visited bus stop
  *
  * @param $plat_nomor
  * @return \Illuminate\Http\JsonResponse
  */
 public function remainingBusStop($plat_nomor)
 {
     $busOperationModel = new BusOperation();
     $busRouteModel = new BusRoute();
     $busStopHistoryModel = new BusStopHistory();
     $response = array();
     try {
         if ($plat_nomor == 'all') {
             $listBusOperation = $busOperationModel->get()->toArray();
             $counter = 0;
             $dataContainer = array();
             foreach ($listBusOperation as $busOperation) {
                 $busStopHistory = $busStopHistoryModel->where('plat_nomor', '=', $plat_nomor)->get()->toArray();
                 $visitedBusStopArray = array();
                 for ($i = 0; $i < sizeof($busStopHistory); $i++) {
                     $visitedBusStopArray[$i] = $busStopHistory[$i]['halte_id'];
                 }
                 $busRoute = $busRouteModel->where('rute_id', '=', $busOperation['rute_id'])->whereNotIn('halte_id', $visitedBusStopArray)->with('detailHalte')->get()->toArray();
                 if ($busRoute != null) {
                     $dataContainer[$counter] = $busRoute;
                 } else {
                     $dataContainer[$counter]['msg'] = 'route is not registered in system';
                 }
                 $counter++;
             }
             $response['code'] = 200;
             $response['data'] = $dataContainer;
         } else {
             $busOperation = $busOperationModel->where('plat_nomor', '=', $plat_nomor)->firstOrFail();
             $busStopHistory = $busStopHistoryModel->where('plat_nomor', '=', $plat_nomor)->get()->toArray();
             $visitedBusStopArray = array();
             for ($i = 0; $i < sizeof($busStopHistory); $i++) {
                 $visitedBusStopArray[$i] = $busStopHistory[$i]['halte_id'];
             }
             $busRoute = $busRouteModel->where('rute_id', '=', $busOperation['rute_id'])->whereNotIn('halte_id', $visitedBusStopArray)->with('detailHalte')->get()->toArray();
             $response['code'] = 200;
             if ($busRoute != null) {
                 $response['data'] = $busRoute;
             } else {
                 $response['data']['msg'] = 'route is not registered in system';
             }
         }
     } catch (\Exception $e) {
         $response['code'] = 200;
         $response['data']['msg'] = 'bus is not registered in system';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }