149 lines
4.9 KiB
PHP
149 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Asset;
|
|
use App\Models\Commune;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class AssetController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$this->authorize('viewAny', Asset::class);
|
|
|
|
$query = Asset::with('commune');
|
|
|
|
if ($request->search) {
|
|
$query->where(function ($q) use ($request) {
|
|
$q->where('nom', 'like', "%{$request->search}%")
|
|
->orWhere('numero_serie', 'like', "%{$request->search}%")
|
|
->orWhere('type', 'like', "%{$request->search}%");
|
|
});
|
|
}
|
|
|
|
if ($request->type) {
|
|
$query->where('type', $request->type);
|
|
}
|
|
|
|
if ($request->commune_id) {
|
|
$query->where('commune_id', $request->commune_id);
|
|
}
|
|
|
|
$assets = $query->latest()->paginate(20)->withQueryString();
|
|
|
|
return Inertia::render('Assets/Index', [
|
|
'assets' => $assets,
|
|
'filters' => $request->only(['search', 'type', 'commune_id']),
|
|
'communes' => Commune::orderBy('nom')->get(),
|
|
'types' => Asset::distinct()->pluck('type'),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
$this->authorize('create', Asset::class);
|
|
|
|
return Inertia::render('Assets/Form', [
|
|
'communes' => Commune::orderBy('nom')->get(),
|
|
'commandes' => \App\Models\Commande::select('id', 'numero_commande', 'objet')->latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$this->authorize('create', Asset::class);
|
|
|
|
$validated = $request->validate([
|
|
'nom' => 'required|string|max:255',
|
|
'type' => 'required|string|max:100',
|
|
'code_ean' => 'nullable|string|max:50',
|
|
'marque' => 'nullable|string|max:100',
|
|
'modele' => 'nullable|string|max:100',
|
|
'numero_serie' => 'nullable|string|max:100',
|
|
'emplacement' => 'nullable|string|max:255',
|
|
'commune_id' => 'nullable|exists:communes,id',
|
|
'commande_id' => 'nullable|exists:commandes,id',
|
|
'date_achat' => 'nullable|date',
|
|
'date_fin_garantie' => 'nullable|date',
|
|
'statut' => 'required|in:en_service,hors_service,en_reparation,stock',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
Asset::create($validated);
|
|
|
|
return redirect()->route('assets.index')
|
|
->with('success', 'Asset ajouté avec succès.');
|
|
}
|
|
|
|
public function show(Asset $asset): Response
|
|
{
|
|
$this->authorize('view', $asset);
|
|
|
|
$asset->load(['commune', 'commande']);
|
|
|
|
return Inertia::render('Assets/Show', [
|
|
'asset' => $asset,
|
|
]);
|
|
}
|
|
|
|
public function edit(Asset $asset): Response
|
|
{
|
|
$this->authorize('update', $asset);
|
|
|
|
return Inertia::render('Assets/Form', [
|
|
'asset' => $asset,
|
|
'communes' => Commune::orderBy('nom')->get(),
|
|
'commandes' => \App\Models\Commande::select('id', 'numero_commande', 'objet')->latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Asset $asset): RedirectResponse
|
|
{
|
|
$this->authorize('update', $asset);
|
|
|
|
$validated = $request->validate([
|
|
'nom' => 'required|string|max:255',
|
|
'type' => 'required|string|max:100',
|
|
'code_ean' => 'nullable|string|max:50',
|
|
'marque' => 'nullable|string|max:100',
|
|
'modele' => 'nullable|string|max:100',
|
|
'numero_serie' => 'nullable|string|max:100',
|
|
'emplacement' => 'nullable|string|max:255',
|
|
'commune_id' => 'nullable|exists:communes,id',
|
|
'commande_id' => 'nullable|exists:commandes,id',
|
|
'date_achat' => 'nullable|date',
|
|
'date_fin_garantie' => 'nullable|date',
|
|
'statut' => 'required|in:en_service,hors_service,en_reparation,stock',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
$asset->update($validated);
|
|
|
|
return redirect()->route('assets.index')
|
|
->with('success', 'Asset mis à jour.');
|
|
}
|
|
|
|
public function destroy(Asset $asset): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $asset);
|
|
|
|
$asset->delete();
|
|
|
|
return redirect()->route('assets.index')
|
|
->with('success', 'Asset supprimé.');
|
|
}
|
|
|
|
public function lookupEan($ean)
|
|
{
|
|
$response = \Illuminate\Support\Facades\Http::get("https://api.upcitemdb.com/prod/trial/lookup", [
|
|
'upc' => $ean
|
|
]);
|
|
|
|
return response()->json($response->json(), $response->status());
|
|
}
|
|
}
|