feat: introduce feed management module, including model, migration, status enum, and Filament resource.
This commit is contained in:
parent
4e21468348
commit
ba6ca892dd
42
app/Enums/FeedStatus.php
Normal file
42
app/Enums/FeedStatus.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
use App\Traits\Enum\WithComment;
|
||||||
|
use App\Traits\Enum\WithValue;
|
||||||
|
use Filament\Support\Contracts\HasColor;
|
||||||
|
use Filament\Support\Contracts\HasLabel;
|
||||||
|
|
||||||
|
enum FeedStatus: int implements HasColor, HasLabel
|
||||||
|
{
|
||||||
|
use WithComment, WithValue;
|
||||||
|
|
||||||
|
case AVAILABLE = 1;
|
||||||
|
case OUT_OF_STOCK = 2;
|
||||||
|
case DISCONTINUED = 3;
|
||||||
|
|
||||||
|
public function getLabel(): ?string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::AVAILABLE => 'Tersedia',
|
||||||
|
self::OUT_OF_STOCK => 'Habis',
|
||||||
|
self::DISCONTINUED => 'Tidak Digunakan',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getColor(): string|array|null
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::AVAILABLE => 'success',
|
||||||
|
self::OUT_OF_STOCK => 'danger',
|
||||||
|
self::DISCONTINUED => 'gray',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function options(): array
|
||||||
|
{
|
||||||
|
return collect(self::cases())
|
||||||
|
->mapWithKeys(fn ($case) => [$case->value => $case->getLabel()])
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
142
app/Filament/Resources/Master/Feeds/FeedResource.php
Normal file
142
app/Filament/Resources/Master/Feeds/FeedResource.php
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Feeds;
|
||||||
|
|
||||||
|
use App\Enums\FeedStatus;
|
||||||
|
use App\Filament\Actions\Cheerful\DeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\EditAction;
|
||||||
|
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\RestoreAction;
|
||||||
|
use App\Filament\Actions\DefaultBulkActions;
|
||||||
|
use App\Filament\Columns\TimestampColumns;
|
||||||
|
use App\Filament\Resources\Master\Feeds\Pages\ManageFeeds;
|
||||||
|
use App\Models\Feed;
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Enums\Width;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
|
use Filament\Tables\Filters\TrashedFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class FeedResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = Feed::class;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Master';
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::CircleStack;
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Pakan';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 3;
|
||||||
|
|
||||||
|
protected static ?string $recordTitleAttribute = 'name';
|
||||||
|
|
||||||
|
protected static ?string $slug = 'master/feeds';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('name')
|
||||||
|
->label('Nama Pakan')
|
||||||
|
->placeholder('Pelet')
|
||||||
|
->autocomplete(false)
|
||||||
|
->autofocus()
|
||||||
|
->required()
|
||||||
|
->maxLength(50),
|
||||||
|
|
||||||
|
Select::make('unit_id')
|
||||||
|
->label('Satuan')
|
||||||
|
->relationship('unit', 'name')
|
||||||
|
->required()
|
||||||
|
->native(false)
|
||||||
|
->searchable()
|
||||||
|
->preload(),
|
||||||
|
|
||||||
|
Select::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->options(FeedStatus::class)
|
||||||
|
->required()
|
||||||
|
->native(false)
|
||||||
|
->default(FeedStatus::AVAILABLE),
|
||||||
|
])
|
||||||
|
->columns(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('name')
|
||||||
|
->label('Nama Pakan')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('stock')
|
||||||
|
->label('Stok')
|
||||||
|
->numeric(decimalPlaces: 2)
|
||||||
|
->suffix(fn (Feed $record) => ' '.$record->unit->alias)
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->badge()
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
...TimestampColumns::make(),
|
||||||
|
])
|
||||||
|
->filters([
|
||||||
|
SelectFilter::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->options(FeedStatus::class)
|
||||||
|
->native(false),
|
||||||
|
|
||||||
|
TrashedFilter::make()
|
||||||
|
->native(false),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make()
|
||||||
|
->modalWidth(Width::Large),
|
||||||
|
|
||||||
|
DeleteAction::make(),
|
||||||
|
|
||||||
|
ForceDeleteAction::make(),
|
||||||
|
|
||||||
|
RestoreAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
...DefaultBulkActions::make('Pakan'),
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->emptyStateIcon(Heroicon::CircleStack)
|
||||||
|
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->deferFilters(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ManageFeeds::route('/'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRecordRouteBindingEloquentQuery(): Builder
|
||||||
|
{
|
||||||
|
return parent::getRecordRouteBindingEloquentQuery()
|
||||||
|
->withoutGlobalScopes([
|
||||||
|
SoftDeletingScope::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
app/Filament/Resources/Master/Feeds/Pages/ManageFeeds.php
Normal file
31
app/Filament/Resources/Master/Feeds/Pages/ManageFeeds.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Feeds\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Actions\Cheerful\CreateAction;
|
||||||
|
use App\Filament\Resources\Master\Feeds\FeedResource;
|
||||||
|
use Filament\Resources\Pages\ManageRecords;
|
||||||
|
use Filament\Support\Enums\Width;
|
||||||
|
|
||||||
|
class ManageFeeds extends ManageRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = FeedResource::class;
|
||||||
|
|
||||||
|
protected static ?string $title = 'Pakan';
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make()
|
||||||
|
->label('Tambah')
|
||||||
|
->modalHeading('Tambah Pakan')
|
||||||
|
->modalSubmitActionLabel('Simpan')
|
||||||
|
->modalCancelActionLabel('Batal')
|
||||||
|
->extraModalFooterActions(fn (CreateAction $action): array => [
|
||||||
|
$action->makeModalSubmitAction('createAnother', arguments: ['another' => true])
|
||||||
|
->label('Simpan dan Tambah Lagi'),
|
||||||
|
])
|
||||||
|
->modalWidth(Width::Large),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
34
app/Models/Feed.php
Normal file
34
app/Models/Feed.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\FeedStatus;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Feed extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'stock' => 'decimal:2',
|
||||||
|
'status' => FeedStatus::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unit(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Unit::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function purchases(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(FeedPurchase::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
database/migrations/2026_02_09_024536_create_feeds_table.php
Normal file
34
database/migrations/2026_02_09_024536_create_feeds_table.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\FeedStatus;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('feeds', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name', 50);
|
||||||
|
$table->foreignId('unit_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->decimal('stock', 15, 2)->default(0);
|
||||||
|
$table->enum('status', FeedStatus::cases())->default(FeedStatus::AVAILABLE->value)->comment(FeedStatus::comment());
|
||||||
|
$table->timestamp('created_at')->useCurrent();
|
||||||
|
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||||
|
$table->softDeletes();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('feeds');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user