isSyncEnabled()) { $this->error('همگام‌سازی پرگار فعال نیست.'); return self::FAILURE; } $days = max(1, (int) $this->option('days')); $execute = (bool) $this->option('execute'); $customerId = (int) $this->option('customer'); $query = Transaction::query() ->whereIn('type', [3, 4]) ->whereNotNull('withdraw_id') ->where('created_at', '>=', now()->subDays($days)) ->where(function ($q) { $q->where('cash', '>=', 1)->orWhere('cash', '<=', -1); }) ->whereNotExists(function ($q) { $q->select(DB::raw(1)) ->from('pargar_sync_logs') ->whereColumn('pargar_sync_logs.withdraw_id', 'transactions.withdraw_id') ->where('pargar_sync_logs.type', PargarSyncService::TYPE_WALLET_CASH); }) ->orderBy('id'); if ($customerId > 0) { $query->where('customer_id', $customerId); } $rows = $query->get(['id', 'customer_id', 'withdraw_id', 'cash', 'note', 'created_at']); if ($rows->isEmpty()) { $this->info('سند جاافتاده‌ای پیدا نشد.'); return self::SUCCESS; } $customers = Customer::whereIn('id', $rows->pluck('customer_id')->unique())->get()->keyBy('id'); $table = []; $queued = 0; $skipped = 0; foreach ($rows as $row) { $customer = $customers->get((int) $row->customer_id); $status = 'آماده'; if ($customer === null || ! $customer->hasValidPargarAccountId()) { $status = 'بدون کد پرگار — رد شد'; $skipped++; } elseif ($execute) { $queuedCount = $sync->queueMissingCashDocuments($customer, [$row], 'requeue_missing'); $status = $queuedCount > 0 ? 'ثبت/موجود شد' : 'ثبت نشد'; $queuedCount > 0 ? $queued++ : $skipped++; } $table[] = [ $row->id, $row->customer_id, $customer->name ?? '—', $row->withdraw_id, number_format((float) $row->cash), (string) $row->created_at, $status, ]; } $this->table( ['تراکنش', 'مشتری', 'نام', 'سند برداشت', 'مبلغ', 'تاریخ', 'وضعیت'], $table ); if (! $execute) { $this->warn('پیش‌نمایش بود. برای ثبت واقعی --execute بزنید.'); return self::SUCCESS; } $this->info("ثبت‌شده: {$queued} — ردشده: {$skipped}"); return self::SUCCESS; } }