label(function ($row) {
return <<
{$row->name}
{$row->size} ml
HTML;
})
->searchable(function ($query, $searchTerm) {
$query->where('name', 'like', "%{$searchTerm}%")
->orWhere('size', 'like', "%{$searchTerm}%");
})
->html(),
Column::make('Harga Beli', 'cost_price')
->label(function ($row) {
$canSeePrice = PriceRequest::where('user_id', auth()->id())
->where('model', Bottle::class)
->whereNotNull('approved_at')
->where('expires_at', '>', now())
->exists();
if ($canSeePrice) {
return currency($row->cost_price, 'Rp');
}
$masked = Str::mask(currency($row->cost_price, 'Rp'), '*', 2);
return Blade::render('
{{ $masked }}
', ['masked' => $masked]);
})
->searchable()
->sortable()
->html(),
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) => [
'id' => $outlet->hash,
'bottle_id' => $row->hash,
'name' => $outlet->name,
'stock' => currency($outlet->pivot->stock, ''),
])->toArray()
)
->outputFormat(function ($index, $value) {
$route = route('studio.catalog.bottle.audit', [$value['id'], $value['bottle_id']]);
return Blade::render('
{{ $value["name"] }}
{{ $value["stock"] }}
', [
'value' => $value,
'route' => $route,
]);
})
->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');
}
}