feat: Decouple feed deliveries from schedules, add soft deletes to deliveries, and dynamically schedule feed processing by time.

This commit is contained in:
Yoga Pangestu 2026-02-26 12:50:54 +07:00
parent 9ae00ebacd
commit 66793f6b2e
8 changed files with 71 additions and 43 deletions

View File

@ -8,25 +8,26 @@
class ProcessFeedSchedule extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'feed:schedule-process';
protected $signature = 'feed:schedule-process {time?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process any pending feed schedules and adjust stock accordingly';
protected $description = 'Process any pending feed schedules for a specific time and adjust stock accordingly';
public function handle(): int
{
$now = now();
$time = $this->argument('time') ?? now()->format('H:i');
$schedules = FeedSchedule::all();
$schedules = FeedSchedule::whereTime('scheduled_at', $time)->get();
if ($schedules->isEmpty()) {
$this->info("No schedules found for time: {$time}");
return 0;
}
foreach ($schedules as $schedule) {
$feed = $schedule->feed;
@ -36,15 +37,14 @@ public function handle(): int
}
FeedDelivery::create([
'feed_schedule_id' => $schedule->id,
'feed_id' => $schedule->feed_id,
'quantity' => $schedule->quantity,
'delivered_at' => $now,
'delivered_at' => now(),
]);
$schedule->save();
}
$this->info('Processed '.$schedules->count()." feed schedules for time: {$time}");
return 0;
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Filament\Resources\Manage\Deliveries;
namespace App\Filament\Resources\Manage\FeedDeliveries;
use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction;
@ -13,7 +13,10 @@
use App\Models\FeedDelivery;
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;
@ -35,30 +38,53 @@ class FeedDeliveryResource extends Resource
protected static ?int $navigationSort = 2;
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $recordTitleAttribute = 'delivered_at';
protected static ?string $slug = 'manage/feed-deliveries';
public static function form(Schema $schema): Schema
{
return $schema
->components([
Select::make('feed_id')
->label('Pakan')
->relationship('feed', 'name')
->required()
->searchable()
->preload(),
TextInput::make('quantity')
->label('Kuantitas')
->placeholder('0')
->autocomplete(false)
->required()
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0)))
->helperText('Satuan kuantitas pakan sesuai dengan satuan yang ada di menu pakan.'),
])
->columns(1);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('feed.name')
->label('Pakan')
->sortable(),
->sortable()
->searchable(),
TextColumn::make('quantity')
->label('Qty')
->numeric(),
->label('Kuantitas')
->numeric(decimalPlaces: 0)
->suffix(fn (FeedDelivery $record) => ' '.$record->feed?->unit?->alias)
->searchable(),
TextColumn::make('delivered_at')
->label('Tanggal Disalurkan')
->dateTime('d M Y H:i')
->sortable(),
TextColumn::make('schedule.id')
->label('Jadwal ID')
->sortable(),
->dateTime('l, d M Y H:i')
->sortable()
->searchable(),
...TimestampColumns::make(),
])

View File

@ -2,7 +2,7 @@
namespace App\Filament\Resources\Manage\FeedDeliveries\Pages;
use App\Filament\Resources\Manage\Deliveries\FeedDeliveryResource;
use App\Filament\Resources\Manage\FeedDeliveries\FeedDeliveryResource;
use Filament\Resources\Pages\ManageRecords;
class ManageFeedDeliveries extends ManageRecords

View File

@ -33,13 +33,13 @@ class FeedScheduleResource extends Resource
protected static string|UnitEnum|null $navigationGroup = 'Kelola';
protected static string|BackedEnum|null $navigationIcon = Heroicon::Clock;
protected static string|BackedEnum|null $navigationIcon = Heroicon::BellAlert;
protected static ?string $navigationLabel = 'Jadwal Pakan';
protected static ?int $navigationSort = 2;
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $recordTitleAttribute = 'scheduled_at';
protected static ?string $slug = 'manage/feed-schedules';
@ -113,7 +113,7 @@ public static function table(Table $table): Table
...DefaultBulkActions::make('Jadwal Pakan'),
]),
])
->emptyStateIcon(Heroicon::Clock)
->emptyStateIcon(Heroicon::BellAlert)
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
->defaultSort('created_at', 'desc')
->deferFilters(false);

View File

@ -5,10 +5,11 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class FeedDelivery extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;
protected $guarded = ['id'];
@ -19,11 +20,6 @@ protected function casts(): array
];
}
public function schedule(): BelongsTo
{
return $this->belongsTo(FeedSchedule::class);
}
public function feed(): BelongsTo
{
return $this->belongsTo(Feed::class);

View File

@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class FeedSchedule extends Model
@ -25,9 +24,4 @@ public function feed(): BelongsTo
{
return $this->belongsTo(Feed::class);
}
public function deliveries(): HasMany
{
return $this->hasMany(FeedDelivery::class);
}
}

View File

@ -13,11 +13,12 @@ public function up(): void
{
Schema::create('feed_deliveries', function (Blueprint $table) {
$table->id();
$table->foreignId('feed_schedule_id')->nullable()->constrained()->cascadeOnDelete();
$table->foreignId('feed_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('quantity');
$table->timestamp('delivered_at')->useCurrent();
$table->timestamps();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes();
});
}

View File

@ -1,5 +1,16 @@
<?php
use App\Models\FeedSchedule;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Facades\Schema;
Schedule::command('feed:schedule-process')->everyMinute();
if (Schema::hasTable('feed_schedules')) {
$times = FeedSchedule::query()
->distinct()
->pluck('scheduled_at');
foreach ($times as $time) {
$formattedTime = date('H:i', strtotime($time));
Schedule::command('feed:schedule-process', [$formattedTime])->at($formattedTime);
}
}