fix some bugs 5

This commit is contained in:
2026-07-21 22:23:09 +03:30
parent 55ce5e83b0
commit 3f8c47b1fc
6 changed files with 215 additions and 36 deletions
+47 -1
View File
@@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Wallet;
/**
* همگام‌سازی کیف با پرگار — فقط با دکمهٔ ادمین.
@@ -280,7 +281,30 @@ class PargarInboundWalletService
$walletInterface = new WalletInterface((int) $customer->id, $ownerType);
$wallet = $walletInterface->wallet;
$goldDiff = $goldInv - (float) ($wallet->gold ?? 0);
$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;
}
$rialDiff = $rialInv - (float) ($wallet->cash ?? 0);
if (abs($goldDiff) < 0.01 && abs($rialDiff) < 1) {
@@ -316,6 +340,8 @@ class PargarInboundWalletService
$data['applied_gold_delta'] = $goldDiff;
$data['applied_rial_delta'] = $rialDiff;
$data['applied_at'] = now()->toDateTimeString();
$data['main_gold_before'] = $mainGold;
$data['non_main_product_gold'] = $nonMainProductGold;
$log->data = json_encode($data, JSON_UNESCAPED_UNICODE);
$log->desc = 'اعمال ماندهٔ پرگار روی کیف اپ';
$log->save();
@@ -328,6 +354,26 @@ class PargarInboundWalletService
];
}
/**
* جمع طلای کیف‌های محصول خارج از 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);
}
private function hasPendingOutboundWalletSync(int $customerId): bool
{
return PargarSyncLog::query()