164 lines
6.1 KiB
PHP
164 lines
6.1 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketIntervento;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
|
|
class TicketInterventoController extends Controller
|
|
{
|
|
public function store(Request $request, Ticket $ticket)
|
|
{
|
|
$this->authorizeTicket($ticket);
|
|
|
|
if (! $ticket->assegnato_a_fornitore_id) {
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('error', 'Assegna prima il ticket a un fornitore.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'note_amministratore' => 'nullable|string|max:3000',
|
|
'articoli_utilizzati' => 'nullable|array',
|
|
'articoli_utilizzati.*' => 'nullable|string|max:120',
|
|
]);
|
|
|
|
$articoli = collect($validated['articoli_utilizzati'] ?? [])
|
|
->map(fn($item) => trim((string) $item))
|
|
->filter()
|
|
->values()
|
|
->all();
|
|
|
|
TicketIntervento::create([
|
|
'ticket_id' => $ticket->id,
|
|
'fornitore_id' => $ticket->assegnato_a_fornitore_id,
|
|
'creato_da_user_id' => Auth::id(),
|
|
'stato' => 'assegnato',
|
|
'note_amministratore' => $validated['note_amministratore'] ?? null,
|
|
'articoli_utilizzati' => $articoli,
|
|
'qr_token' => Str::upper(Str::random(10)),
|
|
]);
|
|
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('success', 'Intervento operativo creato e visibile al fornitore assegnato.');
|
|
}
|
|
|
|
public function verify(Request $request, Ticket $ticket, TicketIntervento $intervento)
|
|
{
|
|
$this->authorizeTicket($ticket);
|
|
$this->ensureInterventoOnTicket($ticket, $intervento);
|
|
|
|
$validated = $request->validate([
|
|
'check_foto_ok' => 'nullable|boolean',
|
|
'check_qr_ok' => 'nullable|boolean',
|
|
'check_admin_ok' => 'nullable|boolean',
|
|
'riferimento_chiusura' => 'nullable|string|max:100',
|
|
'notifica_chiusura' => 'nullable|boolean',
|
|
]);
|
|
|
|
$intervento->fill([
|
|
'check_foto_ok' => (bool) $request->boolean('check_foto_ok'),
|
|
'check_qr_ok' => (bool) $request->boolean('check_qr_ok'),
|
|
'check_admin_ok' => (bool) $request->boolean('check_admin_ok'),
|
|
'riferimento_chiusura' => $validated['riferimento_chiusura'] ?? $intervento->riferimento_chiusura,
|
|
]);
|
|
|
|
if ($intervento->completabile && in_array($intervento->stato, ['in_verifica', 'completato', 'assegnato', 'in_corso'], true)) {
|
|
$intervento->stato = 'fatturabile';
|
|
}
|
|
|
|
if ($request->boolean('notifica_chiusura')) {
|
|
$intervento->notifica_chiusura_inviata_at = now();
|
|
$ticket->messages()->create([
|
|
'user_id' => Auth::id(),
|
|
'messaggio' => 'Notifica di chiusura intervento inviata al richiedente.',
|
|
'canale' => 'interno',
|
|
'direzione' => 'outbound',
|
|
'inviato_il' => now(),
|
|
]);
|
|
}
|
|
|
|
$intervento->save();
|
|
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('success', 'Checklist amministratore aggiornata.');
|
|
}
|
|
|
|
public function invoice(Request $request, Ticket $ticket, TicketIntervento $intervento)
|
|
{
|
|
$this->authorizeTicket($ticket);
|
|
$this->ensureInterventoOnTicket($ticket, $intervento);
|
|
|
|
$validated = $request->validate([
|
|
'fattura_numero' => 'required|string|max:60',
|
|
'fattura_data' => 'required|date',
|
|
'fattura_imponibile' => 'nullable|numeric|min:0',
|
|
'fattura_totale' => 'required|numeric|min:0',
|
|
'fe_riferimento' => 'nullable|string|max:120',
|
|
]);
|
|
|
|
$intervento->update([
|
|
'fattura_numero' => $validated['fattura_numero'],
|
|
'fattura_data' => $validated['fattura_data'],
|
|
'fattura_imponibile' => $validated['fattura_imponibile'] ?? null,
|
|
'fattura_totale' => $validated['fattura_totale'],
|
|
'fe_riferimento' => $validated['fe_riferimento'] ?? null,
|
|
'stato' => 'fatturato',
|
|
]);
|
|
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('success', 'Dati fattura intervento registrati.');
|
|
}
|
|
|
|
public function close(Ticket $ticket, TicketIntervento $intervento)
|
|
{
|
|
$this->authorizeTicket($ticket);
|
|
$this->ensureInterventoOnTicket($ticket, $intervento);
|
|
|
|
if (! $intervento->completabile) {
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('error', 'Completare i tre check (foto, QR, validazione admin) prima della chiusura.');
|
|
}
|
|
|
|
$intervento->update([
|
|
'stato' => 'chiuso',
|
|
'chiuso_at' => now(),
|
|
]);
|
|
|
|
if ($ticket->stato !== 'Chiuso') {
|
|
$ticket->update([
|
|
'stato' => 'Chiuso',
|
|
'data_risoluzione_effettiva' => $ticket->data_risoluzione_effettiva ?? now(),
|
|
'data_chiusura_effettiva' => now(),
|
|
]);
|
|
}
|
|
|
|
$ticket->messages()->create([
|
|
'user_id' => Auth::id(),
|
|
'messaggio' => 'Intervento operativo chiuso definitivamente.',
|
|
'canale' => 'interno',
|
|
'direzione' => 'outbound',
|
|
'inviato_il' => now(),
|
|
]);
|
|
|
|
return redirect()->route('admin.tickets.show', $ticket)
|
|
->with('success', 'Intervento chiuso definitivamente e ticket aggiornato.');
|
|
}
|
|
|
|
private function authorizeTicket(Ticket $ticket): void
|
|
{
|
|
if (($ticket->stabile->amministratore_id ?? null) !== (Auth::user()->amministratore->id_amministratore ?? null)) {
|
|
abort(403);
|
|
}
|
|
}
|
|
|
|
private function ensureInterventoOnTicket(Ticket $ticket, TicketIntervento $intervento): void
|
|
{
|
|
if ((int) $intervento->ticket_id !== (int) $ticket->id) {
|
|
abort(404);
|
|
}
|
|
}
|
|
}
|