Files
boursecu_server/app/Console/Commands/PargarRequeueWalletDocumentsCommand.php
T

112 lines
4.1 KiB
PHP
Raw Normal View History

2026-07-26 14:15:26 +03:30
<?php
namespace App\Console\Commands;
use App\Models\Customer;
use App\Services\PargarSyncService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Rooyesh\Wallet\models\Transaction;
/**
* ساخت دوبارهٔ سند ریالی برای واریز/برداشت‌هایی که سندشان به پرگار نرسیده است.
*
* تا وقتی سند نرسد، ماندهٔ پرگار این پول را ندارد و تطبیق مانده موجودی کیف را صفر می‌کند.
*/
class PargarRequeueWalletDocumentsCommand extends Command
{
protected $signature = 'pargar:requeue-wallet-documents
{--customer= : فقط همین مشتری}
{--days=30 : چند روز اخیر بررسی شود}
{--execute : ثبت واقعی سندها (بدون این فلگ فقط پیش‌نمایش است)}';
protected $description = 'صف‌کردن دوبارهٔ اسناد ریالی جاافتادهٔ پرگار (واریز/برداشت بدون سند)';
public function handle(PargarSyncService $sync): int
{
if (! $sync->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) {
$log = $sync->queueWalletDocument($customer, 'cash', (float) $row->cash, [
'withdraw_id' => (int) $row->withdraw_id,
'note' => $row->note,
'source' => 'requeue_missing',
]);
$status = $log !== null ? "ثبت شد (لاگ {$log->id})" : 'ثبت نشد';
$log !== null ? $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;
}
}