93 lines
3.5 KiB
PHP
93 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Datatable\Manage;
|
|
|
|
use App\Models\Purchase;
|
|
use App\Traits\Datatable\WithAppendColumn;
|
|
use App\Traits\Datatable\WithConfiguration;
|
|
use App\Traits\Datatable\WithPrependColumn;
|
|
use App\Traits\Media\WithMediaHandler;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
|
|
|
|
class PurchasesTable extends DataTableComponent
|
|
{
|
|
use WithAppendColumn, WithConfiguration, WithMediaHandler, WithPrependColumn;
|
|
|
|
protected $model = Purchase::class;
|
|
|
|
protected string $sortColumn = 'purchase_date';
|
|
|
|
protected string $sortDirection = 'desc';
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make('Gudang', 'warehouse.name')->searchable(),
|
|
|
|
Column::make('Nomor Invoice', 'invoice_number')->searchable(),
|
|
|
|
Column::make('Tanggal Belanja', 'purchase_date')
|
|
->format(fn ($value) => formatDateLocalized($value))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Column::make('Total', 'total')
|
|
->format(fn ($value) => formatCurrencyNumber($value, 'Rp'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Column::make('Catatan', 'note')->searchable(),
|
|
|
|
ArrayColumn::make('Item')
|
|
->data(
|
|
fn ($value, $row) => $row->items->map(fn ($item) => [
|
|
'name' => $item->purchasable?->name,
|
|
'quantity' => formatCurrencyNumber($item->quantity, ''),
|
|
'unit_price' => formatCurrencyNumber($item->unit_price, 'Rp'),
|
|
'total_price' => formatCurrencyNumber($item->total_price, 'Rp'),
|
|
])->toArray()
|
|
)
|
|
->outputFormat(
|
|
fn ($index, $value) => "
|
|
<div class='text-[13px] leading-tight mb-1'>
|
|
<div class='font-bold text-gray-800 dark:text-white'>{$value['name']}</div>
|
|
<div class='text-gray-400 text-[12px]'>{$value['quantity']} x {$value['unit_price']}</div>
|
|
<div class='text-gray-400 text-[12px]'>{$value['total_price']}</div>
|
|
</div>"
|
|
)
|
|
->flexCol(['class' => 'flex-col gap-3'])
|
|
->searchable(function ($query, $searchTerm) {
|
|
$query->orWhereHas('items', function (Builder $query) use ($searchTerm) {
|
|
$query->whereHas('purchasable', function (Builder $query) use ($searchTerm) {
|
|
$query->where('name', 'like', "%{$searchTerm}%");
|
|
});
|
|
});
|
|
}),
|
|
|
|
Column::make('Aksi')
|
|
->label(function ($row) {
|
|
$actions = '';
|
|
|
|
if (auth()->user()->can('delete purchase')) {
|
|
$actions .= view('components.actions.table.delete', [
|
|
'id' => $row->hash,
|
|
])->render();
|
|
}
|
|
|
|
return $actions;
|
|
})
|
|
->html()
|
|
->hideIf(auth()->user()->cannot('delete purchase')),
|
|
];
|
|
}
|
|
|
|
public function builder(): Builder
|
|
{
|
|
return Purchase::select('purchases.id', 'warehouse_id', 'invoice_number', 'purchase_date', 'total')
|
|
->with(['warehouse', 'items', 'items.purchasable']);
|
|
}
|
|
}
|