Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
PurchaseByProductReport
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
7 / 7
7
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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 summary
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Reports\Purchase;
4
5use App\Models\admin\PurchaseDetails;
6use App\Reports\Report;
7use Illuminate\Support\Collection;
8use Illuminate\Support\Facades\DB;
9
10class PurchaseByProductReport extends Report
11{
12    public function key(): string
13    {
14        return 'purchase-by-product';
15    }
16
17    public function label(): string
18    {
19        return 'Purchase by Product';
20    }
21
22    public function group(): string
23    {
24        return 'Purchase';
25    }
26
27    public function description(): ?string
28    {
29        return 'Units and cost purchased per product in the selected range.';
30    }
31
32    public function columns(): array
33    {
34        return [
35            ['key' => 'name', 'label' => 'Product'],
36            ['key' => 'qty', 'label' => 'Qty Purchased', 'align' => 'right'],
37            ['key' => 'cost', 'label' => 'Total Cost', 'align' => 'right', 'render' => fn ($r) => money($r->cost)],
38        ];
39    }
40
41    public function rows(array $filters): Collection
42    {
43        return PurchaseDetails::query()
44            ->join('products', 'purchase_details.product_id', '=', 'products.id')
45            ->join('purchases', 'purchase_details.purchase_id', '=', 'purchases.id')
46            ->whereDate('purchases.created_at', '>=', $filters['from'])
47            ->whereDate('purchases.created_at', '<=', $filters['to'])
48            ->select('products.name', DB::raw('SUM(purchase_details.qty) as qty'), DB::raw('SUM(purchase_details.total) as cost'))
49            ->groupBy('products.id', 'products.name')
50            ->orderByDesc('cost')
51            ->get();
52    }
53
54    public function summary(Collection $rows, array $filters): array
55    {
56        return [
57            'Products Purchased' => number_format($rows->count()),
58            'Total Cost' => money($rows->sum('cost')),
59        ];
60    }
61}