feat: add compressed photo upload for LV, unit support, and employees

This commit is contained in:
Ray1111-aa
2026-10-20 16:00:29 +07:00
parent cb284a7432
commit 7126648c1c
10 changed files with 929 additions and 15 deletions
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Hse;
use App\Http\Controllers\Controller;
use App\Models\Employee;
use App\Models\User;
use App\Services\ImageCompressionService;
use App\Services\SimperPdfExporter;
use App\Services\SimpleXlsxExporter;
use App\Services\SimpleXlsxImporter;
@@ -536,6 +537,7 @@ class EmployeeController extends Controller
'position' => ['required', 'string'],
'is_operator' => ['required', 'in:1,0'],
'simper_expired_at' => ['nullable', 'date'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [
'nik.required' => 'NIK wajib diisi.',
'nik.unique' => 'NIK sudah terdaftar dalam sistem.',
@@ -563,6 +565,11 @@ class EmployeeController extends Controller
$user->assignRole('employee');
}
$photoPath = null;
if ($request->hasFile('photo')) {
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
}
Employee::create([
'nik' => $validated['nik'],
'name' => $validated['name'],
@@ -571,6 +578,7 @@ class EmployeeController extends Controller
'position' => $validated['position'],
'is_operator' => $isOperator,
'simper_expired_at' => $simperExpiredAt,
'photo_path' => $photoPath,
'user_id' => $user->id,
]);
@@ -592,6 +600,7 @@ class EmployeeController extends Controller
'position' => ['sometimes', 'required', 'string'],
'is_operator' => ['required', 'in:1,0'],
'simper_expired_at' => ['nullable', 'date'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [
'nik.unique' => 'NIK sudah digunakan oleh karyawan lain.',
'name.required' => 'Nama lengkap wajib diisi.',
@@ -601,6 +610,12 @@ class EmployeeController extends Controller
$isOperator = (bool) $validated['is_operator'];
$simperExpiredAt = $isOperator ? ($validated['simper_expired_at'] ?? $employee->simper_expired_at) : null;
$photoPath = $employee->photo_path;
if ($request->hasFile('photo')) {
ImageCompressionService::deleteIfExists($employee->photo_path);
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
}
$employee->update([
'nik' => $validated['nik'] ?? $employee->nik,
'name' => $validated['name'] ?? $employee->name,
@@ -609,6 +624,7 @@ class EmployeeController extends Controller
'position' => $validated['position'] ?? $employee->position,
'is_operator' => $isOperator,
'simper_expired_at' => $simperExpiredAt,
'photo_path' => $photoPath,
]);
return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui.");
@@ -9,6 +9,7 @@ use App\Models\P2hRecord;
use App\Models\P2hTemplate;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use App\Services\ImageCompressionService;
use App\Services\SimpleXlsxExporter;
use App\Services\VehiclePdfExporter;
use Carbon\Carbon;
@@ -895,6 +896,7 @@ class VehicleController extends Controller
'kir_document' => ['nullable', 'string', 'max:150'],
'ownership' => ['nullable', 'string', 'max:150'],
'division' => ['nullable', 'string', 'max:100'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
]);
return DB::transaction(function () use ($request, $category) {
@@ -918,6 +920,11 @@ class VehicleController extends Controller
return back()->withInput()->with('error', "Kode unit [{$code}] sudah terdaftar di sistem. Silakan gunakan nomor urut lain.");
}
$photoPath = null;
if ($request->hasFile('photo')) {
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75);
}
$unit = Vehicle::create([
'code' => $code,
'category' => 'unit_support',
@@ -939,6 +946,7 @@ class VehicleController extends Controller
'fuel_consumption' => $category->fuel_consumption,
'silo_document' => $request->input('silo_document'),
'kir_document' => $request->input('kir_document'),
'photo_path' => $photoPath,
]);
return back()->with('success', "Unit fisik [{$unit->code}] berhasil ditambahkan ke kategori {$category->unit_category}.");
@@ -962,6 +970,7 @@ class VehicleController extends Controller
'division' => ['nullable', 'string', 'max:100'],
'status' => [$category === 'unit_support' ? 'nullable' : 'required', 'string', 'in:Operation,Standby,Breakdown,Maintenance'],
'current_km' => [$category === 'unit_support' ? 'nullable' : 'required', 'numeric', 'min:0'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
];
// Atribut opsional spesifik Unit Support (Kendaraan Berat)
@@ -994,6 +1003,11 @@ class VehicleController extends Controller
$validated['current_km'] = $validated['current_km'] ?? 0;
}
if ($request->hasFile('photo')) {
$validated['photo_path'] = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75);
}
unset($validated['photo']);
$vehicle = Vehicle::create($validated);
if ($vehicle->isSupportCategory()) {
@@ -1045,6 +1059,7 @@ class VehicleController extends Controller
'stnk_document' => ['nullable', 'string', 'max:150'],
'pajak_document' => ['nullable', 'string', 'max:150'],
'kir_document' => ['nullable', 'string', 'max:150'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
];
$validated = $request->validate($rules, [
@@ -1054,6 +1069,12 @@ class VehicleController extends Controller
'current_km.required' => 'Nilai KM / HM wajib diisi.',
]);
if ($request->hasFile('photo')) {
ImageCompressionService::deleteIfExists($vehicle->photo_path);
$validated['photo_path'] = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75);
}
unset($validated['photo']);
DB::transaction(function () use ($vehicle, $validated, $oldCode, $newCode) {
$vehicle->update($validated);
@@ -1133,6 +1154,13 @@ class VehicleController extends Controller
}
}
if ($vehicle->isSupportCategory()) {
foreach ($vehicle->units as $u) {
ImageCompressionService::deleteIfExists($u->photo_path);
}
}
ImageCompressionService::deleteIfExists($vehicle->photo_path);
$code = $vehicle->code;
$vehicle->delete();