92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Equipments\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Modules\Equipments\Models\Equipment;
|
|
use Modules\Equipments\Models\EquipmentSpecification;
|
|
use Illuminate\Support\Str;
|
|
|
|
class EquipmentController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return Inertia::render('Equipments::Index', [
|
|
'equipments' => Equipment::with('specifications')->latest()->get(),
|
|
'specifications' => EquipmentSpecification::orderBy('name')->get(),
|
|
]);
|
|
}
|
|
|
|
private function ensureCanManageEquipment(): void
|
|
{
|
|
$user = auth()->user();
|
|
if ($user && $user->hasAnyRole(['Site Technical', 'Construction Supervisor', 'Site Operations', 'Site Engineer', 'Site Supervisor', 'Field Engineer', 'Safety Officer', 'Quality Inspector', 'Warehouse Staff'])) {
|
|
abort(403, 'Unauthorized action. Site Operations roles cannot modify equipment records.');
|
|
}
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->ensureCanManageEquipment();
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'owner_name' => 'nullable|string|max:255',
|
|
'hourly_rate' => 'required|numeric|min:0',
|
|
'status' => 'required|string|in:active,inactive',
|
|
'specifications' => 'nullable|array',
|
|
'specifications.*' => 'integer|exists:equipment_specifications,id',
|
|
]);
|
|
|
|
\DB::transaction(function () use ($validated) {
|
|
$equipment = Equipment::create([
|
|
'ulid' => (string) Str::ulid(),
|
|
'name' => $validated['name'],
|
|
'owner_name' => $validated['owner_name'] ?? null,
|
|
'hourly_rate' => $validated['hourly_rate'],
|
|
'status' => $validated['status'],
|
|
]);
|
|
|
|
if (!empty($validated['specifications'])) {
|
|
$equipment->specifications()->sync($validated['specifications']);
|
|
}
|
|
});
|
|
|
|
return redirect()->back()->with('success', 'Equipment record created successfully.');
|
|
}
|
|
|
|
public function update(Request $request, Equipment $equipment)
|
|
{
|
|
$this->ensureCanManageEquipment();
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'owner_name' => 'nullable|string|max:255',
|
|
'hourly_rate' => 'required|numeric|min:0',
|
|
'status' => 'required|string|in:active,inactive',
|
|
'specifications' => 'nullable|array',
|
|
'specifications.*' => 'integer|exists:equipment_specifications,id',
|
|
]);
|
|
|
|
\DB::transaction(function () use ($equipment, $validated) {
|
|
$equipment->update([
|
|
'name' => $validated['name'],
|
|
'owner_name' => $validated['owner_name'] ?? null,
|
|
'hourly_rate' => $validated['hourly_rate'],
|
|
'status' => $validated['status'],
|
|
]);
|
|
|
|
$equipment->specifications()->sync($validated['specifications'] ?? []);
|
|
});
|
|
|
|
return redirect()->back()->with('success', 'Equipment record updated successfully.');
|
|
}
|
|
|
|
public function destroy(Equipment $equipment)
|
|
{
|
|
$this->ensureCanManageEquipment();
|
|
$equipment->delete();
|
|
return redirect()->back()->with('success', 'Equipment record deleted successfully.');
|
|
}
|
|
}
|