51 lines
1.1 KiB
PHP
51 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\FeedDelivery;
|
|
use App\Models\FeedSchedule;
|
|
use Illuminate\Console\Command;
|
|
|
|
class ProcessFeedSchedule extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'feed:schedule-process';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Process any pending feed schedules and adjust stock accordingly';
|
|
|
|
public function handle(): int
|
|
{
|
|
$now = now();
|
|
|
|
$schedules = FeedSchedule::all();
|
|
|
|
foreach ($schedules as $schedule) {
|
|
$feed = $schedule->feed;
|
|
if ($feed) {
|
|
$decrement = min($schedule->quantity, $feed->stock);
|
|
$feed->decrement('stock', $decrement);
|
|
}
|
|
|
|
FeedDelivery::create([
|
|
'feed_schedule_id' => $schedule->id,
|
|
'feed_id' => $schedule->feed_id,
|
|
'quantity' => $schedule->quantity,
|
|
'delivered_at' => $now,
|
|
]);
|
|
|
|
$schedule->save();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|