From 45f2d8bf706987d486f6bf082c6fd48474eac2d3 Mon Sep 17 00:00:00 2001 From: adonis Date: Sat, 18 Jul 2026 15:30:59 +0330 Subject: [PATCH] fix some bugs 3 --- .../Controllers/Customer/OrderController.php | 5 +++- .../Customer/ProductController.php | 2 +- .../Pargar/PargarInboundController.php | 16 ++++++------- app/Services/AssetSummaryService.php | 23 +++++++++++-------- app/Services/WalletBalanceService.php | 23 ++++++++++++------- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Customer/OrderController.php b/app/Http/Controllers/Customer/OrderController.php index 2ae2ae7..5af99c3 100644 --- a/app/Http/Controllers/Customer/OrderController.php +++ b/app/Http/Controllers/Customer/OrderController.php @@ -38,7 +38,10 @@ class OrderController extends Controller $lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0); - $goldAmount = app(WalletBalanceService::class)->effectiveMainWalletGold((int) $customerId); + $goldAmount = app(WalletBalanceService::class)->effectiveMainWalletGold( + (int) $customerId, + (int) ($customer->type ?? 2) + ); $orders = Order::query() ->where('customer_id', $customerId) diff --git a/app/Http/Controllers/Customer/ProductController.php b/app/Http/Controllers/Customer/ProductController.php index 59747ba..2293e66 100644 --- a/app/Http/Controllers/Customer/ProductController.php +++ b/app/Http/Controllers/Customer/ProductController.php @@ -59,7 +59,7 @@ class ProductController extends SuperController } if ($representativeId !== null) { - $mainWalletGold = $balanceService->effectiveMainWalletGold((int) $user->id); + $mainWalletGold = $balanceService->effectiveMainWalletGold((int) $user->id, (int) $user->type); } $products = $products->filter(function ($product) use ($mainWalletProductIds, $representativeId) { diff --git a/app/Http/Controllers/Pargar/PargarInboundController.php b/app/Http/Controllers/Pargar/PargarInboundController.php index 0418230..48ef38c 100644 --- a/app/Http/Controllers/Pargar/PargarInboundController.php +++ b/app/Http/Controllers/Pargar/PargarInboundController.php @@ -271,17 +271,15 @@ class PargarInboundController extends Controller $goldInv = isset($data['GoldInv']) ? (float) str_replace('/', '.', (string) $data['GoldInv']) : 0; $rialInv = isset($data['RialInv']) ? (float) str_replace('/', '.', (string) $data['RialInv']) : 0; - $wallet = $customer->wallet->where('fund_id', null)->first(); - if ($wallet) { - $goldDiff = $goldInv - (float) $wallet->gold; - $rialDiff = $rialInv - (float) $wallet->cash; - } else { - $goldDiff = $goldInv; - $rialDiff = $rialInv; - } + // خواندن و نوشتن روی همان کیف اصلی مشتری — نه first() تصادفی + $ownerType = (int) ($customer->type ?? 2); + $walletInterface = new WalletInterface($customer->id, $ownerType); + $wallet = $walletInterface->wallet; + + $goldDiff = $goldInv - (float) ($wallet->gold ?? 0); + $rialDiff = $rialInv - (float) ($wallet->cash ?? 0); if (abs($goldDiff) >= 0.01 || abs($rialDiff) >= 1) { - $walletInterface = new WalletInterface($customer->id, 2); new TransactionInterface($walletInterface, [ 'cash' => $rialDiff, 'gold' => $goldDiff, diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 264a8ba..245deda 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -59,7 +59,8 @@ class AssetSummaryService $metalsWithProductWallet, $mainWalletProductIds, $userWallets, - $customerId + $customerId, + $ownerType ), $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet), ); @@ -71,7 +72,8 @@ class AssetSummaryService $catalogById, $metalsWithProductWallet, $mainWalletProductIds, - $customerId + $customerId, + $ownerType ); $cashWallet = $this->walletBalanceService->mainWalletCash($userWallets); $assetsRialValue = $this->sumAssetsRialValue( @@ -218,12 +220,13 @@ class AssetSummaryService array $metalsWithProductWallet = [], array $mainWalletProductIds = [], Collection $userWallets, - int $customerId = 0 + int $customerId = 0, + int $ownerType = 0 ): array { $cards = []; $mainWalletGrams = Controller::weightToGrams( $customerId > 0 - ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + ? $this->walletBalanceService->effectiveMainWalletGold($customerId, $ownerType) : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) ); $assignedMetalIds = []; @@ -237,9 +240,10 @@ class AssetSummaryService } $mainWalletWeight = $customerId > 0 - ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + ? $this->walletBalanceService->effectiveMainWalletGold($customerId, $ownerType) : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds); - if ($mainWalletWeight <= 0) { + // صفر کامل را نشان نده؛ منفی را نشان بده تا اختلاف موجودی دیده شود + if (abs($mainWalletWeight) < 0.000001) { return []; } @@ -322,7 +326,7 @@ class AssetSummaryService public function productDisplayQuantity(int $customerId, int $ownerType, int $productId): float { if ($this->configService->isMainWalletProduct($productId)) { - return $this->walletBalanceService->effectiveMainWalletGold($customerId); + return $this->walletBalanceService->effectiveMainWalletGold($customerId, $ownerType); } $card = $this->productInventoryCard($customerId, $ownerType, $productId); @@ -609,7 +613,8 @@ class AssetSummaryService Collection $catalogById, array $metalsWithProductWallet = [], array $mainWalletProductIds = [], - int $customerId = 0 + int $customerId = 0, + int $ownerType = 0 ): array { if ($metalIds === []) { return []; @@ -617,7 +622,7 @@ class AssetSummaryService $mainWalletGrams = Controller::weightToGrams( $customerId > 0 - ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + ? $this->walletBalanceService->effectiveMainWalletGold($customerId, $ownerType) : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) ); $totals = []; diff --git a/app/Services/WalletBalanceService.php b/app/Services/WalletBalanceService.php index 5c964fa..134e5a4 100644 --- a/app/Services/WalletBalanceService.php +++ b/app/Services/WalletBalanceService.php @@ -46,26 +46,33 @@ class WalletBalanceService /** * موجودی مؤثر کیف اصلی برای نمایش آبشده = * gold کیف اصلی + gold کیف‌های legacy محصولات main_wallet_products. - * owner_type در جمع نادیده گرفته می‌شود تا خریدهای قدیمی از دست نروند. + * + * @param int|null $ownerType اگر داده شود فقط همان owner_type جمع می‌شود (هم‌تراز wallet/me). */ - public function effectiveMainWalletGold(int $ownerId): float + public function effectiveMainWalletGold(int $ownerId, ?int $ownerType = null): float { - $mainGold = (float) Wallet::query() + $mainQuery = Wallet::query() ->where('owner', $ownerId) ->whereNull('fund_id') - ->whereNull('product_id') - ->sum('gold'); + ->whereNull('product_id'); + if ($ownerType !== null) { + $mainQuery->where('owner_type', $ownerType); + } + $mainGold = (float) $mainQuery->sum('gold'); $productIds = $this->mainWalletProductIds(); if ($productIds === []) { return round($mainGold, 3); } - $orphanedGold = (float) Wallet::query() + $orphanedQuery = Wallet::query() ->where('owner', $ownerId) ->whereNull('fund_id') - ->whereIn('product_id', $productIds) - ->sum('gold'); + ->whereIn('product_id', $productIds); + if ($ownerType !== null) { + $orphanedQuery->where('owner_type', $ownerType); + } + $orphanedGold = (float) $orphanedQuery->sum('gold'); return round($mainGold + $orphanedGold, 3); }