Files
portal-api/app/Console/Commands/UpdateExpiryDate.php
T

55 lines
1.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\OrderItem;
use Carbon\Carbon;
use Illuminate\Console\Command;
class UpdateExpiryDate extends Command
{
protected $signature = 'app:update-expiry-date {--dry-run : Show what would be updated without making changes}';
protected $description = 'Update expiry_date for existing order items based on created_at + months';
public function handle()
{
$dryRun = $this->option('dry-run');
$query = OrderItem::whereNull('expiry_date')
->whereNotNull('months')
->where('months', '>', 0);
$count = $query->count();
if ($count === 0) {
$this->info('No order items need to be updated.');
return 0;
}
$this->info("Found {$count} order items to update.");
if ($dryRun) {
$this->warn('DRY RUN - No changes will be made.');
$query->chunk(100, function ($items) {
foreach ($items as $item) {
$expiryDate = Carbon::parse($item->created_at)->addMonths($item->months);
$this->line("Order #{$item->order_id}, Item #{$item->id}: would set expiry_date to {$expiryDate}");
}
});
} else {
$updated = 0;
$query->chunk(100, function ($items) use (&$updated) {
foreach ($items as $item) {
$expiryDate = Carbon::parse($item->created_at)->addMonths($item->months);
$item->update(['expiry_date' => $expiryDate]);
$updated++;
}
});
$this->info("Updated {$updated} order items.");
}
return 0;
}
}