<?php
 namespace App\Console\Commands; use App\Models\Order; use App\Models\Setting; use App\Services\OrderCancellationService; use Illuminate\Console\Command; class CancelUnpaidOrdersCommand extends Command { protected $signature = 'orders:cancel-unpaid {--dry-run : Sadece raporla, iptal etme}'; protected $description = 'Order Days Grace süresi dolan ödenmemiş bekleyen siparişleri iptal eder'; public function handle(OrderCancellationService $cancellationService): int { $graceDays = (int) Setting::get('order_days_grace', 0); if ($graceDays <= 0) { $this->info('order_days_grace = 0 → otomatik sipariş iptali devre dışı.'); return self::SUCCESS; } $dryRun = (bool) $this->option('dry-run'); $cutoff = now()->subDays($graceDays); $this->info("Order Days Grace: {$graceDays} gün (cutoff: {$cutoff->toDateTimeString()})"); if ($dryRun) { $this->warn('DRY RUN — değişiklik yapılmayacak.'); } $orders = Order::where('status', 'pending') ->where('created_at', '<', $cutoff) ->with('invoice') ->get(); $this->info("Aday sipariş: {$orders->count()}"); $cancelled = 0; $skipped = 0; foreach ($orders as $order) { if ($order->invoice && $order->invoice->status === 'paid') { $this->line("  → Atlandı #{$order->order_number}: fatura ödenmiş"); $skipped++; continue; } if ($dryRun) { $this->line("  → [dry-run] İptal edilecek #{$order->order_number} (oluşturma: {$order->created_at->toDateString()})"); $cancelled++; continue; } $did = $cancellationService->cancel( $order, __('Ödeme tolerans süresi (:days gün) doldu', ['days' => $graceDays]), 'auto' ); if ($did) { $this->line("  ✓ İptal edildi #{$order->order_number}"); $cancelled++; } else { $skipped++; } } $this->info("Tamamlandı — iptal: {$cancelled}, atlanan: {$skipped}"); return self::SUCCESS; } } 