139 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers;
 | |
| 
 | |
| use App\Http\Requests\HistoryPerusahaanRequest;
 | |
| use App\Models\HistoryKegiatan;
 | |
| use App\Models\HistoryPerusahaan;
 | |
| use App\Models\Kabupaten;
 | |
| use App\Models\Kecamatan;
 | |
| use App\Models\Kelurahan;
 | |
| use App\Models\Perusahaan;
 | |
| use Illuminate\Http\Request;
 | |
| use Illuminate\Support\Facades\DB;
 | |
| use Illuminate\Support\Facades\Log;
 | |
| use Inertia\Inertia;
 | |
| 
 | |
| class HistoryPerusahaanController extends Controller
 | |
| {
 | |
|     public function index()
 | |
|     {
 | |
|         try{
 | |
|             $historyPerusahaan = HistoryPerusahaan::with(['perusahaan', 'historyKegiatan', 'kelurahan.kecamatan.kabupaten'])->orderBy('created_at', 'desc')->get();
 | |
| 
 | |
|              // Eager load companies with their relationships
 | |
|         $perusahaan = Perusahaan::with('kelurahan.kecamatan.kabupaten')->get()
 | |
|         ->map(function ($company) {
 | |
|             $company->Kabupaten = $company->kelurahan?->kecamatan?->kabupaten?->NamaKabupaten ?? '-';
 | |
|             return $company;
 | |
|         });
 | |
| 
 | |
| 
 | |
|             return Inertia::render('admin/history_perusahaan/index_history_perusahaan', [
 | |
|                 'historyPerusahaan' => $historyPerusahaan,
 | |
|                 'perusahaan' => $perusahaan,
 | |
|                 'historyKegiatan' => HistoryKegiatan::all(),
 | |
|                 'kabupaten' => Kabupaten::all(),
 | |
|                 'kecamatan' => Kecamatan::all(),
 | |
|                 'kelurahan' => Kelurahan::all(),
 | |
|             ]);
 | |
|         } catch (\Exception $e) {
 | |
|             Log::error('Error fetching data: ' . $e->getMessage());
 | |
|             return back()->with('error', 'Terjadi kesalahan saat memuat data.');
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function store(HistoryPerusahaanRequest $request)
 | |
|     {
 | |
|         try {
 | |
|             DB::beginTransaction();
 | |
| 
 | |
|             $data = $request->validated();
 | |
| 
 | |
|             if ($request->hasFile('DokumenHistory')) {
 | |
|                 $fileName = time() . '_' . $request->file('DokumenHistory')->getClientOriginalName();
 | |
|                 // Misalnya, jika data memiliki PerusahaanId, gunakan untuk membuat folder
 | |
|                 $folder = 'files/history_perusahaan/' . $data['PerusahaanId'];
 | |
|                 $path = $request->file('DokumenHistory')->storeAs($folder, $fileName, 'public');
 | |
|                 $data['DokumenHistory'] = $path;
 | |
|             }
 | |
| 
 | |
|             HistoryPerusahaan::create($data);
 | |
| 
 | |
|             DB::commit();
 | |
| 
 | |
|             return redirect()
 | |
|                 ->route('admin.history_perusahaan.detail', $data['PerusahaanId'])
 | |
|                 ->with('success', 'History Perusahaan berhasil ditambahkan');
 | |
|         } catch (\Exception $e) {
 | |
|             DB::rollBack();
 | |
|             Log::error('Error creating History Perusahaan: ' . $e->getMessage());
 | |
|             return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public function detail($perusahaanId)
 | |
| {
 | |
|     $perusahaan = Perusahaan::with('historyPerusahaan')->findOrFail($perusahaanId);
 | |
| 
 | |
|     $historyKegiatan = HistoryKegiatan::all();
 | |
| 
 | |
|     $historyPerusahaan = HistoryPerusahaan::with('historyKegiatan')->where('PerusahaanId', $perusahaanId)->get();
 | |
| 
 | |
|     return Inertia::render('admin/history_perusahaan/detail_history_perusahaan', [
 | |
|         'perusahaan' => $perusahaan,
 | |
|         'historyKegiatan' => $historyKegiatan,
 | |
|         'historyPerusahaan' => $historyPerusahaan,
 | |
|     ]);
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| public function update(Request $request, $historyPerusahaanId)
 | |
| {
 | |
|     try {
 | |
|         DB::beginTransaction();
 | |
| 
 | |
|         // Validasi input secara inline
 | |
|         $data = $request->validate([
 | |
|             'PerusahaanId'      => 'required|integer',
 | |
|             'NomorHistory'      => 'required|string',
 | |
|             'TanggalHistory'    => 'required|date',
 | |
|             'HistoryKegiatanId' => 'required|integer',
 | |
|             'KeteranganHistory' => 'nullable|string',
 | |
|             'DokumenHistory'    => 'nullable|file|mimes:pdf',
 | |
|         ]);
 | |
| 
 | |
|         // Cari data HistoryPerusahaan berdasarkan primary key kustom
 | |
|         $history = HistoryPerusahaan::findOrFail($historyPerusahaanId);
 | |
| 
 | |
|         // Jika ada file baru yang diupload, proses upload dan set path file
 | |
|         if ($request->hasFile('DokumenHistory')) {
 | |
|             $fileName = time() . '_' . $request->file('DokumenHistory')->getClientOriginalName();
 | |
|             $folder = 'files/history_perusahaan/' . $data['PerusahaanId'];
 | |
|             $path = $request->file('DokumenHistory')->storeAs($folder, $fileName, 'public');
 | |
|             $data['DokumenHistory'] = $path;
 | |
|         } else {
 | |
|             // Jika tidak ada file baru, jangan mengubah nilai DokumenHistory
 | |
|             unset($data['DokumenHistory']);
 | |
|         }
 | |
| 
 | |
|         // Update record dengan data baru
 | |
|         $history->update($data);
 | |
| 
 | |
|         DB::commit();
 | |
| 
 | |
|         return redirect()
 | |
|             ->route('admin.history_perusahaan.detail', $data['PerusahaanId'])
 | |
|             ->with('success', 'History Perusahaan berhasil diperbarui');
 | |
|     } catch (\Exception $e) {
 | |
|         DB::rollBack();
 | |
|         Log::error('Error updating History Perusahaan: ' . $e->getMessage());
 | |
|         return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
 | |
|     }
 | |
| }
 | |
| 
 | |
| 
 | |
| }
 |