diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index f60bd9f..e771a0c 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -43,7 +43,7 @@ class AssetSummaryService $catalogById = $catalog->keyBy('id'); $inventoryCards = array_merge( - $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $userWallets), + $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds), $this->buildInventoryCards($userWallets, $catalogById) ); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById); @@ -97,48 +97,43 @@ class AssetSummaryService Collection $catalog, Collection $catalogById, ?Wallet $mainWallet, - array $metalIds, - Collection $userWallets + array $metalIds ): array { $cards = []; $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0)); $assignedMetalIds = []; - $metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets, $catalogById); + $mainProductId = $this->configService->mainProductId(); - foreach ($this->resolveMeltedProducts($catalog) as $product) { - if (!$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) { + foreach ($metalIds as $metalId) { + if ($metalId <= 0 || in_array($metalId, $assignedMetalIds, true)) { continue; } - $metalId = (int) ($product->metal_id ?? 0); - if ( - $metalId <= 0 - || in_array($metalId, $assignedMetalIds, true) - || in_array($metalId, $metalsWithProductWallets, true) - ) { - continue; - } - - $meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog, $catalogById); + $meltedGrams = $this->meltedGramsForMetalTotal($mainWalletGrams, (int) $metalId, $metalIds); if ($meltedGrams <= 0) { continue; } - $assignedMetalIds[] = $metalId; - $productPayload = $this->productPayload($product); + $catalogProduct = $this->resolveDisplayProductForMetal($catalog, (int) $metalId, $mainProductId); + if (!$catalogProduct) { + continue; + } + + $assignedMetalIds[] = (int) $metalId; + $productPayload = $this->productPayload($catalogProduct); $cards[] = [ - 'product_id' => (int) $product->id, - 'title' => (string) $product->title, - 'group_type' => $this->meltedGroupTypeLabel($product), - 'metal_id' => $metalId, + 'product_id' => (int) $catalogProduct->id, + 'title' => (string) $catalogProduct->title, + 'group_type' => $this->meltedGroupTypeLabel($catalogProduct), + 'metal_id' => (int) $metalId, 'quantity' => round(Controller::gramsToTradeUnit($meltedGrams), 3), 'unit' => Controller::productQuantityUnit($productPayload), 'display_type' => 'weight', - 'unit_weight' => $this->productUnitWeightLabel($product), - 'show_in_assets' => true, + 'unit_weight' => $this->productUnitWeightLabel($catalogProduct), + 'show_in_assets' => $this->shouldShowInAssets($catalogProduct), 'is_melted' => true, - 'file' => $this->filePayload($product), + 'file' => $this->filePayload($catalogProduct), ]; } @@ -191,36 +186,26 @@ class AssetSummaryService return $group !== '' ? $group : 'آبشده'; } - 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($this->catalogProduct($product, $catalogById)) - ); - } - - /** فلزهایی که کاربر موجودی روی کیف محصول visible دارد — کارت آبشده جدا لازم نیست. */ - private function metalIdsWithVisibleProductWallet(Collection $userWallets, Collection $catalogById): array - { - $metalIds = []; - - foreach ($userWallets as $walletLine) { - if ($walletLine->product_id === null || (float) $walletLine->gold <= 0) { - continue; - } - - $product = $walletLine->product; - if (!$product || !$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) { - continue; - } - - $metalId = (int) ($product->metal_id ?? 0); - if ($metalId > 0) { - $metalIds[] = $metalId; + /** محصول نمایشی برای موجودی کیف اصلی هر فلز. */ + private function resolveDisplayProductForMetal( + Collection $catalog, + int $metalId, + ?int $mainProductId + ): ?Product { + if ($mainProductId) { + $mainProduct = $catalog->firstWhere('id', $mainProductId); + if ($mainProduct && (int) ($mainProduct->metal_id ?? 0) === $metalId) { + return $mainProduct; } } - return array_values(array_unique($metalIds)); + foreach ($this->resolveMeltedProducts($catalog) as $product) { + if ((int) ($product->metal_id ?? 0) === $metalId) { + return $product; + } + } + + return $catalog->first(fn (Product $product) => (int) ($product->metal_id ?? 0) === $metalId); } private function buildInventoryCards(Collection $userWallets, Collection $catalogById): array @@ -238,10 +223,6 @@ class AssetSummaryService } $catalogProduct = $this->catalogProduct($product, $catalogById); - if (!$this->shouldShowInAssets($catalogProduct)) { - continue; - } - $productPayload = $this->productPayload($catalogProduct); $isCountBased = $this->isCountBasedProduct($catalogProduct); @@ -254,7 +235,7 @@ class AssetSummaryService 'unit' => Controller::productQuantityUnit($productPayload), 'display_type' => $isCountBased ? 'count' : 'weight', 'unit_weight' => $this->productUnitWeightLabel($catalogProduct), - 'show_in_assets' => true, + 'show_in_assets' => $this->shouldShowInAssets($catalogProduct), 'file' => $this->filePayload($catalogProduct), ]; } @@ -335,28 +316,6 @@ class AssetSummaryService return (int) $metalId === 1 ? $mainWalletGrams : 0.0; } - 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,10 +326,6 @@ class AssetSummaryService } $catalogProduct = $this->catalogProduct($walletLine->product, $catalogById); - if (!$this->shouldShowInAssets($catalogProduct)) { - continue; - } - $qty = (float) $walletLine->gold; $metalId = (int) ($catalogProduct->metal_id ?? 0);