Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.26% |
18 / 23 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CustomerDefinition | |
78.26% |
18 / 23 |
|
71.43% |
5 / 7 |
7.50 | |
0.00% |
0 / 1 |
| label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| model | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| columns | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyFilters | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
1.19 | |||
| uniqueBy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| defaults | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\ImportExport\Definitions; |
| 4 | |
| 5 | use App\Models\admin\Customer; |
| 6 | use App\Services\ImportExport\Definition; |
| 7 | use Illuminate\Database\Eloquent\Builder; |
| 8 | use Illuminate\Support\Facades\Auth; |
| 9 | |
| 10 | class CustomerDefinition extends Definition |
| 11 | { |
| 12 | public function label(): string |
| 13 | { |
| 14 | return 'Customers'; |
| 15 | } |
| 16 | |
| 17 | public function model(): string |
| 18 | { |
| 19 | return Customer::class; |
| 20 | } |
| 21 | |
| 22 | public function columns(): array |
| 23 | { |
| 24 | return [ |
| 25 | ['key' => 'customer_code', 'label' => 'Code', 'sample' => 'C-001'], |
| 26 | ['key' => 'name', 'label' => 'Name', 'required' => true, 'rules' => 'string|max:255', 'sample' => 'Rahim Uddin'], |
| 27 | ['key' => 'phone', 'label' => 'Phone', 'required' => true, 'rules' => 'string|max:40', 'sample' => '01700000000'], |
| 28 | ['key' => 'email', 'label' => 'Email', 'rules' => 'nullable|email|max:120', 'sample' => 'rahim@example.com'], |
| 29 | ['key' => 'address', 'label' => 'Address', 'rules' => 'nullable|string|max:500', 'sample' => 'House 1, Road 2, Dhaka'], |
| 30 | ['key' => 'description', 'label' => 'Notes', 'rules' => 'nullable|string|max:1000', 'sample' => ''], |
| 31 | ]; |
| 32 | } |
| 33 | |
| 34 | public function query(): Builder |
| 35 | { |
| 36 | return Customer::query()->orderByDesc('id'); |
| 37 | } |
| 38 | |
| 39 | public function applyFilters(Builder $query, array $filters): Builder |
| 40 | { |
| 41 | $search = trim((string) ($filters['search'] ?? '')); |
| 42 | |
| 43 | return $query->when($search !== '', fn ($q) => $q->where(function ($q) use ($search) { |
| 44 | $q->where('name', 'like', "%{$search}%") |
| 45 | ->orWhere('phone', 'like', "%{$search}%") |
| 46 | ->orWhere('email', 'like', "%{$search}%") |
| 47 | ->orWhere('customer_code', 'like', "%{$search}%"); |
| 48 | })); |
| 49 | } |
| 50 | |
| 51 | public function uniqueBy(): array |
| 52 | { |
| 53 | return ['customer_code']; |
| 54 | } |
| 55 | |
| 56 | public function defaults(): array |
| 57 | { |
| 58 | return [ |
| 59 | 'created_by' => Auth::id(), |
| 60 | 'customer_code' => 'c-' . rand(1000, 9999), |
| 61 | ]; |
| 62 | } |
| 63 | } |