Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| CouponUsageReport | |
100.00% |
22 / 22 |
|
100.00% |
7 / 7 |
7 | |
100.00% |
1 / 1 |
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| label | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| group | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| description | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| columns | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| rows | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| summary | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Reports\Sales; |
| 4 | |
| 5 | use App\Models\Order; |
| 6 | use App\Reports\Report; |
| 7 | use Illuminate\Support\Collection; |
| 8 | |
| 9 | class CouponUsageReport extends Report |
| 10 | { |
| 11 | public function key(): string |
| 12 | { |
| 13 | return 'coupon-usage'; |
| 14 | } |
| 15 | |
| 16 | public function label(): string |
| 17 | { |
| 18 | return 'Discount / Coupon Usage'; |
| 19 | } |
| 20 | |
| 21 | public function group(): string |
| 22 | { |
| 23 | return 'Sales'; |
| 24 | } |
| 25 | |
| 26 | public function description(): ?string |
| 27 | { |
| 28 | return 'Every order that used a coupon code in the selected range.'; |
| 29 | } |
| 30 | |
| 31 | public function columns(): array |
| 32 | { |
| 33 | return [ |
| 34 | ['key' => 'bar_code', 'label' => 'Order'], |
| 35 | ['key' => 'coupon_code', 'label' => 'Coupon'], |
| 36 | ['key' => 'created_at', 'label' => 'Date', 'render' => fn ($r) => $r->created_at->format('d M, Y')], |
| 37 | ['key' => 'total', 'label' => 'Order Total', 'align' => 'right', 'render' => fn ($r) => money($r->total)], |
| 38 | ['key' => 'discount', 'label' => 'Discount', 'align' => 'right', 'render' => fn ($r) => money($r->discount)], |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | public function rows(array $filters): Collection |
| 43 | { |
| 44 | return Order::query() |
| 45 | ->whereNotNull('coupon_code') |
| 46 | ->where('coupon_code', '!=', '') |
| 47 | ->whereDate('created_at', '>=', $filters['from']) |
| 48 | ->whereDate('created_at', '<=', $filters['to']) |
| 49 | ->latest() |
| 50 | ->get(['bar_code', 'coupon_code', 'created_at', 'total', 'discount']); |
| 51 | } |
| 52 | |
| 53 | public function summary(Collection $rows, array $filters): array |
| 54 | { |
| 55 | return [ |
| 56 | 'Orders with a Coupon' => number_format($rows->count()), |
| 57 | 'Total Discount Given' => money($rows->sum('discount')), |
| 58 | ]; |
| 59 | } |
| 60 | } |