87 lines
2.9 KiB
PHP
87 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Transaksi;
|
|
use App\Models\Booking;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PembayaranController extends Controller
|
|
{
|
|
/**
|
|
* Tampilkan halaman pembayaran berdasarkan ID booking
|
|
*/
|
|
public function create($id_booking)
|
|
{
|
|
$booking = Booking::findOrFail($id_booking);
|
|
|
|
return view('users.pembayaran', compact('booking'));
|
|
}
|
|
|
|
/**
|
|
* Proses data pembayaran (misalnya simpan data atau ubah status booking)
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// Validasi input
|
|
$request->validate([
|
|
'id_booking' => 'required|exists:booking,id_booking',
|
|
'bukti_pembayaran' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
|
'metode_pembayaran' => 'required|in:Transfer,E-Wallet,Tunai',
|
|
]);
|
|
|
|
// Cari booking berdasarkan ID
|
|
$booking = Booking::with('room')->findOrFail($request->id_booking);
|
|
|
|
// Pastikan status booking adalah 'Pending'
|
|
if ($booking->status_booking != 'Pending') {
|
|
return redirect()->route('users.home')->with('error', 'Booking tidak valid atau sudah dibayar');
|
|
}
|
|
|
|
// Proses upload bukti pembayaran ke folder public/assets/users
|
|
$file = $request->file('bukti_pembayaran');
|
|
$destinationPath = public_path('assets/users');
|
|
$filename = time() . '_' . $file->getClientOriginalName();
|
|
$file->move($destinationPath, $filename);
|
|
|
|
// Simpan path untuk database
|
|
$pembayaranPath = 'assets/users/' . $filename;
|
|
|
|
// Hitung tanggal check-in dan check-out
|
|
$checkIn = Carbon::parse($booking->tanggal_checkin);
|
|
$checkOut = Carbon::parse($booking->tanggal_checkout);
|
|
|
|
// Hitung jumlah bulan ngekos
|
|
$selisihBulan = $checkIn->diffInMonths($checkOut);
|
|
if ($checkOut->day > $checkIn->day) {
|
|
$selisihBulan += 1;
|
|
}
|
|
|
|
// Ambil harga per bulan dari relasi room
|
|
$hargaPerBulan = $booking->room->harga;
|
|
|
|
// Hitung total pembayaran berdasarkan jumlah bulan ngekos
|
|
$jumlahPembayaran = $selisihBulan * $hargaPerBulan;
|
|
|
|
// Simpan transaksi ke database
|
|
Transaksi::create([
|
|
'id_booking' => $booking->id_booking,
|
|
'metode_pembayaran' => $request->metode_pembayaran,
|
|
'status_transaksi' => 'Lunas',
|
|
'bukti_pembayaran' => $pembayaranPath,
|
|
'tanggal_pembayaran' => now(),
|
|
'jumlah_pembayaran' => $jumlahPembayaran,
|
|
]);
|
|
|
|
// Update status booking menjadi 'Dibayar'
|
|
$booking->status_booking = 'Pending';
|
|
$booking->save();
|
|
|
|
// Redirect dengan pesan sukses
|
|
return redirect()->route('users.detail-pesanan', ['id_booking' => $booking->id_booking])
|
|
->with('success', 'Pembayaran berhasil diproses!');
|
|
}
|
|
}
|