Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.87% |
14 / 23 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| AccountCategoryDefinition | |
60.87% |
14 / 23 |
|
28.57% |
2 / 7 |
15.99 | |
0.00% |
0 / 1 |
| label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| model | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| columns | |
68.75% |
11 / 16 |
|
0.00% |
0 / 1 |
4.49 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyFilters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| uniqueBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| defaults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\ImportExport\Definitions; |
| 4 | |
| 5 | use App\Models\AccountCategory; |
| 6 | use App\Services\ImportExport\Definition; |
| 7 | use Illuminate\Database\Eloquent\Builder; |
| 8 | |
| 9 | class AccountCategoryDefinition extends Definition |
| 10 | { |
| 11 | public function label(): string |
| 12 | { |
| 13 | return 'Accounting Categories'; |
| 14 | } |
| 15 | |
| 16 | public function model(): string |
| 17 | { |
| 18 | return AccountCategory::class; |
| 19 | } |
| 20 | |
| 21 | public function columns(): array |
| 22 | { |
| 23 | return [ |
| 24 | ['key' => 'name', 'label' => 'Name', 'required' => true, 'rules' => 'string|max:150', 'sample' => 'Rent'], |
| 25 | ['key' => 'type', 'label' => 'Type', 'required' => true, 'rules' => 'in:income,expense', 'sample' => 'expense'], |
| 26 | ['key' => 'parent_id', 'label' => 'Parent category', 'rules' => 'nullable|string|max:150', |
| 27 | 'sample' => '', 'help' => 'Name or id of the parent', |
| 28 | 'export' => fn ($row) => optional($row->parent)->name, |
| 29 | 'import' => function ($v) { |
| 30 | if (! $v) { |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | return is_numeric($v) |
| 35 | ? AccountCategory::whereKey($v)->value('id') |
| 36 | : AccountCategory::where('name', $v)->value('id'); |
| 37 | }], |
| 38 | ['key' => 'is_active', 'label' => 'Active', 'sample' => '1', |
| 39 | 'import' => fn ($v) => in_array(strtolower((string) $v), ['1', 'yes', 'true', 'active'], true) ? 1 : 0], |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | public function query(): Builder |
| 44 | { |
| 45 | return AccountCategory::query()->with('parent')->orderBy('type')->orderBy('name'); |
| 46 | } |
| 47 | |
| 48 | public function applyFilters(Builder $query, array $filters): Builder |
| 49 | { |
| 50 | $s = trim((string) ($filters['search'] ?? '')); |
| 51 | |
| 52 | return $query->when($s !== '', fn ($q) => $q->where('name', 'like', "%{$s}%")); |
| 53 | } |
| 54 | |
| 55 | public function uniqueBy(): array |
| 56 | { |
| 57 | return ['name', 'type']; |
| 58 | } |
| 59 | |
| 60 | public function defaults(): array |
| 61 | { |
| 62 | return ['is_active' => 1]; |
| 63 | } |
| 64 | } |