Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.50% covered (danger)
12.50%
4 / 32
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurchaseSummaryReport
12.50% covered (danger)
12.50%
4 / 32
57.14% covered (warning)
57.14%
4 / 7
39.83
0.00% covered (danger)
0.00%
0 / 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
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 rows
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 summary
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Reports\Purchase;
4
5use App\Models\admin\Purchase;
6use App\Reports\Report;
7use Illuminate\Support\Collection;
8
9class PurchaseSummaryReport extends Report
10{
11    public function key(): string
12    {
13        return 'purchase-summary';
14    }
15
16    public function label(): string
17    {
18        return 'Purchase Summary';
19    }
20
21    public function group(): string
22    {
23        return 'Purchase';
24    }
25
26    public function description(): ?string
27    {
28        return 'Every purchase in the selected range, with paid/due against each supplier.';
29    }
30
31    public function columns(): array
32    {
33        return [
34            ['key' => 'ref', 'label' => 'Ref'],
35            ['key' => 'supplier', 'label' => 'Supplier'],
36            ['key' => 'date', 'label' => 'Date'],
37            ['key' => 'total', 'label' => 'Total', 'align' => 'right', 'render' => fn ($r) => money($r->total)],
38            ['key' => 'paid', 'label' => 'Paid', 'align' => 'right', 'render' => fn ($r) => money($r->paid)],
39            ['key' => 'due', 'label' => 'Due', 'align' => 'right', 'render' => fn ($r) => money($r->due)],
40        ];
41    }
42
43    public function rows(array $filters): Collection
44    {
45        return Purchase::query()
46            ->with('supplier', 'purchase_payment')
47            ->whereDate('created_at', '>=', $filters['from'])
48            ->whereDate('created_at', '<=', $filters['to'])
49            ->latest()
50            ->get()
51            ->map(fn (Purchase $p) => (object) [
52                'ref' => $p->ref,
53                'supplier' => $p->supplier->name ?? '—',
54                'date' => $p->date,
55                'total' => (float) optional($p->purchase_payment)->total,
56                'paid' => (float) optional($p->purchase_payment)->paid,
57                'due' => (float) optional($p->purchase_payment)->due,
58            ]);
59    }
60
61    public function summary(Collection $rows, array $filters): array
62    {
63        return [
64            'Purchases' => number_format($rows->count()),
65            'Total' => money($rows->sum('total')),
66            'Paid' => money($rows->sum('paid')),
67            'Due' => money($rows->sum('due')),
68        ];
69    }
70}