2026-07-21 12:36:59 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\PargarSyncLog;
|
2026-07-21 14:13:26 +03:30
|
|
|
use Illuminate\Support\Facades\Cache;
|
2026-07-21 12:36:59 +03:30
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
2026-07-21 22:23:09 +03:30
|
|
|
use Rooyesh\Wallet\models\Wallet;
|
2026-07-21 12:36:59 +03:30
|
|
|
|
|
|
|
|
/**
|
2026-07-22 01:05:00 +03:30
|
|
|
* همگامسازی کیف با پرگار.
|
2026-07-21 14:13:26 +03:30
|
|
|
*
|
2026-07-21 14:33:16 +03:30
|
|
|
* قواعد سخت:
|
2026-07-22 01:05:00 +03:30
|
|
|
* 1) webhook اسناد inbound فقط لاگ میکند؛ کیف را با replay تاریخچه عوض نمیکند.
|
|
|
|
|
* 2) مانده برای مشتریانی که سند ارسالیِ pending به پرگار ندارند → خودکار روی کیف ست میشود.
|
|
|
|
|
* 3) اگر سند پنل هنوز در پرگار ثبت نشده، مانده اعمال نمیشود؛ بعد از تأیید ثبت در پرگار،
|
|
|
|
|
* ماندهٔ جدید خودکار سینک میشود (تا موجودی مشتری بیدلیل کم/زیاد نشود).
|
|
|
|
|
* 4) دکمهٔ ادمین میتواند force کند.
|
|
|
|
|
* 5) اعمال تکتک سطرهای تاریخچهٔ پرگار ممنوع است.
|
2026-07-21 12:36:59 +03:30
|
|
|
*/
|
|
|
|
|
class PargarInboundWalletService
|
|
|
|
|
{
|
|
|
|
|
public const TX_TYPE_PARGAR = 22;
|
|
|
|
|
|
2026-07-21 14:23:26 +03:30
|
|
|
public const KILL_SWITCH_CACHE_KEY = 'pargar_wallet_apply_disabled';
|
2026-07-21 14:13:26 +03:30
|
|
|
|
2026-07-21 21:38:48 +03:30
|
|
|
/** سقف تعداد دسته برای علامتزدن در هر کلیک */
|
|
|
|
|
private const MAX_BATCHES_PER_SYNC = 50;
|
2026-07-21 14:33:16 +03:30
|
|
|
|
2026-07-21 12:36:59 +03:30
|
|
|
public function __construct(
|
|
|
|
|
private readonly WalletBroadcastService $walletBroadcast
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:23:26 +03:30
|
|
|
public static function isApplyFrozen(): bool
|
|
|
|
|
{
|
2026-07-21 14:33:16 +03:30
|
|
|
return (bool) Cache::get(self::KILL_SWITCH_CACHE_KEY, false);
|
2026-07-21 14:23:26 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function freezeApplies(bool $frozen = true): void
|
|
|
|
|
{
|
|
|
|
|
Cache::forever(self::KILL_SWITCH_CACHE_KEY, $frozen);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 12:36:59 +03:30
|
|
|
/**
|
2026-07-22 01:05:00 +03:30
|
|
|
* آیا سندی از پنل در صف ثبت داخل پرگار است؟ (کیف یا سفارش)
|
|
|
|
|
* در این حالت مانده را اعمال نمیکنیم تا بعد از ثبت سند، ماندهٔ درست بیاید.
|
|
|
|
|
*/
|
|
|
|
|
public function hasPendingOutboundToPargar(Customer $customer): bool
|
|
|
|
|
{
|
|
|
|
|
return PargarSyncLog::query()
|
|
|
|
|
->where('customer_id', $customer->id)
|
|
|
|
|
->whereIn('type', [
|
|
|
|
|
PargarSyncService::TYPE_WALLET_CASH,
|
|
|
|
|
PargarSyncService::TYPE_WALLET_GOLD,
|
|
|
|
|
PargarSyncService::TYPE_ORDER,
|
|
|
|
|
])
|
|
|
|
|
->where('sync_status', PargarSyncService::STATUS_PENDING)
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* دکمهٔ ادمین / همگامسازی خودکار:
|
|
|
|
|
* ۱) علامت مصرف دستههای سند inbound (بدون نوشتن روی کیف)
|
2026-07-21 21:38:48 +03:30
|
|
|
* ۲) همترازی مانده با آخرین لاگ mande
|
2026-07-21 14:33:16 +03:30
|
|
|
*
|
2026-07-22 01:05:00 +03:30
|
|
|
* @param bool $forceMande ادمین: حتی با سند ارسالی pending
|
|
|
|
|
* @param bool $auto مسیر خودکار: سند ارسالی pending محترم بماند
|
2026-07-21 14:13:26 +03:30
|
|
|
* @return array{
|
2026-07-21 21:38:48 +03:30
|
|
|
* documents: array{applied: int, skipped: int, batches: int, acknowledged: int, errors: list<string>},
|
2026-07-21 14:13:26 +03:30
|
|
|
* mande: array{applied: bool, gold_delta?: float, rial_delta?: float, message: string}
|
|
|
|
|
* }
|
2026-07-21 12:49:08 +03:30
|
|
|
*/
|
2026-07-22 01:05:00 +03:30
|
|
|
public function syncWalletFromPargar(Customer $customer, bool $forceMande = false, bool $auto = false): array
|
2026-07-21 12:49:08 +03:30
|
|
|
{
|
2026-07-21 14:33:16 +03:30
|
|
|
if (self::isApplyFrozen()) {
|
2026-07-21 14:23:26 +03:30
|
|
|
return [
|
2026-07-21 21:38:48 +03:30
|
|
|
'documents' => [
|
|
|
|
|
'applied' => 0,
|
|
|
|
|
'skipped' => 0,
|
|
|
|
|
'batches' => 0,
|
|
|
|
|
'acknowledged' => 0,
|
|
|
|
|
'errors' => [],
|
|
|
|
|
],
|
2026-07-21 14:23:26 +03:30
|
|
|
'mande' => [
|
|
|
|
|
'applied' => false,
|
2026-07-21 14:33:16 +03:30
|
|
|
'message' => 'اعمال کیف یخزده است — php artisan pargar:wallet-freeze off',
|
2026-07-21 14:23:26 +03:30
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:38:48 +03:30
|
|
|
$lock = Cache::lock('pargar-wallet-sync:'.$customer->id, 60);
|
2026-07-21 14:13:26 +03:30
|
|
|
if (! $lock->get()) {
|
|
|
|
|
return [
|
2026-07-21 21:38:48 +03:30
|
|
|
'documents' => [
|
|
|
|
|
'applied' => 0,
|
|
|
|
|
'skipped' => 0,
|
|
|
|
|
'batches' => 0,
|
|
|
|
|
'acknowledged' => 0,
|
|
|
|
|
'errors' => ['همگامسازی در حال اجراست'],
|
|
|
|
|
],
|
2026-07-21 14:33:16 +03:30
|
|
|
'mande' => ['applied' => false, 'message' => 'همگامسازی دیگری برای این مشتری در حال اجراست'],
|
2026-07-21 14:13:26 +03:30
|
|
|
];
|
2026-07-21 12:49:08 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:13:26 +03:30
|
|
|
try {
|
2026-07-22 01:05:00 +03:30
|
|
|
$this->attachOrphanDocumentLogs($customer);
|
|
|
|
|
$this->attachOrphanMandeLogs($customer);
|
|
|
|
|
|
|
|
|
|
// ۱) تاریخچهٔ اسناد inbound را روی کیف replay نکن — فقط مصرفشده علامت بزن
|
2026-07-21 21:38:48 +03:30
|
|
|
$documents = $this->acknowledgePendingDocumentBatches($customer);
|
2026-07-21 14:33:16 +03:30
|
|
|
|
2026-07-21 21:38:48 +03:30
|
|
|
// ۲) منبع حقیقت موجودی = ماندهٔ پرگار
|
2026-07-22 01:05:00 +03:30
|
|
|
// خودکار: اگر سند پنل هنوز در پرگار ثبت نشده → اعمال نکن (force=false)
|
|
|
|
|
// ادمین: force تا بتوان اصلاح کرد
|
|
|
|
|
$mande = $this->applyLatestMandeDifference(
|
|
|
|
|
$customer,
|
|
|
|
|
force: $auto ? false : ($forceMande || true)
|
|
|
|
|
);
|
2026-07-21 14:33:16 +03:30
|
|
|
|
2026-07-21 21:38:48 +03:30
|
|
|
if (($documents['acknowledged'] ?? 0) > 0 || ($mande['applied'] ?? false)) {
|
2026-07-21 14:33:16 +03:30
|
|
|
$this->walletBroadcast->broadcastForCustomer(
|
|
|
|
|
(int) $customer->id,
|
|
|
|
|
(int) ($customer->type ?? 2)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:13:26 +03:30
|
|
|
return [
|
2026-07-21 14:33:16 +03:30
|
|
|
'documents' => $documents,
|
|
|
|
|
'mande' => $mande,
|
2026-07-21 14:13:26 +03:30
|
|
|
];
|
|
|
|
|
} finally {
|
|
|
|
|
$lock->release();
|
|
|
|
|
}
|
2026-07-21 12:49:08 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:33:16 +03:30
|
|
|
/**
|
2026-07-21 21:38:48 +03:30
|
|
|
* دستههای ورودی پرگار را بدون تغییر کیف، مصرفشده علامت میزند.
|
|
|
|
|
* (اعمال تکتک bed/bes تاریخچه = تورم کیف و 504)
|
2026-07-21 14:33:16 +03:30
|
|
|
*
|
2026-07-21 21:38:48 +03:30
|
|
|
* @return array{applied: int, skipped: int, batches: int, acknowledged: int, errors: list<string>}
|
2026-07-21 14:33:16 +03:30
|
|
|
*/
|
2026-07-21 21:38:48 +03:30
|
|
|
public function acknowledgePendingDocumentBatches(Customer $customer): array
|
2026-07-21 14:33:16 +03:30
|
|
|
{
|
|
|
|
|
$result = [
|
|
|
|
|
'applied' => 0,
|
|
|
|
|
'skipped' => 0,
|
|
|
|
|
'batches' => 0,
|
2026-07-21 21:38:48 +03:30
|
|
|
'acknowledged' => 0,
|
2026-07-21 14:33:16 +03:30
|
|
|
'errors' => [],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$batches = PargarSyncLog::query()
|
|
|
|
|
->where('type', PargarSyncService::TYPE_DOCUMENT)
|
|
|
|
|
->where('customer_id', $customer->id)
|
|
|
|
|
->orderBy('id')
|
|
|
|
|
->limit(200)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
foreach ($batches as $batch) {
|
|
|
|
|
if ($result['batches'] >= self::MAX_BATCHES_PER_SYNC) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = json_decode((string) $batch->data, true);
|
|
|
|
|
if (! is_array($data)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($data['transactions']) || ! is_array($data['transactions'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! empty($data['wallet_batch_applied']) || ! empty($data['wallet_applied'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result['batches']++;
|
2026-07-21 21:38:48 +03:30
|
|
|
$txCount = count($data['transactions']);
|
|
|
|
|
$result['skipped'] += $txCount;
|
2026-07-21 14:33:16 +03:30
|
|
|
|
2026-07-21 21:31:16 +03:30
|
|
|
try {
|
2026-07-21 21:38:48 +03:30
|
|
|
$data['wallet_batch_ack_only'] = true;
|
|
|
|
|
$data['wallet_batch_ack_reason'] = 'mande_is_source_of_truth';
|
2026-07-21 21:31:16 +03:30
|
|
|
$this->markDocumentBatchApplied($batch, $data);
|
2026-07-21 21:38:48 +03:30
|
|
|
$result['acknowledged']++;
|
2026-07-21 21:31:16 +03:30
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$result['errors'][] = 'علامت دسته #'.$batch->id.' ناموفق: '.$e->getMessage();
|
2026-07-21 21:38:48 +03:30
|
|
|
Log::error('Pargar acknowledgeDocumentBatch failed', [
|
2026-07-21 21:31:16 +03:30
|
|
|
'log_id' => $batch->id,
|
|
|
|
|
'customer_id' => $customer->id,
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
2026-07-21 14:33:16 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:31:16 +03:30
|
|
|
/**
|
2026-07-21 21:38:48 +03:30
|
|
|
* @deprecated از sync دکمه حذف شد — فقط برای سازگاری احتمالی تست/کالهای قدیمی
|
|
|
|
|
*
|
|
|
|
|
* @return array{applied: int, skipped: int, batches: int, errors: list<string>}
|
|
|
|
|
*/
|
|
|
|
|
public function applyPendingDocumentBatches(Customer $customer): array
|
|
|
|
|
{
|
|
|
|
|
$ack = $this->acknowledgePendingDocumentBatches($customer);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'applied' => 0,
|
|
|
|
|
'skipped' => (int) ($ack['skipped'] ?? 0),
|
|
|
|
|
'batches' => (int) ($ack['batches'] ?? 0),
|
|
|
|
|
'errors' => $ack['errors'] ?? [],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* علامت مصرف دسته — بدون Pretty-Print؛ در صورت شکست ذخیره، stub فشرده مینویسد.
|
2026-07-21 21:31:16 +03:30
|
|
|
*
|
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
|
*/
|
|
|
|
|
private function markDocumentBatchApplied(PargarSyncLog $batch, array $data): void
|
|
|
|
|
{
|
|
|
|
|
$data['wallet_batch_applied'] = true;
|
|
|
|
|
$data['wallet_batch_applied_at'] = now()->toDateTimeString();
|
|
|
|
|
|
|
|
|
|
if (is_string($batch->desc) && ! str_contains($batch->desc, '[اعمالشده]')) {
|
|
|
|
|
$batch->desc = mb_substr('[اعمالشده] '.$batch->desc, 0, 250);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:38:48 +03:30
|
|
|
// ack-only: همیشه stub فشرده — نیازی به نگهداشتن صدها سطر تاریخچه نیست
|
|
|
|
|
if (! empty($data['wallet_batch_ack_only'])) {
|
|
|
|
|
$batch->data = $this->encodeAppliedBatchCompact($data);
|
|
|
|
|
$batch->save();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:31:16 +03:30
|
|
|
$encoded = json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
if ($encoded === false) {
|
|
|
|
|
throw new \RuntimeException('json_encode failed for pargar batch #'.$batch->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$batch->data = $encoded;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$batch->save();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$batch->data = $this->encodeAppliedBatchCompact($data);
|
|
|
|
|
$batch->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
|
*/
|
|
|
|
|
private function encodeAppliedBatchCompact(array $data): string
|
|
|
|
|
{
|
|
|
|
|
$transactions = is_array($data['transactions'] ?? null) ? $data['transactions'] : [];
|
|
|
|
|
|
|
|
|
|
return (string) json_encode([
|
|
|
|
|
'accountName' => $data['accountName'] ?? null,
|
|
|
|
|
'accountId' => $data['accountId'] ?? null,
|
|
|
|
|
'transactions_count' => count($transactions),
|
|
|
|
|
'transactions_fp' => hash('sha256', (string) json_encode($transactions, JSON_UNESCAPED_UNICODE)),
|
|
|
|
|
'wallet_batch_applied' => true,
|
|
|
|
|
'wallet_batch_applied_at' => $data['wallet_batch_applied_at'] ?? now()->toDateTimeString(),
|
2026-07-21 21:38:48 +03:30
|
|
|
'wallet_batch_ack_only' => true,
|
2026-07-21 21:31:16 +03:30
|
|
|
'payload_compacted' => true,
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 12:49:08 +03:30
|
|
|
/**
|
|
|
|
|
* @return array{applied: bool, gold_delta?: float, rial_delta?: float, message: string}
|
|
|
|
|
*/
|
2026-07-21 14:13:26 +03:30
|
|
|
public function applyLatestMandeDifference(Customer $customer, bool $force = false): array
|
2026-07-21 12:49:08 +03:30
|
|
|
{
|
2026-07-22 01:05:00 +03:30
|
|
|
// دکمهٔ ادمین (force): حتی با سند pending خروجی، مانده را همتراز کن
|
|
|
|
|
if (! $force && $this->hasPendingOutboundToPargar($customer)) {
|
2026-07-21 13:25:18 +03:30
|
|
|
return [
|
|
|
|
|
'applied' => false,
|
2026-07-22 01:05:00 +03:30
|
|
|
'message' => 'سند پنل هنوز در پرگار ثبت نشده — مانده بعد از ثبت سند اعمال میشود',
|
2026-07-21 13:25:18 +03:30
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 12:49:08 +03:30
|
|
|
$log = PargarSyncLog::query()
|
|
|
|
|
->where('customer_id', $customer->id)
|
|
|
|
|
->where('type', PargarSyncService::TYPE_MANDE)
|
|
|
|
|
->orderByDesc('id')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($log === null) {
|
2026-07-21 14:33:16 +03:30
|
|
|
return ['applied' => false, 'message' => 'لاگ ماندهٔ پرگار برای این مشتری نیست'];
|
2026-07-21 12:49:08 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = json_decode((string) $log->data, true) ?: [];
|
2026-07-21 14:13:26 +03:30
|
|
|
if (! $force && ! empty($data['wallet_applied_from_mande'])) {
|
2026-07-21 14:33:16 +03:30
|
|
|
return ['applied' => false, 'message' => 'مانده قبلاً همتراز شده است'];
|
2026-07-21 12:49:08 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$goldInv = $this->parseNumber($data['GoldInv'] ?? 0);
|
|
|
|
|
$rialInv = $this->parseNumber($data['RialInv'] ?? 0);
|
|
|
|
|
|
|
|
|
|
$ownerType = (int) ($customer->type ?? 2);
|
|
|
|
|
$walletInterface = new WalletInterface((int) $customer->id, $ownerType);
|
|
|
|
|
$wallet = $walletInterface->wallet;
|
|
|
|
|
|
2026-07-21 22:23:09 +03:30
|
|
|
$balanceService = app(WalletBalanceService::class);
|
|
|
|
|
$mainGold = $balanceService->effectiveMainWalletGold((int) $customer->id, $ownerType);
|
|
|
|
|
$nonMainProductGold = $this->sumNonMainProductWalletGold((int) $customer->id, $ownerType);
|
|
|
|
|
|
|
|
|
|
// GoldInv پرگار = آبشدهٔ کیف اصلی. طلای محصولات جدا (مثل قلک سرمایهگذاری) نباید کپی شود.
|
|
|
|
|
$goldDiff = $goldInv - $mainGold;
|
|
|
|
|
|
|
|
|
|
// تکرار شناختهشده: مانده ≈ طلای محصول جدا و کیف اصلی هم همان مقدار را دارد
|
|
|
|
|
if (
|
|
|
|
|
$nonMainProductGold >= 0.01
|
|
|
|
|
&& abs($goldInv - $nonMainProductGold) < 0.05
|
|
|
|
|
&& abs($mainGold - $nonMainProductGold) < 0.05
|
|
|
|
|
) {
|
|
|
|
|
$goldDiff = -$mainGold;
|
|
|
|
|
} elseif (
|
|
|
|
|
$goldDiff > 0.01
|
|
|
|
|
&& $nonMainProductGold >= 0.01
|
|
|
|
|
&& abs($goldInv - $nonMainProductGold) < 0.05
|
|
|
|
|
&& $mainGold < 0.01
|
|
|
|
|
) {
|
|
|
|
|
// GoldInv فقط همان موجودی محصول است — روی کیف اصلی نریز
|
|
|
|
|
$goldDiff = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 12:49:08 +03:30
|
|
|
$rialDiff = $rialInv - (float) ($wallet->cash ?? 0);
|
|
|
|
|
|
|
|
|
|
if (abs($goldDiff) < 0.01 && abs($rialDiff) < 1) {
|
|
|
|
|
$data['wallet_applied_from_mande'] = true;
|
2026-07-21 21:31:16 +03:30
|
|
|
$log->data = json_encode($data, JSON_UNESCAPED_UNICODE);
|
2026-07-21 12:49:08 +03:30
|
|
|
$log->save();
|
|
|
|
|
|
2026-07-21 14:33:16 +03:30
|
|
|
return ['applied' => false, 'message' => 'مانده با پرگار یکسان است'];
|
2026-07-21 12:49:08 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$input = [
|
|
|
|
|
'title' => 'تطبیق ماندهٔ پرگار',
|
2026-07-21 14:13:26 +03:30
|
|
|
'note' => 'mande-log:'.$log->id,
|
2026-07-21 12:49:08 +03:30
|
|
|
'expire_at' => now(),
|
|
|
|
|
'active' => true,
|
|
|
|
|
];
|
|
|
|
|
if (abs($goldDiff) >= 0.01) {
|
|
|
|
|
$input['gold'] = abs($goldDiff);
|
|
|
|
|
}
|
|
|
|
|
if (abs($rialDiff) >= 1) {
|
|
|
|
|
$input['cash'] = abs($rialDiff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new TransactionInterface(
|
|
|
|
|
$walletInterface,
|
|
|
|
|
$input,
|
|
|
|
|
self::TX_TYPE_PARGAR,
|
|
|
|
|
$goldDiff > 0,
|
|
|
|
|
$rialDiff > 0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data['wallet_applied_from_mande'] = true;
|
|
|
|
|
$data['applied_gold_delta'] = $goldDiff;
|
|
|
|
|
$data['applied_rial_delta'] = $rialDiff;
|
2026-07-21 14:13:26 +03:30
|
|
|
$data['applied_at'] = now()->toDateTimeString();
|
2026-07-21 22:23:09 +03:30
|
|
|
$data['main_gold_before'] = $mainGold;
|
|
|
|
|
$data['non_main_product_gold'] = $nonMainProductGold;
|
2026-07-21 21:31:16 +03:30
|
|
|
$log->data = json_encode($data, JSON_UNESCAPED_UNICODE);
|
2026-07-21 12:49:08 +03:30
|
|
|
$log->desc = 'اعمال ماندهٔ پرگار روی کیف اپ';
|
|
|
|
|
$log->save();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'applied' => true,
|
|
|
|
|
'gold_delta' => $goldDiff,
|
|
|
|
|
'rial_delta' => $rialDiff,
|
2026-07-21 14:33:16 +03:30
|
|
|
'message' => 'مانده با پرگار همتراز شد',
|
2026-07-21 12:49:08 +03:30
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 22:23:09 +03:30
|
|
|
/**
|
|
|
|
|
* جمع طلای کیفهای محصول خارج از main_wallet_products (مثل قلک سرمایهگذاری).
|
|
|
|
|
*/
|
|
|
|
|
private function sumNonMainProductWalletGold(int $customerId, int $ownerType): float
|
|
|
|
|
{
|
|
|
|
|
$mainWalletProductIds = app(ConfigService::class)->mainWalletProductIds();
|
|
|
|
|
|
|
|
|
|
$query = Wallet::query()
|
|
|
|
|
->where('owner', $customerId)
|
|
|
|
|
->where('owner_type', $ownerType)
|
|
|
|
|
->whereNull('fund_id')
|
|
|
|
|
->whereNotNull('product_id');
|
|
|
|
|
|
|
|
|
|
if ($mainWalletProductIds !== []) {
|
|
|
|
|
$query->whereNotIn('product_id', $mainWalletProductIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return round((float) $query->sum('gold'), 3);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 12:36:59 +03:30
|
|
|
private function parseNumber(mixed $value): float
|
|
|
|
|
{
|
|
|
|
|
if ($value === null || $value === '') {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (float) str_replace('/', '.', (string) $value);
|
|
|
|
|
}
|
2026-07-22 00:31:38 +03:30
|
|
|
|
|
|
|
|
/**
|
2026-07-22 01:05:00 +03:30
|
|
|
* همگامسازی خودکار در پسزمینه — بعد از مانده / تأیید سند در پرگار / import.
|
2026-07-22 00:31:38 +03:30
|
|
|
*/
|
|
|
|
|
public function queueWalletSyncAfterResponse(int $customerId): void
|
|
|
|
|
{
|
|
|
|
|
dispatch(function () use ($customerId) {
|
|
|
|
|
$customer = Customer::find($customerId);
|
|
|
|
|
if ($customer === null || ! $customer->hasValidPargarAccountId()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-07-22 01:05:00 +03:30
|
|
|
$this->syncWalletFromPargar($customer, forceMande: false, auto: true);
|
2026-07-22 00:31:38 +03:30
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
Log::warning('Pargar auto wallet sync failed', [
|
|
|
|
|
'customer_id' => $customerId,
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
})->afterResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* لاگهای ماندهای که قبل از ساخت/لینک مشتری با pargar_ref آمدهاند را به مشتری وصل میکند.
|
|
|
|
|
*/
|
|
|
|
|
public function attachOrphanMandeLogs(Customer $customer): int
|
|
|
|
|
{
|
|
|
|
|
if (! $customer->hasValidPargarAccountId()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$accountId = trim((string) $customer->pargar_account_id);
|
|
|
|
|
|
|
|
|
|
return PargarSyncLog::query()
|
|
|
|
|
->where('type', PargarSyncService::TYPE_MANDE)
|
|
|
|
|
->where('pargar_ref', $accountId)
|
|
|
|
|
->where(function ($q) {
|
|
|
|
|
$q->whereNull('customer_id')->orWhere('customer_id', 0);
|
|
|
|
|
})
|
|
|
|
|
->update(['customer_id' => $customer->id]);
|
|
|
|
|
}
|
2026-07-22 01:05:00 +03:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* اسناد یتیم (قبل از لینک مشتری) را به customer_id وصل میکند.
|
|
|
|
|
*/
|
|
|
|
|
public function attachOrphanDocumentLogs(Customer $customer): int
|
|
|
|
|
{
|
|
|
|
|
if (! $customer->hasValidPargarAccountId()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$accountId = trim((string) $customer->pargar_account_id);
|
|
|
|
|
|
|
|
|
|
return PargarSyncLog::query()
|
|
|
|
|
->where('type', PargarSyncService::TYPE_DOCUMENT)
|
|
|
|
|
->where('pargar_ref', $accountId)
|
|
|
|
|
->where(function ($q) {
|
|
|
|
|
$q->whereNull('customer_id')->orWhere('customer_id', 0);
|
|
|
|
|
})
|
|
|
|
|
->update(['customer_id' => $customer->id]);
|
|
|
|
|
}
|
2026-07-21 12:36:59 +03:30
|
|
|
}
|