instalasi-sim-rs/app/Http/Controllers/PatientController.php

179 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Patient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Inertia\Inertia;
use Inertia\Response;
class PatientController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): Response
{
$patients = Patient::orderBy('created_at', 'desc')
->select([
'id',
'medical_record_number',
'name',
'gender',
'nik',
'birth_date',
'birth_place',
'phone_number',
'blood_type',
'religion',
'is_active',
'created_at',
])
->paginate(10)
->through(function ($patient) {
return [
'id' => $patient->id,
'medical_record_number' => $patient->medical_record_number,
'name' => $patient->name,
'gender' => $patient->gender,
'nik' => $patient->nik,
'birth_date' => $patient->birth_date->format('Y-m-d'),
'birth_place' => $patient->birth_place,
'phone_number' => $patient->phone_number,
'blood_type' => $patient->blood_type,
'religion' => $patient->religion,
'is_active' => (bool) $patient->is_active,
'created_at' => $patient->created_at->format('Y-m-d H:i:s'),
];
});
return Inertia::render('patients/index', [
'patients' => $patients,
'status' => session('status'),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): Response
{
return Inertia::render('patients/form', [
'mode' => 'create',
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'gender' => 'required|in:laki-laki,perempuan',
'nik' => 'nullable|string|size:16|unique:m_patient,nik',
'birth_date' => 'required|date',
'birth_place' => 'nullable|string|max:100',
'address' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:15',
'email' => 'nullable|email|max:100|unique:m_patient,email',
'blood_type' => 'nullable|in:A,B,AB,O,unknown',
'religion' => 'nullable|in:islam,kristen,katolik,hindu,budha,konghucu,other',
'marital_status' => 'nullable|in:lajang,menikah,cerai,duda/janda',
'emergency_contact_name' => 'nullable|string|max:100',
'emergency_contact_phone' => 'nullable|string|max:15',
'emergency_contact_relation' => 'nullable|string|max:100',
]);
DB::transaction(function () use ($validated) {
Patient::create($validated);
});
return redirect()->route('patients.index')
->with('status', 'Pasien berhasil ditambahkan');
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Patient $patient): Response
{
return Inertia::render('patients/form', [
'mode' => 'edit',
'patient' => [
'id' => $patient->id,
'medical_record_number' => $patient->medical_record_number,
'name' => $patient->name,
'gender' => $patient->gender,
'nik' => $patient->nik,
'birth_date' => $patient->birth_date->format('Y-m-d'),
'birth_place' => $patient->birth_place,
'address' => $patient->address,
'phone_number' => $patient->phone_number,
'email' => $patient->email,
'blood_type' => $patient->blood_type,
'religion' => $patient->religion,
'marital_status' => $patient->marital_status,
'emergency_contact_name' => $patient->emergency_contact_name,
'emergency_contact_phone' => $patient->emergency_contact_phone,
'emergency_contact_relation' => $patient->emergency_contact_relation,
'is_active' => $patient->is_active,
],
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Patient $patient)
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'gender' => 'required|in:laki-laki,perempuan',
'nik' => [
'nullable',
'string',
'size:16',
Rule::unique('m_patient', 'nik')->ignore($patient->id)
],
'birth_date' => 'required|date',
'birth_place' => 'nullable|string|max:100',
'address' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:15',
'email' => [
'nullable',
'email',
'max:100',
Rule::unique('m_patient', 'email')->ignore($patient->id)
],
'blood_type' => 'nullable|in:A,B,AB,O,unknown',
'religion' => 'nullable|in:islam,kristen,katolik,hindu,budha,konghucu,other',
'marital_status' => 'nullable|in:lajang,menikah,cerai,duda/janda',
'emergency_contact_name' => 'nullable|string|max:100',
'emergency_contact_phone' => 'nullable|string|max:15',
'emergency_contact_relation' => 'nullable|string|max:100',
'is_active' => 'required|boolean',
]);
DB::transaction(function () use ($validated, $patient) {
$patient->update($validated);
});
return redirect()->route('patients.index')
->with('status', 'Data pasien berhasil diperbarui');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Patient $patient)
{
$patient->delete();
return redirect()->route('patients.index')
->with('status', 'Pasien berhasil dihapus');
}
}