Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ReportsController | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Reports\GenericExport; |
| 7 | use App\Services\Reporting\ReportService; |
| 8 | use Barryvdh\DomPDF\Facade\Pdf; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\View\View; |
| 11 | use Maatwebsite\Excel\Facades\Excel; |
| 12 | |
| 13 | /** |
| 14 | * The Reports menu — App\Reports\Report is the contract every report |
| 15 | * implements; this controller just runs one (App\Services\Reporting\ |
| 16 | * ReportService::run()) and either shows it on screen or exports it. |
| 17 | * Not to be confused with the legacy admin\ReportController — that's the |
| 18 | * older Sale-based reporting screens, still linked from a couple of POS |
| 19 | * due-payment pages, kept as-is and untouched by this engine. |
| 20 | */ |
| 21 | class ReportsController extends Controller |
| 22 | { |
| 23 | public function __construct(private readonly ReportService $reports) {} |
| 24 | |
| 25 | public function index(): View |
| 26 | { |
| 27 | return view('admin.reports.index', ['groups' => $this->reports->grouped()]); |
| 28 | } |
| 29 | |
| 30 | public function show(string $key, Request $request) |
| 31 | { |
| 32 | $result = $this->reports->run($key, $request->query()); |
| 33 | $columns = $result['report']->columns(); |
| 34 | |
| 35 | return match ($request->query('export')) { |
| 36 | 'xlsx' => Excel::download( |
| 37 | new GenericExport($result['rows'], $columns), |
| 38 | $key . '.xlsx' |
| 39 | ), |
| 40 | 'pdf' => Pdf::loadView('admin.reports.printable', $result + ['columns' => $columns]) |
| 41 | ->download($key . '.pdf'), |
| 42 | 'print' => view('admin.reports.printable', $result + ['columns' => $columns, 'autoPrint' => true]), |
| 43 | default => view('admin.reports.show', $result + ['columns' => $columns]), |
| 44 | }; |
| 45 | } |
| 46 | } |