Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.62% |
21 / 32 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ProductDefinition | |
65.62% |
21 / 32 |
|
25.00% |
2 / 8 |
38.91 | |
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 | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
11 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyFilters | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
1.02 | |||
| uniqueBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| defaults | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| beforeSave | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\ImportExport\Definitions; |
| 4 | |
| 5 | use App\Models\admin\Product; |
| 6 | use App\Models\Brand; |
| 7 | use App\Models\Category; |
| 8 | use App\Services\ImportExport\Definition; |
| 9 | use Illuminate\Database\Eloquent\Builder; |
| 10 | use Illuminate\Support\Facades\Auth; |
| 11 | use Illuminate\Support\Str; |
| 12 | |
| 13 | class ProductDefinition extends Definition |
| 14 | { |
| 15 | public function label(): string |
| 16 | { |
| 17 | return 'Products'; |
| 18 | } |
| 19 | |
| 20 | public function model(): string |
| 21 | { |
| 22 | return Product::class; |
| 23 | } |
| 24 | |
| 25 | public function columns(): array |
| 26 | { |
| 27 | return [ |
| 28 | ['key' => 'sku_no', 'label' => 'SKU', 'rules' => 'nullable|string|max:255', 'sample' => 'MCB-63A'], |
| 29 | ['key' => 'name', 'label' => 'Name', 'required' => true, 'rules' => 'string|max:255', 'sample' => '63A MCB'], |
| 30 | ['key' => 'category_id', 'label' => 'Category', 'rules' => 'nullable|string|max:255', 'sample' => 'Circuit Breakers', |
| 31 | 'export' => fn ($row) => optional(Category::find($row->category_id))->name, |
| 32 | 'import' => fn ($v) => $v ? (is_numeric($v) ? Category::whereKey($v)->value('id') : Category::where('name', $v)->value('id')) : null], |
| 33 | ['key' => 'brand_id', 'label' => 'Brand', 'rules' => 'nullable|string|max:255', 'sample' => 'Siemens', |
| 34 | 'export' => fn ($row) => optional(Brand::find($row->brand_id))->name, |
| 35 | 'import' => fn ($v) => $v ? (is_numeric($v) ? Brand::whereKey($v)->value('id') : Brand::where('name', $v)->value('id')) : null], |
| 36 | ['key' => 'price', 'label' => 'Regular price', 'rules' => 'nullable|numeric|min:0', 'sample' => '1200', |
| 37 | 'import' => fn ($v) => $v === '' || $v === null ? null : (float) $v], |
| 38 | ['key' => 'selling_price', 'label' => 'Sale price', 'rules' => 'nullable|numeric|min:0', 'sample' => '1100', |
| 39 | 'import' => fn ($v) => $v === '' || $v === null ? null : (float) $v], |
| 40 | ['key' => 'stock', 'label' => 'Stock', 'rules' => 'nullable|integer', 'sample' => '50', |
| 41 | 'import' => fn ($v) => $v === '' || $v === null ? null : (int) $v], |
| 42 | ['key' => 'short_details', 'label' => 'Short description', 'rules' => 'nullable|string|max:1000', 'sample' => ''], |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | public function query(): Builder |
| 47 | { |
| 48 | return Product::query()->orderByDesc('id'); |
| 49 | } |
| 50 | |
| 51 | public function applyFilters(Builder $query, array $filters): Builder |
| 52 | { |
| 53 | $s = trim((string) ($filters['search'] ?? '')); |
| 54 | |
| 55 | return $query->when($s !== '', fn ($q) => $q->where(function ($q) use ($s) { |
| 56 | $q->where('name', 'like', "%{$s}%")->orWhere('sku_no', 'like', "%{$s}%"); |
| 57 | })); |
| 58 | } |
| 59 | |
| 60 | public function uniqueBy(): array |
| 61 | { |
| 62 | return ['sku_no']; |
| 63 | } |
| 64 | |
| 65 | public function defaults(): array |
| 66 | { |
| 67 | return [ |
| 68 | 'created_by' => Auth::id(), |
| 69 | 'slug' => Str::random(10), |
| 70 | 'product_code' => 'p-' . now()->format('dmy') . '-' . rand(1000, 9999), |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | public function beforeSave($model, array $row): void |
| 75 | { |
| 76 | if (empty($model->slug) || $model->wasRecentlyCreated === false) { |
| 77 | $model->slug = $model->slug ?: Str::slug(($row['name'] ?? 'product') . '-' . Str::random(4)); |
| 78 | } |
| 79 | } |
| 80 | } |