309 lines
8.2 KiB
PHP
309 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Penghuni;
|
|
use App\Models\Rumah;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
// =========================
|
|
// BERANDA USER
|
|
// =========================
|
|
public function index()
|
|
{
|
|
$penghuni = Penghuni::with('rumah')
|
|
->where('email', Auth::user()->email)
|
|
->first();
|
|
|
|
return view('user.home', compact('penghuni'));
|
|
}
|
|
|
|
// =========================
|
|
// PROFIL
|
|
// =========================
|
|
public function profil()
|
|
{
|
|
$penghuni = Penghuni::where('email', Auth::user()->email)->first();
|
|
return view('user.profile.edit', compact('penghuni'));
|
|
}
|
|
|
|
public function updateProfil(Request $request)
|
|
{
|
|
$penghuni = Penghuni::where('email', Auth::user()->email)->first();
|
|
|
|
$penghuni->update([
|
|
'nama' => $request->nama,
|
|
'telepon' => $request->telepon,
|
|
'alamat' => $request->alamat,
|
|
]);
|
|
|
|
return back()->with('success', 'Profil berhasil diupdate');
|
|
}
|
|
|
|
// =========================
|
|
// RUMAH
|
|
// =========================
|
|
public function rumah()
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if (!$user) {
|
|
return redirect('/login');
|
|
}
|
|
|
|
$penghuni = Penghuni::with('rumah')
|
|
->where('email', $user->email)
|
|
->first();
|
|
|
|
// 🔥 TAMBAHAN INI (WAJIB)
|
|
$rumahList = Rumah::all();
|
|
|
|
return view('user.rumah', compact('penghuni', 'rumahList'));
|
|
}
|
|
// =========================
|
|
// 🔥 TAMBAHAN: PILIH RUMAH
|
|
// =========================
|
|
public function pilihRumah($id)
|
|
{
|
|
$rumah = Rumah::findOrFail($id);
|
|
|
|
// kalau sudah terisi
|
|
if ($rumah->status == 'Terisi') {
|
|
return back()->with('error', 'Rumah sudah terisi');
|
|
}
|
|
|
|
// cari penghuni berdasarkan email user login
|
|
$penghuni = Penghuni::where('email', Auth::user()->email)->first();
|
|
|
|
// kalau belum ada, buat otomatis
|
|
if (!$penghuni) {
|
|
$penghuni = Penghuni::create([
|
|
'nama' => Auth::user()->name,
|
|
'email' => Auth::user()->email,
|
|
'status' => 'Aktif'
|
|
]);
|
|
}
|
|
|
|
// update rumah ke penghuni
|
|
$penghuni->update([
|
|
'rumah_id' => $rumah->id
|
|
]);
|
|
|
|
// update status rumah
|
|
$rumah->update([
|
|
'status' => 'Terisi'
|
|
]);
|
|
|
|
return back()->with('success', 'Berhasil memilih rumah');
|
|
}
|
|
|
|
// // =========================
|
|
// // IURAN
|
|
// // =========================
|
|
// public function iuran()
|
|
// {
|
|
// return view('user.iuran');
|
|
// }
|
|
|
|
// // =========================
|
|
// // PENGADUAN
|
|
// // =========================
|
|
// public function pengaduan()
|
|
// {
|
|
// return view('user.pengaduan');
|
|
// }
|
|
|
|
// public function storePengaduan(Request $request)
|
|
// {
|
|
// // sementara dummy dulu
|
|
// return back()->with('success', 'Pengaduan berhasil dikirim');
|
|
// }
|
|
|
|
// // =========================
|
|
// // PENGUMUMAN
|
|
// // =========================
|
|
// public function pengumuman()
|
|
// {
|
|
// return view('user.pengumuman');
|
|
// }
|
|
|
|
public function simpanRumah(Request $request)
|
|
{
|
|
|
|
$penghuni = Penghuni::where('email', Auth::user()->email)->first();
|
|
|
|
if (!$penghuni) {
|
|
return redirect()->route('user.home')
|
|
->with('error', 'Isi data penghuni dulu!');
|
|
}
|
|
|
|
// kalau belum ada rumah → create
|
|
$rumah = Rumah::updateOrCreate(
|
|
['id' => $request->rumah_id],
|
|
[
|
|
'blok' => $request->blok,
|
|
'no_rumah' => $request->no_rumah,
|
|
'status' => 'Terisi',
|
|
'luas_tanah' => $request->luas_tanah,
|
|
'harga' => $request->harga,
|
|
'keterangan' => $request->keterangan,
|
|
]
|
|
);
|
|
|
|
$penghuni = Penghuni::where('email', Auth::user()->email)->first();
|
|
|
|
if (!$penghuni) {
|
|
$penghuni = Penghuni::create([
|
|
'nama' => Auth::user()->name,
|
|
'email' => Auth::user()->email,
|
|
'status' => 'Aktif'
|
|
]);
|
|
}
|
|
|
|
$penghuni->update([
|
|
'rumah_id' => $rumah->id
|
|
]);
|
|
|
|
return redirect()->route('user.rumah')
|
|
->with('success', 'Rumah berhasil dipilih');
|
|
}
|
|
|
|
public function simpanPenghuni(Request $request)
|
|
{
|
|
// 🔥 VALIDASI DINAMIS
|
|
$rules = [
|
|
'nama' => 'required',
|
|
'no_ktp' => 'required',
|
|
'status_huni' => 'required',
|
|
];
|
|
|
|
if ($request->status_huni == 'Kontrak') {
|
|
$rules['tanggal_keluar'] = 'required|date';
|
|
}
|
|
|
|
$request->validate($rules);
|
|
|
|
Penghuni::create([
|
|
'nama' => $request->nama,
|
|
'no_ktp' => $request->no_ktp,
|
|
'email' => Auth::user()->email,
|
|
'telepon' => $request->telepon,
|
|
'alamat' => $request->alamat,
|
|
'status_huni' => $request->status_huni,
|
|
'tanggal_masuk' => $request->tanggal_masuk ?? now(),
|
|
'tanggal_keluar' => $request->status_huni == 'Kontrak'
|
|
? $request->tanggal_keluar
|
|
: null,
|
|
'status' => 'Aktif'
|
|
]);
|
|
|
|
return redirect()->route('user.home')
|
|
->with('success', 'Data penghuni berhasil disimpan');
|
|
}
|
|
|
|
public function ambilRumah(Request $request)
|
|
{
|
|
$rumah = Rumah::findOrFail($request->rumah_id);
|
|
|
|
// ❌ jika sudah terisi
|
|
if ($rumah->status == 'Terisi') {
|
|
return back()->with('error', 'Rumah sudah diambil!');
|
|
}
|
|
|
|
// ambil penghuni login
|
|
$penghuni = Penghuni::where('email', Auth::user()->email);
|
|
|
|
if (!$penghuni) {
|
|
return back()->with('error', 'Isi data penghuni dulu!');
|
|
}
|
|
|
|
// update rumah jadi terisi
|
|
$rumah->update([
|
|
'status' => 'Terisi'
|
|
]);
|
|
|
|
// hubungkan ke penghuni
|
|
$penghuni->update([
|
|
'rumah_id' => $rumah->id
|
|
]);
|
|
|
|
return redirect()->route('user.rumah')
|
|
->with('success', 'Berhasil mengambil rumah!');
|
|
}
|
|
|
|
public function storeRumah(Request $request)
|
|
{
|
|
$request->validate([
|
|
'blok' => 'required',
|
|
'no_rumah' => 'required',
|
|
'status' => 'required',
|
|
'foto' => 'nullable|image|mimes:jpg,png,jpeg|max:2048'
|
|
]);
|
|
|
|
// 🔥 HANDLE FOTO
|
|
$namaFile = null;
|
|
|
|
if ($request->hasFile('foto')) {
|
|
$file = $request->file('foto');
|
|
$namaFile = time() . '.' . $file->getClientOriginalExtension();
|
|
|
|
$file->move(public_path('foto_rumah'), $namaFile);
|
|
}
|
|
|
|
Rumah::create([
|
|
'blok' => $request->blok,
|
|
'no_rumah' => $request->no_rumah,
|
|
'status' => $request->status,
|
|
'luas_tanah' => $request->luas_tanah,
|
|
'harga' => $request->harga,
|
|
'keterangan' => $request->keterangan,
|
|
'foto' => $namaFile // 🔥 INI WAJIB ADA
|
|
]);
|
|
|
|
return back()->with('success', 'Rumah berhasil ditambahkan');
|
|
}
|
|
|
|
public function updateRumah(Request $request, $id)
|
|
{
|
|
$rumah = \App\Models\Rumah::findOrFail($id);
|
|
|
|
$rumah->update([
|
|
'blok' => $request->blok,
|
|
'no_rumah' => $request->no_rumah,
|
|
'status' => $request->status,
|
|
'luas_tanah' => $request->luas_tanah,
|
|
'harga' => $request->harga,
|
|
'keterangan' => $request->keterangan,
|
|
]);
|
|
|
|
return back()->with('success', 'Berhasil update rumah');
|
|
}
|
|
|
|
// =========================
|
|
// UPLOAD PEMBAYARAN
|
|
// =========================
|
|
// public function uploadPembayaran()
|
|
// {
|
|
// return view('user.upload_pembayaran');
|
|
// }
|
|
|
|
// // =========================
|
|
// // STATUS PEMBAYARAN
|
|
// // =========================
|
|
// public function statusPembayaran()
|
|
// {
|
|
// return view('user.status_pembayaran');
|
|
// }
|
|
|
|
// // =========================
|
|
// // STATUS PENGADUAN
|
|
// // =========================
|
|
// public function statusPengaduan()
|
|
// {
|
|
// return view('user.status_pengaduan');
|
|
// }
|
|
}
|