Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
OrderStatusReport
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 label
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 group
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 columns
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 rows
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 summary
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Reports\Sales;
4
5use App\Models\Order;
6use App\Reports\Report;
7use Illuminate\Support\Collection;
8
9class OrderStatusReport extends Report
10{
11    public function key(): string
12    {
13        return 'order-status';
14    }
15
16    public function label(): string
17    {
18        return 'Orders by Status';
19    }
20
21    public function group(): string
22    {
23        return 'Sales';
24    }
25
26    public function description(): ?string
27    {
28        return 'Where every order in the selected range currently stands.';
29    }
30
31    public function columns(): array
32    {
33        return [
34            ['key' => 'status', 'label' => 'Status'],
35            ['key' => 'orders', 'label' => 'Orders', 'align' => 'right'],
36            ['key' => 'value', 'label' => 'Value', 'align' => 'right', 'render' => fn ($r) => money($r->value)],
37        ];
38    }
39
40    public function rows(array $filters): Collection
41    {
42        return Order::query()
43            ->whereDate('created_at', '>=', $filters['from'])
44            ->whereDate('created_at', '<=', $filters['to'])
45            ->get(['status', 'total'])
46            ->groupBy(fn ($o) => $o->status ?: 'Pending')
47            ->map(fn ($group, $status) => (object) [
48                'status' => $status,
49                'orders' => $group->count(),
50                'value' => (float) $group->sum('total'),
51            ])
52            ->sortByDesc('orders')
53            ->values();
54    }
55
56    public function summary(Collection $rows, array $filters): array
57    {
58        return [
59            'Total Orders' => number_format($rows->sum('orders')),
60            'Total Value' => money($rows->sum('value')),
61        ];
62    }
63}