netgescon-day0/app/Http/Controllers/Fornitore/DipendenteController.php

163 lines
6.1 KiB
PHP

<?php
namespace App\Http\Controllers\Fornitore;
use App\Http\Controllers\Controller;
use App\Models\Fornitore;
use App\Models\FornitoreDipendente;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;
class DipendenteController extends Controller
{
public function index()
{
$fornitore = $this->resolveFornitoreForUser();
$dipendenti = FornitoreDipendente::query()
->with('user')
->where('fornitore_id', $fornitore->id)
->orderBy('attivo', 'desc')
->orderBy('cognome')
->orderBy('nome')
->paginate(20);
return view('fornitore.dipendenti.index', [
'fornitore' => $fornitore,
'dipendenti' => $dipendenti,
]);
}
public function store(Request $request): RedirectResponse
{
$fornitore = $this->resolveFornitoreForUser();
$validated = $request->validate([
'nome' => ['required', 'string', 'max:100'],
'cognome' => ['nullable', 'string', 'max:100'],
'email' => ['nullable', 'email', 'max:255'],
'telefono' => ['nullable', 'string', 'max:50'],
'note' => ['nullable', 'string', 'max:2000'],
'create_user_access' => ['nullable', 'boolean'],
]);
$email = trim((string) ($validated['email'] ?? ''));
if ($email !== '') {
$exists = FornitoreDipendente::query()
->where('fornitore_id', $fornitore->id)
->whereRaw('LOWER(email) = ?', [mb_strtolower($email)])
->exists();
if ($exists) {
return back()->withErrors(['email' => 'Esiste gia un dipendente con questa email.'])->withInput();
}
}
$dipendente = new FornitoreDipendente();
$dipendente->fornitore_id = (int) $fornitore->id;
$dipendente->nome = trim((string) $validated['nome']);
$dipendente->cognome = trim((string) ($validated['cognome'] ?? '')) ?: null;
$dipendente->email = $email !== '' ? $email : null;
$dipendente->telefono = trim((string) ($validated['telefono'] ?? '')) ?: null;
$dipendente->note = trim((string) ($validated['note'] ?? '')) ?: null;
$dipendente->attivo = true;
$dipendente->created_by_user_id = Auth::id();
$dipendente->updated_by_user_id = Auth::id();
if ((bool) ($validated['create_user_access'] ?? false) && $email !== '') {
$user = User::query()->whereRaw('LOWER(email) = ?', [mb_strtolower($email)])->first();
if (! $user) {
$generatedPassword = Str::random(12);
$user = User::query()->create([
'name' => trim($dipendente->nome . ' ' . ($dipendente->cognome ?? '')),
'email' => $email,
'password' => Hash::make($generatedPassword),
'email_verified_at' => now(),
]);
}
$role = Role::query()->where('name', 'fornitore')->first();
if ($role) {
$user->assignRole($role);
}
$dipendente->user_id = (int) $user->id;
}
$dipendente->save();
return redirect()->route('fornitore.dipendenti.index')
->with('success', 'Dipendente creato correttamente.');
}
public function update(Request $request, FornitoreDipendente $dipendente): RedirectResponse
{
$fornitore = $this->resolveFornitoreForUser();
$this->authorizeDipendente($dipendente, $fornitore);
$validated = $request->validate([
'nome' => ['required', 'string', 'max:100'],
'cognome' => ['nullable', 'string', 'max:100'],
'telefono' => ['nullable', 'string', 'max:50'],
'attivo' => ['nullable', 'boolean'],
'note' => ['nullable', 'string', 'max:2000'],
]);
$dipendente->update([
'nome' => trim((string) $validated['nome']),
'cognome' => trim((string) ($validated['cognome'] ?? '')) ?: null,
'telefono' => trim((string) ($validated['telefono'] ?? '')) ?: null,
'attivo' => (bool) ($validated['attivo'] ?? false),
'note' => trim((string) ($validated['note'] ?? '')) ?: null,
'updated_by_user_id' => Auth::id(),
]);
return redirect()->route('fornitore.dipendenti.index')
->with('success', 'Dipendente aggiornato.');
}
public function toggle(FornitoreDipendente $dipendente): RedirectResponse
{
$fornitore = $this->resolveFornitoreForUser();
$this->authorizeDipendente($dipendente, $fornitore);
$dipendente->update([
'attivo' => ! $dipendente->attivo,
'updated_by_user_id' => Auth::id(),
]);
return back()->with('success', 'Stato dipendente aggiornato.');
}
private function resolveFornitoreForUser(): Fornitore
{
$user = Auth::user();
$fornitore = Fornitore::query()
->where('email', (string) ($user?->email ?? ''))
->first();
if (! $fornitore && $user) {
$link = FornitoreDipendente::query()
->where('user_id', (int) $user->id)
->where('attivo', true)
->first();
if ($link) {
$fornitore = Fornitore::query()->find((int) $link->fornitore_id);
}
}
abort_unless($fornitore, 403, 'Profilo fornitore non collegato.');
return $fornitore;
}
private function authorizeDipendente(FornitoreDipendente $dipendente, Fornitore $fornitore): void
{
abort_unless((int) $dipendente->fornitore_id === (int) $fornitore->id, 403);
}
}