From 066a2702f1d3e7ca4629fe01c91b2cbeebb72b94 Mon Sep 17 00:00:00 2001 From: adonis Date: Wed, 10 Jun 2026 14:04:52 +0330 Subject: [PATCH] fix wallet 4 --- app/Services/AssetSummaryService.php | 151 +++++++++++++-------------- 1 file changed, 75 insertions(+), 76 deletions(-) diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 0a9d108..f60bd9f 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -40,13 +40,15 @@ class AssetSummaryService $hasCountBased = $this->catalogHasCountBased($catalog); $hasWeightBased = $this->catalogHasWeightBased($catalog); + $catalogById = $catalog->keyBy('id'); + $inventoryCards = array_merge( - $this->buildMeltedInventoryCards($catalog, $mainWallet, $metalIds, $userWallets), - $this->buildInventoryCards($userWallets) + $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $userWallets), + $this->buildInventoryCards($userWallets, $catalogById) ); - $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalog, $inventoryCards); + $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById); $cashWallet = (float) ($mainWallet->cash ?? 0); - $assetsRialValue = $this->sumAssetsRialValue($userWallets); + $assetsRialValue = $this->sumAssetsRialValue($userWallets, $catalogById); $totalRialValue = $cashWallet + $assetsRialValue; return [ @@ -93,6 +95,7 @@ class AssetSummaryService private function buildMeltedInventoryCards( Collection $catalog, + Collection $catalogById, ?Wallet $mainWallet, array $metalIds, Collection $userWallets @@ -100,10 +103,10 @@ class AssetSummaryService $cards = []; $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0)); $assignedMetalIds = []; - $metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets); + $metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets, $catalogById); foreach ($this->resolveMeltedProducts($catalog) as $product) { - if (!$this->shouldShowInAssets($product)) { + if (!$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) { continue; } @@ -116,7 +119,7 @@ class AssetSummaryService continue; } - $meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog); + $meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog, $catalogById); if ($meltedGrams <= 0) { continue; } @@ -133,7 +136,7 @@ class AssetSummaryService 'unit' => Controller::productQuantityUnit($productPayload), 'display_type' => 'weight', 'unit_weight' => $this->productUnitWeightLabel($product), - 'show_in_assets' => $this->shouldShowInAssets($product), + 'show_in_assets' => true, 'is_melted' => true, 'file' => $this->filePayload($product), ]; @@ -142,6 +145,11 @@ class AssetSummaryService return $cards; } + private function catalogProduct(Product $product, Collection $catalogById): Product + { + return $catalogById->get((int) $product->id) ?? $product; + } + /** * محصولات آبشده: دسته «آبشده»/«کاتد مس» + محصول اصلی تنظیمات (main_product). */ @@ -183,16 +191,16 @@ class AssetSummaryService return $group !== '' ? $group : 'آبشده'; } - private function hasVisibleMeltedProductForMetal(Collection $catalog, int $metalId): bool + private function hasVisibleMeltedProductForMetal(Collection $catalog, Collection $catalogById, int $metalId): bool { return $this->resolveMeltedProducts($catalog)->contains( fn (Product $product) => (int) ($product->metal_id ?? 0) === (int) $metalId - && $this->shouldShowInAssets($product) + && $this->shouldShowInAssets($this->catalogProduct($product, $catalogById)) ); } - /** فلزهایی که کاربر موجودی روی کیف محصول دارد — کارت آبشده جدا لازم نیست. */ - private function metalIdsWithVisibleProductWallet(Collection $userWallets): array + /** فلزهایی که کاربر موجودی روی کیف محصول visible دارد — کارت آبشده جدا لازم نیست. */ + private function metalIdsWithVisibleProductWallet(Collection $userWallets, Collection $catalogById): array { $metalIds = []; @@ -202,7 +210,7 @@ class AssetSummaryService } $product = $walletLine->product; - if (!$product || !$this->shouldShowInAssets($product)) { + if (!$product || !$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) { continue; } @@ -215,7 +223,7 @@ class AssetSummaryService return array_values(array_unique($metalIds)); } - private function buildInventoryCards(Collection $userWallets): array + private function buildInventoryCards(Collection $userWallets, Collection $catalogById): array { $cards = []; @@ -225,24 +233,29 @@ class AssetSummaryService } $product = $walletLine->product; - if (!$product || !$this->shouldShowInAssets($product)) { + if (!$product) { continue; } - $productPayload = $this->productPayload($product); - $isCountBased = $this->isCountBasedProduct($product); + $catalogProduct = $this->catalogProduct($product, $catalogById); + if (!$this->shouldShowInAssets($catalogProduct)) { + continue; + } + + $productPayload = $this->productPayload($catalogProduct); + $isCountBased = $this->isCountBasedProduct($catalogProduct); $cards[] = [ - 'product_id' => (int) $product->id, - 'title' => (string) $product->title, - 'group_type' => (string) ($product->group_type ?? ''), - 'metal_id' => (int) ($product->metal_id ?? 0), + 'product_id' => (int) $catalogProduct->id, + 'title' => (string) $catalogProduct->title, + 'group_type' => (string) ($catalogProduct->group_type ?? ''), + 'metal_id' => (int) ($catalogProduct->metal_id ?? 0), 'quantity' => round((float) $walletLine->gold, 3), 'unit' => Controller::productQuantityUnit($productPayload), 'display_type' => $isCountBased ? 'count' : 'weight', - 'unit_weight' => $this->productUnitWeightLabel($product), - 'show_in_assets' => $this->shouldShowInAssets($product), - 'file' => $this->filePayload($product), + 'unit_weight' => $this->productUnitWeightLabel($catalogProduct), + 'show_in_assets' => true, + 'file' => $this->filePayload($catalogProduct), ]; } @@ -254,15 +267,13 @@ class AssetSummaryService ?Wallet $mainWallet, array $metalIds, string $weightUnit, - Collection $catalog, - array $inventoryCards + Collection $catalogById ): array { if ($metalIds === []) { return []; } $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0)); - $metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets); $totals = []; foreach ($metalIds as $metalId) { @@ -273,40 +284,25 @@ class AssetSummaryService continue; } - $product = $walletLine->product; - if (!$this->shouldShowInAssets($product)) { + $catalogProduct = $this->catalogProduct($walletLine->product, $catalogById); + if ((int) ($catalogProduct->metal_id ?? 0) !== (int) $metalId) { continue; } - if ((int) $product->metal_id !== (int) $metalId) { - continue; - } - - if ((string) ($product->group_type ?? '') === 'سکه') { + if ((string) ($catalogProduct->group_type ?? '') === 'سکه') { continue; } $productGrams += Controller::productLineWeightToGrams( - $product, + $catalogProduct, $walletLine->gold ); } - $meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog); - if (in_array((int) $metalId, $metalsWithProductWallets, true)) { - $meltedGrams = 0.0; - } - + $meltedGrams = $this->meltedGramsForMetalTotal($mainWalletGrams, $metalId, $metalIds); $productWeight = round(Controller::gramsToTradeUnit($productGrams), 3); $meltedWeight = round(Controller::gramsToTradeUnit($meltedGrams), 3); - $visibleCardWeight = round($this->visibleCardWeightForMetal($inventoryCards, (int) $metalId), 3); - $totalWeight = $visibleCardWeight > 0 - ? $visibleCardWeight - : round($productWeight + $meltedWeight, 3); - - if ($totalWeight <= 0) { - continue; - } + $totalWeight = round($productWeight + $meltedWeight, 3); $totals[] = [ 'metal_id' => (int) $metalId, @@ -322,35 +318,16 @@ class AssetSummaryService return $totals; } - private function visibleCardWeightForMetal(array $inventoryCards, int $metalId): float - { - $total = 0.0; - - foreach ($inventoryCards as $card) { - if ((int) ($card['metal_id'] ?? 0) !== $metalId) { - continue; - } - - $total += (float) ($card['quantity'] ?? 0); - } - - return $total; - } - - private function meltedGramsForMetal( + /** جمع واقعی کیف اصلی برای هر فلز — مستقل از show_in_assets. */ + private function meltedGramsForMetalTotal( float $mainWalletGrams, int $metalId, - array $catalogMetalIds, - Collection $catalog + array $catalogMetalIds ): float { if ($mainWalletGrams <= 0) { return 0.0; } - if (!$this->hasVisibleMeltedProductForMetal($catalog, $metalId)) { - return 0.0; - } - if (count($catalogMetalIds) === 1) { return (int) $catalogMetalIds[0] === (int) $metalId ? $mainWalletGrams : 0.0; } @@ -358,7 +335,29 @@ class AssetSummaryService return (int) $metalId === 1 ? $mainWalletGrams : 0.0; } - private function sumAssetsRialValue(Collection $userWallets): float + private function meltedGramsForMetal( + float $mainWalletGrams, + int $metalId, + array $catalogMetalIds, + Collection $catalog, + Collection $catalogById + ): float { + if ($mainWalletGrams <= 0) { + return 0.0; + } + + if (!$this->hasVisibleMeltedProductForMetal($catalog, $catalogById, $metalId)) { + return 0.0; + } + + if (count($catalogMetalIds) === 1) { + return (int) $catalogMetalIds[0] === (int) $metalId ? $mainWalletGrams : 0.0; + } + + return (int) $metalId === 1 ? $mainWalletGrams : 0.0; + } + + private function sumAssetsRialValue(Collection $userWallets, Collection $catalogById): float { $total = 0.0; @@ -367,18 +366,18 @@ class AssetSummaryService continue; } - $product = $walletLine->product; - if (!$this->shouldShowInAssets($product)) { + $catalogProduct = $this->catalogProduct($walletLine->product, $catalogById); + if (!$this->shouldShowInAssets($catalogProduct)) { continue; } $qty = (float) $walletLine->gold; - $metalId = (int) ($product->metal_id ?? 0); + $metalId = (int) ($catalogProduct->metal_id ?? 0); if ($metalId === 1) { - $total += (float) ($product->price ?? 0) * $qty; + $total += (float) ($catalogProduct->price ?? 0) * $qty; } elseif ($metalId === 2 || $metalId === 3) { - $total += ((float) ($product->price ?? 0) * (float) ($product->amount ?? 1)) * $qty; + $total += ((float) ($catalogProduct->price ?? 0) * (float) ($catalogProduct->amount ?? 1)) * $qty; } }