212 lines
7.7 KiB
PHP
212 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Insurance;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Validation\Rule;
|
|
use Inertia\Inertia;
|
|
|
|
class InsuranceController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$insurances = Insurance::orderBy('created_at', 'desc')
|
|
->select([
|
|
'id',
|
|
'code',
|
|
'name',
|
|
'phone_number',
|
|
'contact_person',
|
|
'coverage_percentage',
|
|
'agreement_start_date',
|
|
'agreement_end_date',
|
|
'is_active',
|
|
'created_at',
|
|
])
|
|
->paginate(10)
|
|
->through(function ($insurance) {
|
|
return [
|
|
'id' => $insurance->id,
|
|
'code' => $insurance->code,
|
|
'name' => $insurance->name,
|
|
'phone_number' => $insurance->phone_number,
|
|
'contact_person' => $insurance->contact_person,
|
|
'coverage_percentage' => (float) $insurance->coverage_percentage,
|
|
'agreement_period' => $insurance->agreement_start_date
|
|
? $insurance->agreement_start_date->format('d/m/Y') . ' - '
|
|
. ($insurance->agreement_end_date ? $insurance->agreement_end_date->format('d/m/Y') : 'Sekarang')
|
|
: '-',
|
|
'is_active' => (bool) $insurance->is_active,
|
|
'created_at' => $insurance->created_at->format('d/m/Y H:i'),
|
|
];
|
|
});
|
|
|
|
return Inertia::render('insurances/index', [
|
|
'insurances' => $insurances,
|
|
'status' => session('status'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return Inertia::render('insurances/form', [
|
|
'mode' => 'create',
|
|
'defaults' => [
|
|
'coverage_percentage' => 100.00,
|
|
'is_active' => true
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'code' => 'required|string|max:20|unique:m_insurance,code',
|
|
'name' => 'required|string|max:100',
|
|
'address' => 'nullable|string|max:255',
|
|
'phone_number' => 'nullable|string|max:15',
|
|
'email' => 'nullable|email|max:100',
|
|
'contact_person' => 'nullable|string|max:100',
|
|
'contact_person_phone' => 'nullable|string|max:15',
|
|
'coverage_percentage' => 'required|numeric|between:0,100',
|
|
'coverage_description' => 'nullable|string',
|
|
'agreement_details' => 'nullable|string',
|
|
'agreement_start_date' => 'nullable|date',
|
|
'agreement_end_date' => 'nullable|date|after_or_equal:agreement_start_date',
|
|
'agreement_file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048',
|
|
'is_active' => 'required|boolean'
|
|
]);
|
|
|
|
DB::transaction(function () use ($validated, $request) {
|
|
$insuranceData = $validated;
|
|
|
|
// Handle file upload
|
|
if ($request->hasFile('agreement_file')) {
|
|
$file = $request->file('agreement_file');
|
|
$fileName = 'agreement_' . time() . '_' . $validated['code'] . '.' . $file->getClientOriginalExtension();
|
|
|
|
// Simpan file ke folder public/insurance_agreements
|
|
$path = $file->storeAs('insurance_agreements', $fileName, 'public');
|
|
|
|
$insuranceData['agreement_file_path'] = $path;
|
|
}
|
|
|
|
Insurance::create($insuranceData);
|
|
});
|
|
|
|
return redirect()->route('insurances.index')
|
|
->with('status', 'Asuransi berhasil ditambahkan');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Insurance $insurance)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Insurance $insurance)
|
|
{
|
|
return Inertia::render('insurances/form', [
|
|
'mode' => 'edit',
|
|
'insurance' => [
|
|
'id' => $insurance->id,
|
|
'code' => $insurance->code,
|
|
'name' => $insurance->name,
|
|
'address' => $insurance->address,
|
|
'phone_number' => $insurance->phone_number,
|
|
'email' => $insurance->email,
|
|
'contact_person' => $insurance->contact_person,
|
|
'contact_person_phone' => $insurance->contact_person_phone,
|
|
'coverage_percentage' => (float) $insurance->coverage_percentage,
|
|
'coverage_description' => $insurance->coverage_description,
|
|
'agreement_details' => $insurance->agreement_details,
|
|
'agreement_start_date' => $insurance->agreement_start_date?->format('Y-m-d'),
|
|
'agreement_end_date' => $insurance->agreement_end_date?->format('Y-m-d'),
|
|
'agreement_file_path' => $insurance->agreement_file_path,
|
|
'is_active' => $insurance->is_active
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Insurance $insurance)
|
|
{
|
|
$validated = $request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
'max:20',
|
|
Rule::unique('m_insurance', 'code')->ignore($insurance->id)
|
|
],
|
|
'name' => 'required|string|max:100',
|
|
'address' => 'nullable|string|max:255',
|
|
'phone_number' => 'nullable|string|max:15',
|
|
'email' => 'nullable|email|max:100',
|
|
'contact_person' => 'nullable|string|max:100',
|
|
'contact_person_phone' => 'nullable|string|max:15',
|
|
'coverage_percentage' => 'required|numeric|between:0,100',
|
|
'coverage_description' => 'nullable|string',
|
|
'agreement_details' => 'nullable|string',
|
|
'agreement_start_date' => 'nullable|date',
|
|
'agreement_end_date' => 'nullable|date|after_or_equal:agreement_start_date',
|
|
'agreement_file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048',
|
|
'is_active' => 'required|boolean'
|
|
]);
|
|
|
|
DB::transaction(function () use ($validated, $request, $insurance) {
|
|
$updateData = $validated;
|
|
|
|
// Handle file upload jika ada file baru
|
|
if ($request->hasFile('agreement_file')) {
|
|
// Hapus file lama jika ada
|
|
if ($insurance->agreement_file_path) {
|
|
Storage::disk('public')->delete($insurance->agreement_file_path);
|
|
}
|
|
|
|
$file = $request->file('agreement_file');
|
|
$fileName = 'agreement_' . time() . '_' . $validated['code'] . '.' . $file->getClientOriginalExtension();
|
|
$path = $file->storeAs('insurance_agreements', $fileName, 'public');
|
|
|
|
$updateData['agreement_file_path'] = $path;
|
|
}
|
|
|
|
$insurance->update($updateData);
|
|
});
|
|
|
|
return redirect()->route('insurances.index')
|
|
->with('status', 'Data asuransi berhasil diperbarui');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Insurance $insurance)
|
|
{
|
|
DB::transaction(function () use ($insurance) {
|
|
$insurance->delete();
|
|
});
|
|
|
|
return redirect()->route('insurances.index')
|
|
->with('status', 'Asuransi berhasil dihapus');
|
|
}
|
|
}
|