70 lines
2.8 KiB
PHP
70 lines
2.8 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\RubricaUniversale;
|
|
use App\Models\Stabile;
|
|
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();
|
|
|
|
$stabili = Stabile::query()
|
|
->when($adminId, function ($q) use ($adminId): void {
|
|
$q->where('amministratore_id', $adminId);
|
|
})
|
|
->orderBy('denominazione')
|
|
->limit(12)
|
|
->get(['id', 'denominazione', 'indirizzo', 'citta', 'stato']);
|
|
|
|
$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,
|
|
'stabili' => $stabili,
|
|
'status' => $status,
|
|
'phoneQuery' => $phoneQuery,
|
|
'callerMatches' => $callerMatches,
|
|
]);
|
|
}
|
|
}
|