From eedde65be6c1025cf3f11277d490e7e59f445f94 Mon Sep 17 00:00:00 2001 From: adonis Date: Sat, 18 Jul 2026 15:04:08 +0330 Subject: [PATCH] fix some bugs --- .../Controllers/Customer/OrderController.php | 6 +-- .../Customer/ProductController.php | 45 +++++++++++-------- app/Services/AssetSummaryService.php | 44 +++++++++++++++--- app/Services/WalletBalanceService.php | 28 ++++++++++++ 4 files changed, 94 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/Customer/OrderController.php b/app/Http/Controllers/Customer/OrderController.php index ba094e3..2ae2ae7 100644 --- a/app/Http/Controllers/Customer/OrderController.php +++ b/app/Http/Controllers/Customer/OrderController.php @@ -9,6 +9,7 @@ use App\Models\Customer; use App\Models\Order; use App\Models\Price; use App\Models\Setting; +use App\Services\WalletBalanceService; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; @@ -37,10 +38,7 @@ class OrderController extends Controller $lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0); - $mainWallet = $customer->wallet->first( - fn ($wallet) => is_null($wallet->product_id) && is_null($wallet->fund_id) - ) ?? $customer->wallet->first(); - $goldAmount = (float) ($mainWallet->gold ?? 0); + $goldAmount = app(WalletBalanceService::class)->effectiveMainWalletGold((int) $customerId); $orders = Order::query() ->where('customer_id', $customerId) diff --git a/app/Http/Controllers/Customer/ProductController.php b/app/Http/Controllers/Customer/ProductController.php index 0540998..36d364b 100644 --- a/app/Http/Controllers/Customer/ProductController.php +++ b/app/Http/Controllers/Customer/ProductController.php @@ -8,6 +8,7 @@ use App\Http\Controllers\SuperController; use App\Models\Fund; use App\Models\Product; use App\Services\AssetSummaryService; +use App\Services\WalletBalanceService; use Illuminate\Http\Request; use Rooyesh\Wallet\interfaces\TransactionInterface; use Rooyesh\Wallet\interfaces\WalletInterface; @@ -27,7 +28,7 @@ class ProductController extends SuperController $query = Product::with($this->with); parent::__construct($model, $this->with, $select, null,$query,$search); } - public function pTable(Request $request, AssetSummaryService $assetSummaryService) + public function pTable(Request $request, AssetSummaryService $assetSummaryService, WalletBalanceService $balanceService) { $user = auth('customer')->user(); $mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds(); @@ -58,11 +59,7 @@ class ProductController extends SuperController } if ($representativeId !== null) { - $mainWallet = Wallet::where('owner', $user->id) - ->whereNull('fund_id') - ->whereNull('product_id') - ->first(); - $mainWalletGold = (float) ($mainWallet->gold ?? 0); + $mainWalletGold = $balanceService->effectiveMainWalletGold((int) $user->id); } $products = $products->filter(function ($product) use ($mainWalletProductIds, $representativeId) { @@ -74,7 +71,7 @@ class ProductController extends SuperController } // Prepare the response - $user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct) { + $user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct, $assetSummaryService) { $product->unit = self::productQuantityUnit([ 'unit' => $product->unit ?? null, 'direct_price' => $product->direct_price ?? null, @@ -88,13 +85,13 @@ class ProductController extends SuperController $card = $inventoryByProduct->get((int) $product->id); $displayQty = $card !== null ? (float) ($card['quantity'] ?? 0) - : null; + : $assetSummaryService->productDisplayQuantity((int) $user->id, (int) $user->type, (int) $product->id); if ($representativeId !== null && (int) $product->id === $representativeId) { - // کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی است + // کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی (+ legacy) است $walletInterface = new WalletInterface($user->id, $user->type, null, $product->id); $handleWallet = $walletInterface->wallet; - $handleWallet->gold = $displayQty ?? $mainWalletGold; + $handleWallet->gold = $displayQty > 0 ? $displayQty : $mainWalletGold; $product->setRelation('wallet', $handleWallet); } elseif ($displayQty !== null) { if ($product->wallet) { @@ -158,21 +155,33 @@ class ProductController extends SuperController ], 5, true); return response()->json(true); } - public function show($id){ + public function show($id, AssetSummaryService $assetSummaryService){ + $customer = auth('customer')->user(); $query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id); $wallet = $query->walletMy; - // محصول متصل به کیف اصلی: موجودی از کیف اصلی خوانده می‌شود + // محصول متصل به کیف اصلی: موجودی مؤثر (کیف اصلی + legacy) — نه فقط ردیف product handle if (self::isMainWalletProduct((int) $id)) { - $mainWallet = Wallet::where('owner', auth('customer')->id()) - ->whereNull('fund_id') - ->whereNull('product_id') - ->first(); + $mainWalletGold = $assetSummaryService->productDisplayQuantity( + (int) $customer->id, + (int) $customer->type, + (int) $id + ); if ($wallet) { - $wallet->gold = (float) ($mainWallet->gold ?? 0); + $wallet->gold = $mainWalletGold; } else { - $wallet = $mainWallet; + $wallet = Wallet::where('owner', $customer->id) + ->whereNull('fund_id') + ->whereNull('product_id') + ->first() ?? new Wallet(['gold' => $mainWalletGold]); + $wallet->gold = $mainWalletGold; } + } elseif ($wallet) { + $wallet->gold = $assetSummaryService->productDisplayQuantity( + (int) $customer->id, + (int) $customer->type, + (int) $id + ); } $query['wallet'] = $wallet; diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 829b090..264a8ba 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -51,10 +51,28 @@ class AssetSummaryService $metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds, $mainWallet); $inventoryCards = array_merge( - $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds, $userWallets), + $this->buildMeltedInventoryCards( + $catalog, + $catalogById, + $mainWallet, + $metalIds, + $metalsWithProductWallet, + $mainWalletProductIds, + $userWallets, + $customerId + ), $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet), ); - $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds); + $metalTotals = $this->buildMetalTotals( + $userWallets, + $mainWallet, + $metalIds, + $weightUnit, + $catalogById, + $metalsWithProductWallet, + $mainWalletProductIds, + $customerId + ); $cashWallet = $this->walletBalanceService->mainWalletCash($userWallets); $assetsRialValue = $this->sumAssetsRialValue( $metalTotals, @@ -199,11 +217,14 @@ class AssetSummaryService array $metalIds, array $metalsWithProductWallet = [], array $mainWalletProductIds = [], - Collection $userWallets + Collection $userWallets, + int $customerId = 0 ): array { $cards = []; $mainWalletGrams = Controller::weightToGrams( - $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) + $customerId > 0 + ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) ); $assignedMetalIds = []; $mainProductId = $this->configService->mainProductId(); @@ -215,7 +236,9 @@ class AssetSummaryService return []; } - $mainWalletWeight = $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds); + $mainWalletWeight = $customerId > 0 + ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds); if ($mainWalletWeight <= 0) { return []; } @@ -298,6 +321,10 @@ class AssetSummaryService /** موجودی نمایشی محصول — همسان با inventory_cards */ public function productDisplayQuantity(int $customerId, int $ownerType, int $productId): float { + if ($this->configService->isMainWalletProduct($productId)) { + return $this->walletBalanceService->effectiveMainWalletGold($customerId); + } + $card = $this->productInventoryCard($customerId, $ownerType, $productId); if ($card !== null) { return (float) ($card['quantity'] ?? 0); @@ -581,14 +608,17 @@ class AssetSummaryService string $weightUnit, Collection $catalogById, array $metalsWithProductWallet = [], - array $mainWalletProductIds = [] + array $mainWalletProductIds = [], + int $customerId = 0 ): array { if ($metalIds === []) { return []; } $mainWalletGrams = Controller::weightToGrams( - $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) + $customerId > 0 + ? $this->walletBalanceService->effectiveMainWalletGold($customerId) + : $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) ); $totals = []; diff --git a/app/Services/WalletBalanceService.php b/app/Services/WalletBalanceService.php index 60c31ee..5c964fa 100644 --- a/app/Services/WalletBalanceService.php +++ b/app/Services/WalletBalanceService.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Controller; use App\Models\Price; use App\Models\Product; use Illuminate\Support\Collection; +use Rooyesh\Wallet\models\Wallet; /** * محاسبهٔ موجودی کیف — منبع واحد برای ادمین و APIها. @@ -42,6 +43,33 @@ class WalletBalanceService return (float) ($this->lineValue($line, 'gold') ?? 0); } + /** + * موجودی مؤثر کیف اصلی برای نمایش آبشده = + * gold کیف اصلی + gold کیف‌های legacy محصولات main_wallet_products. + * owner_type در جمع نادیده گرفته می‌شود تا خریدهای قدیمی از دست نروند. + */ + public function effectiveMainWalletGold(int $ownerId): float + { + $mainGold = (float) Wallet::query() + ->where('owner', $ownerId) + ->whereNull('fund_id') + ->whereNull('product_id') + ->sum('gold'); + + $productIds = $this->mainWalletProductIds(); + if ($productIds === []) { + return round($mainGold, 3); + } + + $orphanedGold = (float) Wallet::query() + ->where('owner', $ownerId) + ->whereNull('fund_id') + ->whereIn('product_id', $productIds) + ->sum('gold'); + + return round($mainGold + $orphanedGold, 3); + } + /** * جمع طلای قابل‌نمایش: کیف اصلی + کیف محصولات + قلک‌ها. * کیف‌های legacy محصولات main_wallet_products شمرده نمی‌شوند.