Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| PurchaseRegisterReport | |
100.00% |
32 / 32 |
|
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% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| rows | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| summary | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Reports\Purchase; |
| 4 | |
| 5 | use App\Models\admin\PurchaseDetails; |
| 6 | use App\Reports\Report; |
| 7 | use Illuminate\Support\Collection; |
| 8 | |
| 9 | /** Every purchase line item in the range — the detailed register, one row per product per purchase. */ |
| 10 | class PurchaseRegisterReport extends Report |
| 11 | { |
| 12 | public function key(): string |
| 13 | { |
| 14 | return 'purchase-register'; |
| 15 | } |
| 16 | |
| 17 | public function label(): string |
| 18 | { |
| 19 | return 'Purchase Register'; |
| 20 | } |
| 21 | |
| 22 | public function group(): string |
| 23 | { |
| 24 | return 'Purchase'; |
| 25 | } |
| 26 | |
| 27 | public function description(): ?string |
| 28 | { |
| 29 | return 'Line-item detail of every purchase in the selected range.'; |
| 30 | } |
| 31 | |
| 32 | public function columns(): array |
| 33 | { |
| 34 | return [ |
| 35 | ['key' => 'ref', 'label' => 'Purchase'], |
| 36 | ['key' => 'date', 'label' => 'Date'], |
| 37 | ['key' => 'supplier', 'label' => 'Supplier'], |
| 38 | ['key' => 'product', 'label' => 'Product'], |
| 39 | ['key' => 'qty', 'label' => 'Qty', 'align' => 'right'], |
| 40 | ['key' => 'price', 'label' => 'Unit Price', 'align' => 'right', 'render' => fn ($r) => money($r->price)], |
| 41 | ['key' => 'total', 'label' => 'Total', 'align' => 'right', 'render' => fn ($r) => money($r->total)], |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | public function rows(array $filters): Collection |
| 46 | { |
| 47 | return PurchaseDetails::query() |
| 48 | ->with('product', 'purchase.supplier') |
| 49 | ->whereHas('purchase', fn ($q) => $q |
| 50 | ->whereDate('created_at', '>=', $filters['from']) |
| 51 | ->whereDate('created_at', '<=', $filters['to'])) |
| 52 | ->get() |
| 53 | ->map(fn (PurchaseDetails $d) => (object) [ |
| 54 | 'ref' => $d->purchase->ref, |
| 55 | 'date' => $d->purchase->date, |
| 56 | 'supplier' => $d->purchase->supplier->name ?? '—', |
| 57 | 'product' => $d->product->name ?? '—', |
| 58 | 'qty' => (float) $d->qty, |
| 59 | 'price' => (float) $d->purchase_price, |
| 60 | 'total' => (float) $d->total, |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | public function summary(Collection $rows, array $filters): array |
| 65 | { |
| 66 | return [ |
| 67 | 'Line Items' => number_format($rows->count()), |
| 68 | 'Total' => money($rows->sum('total')), |
| 69 | ]; |
| 70 | } |
| 71 | } |