skl/app/Http/Controllers/PelaporanALController.php

143 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Pelaporan;
use App\Models\PeriodePelaporan;
use App\Models\Perusahaan;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
class PelaporanALController extends Controller
{
public function index(){
try {
// Mendapatkan data perusahaan dan periode pelaporan
$companies = Perusahaan::all();
$periodes = PeriodePelaporan::all();
// Mendapatkan pelaporan dengan relasi perusahaan dan periode
$pelaporan = Pelaporan::with(['Perusahaan', 'PeriodePelaporan'])->get();
return Inertia::render('admin/pelaporan/AL/index_AL', [
'companies' => $companies,
'periodes' => $periodes,
'pelaporan' => $pelaporan,
]);
} catch (QueryException $e) {
Log::error('Error fetching data for PelaporanALController index: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan saat mengambil data laporan.');
} catch (Exception $e) {
Log::error('Unexpected error in PelaporanALController index: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan tak terduga.');
}
}
// public function indexIpal()
// {
// try {
// return Inertia::render('admin/pelaporan/AL/index_AL_IPAL');
// } catch (\Exception $e) {
// Log::error('Error rendering view: ' . $e->getMessage());
// return back()->with('error', 'Something went wrong.');
// }
// }
public function indexIpal(){
try {
// Data IPAL
$ipals = Pelaporan::all();
return Inertia::render('admin/pelaporan/AL/index_AL_IPAL', [
'ipals' => $ipals,
]);
} catch (QueryException $e) {
Log::error('Error fetching data for IPAL in PelaporanALController indexIpal: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan saat mengambil data IPAL.');
} catch (Exception $e) {
Log::error('Unexpected error in PelaporanALController indexIpal: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan tak terduga.');
}
}
public function store(Request $request)
{
try {
// Validasi input
$request->validate([
'company_id' => 'required|exists:perusahaan,id',
'period_id' => 'required|exists:periode_pelaporan,id',
'tahun' => 'required|integer',
]);
// Membuat pelaporan baru
Pelaporan::create([
'PerusahaanId' => $request->company_id,
'PeriodePelaporanId' => $request->period_id,
'Tahun' => $request->tahun,
]);
return redirect()->route('pelaporan.index')->with('success', 'Pelaporan berhasil ditambahkan.');
} catch (QueryException $e) {
Log::error('Error while storing Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan saat menambahkan pelaporan.');
} catch (Exception $e) {
Log::error('Unexpected error while storing Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan tak terduga.');
}
}
public function update(Request $request, $id)
{
try {
// Validasi input
$request->validate([
'company_id' => 'required|exists:perusahaan,id',
'period_id' => 'required|exists:periode_pelaporan,id',
'tahun' => 'required|integer',
]);
// Menemukan pelaporan berdasarkan ID
$pelaporan = Pelaporan::findOrFail($id);
// Mengupdate data pelaporan
$pelaporan->update([
'PerusahaanId' => $request->company_id,
'PeriodePelaporanId' => $request->period_id,
'Tahun' => $request->tahun,
]);
return redirect()->route('pelaporan.index')->with('success', 'Pelaporan berhasil diperbarui.');
} catch (QueryException $e) {
Log::error('Error while updating Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan saat memperbarui pelaporan.');
} catch (Exception $e) {
Log::error('Unexpected error while updating Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan tak terduga.');
}
}
public function destroy($id)
{
try {
// Menemukan pelaporan berdasarkan ID
$pelaporan = Pelaporan::findOrFail($id);
// Menghapus pelaporan
$pelaporan->delete();
return redirect()->route('pelaporan.index')->with('success', 'Pelaporan berhasil dihapus.');
} catch (QueryException $e) {
Log::error('Error while deleting Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan saat menghapus pelaporan.');
} catch (Exception $e) {
Log::error('Unexpected error while deleting Pelaporan: ' . $e->getMessage());
return back()->with('error', 'Terjadi kesalahan tak terduga.');
}
}
}