97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Sewa;
|
|
use App\Models\Setting;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class RiwayatController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$sewas = Sewa::with(['paket', 'kota'])
|
|
->where('user_id', Auth::id())
|
|
->where('status', '!=', 'draft')
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
return view('riwayat', compact('sewas'));
|
|
}
|
|
|
|
public function updateStatus(Request $request, $id)
|
|
{
|
|
if (Auth::user()->tipe_pengguna !== 'admin') {
|
|
return redirect()->route('dashboard')->with('error', 'Akses ditolak.');
|
|
}
|
|
|
|
$request->validate([
|
|
'status' => 'required|in:completed'
|
|
]);
|
|
|
|
$sewa = Sewa::findOrFail($id);
|
|
|
|
if ($sewa->status !== 'confirmed') {
|
|
return back()->with('error', 'Status hanya bisa diubah jika saat ini adalah confirmed.');
|
|
}
|
|
|
|
$sewa->status = $request->status;
|
|
$sewa->save();
|
|
|
|
return back()->with('success', 'Status berhasil diperbarui menjadi completed.');
|
|
}
|
|
|
|
public function hapus($id)
|
|
{
|
|
$sewa = Sewa::findOrFail($id);
|
|
|
|
if ($sewa->user_id != Auth::id()) {
|
|
return redirect()->route('riwayat')
|
|
->with('error', 'Anda tidak memiliki akses untuk menghapus pesanan ini.');
|
|
}
|
|
|
|
if (!in_array($sewa->status, ['completed', 'dibatalkan'])) {
|
|
return redirect()->route('riwayat')
|
|
->with('error', 'Hanya pesanan yang sudah selesai atau dibatalkan yang dapat dihapus.');
|
|
}
|
|
|
|
if ($sewa->bukti_pembayaran) {
|
|
Storage::disk('public')->delete($sewa->bukti_pembayaran);
|
|
}
|
|
if ($sewa->foto_jaminan) {
|
|
Storage::disk('public')->delete($sewa->foto_jaminan);
|
|
}
|
|
|
|
$sewa->delete();
|
|
|
|
return redirect()->route('riwayat')
|
|
->with('success', 'Riwayat pesanan berhasil dihapus.');
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$sewa = Sewa::with(['paket', 'kota'])->findOrFail($id);
|
|
|
|
// Pastikan hanya user yang berhak yang bisa akses
|
|
if ($sewa->user_id != Auth::id()) {
|
|
return response()->json(['error' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
$nomor_rekening = Setting::where('key', 'nomor_rekening')->first()->value ?? null;
|
|
|
|
return response()->json([
|
|
'tanggal_pembayaran' => $sewa->tanggal_pembayaran,
|
|
'status_pembayaran' => $sewa->status,
|
|
'lokasi' => $sewa->lokasi,
|
|
'kota' => $sewa->kota,
|
|
'ongkir' => $sewa->ongkir,
|
|
'bukti_pembayaran' => $sewa->bukti_pembayaran,
|
|
'foto_jaminan' => $sewa->foto_jaminan,
|
|
'jenis_jaminan' => $sewa->jenis_jaminan,
|
|
'nomor_rekening' => $nomor_rekening,
|
|
]);
|
|
}
|
|
}
|