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 class ProcessFeedSchedule extends Command
{ {
/** protected $signature = 'feed:schedule-process {time?}';
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'feed:schedule-process';
/** /**
* The console command description. * The console command description.
* *
* @var string * @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 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) { foreach ($schedules as $schedule) {
$feed = $schedule->feed; $feed = $schedule->feed;
@ -36,15 +37,14 @@ public function handle(): int
} }
FeedDelivery::create([ FeedDelivery::create([
'feed_schedule_id' => $schedule->id,
'feed_id' => $schedule->feed_id, 'feed_id' => $schedule->feed_id,
'quantity' => $schedule->quantity, 'quantity' => $schedule->quantity,
'delivered_at' => $now, 'delivered_at' => now(),
]); ]);
$schedule->save();
} }
$this->info('Processed '.$schedules->count()." feed schedules for time: {$time}");
return 0; return 0;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Filament\Resources\Manage\Deliveries; namespace App\Filament\Resources\Manage\FeedDeliveries;
use App\Enums\RoleEnum; use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction; use App\Filament\Actions\Cheerful\DeleteAction;
@ -13,7 +13,10 @@
use App\Models\FeedDelivery; use App\Models\FeedDelivery;
use BackedEnum; use BackedEnum;
use Filament\Actions\BulkActionGroup; use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width; use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon; use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn; use Filament\Tables\Columns\TextColumn;
@ -35,30 +38,53 @@ class FeedDeliveryResource extends Resource
protected static ?int $navigationSort = 2; protected static ?int $navigationSort = 2;
protected static ?string $recordTitleAttribute = 'name'; protected static ?string $recordTitleAttribute = 'delivered_at';
protected static ?string $slug = 'manage/feed-deliveries'; 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 public static function table(Table $table): Table
{ {
return $table return $table
->columns([ ->columns([
TextColumn::make('feed.name') TextColumn::make('feed.name')
->label('Pakan') ->label('Pakan')
->sortable(), ->sortable()
->searchable(),
TextColumn::make('quantity') TextColumn::make('quantity')
->label('Qty') ->label('Kuantitas')
->numeric(), ->numeric(decimalPlaces: 0)
->suffix(fn (FeedDelivery $record) => ' '.$record->feed?->unit?->alias)
->searchable(),
TextColumn::make('delivered_at') TextColumn::make('delivered_at')
->label('Tanggal Disalurkan') ->label('Tanggal Disalurkan')
->dateTime('d M Y H:i') ->dateTime('l, d M Y H:i')
->sortable(), ->sortable()
->searchable(),
TextColumn::make('schedule.id')
->label('Jadwal ID')
->sortable(),
...TimestampColumns::make(), ...TimestampColumns::make(),
]) ])

View File

@ -2,7 +2,7 @@
namespace App\Filament\Resources\Manage\FeedDeliveries\Pages; 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; use Filament\Resources\Pages\ManageRecords;
class ManageFeedDeliveries extends 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|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 ?string $navigationLabel = 'Jadwal Pakan';
protected static ?int $navigationSort = 2; protected static ?int $navigationSort = 2;
protected static ?string $recordTitleAttribute = 'name'; protected static ?string $recordTitleAttribute = 'scheduled_at';
protected static ?string $slug = 'manage/feed-schedules'; protected static ?string $slug = 'manage/feed-schedules';
@ -113,7 +113,7 @@ public static function table(Table $table): Table
...DefaultBulkActions::make('Jadwal Pakan'), ...DefaultBulkActions::make('Jadwal Pakan'),
]), ]),
]) ])
->emptyStateIcon(Heroicon::Clock) ->emptyStateIcon(Heroicon::BellAlert)
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.') ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
->defaultSort('created_at', 'desc') ->defaultSort('created_at', 'desc')
->deferFilters(false); ->deferFilters(false);

View File

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

View File

@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
class FeedSchedule extends Model class FeedSchedule extends Model
@ -25,9 +24,4 @@ public function feed(): BelongsTo
{ {
return $this->belongsTo(Feed::class); 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) { Schema::create('feed_deliveries', function (Blueprint $table) {
$table->id(); $table->id();
$table->foreignId('feed_schedule_id')->nullable()->constrained()->cascadeOnDelete();
$table->foreignId('feed_id')->constrained()->cascadeOnDelete(); $table->foreignId('feed_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('quantity'); $table->unsignedInteger('quantity');
$table->timestamp('delivered_at')->useCurrent(); $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 <?php
use App\Models\FeedSchedule;
use Illuminate\Support\Facades\Schedule; 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);
}
}