Last active
April 13, 2022 09:02
-
-
Save aldiskatel/5f0f35d26e17ff474e0751ef9450f716 to your computer and use it in GitHub Desktop.
Gsn Sukma SKA PU ref PeminjamanKendaraan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-12"> | |
<div class="card"> | |
<div class="card-header">{{ __('Data Peminjaman Kendaraan') }}</div> | |
<div class="card-body"> | |
{{-- flash data --}} | |
@if ($message = Session::get('success')) | |
<div class="alert alert-success alert-block"> | |
<button type="button" class="close" data-dismiss="alert">×</button> | |
<strong>{{ $message }}</strong> | |
</div> | |
@endif | |
<div class="export-excel"> | |
<form action="{{ route('peminjaman-kendaraan.export') }}" target="_blank"> | |
@csrf | |
<h4>Export Ke Excel</h4> | |
<div class="row"> | |
<div class="col-sm-4"> | |
<div class="form-group"> | |
<label for="">Dari</label> | |
<input type="month" my-date="" my-date-format="MM/YYYY" value="" name="dari"> | |
</div> | |
</div> | |
<div class="col-sm-4"> | |
<div class="form-group"> | |
<label for="">sampai</label> | |
<input type="month" my-date="" my-date-format="MM/YYYY" value="" name="sampai"> | |
</div> | |
</div> | |
</div> | |
<button type="submit" class="btn btn-sm btn-primary">Export</button> | |
</form> | |
</div> | |
<hr> | |
{{-- end flas data --}} | |
<div class="table-responsive"> | |
<div class="datatable"> | |
<table class="table table-bordered table-hover" id="dataTable"> | |
<thead> | |
<tr> | |
<th scope="col">NO</th> | |
<th scope="col">Di Pinjam Oleh</th> | |
<th scope="col">Peminjaman</th> | |
<th>Jumlah Karyawan</th> | |
<th scope="col">Jumlah Kendaraan</th> | |
<th>Keperluan</th> | |
<th scope="col">Type</th> | |
<th scope="col">Dari</th> | |
<th scope="col">Sampai</th> | |
<th scope="col">Status</th> | |
<th>Perpanjangan</th> | |
<th>Aksi</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach ($peminjamanKendaraan as $item) | |
<tr> | |
<td>{{ $loop->iteration }}</td> | |
<td>{{ $item->dipinjamOleh->name }}</td> | |
<td style="width: 400px"> | |
@if (!$item->rejected_at || !$item->canceled_at ) | |
@if ($item->driver->count()) | |
<span class="d-block">Driver</span> | |
<ol> | |
@foreach ($item->driver as $driver) | |
<li>{{$driver->nama}}</li> | |
@endforeach | |
</ol> | |
@endif | |
@if ($item->kendaraan->count()) | |
<span class="d-block">kendaraan</span> | |
<ol> | |
@foreach ($item->kendaraan as $kendaraan) | |
<li>{{$kendaraan->nama}}</li> | |
@endforeach | |
</ol> | |
@endif | |
@if ($item->kendaraan->count() && !$item->driver->count()) | |
Belum ada | |
@endif | |
@else | |
- | |
@endif | |
</td> | |
<td>{{ $item->jumlah_karyawan }}</td> | |
<td>{{ $item->kendaraan->count() }}</td> | |
<td>{{ $item->keperluan }}</td> | |
<td>{{ $item->type }}</td> | |
<td>{{ $item->dari->format('d/m/Y H:i') }}</td> | |
<td>{{ $item->sampai->format('d/m/Y H:i') }}</td> | |
<td> | |
{{ $item->status }} | |
</td> | |
<td> | |
@if ($item->perpanjangan()->waitForApproval()->count()) | |
<div> | |
1 menunggu persetujuan | |
</div> | |
<button class="btn btn-primary btn-xs" | |
onclick="showModalApprovalPerpanjang({{$item->perpanjangan()->waitForApproval()->id}}, '{{$item->perpanjangan()->waitForApproval()->tanggal->format('d/M/Y H:i')}}', '{{$item->perpanjangan()->waitForApproval()->alasan }}')">Setujui | |
Perpanjangan</button> | |
@else | |
@if ($item->perpanjangan()->lastApproved()->count()) | |
{{$item->perpanjangan()->lastApproved()->tanggal->format('d/m/Y H:i')}} | |
@endif | |
@endif | |
</td> | |
<td align="center" style="width: 200px"> | |
@if (!$item->approved_at) | |
<button class="btn btn-success btn-xs" | |
onclick="showModalApproval({{$item->id}})">Approve</button> | |
@endif | |
@if (!$item->rejected_at) | |
<button class="btn btn-danger btn-xs" | |
onclick="showModalReject({{$item->id}})">Reject</button> | |
@endif | |
</td> | |
</tr> | |
@endforeach | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<form action="{{ route('peminjaman-kendaraan.approve') }}"> | |
@csrf | |
@method('GEt') | |
<!-- Modal --> | |
<div class="modal fade" id="modal-approval" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" | |
aria-hidden="true"> | |
<div class="modal-dialog modal-lg" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title">Modal Persetujuan</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
<input type="hidden" name="peminjaman_kendaraan_id" id="peminjaman_kendaraan_id"> | |
<p>Silahkan Pilih Driver</p> | |
<div class="form-group"> | |
<label for="">Pilih Driver</label> | |
<select name="driver_id[]" id="driver_id" class="form-control" multiple required></select> | |
</div> | |
<p>Silahkan Pilih Kendaraan yang akan dipinjamkan, kemudian tekan tombol Approve</p> | |
<div class="form-group"> | |
<label for="">Pilih Kendaraan</label> | |
<select name="kendaraan_id[]" id="kendaraan_id" class="form-control" multiple required></select> | |
</div> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> | |
<button type="submit" class="btn btn-primary">Approve</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</form> | |
<form action="{{ route('peminjaman-kendaraan.approve-perpanjangan') }}"> | |
@csrf | |
@method('GEt') | |
<!-- Modal --> | |
<div class="modal fade" id="modal-approval-perpanjangan" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" | |
aria-hidden="true"> | |
<div class="modal-dialog modal-lg" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title">Modal Persetujuan</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
<input type="hidden" name="perpanjang_id" id="perpanjangan_peminjaman_kendaraan_id"> | |
<p>Pengajuan perpanjangan sampai tanggal <span id="tanggal-perpanjangan"></span> dengan alasan | |
"<span id="alasan-perpanjangan"></span>". Apakah admin menyetujui perpanjangan ini ?</p> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> | |
<button type="submit" class="btn btn-danger" name="option" value="reject">Tolak</button> | |
<button type="submit" class="btn btn-primary" name="option" value="approve">Approve</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</form> | |
<!-- Modal --> | |
<form action="{{ route('peminjaman-kendaraan.reject') }}"> | |
@csrf | |
@method('GET') | |
<input type="hidden" name="peminjaman_kendaraan_id" id="reject-peminjaman_kendaraan_id"> | |
<div class="modal fade" id="modal-reject" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" | |
aria-hidden="true"> | |
<div class="modal-dialog" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title">Modal Penolakan</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
<p>Anda akan menolak peminjaman kendaraan ini, apakah anda yakin ?</p> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button> | |
<button type="submit" class="btn btn-danger">Tolak</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</form> | |
@endsection | |
@push('styles') | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css"> | |
@endpush | |
@push('scripts') | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap-select.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> | |
<script> | |
$('#kendaraan_id').selectpicker(); | |
function showModalApproval(id){ | |
$('#modal-approval').modal('show'); | |
$('#peminjaman_kendaraan_id').val(id); | |
getKendaraanTersedia(id); | |
getDriverTersedia(id); | |
} | |
function showModalApprovalPerpanjang(id, tanggal, alasan){ | |
$('#modal-approval-perpanjangan').modal('show'); | |
$('#perpanjangan_peminjaman_kendaraan_id').val(id); | |
$('#tanggal-perpanjangan').html(tanggal); | |
$('#alasan-perpanjangan').html(alasan); | |
} | |
function showModalReject(id){ | |
$('#modal-reject').modal('show'); | |
$('#reject-peminjaman_kendaraan_id').val(id); | |
} | |
function getKendaraanTersedia(id){ | |
var url = '{{ route('peminjaman-kendaraan.get-kendaraan-tersedia') }}'; | |
axios.get(url, { | |
params: { | |
id:id | |
} | |
}) | |
.then(function (response) { | |
console.log(response); | |
$('#kendaraan_id').find('option').remove(); | |
$.each(response.data, function (indexInArray, valueOfElement) { | |
$('#kendaraan_id').append(`<option value="`+valueOfElement.id+`">`+valueOfElement.nama+` - `+valueOfElement.nopol+`</option>`); | |
}); | |
$('#kendaraan_id').selectpicker('refresh'); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
.then(function () { | |
// always executed | |
}); | |
} | |
function getDriverTersedia(id){ | |
var url = '{{ route('peminjaman-kendaraan.get-driver-tersedia') }}'; | |
axios.get(url, { | |
params: { | |
id:id | |
} | |
}) | |
.then(function (response) { | |
console.log(response); | |
$('#driver_id').find('option').remove(); | |
$.each(response.data, function (indexInArray, valueOfElement) { | |
$('#driver_id').append(`<option value="`+valueOfElement.id+`">`+valueOfElement.nama+`</option>`); | |
}); | |
$('#driver_id').selectpicker('refresh'); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
.then(function () { | |
// always executed | |
}); | |
} | |
</script> | |
@endpush |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreateKendaraanTables extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('master_peminjaman_kendaraan_kendaraan', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('nopol')->unique(); | |
$table->string('nama'); | |
$table->string('kapasitas'); | |
$table->timestamps(); | |
$table->softDeletes(); | |
}); | |
Schema::create('master_peminjaman_kendaraan_periode_peminjaman', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('nama')->unique(); | |
$table->time('dari'); | |
$table->time('sampai'); | |
$table->timestamps(); | |
$table->softDeletes(); | |
}); | |
Schema::create('peminjaman_kendaraan', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->enum('type', ['perhari', 'perjam']); | |
$table->bigInteger('dipinjam_oleh')->unsigned(); | |
$table->bigInteger('periode_peminjaman_id')->unsigned()->nullable(); | |
$table->timestamp('dari')->nullable(); | |
$table->timestamp('sampai')->nullable(); | |
$table->string('keperluan')->nullable(); | |
$table->string('jumlah_karyawan')->nullable(); | |
$table->string('jumlah_kendaraan')->nullable(); | |
$table->timestamp('approved_at')->nullable(); | |
$table->timestamp('rejected_at')->nullable(); | |
$table->bigInteger('approved_by')->unsigned()->nullable(); | |
$table->bigInteger('rejected_by')->unsigned()->nullable(); | |
$table->timestamps(); | |
$table->softDeletes(); | |
$table->foreign('periode_peminjaman_id')->references('id')->on('master_peminjaman_kendaraan_periode_peminjaman')->onDelete('cascade'); | |
$table->foreign('dipinjam_oleh')->references('id')->on('users')->onDelete('cascade'); | |
$table->foreign('approved_by')->references('id')->on('users')->onDelete('cascade'); | |
$table->foreign('rejected_by')->references('id')->on('users')->onDelete('cascade'); | |
}); | |
Schema::create('peminjaman_kendaraan_kendaraan', function (Blueprint $table) { | |
$table->bigInteger('peminjaman_kendaraan_id')->unsigned(); | |
$table->bigInteger('kendaraan_id')->unsigned(); | |
$table->foreign('peminjaman_kendaraan_id')->references('id')->on('peminjaman_kendaraan')->onDelete('cascade'); | |
$table->foreign('kendaraan_id')->references('id')->on('master_peminjaman_kendaraan_kendaraan')->onDelete('cascade'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('peminjaman_kendaraan_kendaraan'); | |
Schema::dropIfExists('peminjaman_kendaraan'); | |
Schema::dropIfExists('master_peminjaman_kendaraan_periode_peminjaman'); | |
Schema::dropIfExists('master_peminjaman_kendaraan_kendaraan'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreatePeminjamanKendaraanPerpanjanganTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('peminjaman_kendaraan_perpanjangan', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->bigInteger('peminjaman_kendaraan_id'); | |
$table->string('alasan')->nullable(); | |
$table->timestamp('tanggal')->nullable(); | |
$table->timestamp('approved_at')->nullable(); | |
$table->bigInteger('approved_by')->nullable(); | |
$table->timestamp('rejected_at')->nullable(); | |
$table->bigInteger('rejected_by')->nullable(); | |
$table->timestamps(); | |
$table->softDeletes(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('peminjaman_kendaraan_perpanjangan'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use App\Models\Master\PeminjamanKendaraan\Kendaraan; | |
use App\Models\Master\PeminjamanKendaraan\PeriodePeminjaman; | |
use App\User; | |
use Carbon\Carbon; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use Illuminate\Support\Facades\Auth; | |
class PeminjamanKendaraan extends Model | |
{ | |
use SoftDeletes; | |
protected $table = 'peminjaman_kendaraan'; | |
protected $dates = [ | |
'dari', | |
'sampai' | |
]; | |
protected $appends = [ | |
'status' | |
]; | |
public function dipinjamOleh() | |
{ | |
return $this->belongsTo(User::class, 'dipinjam_oleh'); | |
} | |
public function periodePeminjaman() | |
{ | |
return $this->belongsTo(PeriodePeminjaman::class, 'periode_peminjaman_id'); | |
} | |
public function kendaraan() | |
{ | |
return $this->belongsToMany(Kendaraan::class, 'peminjaman_kendaraan_kendaraan', 'peminjaman_kendaraan_id', 'kendaraan_id'); | |
} | |
public function driver() | |
{ | |
return $this->belongsToMany(Driver::class, 'peminjaman_kendaraan_driver', 'peminjaman_kendaraan_id', 'driver_id'); | |
} | |
public function perpanjangan() | |
{ | |
return $this->hasOne(PeminjamanKendaraanPerpanjangan::class, 'peminjaman_kendaraan_id'); | |
} | |
public static function approve($request) | |
{ | |
$data = self::find($request->peminjaman_kendaraan_id); | |
$data->rejected_at = null; | |
$data->rejected_by = null; | |
$data->approved_at = Carbon::now(); | |
$data->approved_by = Auth::user()->id; | |
$data->save(); | |
$data->kendaraan()->sync($request->kendaraan_id); | |
$data->driver()->sync($request->driver_id); | |
return $data; | |
} | |
public static function reject($request) | |
{ | |
$data = self::find($request->peminjaman_kendaraan_id); | |
$data->approved_at = null; | |
$data->approved_by = null; | |
$data->rejected_at = Carbon::now(); | |
$data->rejected_by = Auth::user()->id; | |
$data->save(); | |
return $data; | |
} | |
public static function approvePerpanjangan($request) | |
{ | |
$perpanjangan = PeminjamanKendaraanPerpanjangan::find($request->perpanjang_id); | |
if ($request->option == 'approve') { | |
$perpanjangan->approved_at = Carbon::now(); | |
$perpanjangan->approved_by = Auth::user()->id; | |
} else { | |
$perpanjangan->approved_at = Carbon::now(); | |
$perpanjangan->approved_by = Auth::user()->id; | |
} | |
$perpanjangan->save(); | |
return $perpanjangan; | |
} | |
public static function mustFinishedNow() | |
{ | |
$data = self::whereNotNull('approved_at')->where('sampai', '<', Carbon::now()) | |
->orWhereHas('perpanjangan', function ($q) { | |
$q->whereNotNull('approved_at')->where('tanggal', '<', Carbon::now()); | |
}) | |
->update([ | |
'finished_at' => Carbon::now() | |
]); | |
return $data; | |
} | |
public function getStatusAttribute() | |
{ | |
if ($this->canceled_at) { | |
return 'Dibatalkan'; | |
} elseif ($this->rejected_at) { | |
return 'Tidak dapat diproses'; | |
} elseif (!$this->approved_at && !$this->rejected_at) { | |
return 'Menunggu Persetujuan'; | |
} elseif ($this->perpanjangan()->whereNull('approved_at')->first()) { | |
return 'Menunggu persetujuan perpanjangan'; | |
} elseif ($this->finished_at) { | |
return 'Selesai'; | |
} elseif ($this->perpanjangan()->whereNotNull('approved_at')->whereDate('tanggal', '>=', Carbon::now())->count()) { | |
return 'Diperpanjang'; | |
} elseif ($this->approved_at) { | |
return 'Disetujui'; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Exports\PeminjamanKendaraanExport; | |
use App\Helpers\FCM; | |
use App\Models\Master\PeminjamanKendaraan\Kendaraan; | |
use App\Models\PeminjamanKendaraan; | |
use Illuminate\Http\Request; | |
use App\Models\Driver; | |
use App\Notification; | |
use Maatwebsite\Excel\Facades\Excel; | |
class PeminjamanKendaraanController extends Controller | |
{ | |
public function index() | |
{ | |
$data['judul'] = 'Peminjaman Kendaraan'; | |
$data['peminjamanKendaraan'] = PeminjamanKendaraan::orderBy('updated_at', 'desc')->get(); | |
return view('peminjaman-kendaraan.index', $data); | |
} | |
public function getKendaraanTersedia(Request $request) | |
{ | |
$data = Kendaraan::getKendaraanTersedia($request->id); | |
return response($data); | |
} | |
public function getDriverTersedia(Request $request) | |
{ | |
$data = Driver::getDriverTersedia($request->id); | |
return response($data); | |
} | |
public function approve(Request $request) | |
{ | |
$data = PeminjamanKendaraan::approve($request); | |
Notification::createData([ | |
'body' => 'Admin telah menyetujui peminjaman mobil anda untuk tanggal ' . $data->dari->format('d/m/Y'), | |
'user_id' => $data->dipinjam_oleh | |
]); | |
FCM::send($data->dipinjam_oleh, [ | |
'body' => 'Permintaan peminjaman kendaraan disetujui' | |
]); | |
return redirect()->route('peminjaman-kendaraan.index')->with(['success' => 'Berhasil menyetujui peminjaman kendaraan']); | |
} | |
public function reject(Request $request) | |
{ | |
$data = PeminjamanKendaraan::reject($request); | |
Notification::createData([ | |
'body' => 'Admin menyatakan peminjaman mobil anda belum dapat diproses untuk tanggal ' . $data->dari->format('d/m/Y') . ' karena penuh. Silakan menghubungi Tim MI untuk informasi lanjut.', | |
'user_id' => $data->dipinjam_oleh | |
]); | |
FCM::send($data->dipinjam_oleh, [ | |
'body' => 'Admin menyatakan peminjaman mobil anda belum dapat diproses untuk tanggal ' . $data->dari->format('d/m/Y') . ' karena penuh. Silakan menghubungi Tim MI untuk informasi lanjut.', | |
]); | |
return redirect()->route('peminjaman-kendaraan.index')->with(['success' => 'Berhasil menolak peminjaman kendaraan']); | |
} | |
public function approvePerpanjangan(Request $request) | |
{ | |
$data = PeminjamanKendaraan::approvePerpanjangan($request); | |
Notification::createData([ | |
'body' => 'Admin telah menyetujui perpanjang peminjaman mobil anda untuk tanggal ' . $data->tanggal->format('d/m/Y'), | |
'user_id' => $data->peminjaman->dipinjam_oleh | |
]); | |
FCM::send($data->peminjaman->dipinjam_oleh, [ | |
'body' => 'Admin telah menyetujui perpanjang peminjaman mobil anda untuk tanggal ' . $data->tanggal->format('d/m/Y'), | |
]); | |
return redirect()->route('peminjaman-kendaraan.index')->with(['success' => 'Berhasil menyetujui perminatann perpanjang peminjaman kendaraan']); | |
} | |
public function export(Request $request) | |
{ | |
return Excel::download(new PeminjamanKendaraanExport($request), 'peminjaman-kendaraan.xlsx'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment