netgescon-day0/app/Http/Controllers/Admin/MobileHubController.php

60 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\RubricaUniversale;
use App\Models\Ticket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class MobileHubController extends Controller
{
public function index(Request $request)
{
$user = Auth::user();
$adminId = $user?->amministratore?->id;
$status = (string) $request->query('status', 'open');
$phoneQuery = trim((string) $request->query('phone', ''));
$tickets = Ticket::query()
->with(['stabile', 'categoriaTicket'])
->when($adminId, function ($q) use ($adminId): void {
$q->whereHas('stabile', fn($sq) => $sq->where('amministratore_id', $adminId));
})
->when($status === 'open', function ($q): void {
$q->whereIn('stato', ['Aperto', 'Preso in Carico', 'In Lavorazione']);
})
->when($status === 'urgent', function ($q): void {
$q->where('priorita', 'Urgente')
->whereIn('stato', ['Aperto', 'Preso in Carico', 'In Lavorazione']);
})
->latest('data_apertura')
->limit(25)
->get();
$callerMatches = collect();
if ($phoneQuery !== '') {
$digits = preg_replace('/\D+/', '', $phoneQuery) ?: '';
if ($digits !== '') {
$needle = '%' . $digits . '%';
$callerMatches = RubricaUniversale::query()
->where(function ($q) use ($needle): void {
$q->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_cellulare, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$needle])
->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_ufficio, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$needle])
->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_casa, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$needle]);
})
->limit(12)
->get();
}
}
return view('admin.mobile.hub', [
'tickets' => $tickets,
'status' => $status,
'phoneQuery' => $phoneQuery,
'callerMatches' => $callerMatches,
]);
}
}