layer-chicken/app/Console/Commands/ProcessFeedSchedule.php

51 lines
1.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\FeedDelivery;
use App\Models\FeedSchedule;
use Illuminate\Console\Command;
class ProcessFeedSchedule extends Command
{
protected $signature = 'feed:schedule-process {time?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process any pending feed schedules for a specific time and adjust stock accordingly';
public function handle(): int
{
$time = $this->argument('time') ?? now()->format('H:i');
$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 ($schedule->feeds as $feed) {
$quantity = $feed->pivot->quantity;
$decrement = min($quantity, $feed->stock);
$feed->decrement('stock', $decrement);
FeedDelivery::create([
'feed_id' => $feed->id,
'quantity' => $quantity,
'delivered_at' => now(),
]);
}
}
$this->info('Processed '.$schedules->count()." feed schedules for time: {$time}");
return 0;
}
}