ProductTable::columns()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 49
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 41
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 49
rs 8.6417
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Product;
4
5
use Adminetic\Website\Models\Admin\Product;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Support\Facades\Blade;
8
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9
use Rappasoft\LaravelLivewireTables\Views\Column;
10
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
11
12
class ProductTable extends DataTableComponent
13
{
14
    public function builder(): Builder
15
    {
16
        return Product::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return Adminetic\Website...')->orderBy('position') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
17
            ->with('category')->orderBy('position'); // Eager load anything; // Select some things
18
    }
19
20
    public array $bulkActions = [
21
        'bulkDelete' => 'Bulk Delete',
22
    ];
23
24
    public function bulkDelete()
25
    {
26
        Product::whereIn('id', $this->getSelected())->delete();
27
    }
28
29
    public function filters(): array
30
    {
31
        return [
32
            SelectFilter::make('Active')
33
                ->options([
34
                    '' => 'All',
35
                    '1' => 'Active',
36
                    '0' => 'Inactive',
37
                ])
38
                ->filter(function (Builder $builder, string $value) {
39
                    if ($value === '1') {
40
                        $builder->where('active', true);
41
                    } elseif ($value === '0') {
42
                        $builder->where('active', false);
43
                    }
44
                }),
45
        ];
46
    }
47
48
    public function configure(): void
49
    {
50
        $this->setPrimaryKey('id');
51
52
        $this->setEagerLoadAllRelationsEnabled();
53
54
        $this->setEmptyMessage('No product found');
55
56
        $this->setReorderStatus(true);
57
58
        $this->setDefaultReorderSort('position', 'asc');
59
    }
60
61
    public function reorder($items): void
62
    {
63
        foreach ($items as $item) {
64
            Product::find((int) $item['value'])->update(['position' => (int) $item['order']]);
65
        }
66
    }
67
68
    public function columns(): array
69
    {
70
        return [
71
            Column::make('ID', 'id')
72
                ->sortable()
73
                ->searchable(),
74
            Column::make('Name', 'name')
75
                ->sortable()
76
                ->searchable(),
77
            Column::make('Category', 'category_id')
78
                ->format(
79
                    fn ($value, $row, Column $column) => $row->category->name ?? '-'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => $row->category->name ?? '-'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
                )
81
                ->sortable()
82
                ->searchable()
83
                ->collapseOnTablet(),
84
            Column::make('Active', 'active')
85
                ->format(
86
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
                )
88
                ->html()
89
                ->collapseOnTablet(),
90
            Column::make('SP', 'selling_price')
91
                ->format(
92
                    fn ($value, $row, Column $column) => ! is_null($row->selling_price) ? (currency().$row->selling_price) : ''
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => ! is_null($row->selling_price) ? (currency().$row->selling_price) : ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
                )
94
                ->html()
95
                ->collapseOnTablet(),
96
            Column::make('CP', 'cost_price')
97
                ->format(
98
                    fn ($value, $row, Column $column) => ! is_null($row->cost_price) ? (currency().$row->cost_price) : ''
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

98
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => ! is_null($row->cost_price) ? (currency().$row->cost_price) : ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
                )
100
                ->html()
101
                ->collapseOnTablet(),
102
            Column::make('Discount', 'discount')
103
                ->format(
104
                    fn ($value, $row, Column $column) => ! is_null($row->discount) ? (currency().$row->discount) : ''
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

104
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => ! is_null($row->discount) ? (currency().$row->discount) : ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
                )
106
                ->html()
107
                ->collapseOnTablet(),
108
            Column::make('Quantity', 'quantity')
109
                ->sortable()
110
                ->searchable(),
111
            Column::make('Action')
112
                ->label(
113
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="product" />', ['model' => $row])
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="product" />', ['model' => $row])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
                )
115
                ->html()
116
                ->collapseOnTablet(),
117
        ];
118
    }
119
}
120