Files
sembara/app/Filament/Pages/Inventory/Stocks.php
T

117 lines
4.7 KiB
PHP

<?php
namespace App\Filament\Pages\Inventory;
use App\Services\StockService;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Pages\Page;
use Filament\Schemas\Components\EmbeddedTable;
use Filament\Schemas\Schema;
use Filament\Support\Enums\FontWeight;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Gate;
use UnitEnum;
class Stocks extends Page implements HasTable
{
use InteractsWithTable;
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-archive-box';
protected static string|UnitEnum|null $navigationGroup = 'Inventori';
protected static ?int $navigationSort = 0;
protected static ?string $navigationLabel = 'Stok';
protected static ?string $slug = 'inventory/stocks';
protected string $view = 'filament.pages.blank';
public function getTitle(): string|Htmlable
{
return 'Stok';
}
public function table(Table $table): Table
{
/** @var StockService $service */
$service = app(StockService::class);
return $table
->records(fn (?string $sortColumn, ?string $sortDirection, int|string $page, int|string $recordsPerPage) => $service->paginate(
sortColumn: $sortColumn,
sortDirection: $sortDirection,
page: (int) $page,
perPage: (int) $recordsPerPage,
))
->columns([
TextColumn::make('products.code')->label('Kode')->badge()->sortable(false),
TextColumn::make('products.name')->label('Produk')->weight(FontWeight::SemiBold),
TextColumn::make('quantity')->label('Saldo')->numeric()->sortable(),
TextColumn::make('products.stock_minimum')->label('Min.'),
TextColumn::make('status')
->label('Status')
->state(fn (array $record): string => (int) $record['quantity'] <= (int) ($record['products']['stock_minimum'] ?? 0) ? 'kritis' : 'aman')
->badge()
->color(fn (string $state): string => match ($state) {
'kritis' => 'danger',
default => 'success',
}),
TextColumn::make('updated_at')->label('Diperbarui')->dateTime('d M Y H:i', 'Asia/Jakarta')->sortable(),
])
->recordActions([
Action::make('restok')
->label('Restok')
->icon('heroicon-o-plus-circle')
->visible(fn (): bool => Gate::check('manage-stock'))
->schema(fn (Schema $schema): Schema => $schema->components([
TextInput::make('quantity')->label('Jumlah Masuk')->numeric()->minValue(1)->required(),
Textarea::make('notes')->label('Catatan'),
]))
->action(function (array $data, array $record): void {
app(StockService::class)->restock(
$record['product_id'],
(int) $data['quantity'],
$data['notes'] ?? null,
auth()->user(),
);
}),
Action::make('koreksi')
->label('Koreksi')
->icon('heroicon-o-adjustments-horizontal')
->color('warning')
->visible(fn (): bool => Gate::check('manage-stock'))
->schema(fn (Schema $schema): Schema => $schema->components([
TextInput::make('new_quantity')->label('Saldo Baru')->numeric()->minValue(0)->required()
->default(fn (array $record): int => (int) $record['quantity']),
Textarea::make('notes')->label('Alasan Koreksi'),
]))
->action(function (array $data, array $record): void {
app(StockService::class)->adjust(
$record['product_id'],
(int) $data['new_quantity'],
$data['notes'] ?? null,
auth()->user(),
);
}),
])
->paginated([10, 25, 50]);
}
public function content(Schema $schema): Schema
{
return $schema
->components([
EmbeddedTable::make(),
]);
}
}