100 lines
3.8 KiB
PHP
100 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Datatable\Studio\Catalog;
|
|
|
|
use App\Models\Bottle;
|
|
use App\Traits\Datatable\WithConfiguration;
|
|
use App\Traits\Datatable\WithPrependColumn;
|
|
use App\Traits\WithMediaHandler;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
|
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
|
|
|
|
class BottlesTable extends DataTableComponent
|
|
{
|
|
use WithConfiguration, WithMediaHandler, WithPrependColumn;
|
|
|
|
protected $model = Bottle::class;
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make('Nama')
|
|
->label(function ($row) {
|
|
return <<<HTML
|
|
<div>
|
|
<div>{$row->name}</div>
|
|
<span class="text-xs text-gray-400">{$row->size} ml</span>
|
|
</div>
|
|
HTML;
|
|
})
|
|
->searchable(function ($query, $searchTerm) {
|
|
$query->where('name', 'like', "%{$searchTerm}%")
|
|
->orWhere('size', 'like', "%{$searchTerm}%");
|
|
})
|
|
->html(),
|
|
|
|
Column::make('Harga Beli', 'cost_price')
|
|
->label(fn ($row, $column) => currency($row->cost_price, 'Rp'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Column::make('Harga Jual', 'sale_price')
|
|
->label(fn ($row, $column) => currency($row->sale_price, 'Rp'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
ImageColumn::make('Gambar')
|
|
->location(fn ($row) => optional($row->getMedia('image')->first())->getUrl() ?? asset('assets/images/logo.png'))
|
|
->attributes(fn ($row) => [
|
|
'style' => 'width: 50px; height: 50px;',
|
|
'alt' => $row->name,
|
|
]),
|
|
|
|
ArrayColumn::make('Outlet')
|
|
->data(
|
|
fn ($value, $row) => $row->outlets->map(fn ($outlet) => [
|
|
'name' => $outlet->name,
|
|
'stock' => $outlet->pivot->stock,
|
|
])->toArray()
|
|
)
|
|
->outputFormat(fn ($index, $value) => Blade::render('
|
|
<div class="flex items-center space-x-2 bg-gray-50 border border-gray-200 rounded-lg px-2 py-1 text-xs">
|
|
<span class="font-medium text-gray-700">{{ $value["name"] }}</span>
|
|
<span class="text-[10px] text-white bg-emerald-500 rounded px-1.5 py-0.5">{{ $value["stock"] }}</span>
|
|
</div>', ['value' => $value]))
|
|
->flexRow(['class' => 'gap-2 flex-wrap']),
|
|
|
|
Column::make('Aksi')
|
|
->label(function ($row) {
|
|
$actions = '';
|
|
|
|
if (auth()->user()->can('update bottle')) {
|
|
$actions .= view('components.datatables.edit', [
|
|
'id' => $row->hash,
|
|
'editRoute' => route('studio.catalog.bottle.edit', $row->hash),
|
|
])->render();
|
|
}
|
|
|
|
if (auth()->user()->can('delete bottle')) {
|
|
$actions .= view('components.datatables.delete', [
|
|
'id' => $row->hash,
|
|
'deleteRoute' => route('studio.catalog.bottle.delete', $row->hash),
|
|
])->render();
|
|
}
|
|
|
|
return $actions;
|
|
})
|
|
->html(),
|
|
];
|
|
}
|
|
|
|
public function builder(): Builder
|
|
{
|
|
return Bottle::select('id', 'name', 'size', 'cost_price', 'sale_price')->with('outlets');
|
|
}
|
|
}
|