/**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $pengguna = Pengguna::find($id);
     if (!$pengguna) {
         return "Not Found";
     }
     $inBooking = Booking::where('id_pembooking', '=', $id)->count();
     $inTransaksi = Transaksi::where('id_peminjam', '=', $id)->count();
     if ($inBooking > 0 || $inTransaksi > 0) {
         return "Tidak dapat menghapus";
     } else {
         $pengguna->delete();
         return 1;
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $rules = array('jenis_barang' => 'required', 'id_pembooking' => 'required|integer', 'waktu_booking_mulai' => 'required', 'waktu_booking_selesai' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return $validator->messages()->toJson();
     } else {
         if (!checkDateTime(Input::get('waktu_booking_mulai'))) {
             return "Waktu booking mulai tidak valid";
         }
         if (!checkDateTime(Input::get('waktu_booking_selesai'))) {
             return "Waktu booking selesai tidak valid";
         }
         if (strtotime(Input::get('waktu_booking_mulai')) >= strtotime(Input::get('waktu_booking_selesai'))) {
             return "Waktu booking mulai > waktu booking selesai";
         }
         /* cek id pembooking */
         $pengguna = Pengguna::find(Input::get('id_pembooking'));
         if (!$pengguna) {
             return "ID pengguna tidak ditemukan";
         }
         $booking = Booking::find($id);
         if (!$booking) {
             return "Not Found";
         }
         /* cek apakah jenis peralatan berubah
          * jika berubah, cari alat baru
          */
         $id_barang_old = $booking->id_barang;
         $barang_old = Peralatan::find($id_barang_old);
         $jenis_barang_old = $barang_old->jenis;
         $selected_id = -1;
         if (strcmp(Input::get('jenis_barang'), $jenis_barang_old) == 0) {
             $selected_id = $id_barang_old;
         } else {
             //cari peralatan baru jika jenis berubah
             $alat_sesuai_jenis = Peralatan::where('jenis', '=', Input::get('jenis_barang'))->get();
             $input_booking_mulai_time = strtotime(Input::get('waktu_booking_mulai'));
             $input_booking_selesai_time = strtotime(Input::get('waktu_booking_selesai'));
             foreach ($alat_sesuai_jenis as $alat) {
                 $available = true;
                 /* Cek di tabel booking */
                 $booking_of_alat = Booking::where('id_barang', '=', $alat->id)->get();
                 foreach ($booking_of_alat as $booking) {
                     $booking_mulai_time = strtotime($booking->waktu_booking_mulai);
                     $booking_selesai_time = strtotime($booking->waktu_booking_selesai);
                     if ($input_booking_mulai_time > $booking_mulai_time && $input_booking_mulai_time < $booking_selesai_time || $input_booking_selesai_time > $booking_mulai_time && $input_booking_selesai_time < $booking_selesai_time || $booking_mulai_time > $input_booking_mulai_time && $booking_mulai_time < $input_booking_selesai_time || $booking_selesai_time > $input_booking_mulai_time && $booking_selesai_time < $input_booking_selesai_time) {
                         $available = false;
                     }
                 }
                 /* Cek di tabel transaksi */
                 if ($available) {
                     $transaksi_of_alat = Transaksi::where('id_barang', '=', $alat->id)->get();
                     foreach ($transaksi_of_alat as $transaksi) {
                         $transaksi_pinjam_time = $transaksi->waktu_pinjam;
                         $transaksi_rencana_kembali_time = $transaksi->waktu_rencana_kembali;
                         if ($input_booking_mulai_time > $transaksi_pinjam_time && $input_booking_mulai_time < $transaksi_rencana_kembali_time || $input_booking_selesai_time > $transaksi_pinjam_time && $input_booking_selesai_time < $transaksi_rencana_kembali_time || $transaksi_pinjam_time > $input_booking_mulai_time && $transaksi_pinjam_time < $input_booking_selesai_time || $transaksi_rencana_kembali_time > $input_booking_mulai_time && $transaksi_rencana_kembali_time < $input_booking_selesai_time) {
                             $available = false;
                         }
                     }
                 }
                 if ($available) {
                     $selected_id = $alat->id;
                 }
             }
         }
         if ($selected_id < 1) {
             return "Tidak ada alat tersedia";
         } else {
             /* cek tanggal */
             if (strtotime(Input::get('waktu_booking_selesai')) <= strtotime(Input::get('waktu_booking_mulai'))) {
                 return "Tanggal tidak valid";
             } else {
                 // store
                 $booking->id_barang = $selected_id;
                 $booking->id_pembooking = Input::get('id_pembooking');
                 $booking->waktu_booking_mulai = Input::get('waktu_booking_mulai');
                 $booking->waktu_booking_selesai = Input::get('waktu_booking_selesai');
                 $booking->save();
                 return $booking->id;
             }
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $rules = array('jenis_barang' => 'required', 'id_peminjam' => 'required|integer', 'waktu_pinjam' => 'required', 'waktu_rencana_kembali' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     // process the update
     if ($validator->fails()) {
         return $validator->messages()->toJson();
     } else {
         //cek id pengguna
         $pengguna = Pengguna::find(Input::get('id_peminjam'));
         if (!$pengguna) {
             return "ID pengguna tidak ditemukan";
         }
         // update
         $transaksi = Transaksi::find($id);
         if (!$transaksi) {
             return "Not Found";
         }
         if (!checkDateTime(Input::get('waktu_pinjam'))) {
             return "Waktu pinjam tidak valid";
         }
         if (!checkDateTime(Input::get('waktu_rencana_kembali'))) {
             return "Waktu rencana kembali tidak valid";
         }
         if (strtotime(Input::get('waktu_rencana_kembali')) <= strtotime(Input::get('waktu_pinjam'))) {
             return "Waktu tidak valid";
         }
         if (Input::get('waktu_kembali') != null) {
             if (!checkDateTime(Input::get('waktu_kembali'))) {
                 return "Waktu kembali tidak valid";
             }
             if (strtotime(Input::get('waktu_kembali')) <= strtotime(Input::get('waktu_pinjam'))) {
                 return "Waktu tidak valid";
             }
         }
         $id_barang_old = $transaksi->id_barang;
         $barang_old = Peralatan::find($id_barang_old);
         $jenis_barang_old = $barang_old->jenis;
         $selected_id = -1;
         if (strcmp(Input::get('jenis_barang'), $jenis_barang_old) == 0) {
             $selected_id = $id_barang_old;
         } else {
             // cari peralatan baru
             $waktu_pinjam_time = strtotime(Input::get('waktu_pinjam'));
             $waktu_rencana_kembali_time = strtotime(Input::get('waktu_rencana_kembali'));
             foreach ($alat_sesuai_jenis as $alat) {
                 $available = true;
                 if (strcmp($alat->status, "Baik") != 0 || strcmp($alat->ketersediaan, "Tersedia") != 0) {
                     $available = false;
                     break;
                 }
                 //cek di data booking
                 $booking_of_alat = Booking::where('id_barang', '=', $alat->id)->get();
                 foreach ($booking_of_alat as $booking) {
                     $booking_mulai_time = strtotime($booking->waktu_booking_mulai);
                     $booking_selseai_time = strtotime($booking->waktu_booking_selesai);
                     if ($waktu_pinjam_time > $booking_mulai_time && $waktu_pinjam_time < $booking_selesai_time || $waktu_rencana_kembali_time > $booking_mulai_time && $waktu_rencana_kembali_time < $booking_selesai_time || $booking_mulai_time > $waktu_pinjam_time && $booking_mulai_time < $waktu_rencana_kembali_time || $booking_selesai_time > $waktu_pinjam_time && $booking_selesai_time < $waktu_rencana_kembali_time) {
                         $available = false;
                     }
                 }
                 if ($available) {
                     $selected_id = $alat->id;
                 }
             }
         }
         $_waktu_kembali = "";
         if (strcmp($transaksi->waktu_kembali, "0000-00-00 00:00:00") != 0) {
             $_waktu_kembali = Input::get('waktu_kembali');
         }
         if ($selected_id < 1) {
             return "Tidak ada alat tersedia";
         } else {
             $transaksi->id_barang = $selected_id;
             $transaksi->id_peminjam = Input::get('id_peminjam');
             $transaksi->waktu_pinjam = Input::get('waktu_pinjam');
             $transaksi->waktu_rencana_kembali = Input::get('waktu_rencana_kembali');
             $transaksi->waktu_kembali = $_waktu_kembali;
             $transaksi->save();
             return $transaksi->id;
         }
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param \Illuminate\Http\Request $request        	
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     Pengguna::create($request::all());
     return response()->json(array('success' => true));
 }