feat: Tambah kontroler HistoryPerusahaan, HistoryPerusahaanRequest, HistoryPerusahaan model, dan migrasi CreateTableHistoryPerusahaan
parent
e1dc04a279
commit
39524477f1
|
@ -0,0 +1,138 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -24,6 +24,7 @@ class PerusahaanController extends Controller
|
|||
{
|
||||
try {
|
||||
$perusahaan = Perusahaan::with('jenisKegiatan', 'kelurahan.kecamatan.kabupaten', 'verifikator', 'jenisDokIL')->get();
|
||||
|
||||
return Inertia::render('admin/perusahaan/index_perusahaan', [
|
||||
'perusahaan' => $perusahaan,
|
||||
'jenisKegiatan' => JenisKegiatan::all(),
|
||||
|
@ -155,15 +156,46 @@ class PerusahaanController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
public function update(PerusahaanRequest $request, Perusahaan $perusahaan): JsonResponse
|
||||
{
|
||||
$perusahaan->update($request->validated());
|
||||
// public function update(PerusahaanRequest $request, Perusahaan $perusahaan): JsonResponse
|
||||
// {
|
||||
// $perusahaan->update($request->validated());
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data perusahaan berhasil diperbarui',
|
||||
'data' => $perusahaan
|
||||
]);
|
||||
// return response()->json([
|
||||
// 'status' => 'success',
|
||||
// 'message' => 'Data perusahaan berhasil diperbarui',
|
||||
// 'data' => $perusahaan
|
||||
// ]);
|
||||
// }
|
||||
|
||||
public function update(PerusahaanRequest $request, Perusahaan $perusahaan)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$data = $request->validated();
|
||||
|
||||
if (!$request->hasFile('ILDokumen')) {
|
||||
unset($data['ILDokumen']);
|
||||
} else {
|
||||
if ($perusahaan->ILDokumen && Storage::disk('public')->exists($perusahaan->ILDokumen)) {
|
||||
Storage::disk('public')->delete($perusahaan->SanksiFile);
|
||||
}
|
||||
|
||||
$fileName = time() . '_' . $request->file('ILDokumen')->getClientOriginalName();
|
||||
$path = $request->file('ILDokumen')->storeAs('files/il', $fileName, 'public');
|
||||
$data['ILDokumen'] = $path;
|
||||
}
|
||||
|
||||
$perusahaan->update($data);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('admin.perusahaan.index')->with('success', 'Perusahaan berhasil diperbarui');
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Log::error('Error updating perusahaan: ' . $e->getMessage());
|
||||
return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(Perusahaan $perusahaan): JsonResponse
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class HistoryPerusahaanRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'PerusahaanId' => 'required|integer',
|
||||
'HistoryKegiatanId' => 'required|integer',
|
||||
'TanggalHistory' => 'required|date',
|
||||
'NomorHistory' => 'nullable|string|max:100',
|
||||
'KeteranganHistory' => 'nullable|string',
|
||||
'DokumenHistory' => 'required|file|mimes:pdf',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class HistoryPerusahaan extends Model
|
||||
{
|
||||
protected $table = 'HistoryPerusahaan';
|
||||
protected $primaryKey = 'HistoryPerusahaanId';
|
||||
protected $fillable = [
|
||||
'PerusahaanId',
|
||||
'HistoryKegiatanId',
|
||||
'TanggalHistory',
|
||||
'NomorHistory',
|
||||
'KeteranganHistory',
|
||||
'DokumenHistory',
|
||||
];
|
||||
|
||||
public function perusahaan()
|
||||
{
|
||||
return $this->belongsTo(Perusahaan::class, 'PerusahaanId', 'PerusahaanId');
|
||||
}
|
||||
|
||||
public function historyKegiatan()
|
||||
{
|
||||
return $this->belongsTo(HistoryKegiatan::class, 'HistoryKegiatanId', 'HistoryKegiatanId');
|
||||
}
|
||||
|
||||
public function kelurahan()
|
||||
{
|
||||
return $this->belongsTo(Kelurahan::class, 'KelurahanId', 'KelurahanId');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('HistoryPerusahaan', function (Blueprint $table) {
|
||||
$table->id('HistoryPerusahaanId');
|
||||
$table->unsignedInteger('PerusahaanId');
|
||||
$table->unsignedInteger('HistoryKegiatanId');
|
||||
$table->date('TanggalHistory')->comment('tanggal surat');
|
||||
$table->string('NomorHistory', 100)->nullable()->comment('nomor surat');
|
||||
$table->text('KeteranganHistory')->nullable();
|
||||
$table->string('DokumenHistory', 255);
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('PerusahaanId')->references('PerusahaanId')->on('Perusahaan')->onDelete('set null');
|
||||
$table->foreign('HistoryKegiatanId')->references('HistoryKegiatanId')->on('HistoryKegiatan')->onDelete('set null');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('HistoryPerusahaan');
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue