public static function getEloquentQuery(): Builder
{
$today = Carbon::now()->format('Y-m-d');
$yesterday = Carbon::now()->subDay()->format('Y-m-d');
// Crea sottoquery per il giorno attuale (2024-09-18)
$subqueryTab0 = GridJobImport::select('ijob')
->selectRaw('SUM(irighe) as irighe')
->where('cstato', 'A')
->where('tuts', '>=', $today)
->groupBy('ijob');
// Crea sottoquery per il giorno precedente (2024-09-17)
$subqueryTab1 = GridJobImport::select('ijob')
->selectRaw('SUM(irighe) as irighe')
->where('cstato', 'A')
->where('tuts', '>=', $yesterday)
->groupBy('ijob');
// Eloquent query con leftJoinSub
return GridJobImport::query()
->leftJoinSub($subqueryTab0, 'tab0', function ($join) {
$join->on('jobs_import_grid.ijob', '=', 'tab0.ijob');
})
->leftJoinSub($subqueryTab1, 'tab1', function ($join) {
$join->on('jobs_import_grid.ijob', '=', 'tab1.ijob');
})
->select('jobs_import_grid.ijob')
->selectRaw("CONCAT(COUNT(jobs_import_grid.ijob), ' (', COALESCE(SUM(tab0.irighe), 0), ')') AS '2024-09-18'")
->selectRaw("CONCAT(COUNT(jobs_import_grid.ijob), ' (', COALESCE(SUM(tab1.irighe), 0), ')') AS '2024-09-17'")
->groupBy('jobs_import_grid.ijob');
}