fix wallet 2

This commit is contained in:
2026-06-10 13:28:35 +03:30
parent 539e38d034
commit 956548f89e
+50 -13
View File
@@ -41,10 +41,10 @@ class AssetSummaryService
$hasWeightBased = $this->catalogHasWeightBased($catalog); $hasWeightBased = $this->catalogHasWeightBased($catalog);
$inventoryCards = array_merge( $inventoryCards = array_merge(
$this->buildMeltedInventoryCards($catalog, $mainWallet, $metalIds, $weightUnit, $userWallets), $this->buildMeltedInventoryCards($catalog, $mainWallet, $metalIds, $userWallets),
$this->buildInventoryCards($userWallets) $this->buildInventoryCards($userWallets)
); );
$metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalog); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalog, $inventoryCards);
$cashWallet = (float) ($mainWallet->cash ?? 0); $cashWallet = (float) ($mainWallet->cash ?? 0);
$assetsRialValue = $this->sumAssetsRialValue($userWallets); $assetsRialValue = $this->sumAssetsRialValue($userWallets);
$totalRialValue = $cashWallet + $assetsRialValue; $totalRialValue = $cashWallet + $assetsRialValue;
@@ -95,7 +95,6 @@ class AssetSummaryService
Collection $catalog, Collection $catalog,
?Wallet $mainWallet, ?Wallet $mainWallet,
array $metalIds, array $metalIds,
string $weightUnit,
Collection $userWallets Collection $userWallets
): array { ): array {
$cards = []; $cards = [];
@@ -134,7 +133,7 @@ class AssetSummaryService
'unit' => Controller::productQuantityUnit($productPayload), 'unit' => Controller::productQuantityUnit($productPayload),
'display_type' => 'weight', 'display_type' => 'weight',
'unit_weight' => $this->productUnitWeightLabel($product), 'unit_weight' => $this->productUnitWeightLabel($product),
'show_in_assets' => true, 'show_in_assets' => $this->shouldShowInAssets($product),
'is_melted' => true, 'is_melted' => true,
'file' => $this->filePayload($product), 'file' => $this->filePayload($product),
]; ];
@@ -242,7 +241,7 @@ class AssetSummaryService
'unit' => Controller::productQuantityUnit($productPayload), 'unit' => Controller::productQuantityUnit($productPayload),
'display_type' => $isCountBased ? 'count' : 'weight', 'display_type' => $isCountBased ? 'count' : 'weight',
'unit_weight' => $this->productUnitWeightLabel($product), 'unit_weight' => $this->productUnitWeightLabel($product),
'show_in_assets' => true, 'show_in_assets' => $this->shouldShowInAssets($product),
'file' => $this->filePayload($product), 'file' => $this->filePayload($product),
]; ];
} }
@@ -255,13 +254,15 @@ class AssetSummaryService
?Wallet $mainWallet, ?Wallet $mainWallet,
array $metalIds, array $metalIds,
string $weightUnit, string $weightUnit,
Collection $catalog Collection $catalog,
array $inventoryCards
): array { ): array {
if ($metalIds === []) { if ($metalIds === []) {
return []; return [];
} }
$mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0)); $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0));
$metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets);
$totals = []; $totals = [];
foreach ($metalIds as $metalId) { foreach ($metalIds as $metalId) {
@@ -272,23 +273,40 @@ class AssetSummaryService
continue; continue;
} }
if ((int) $walletLine->product->metal_id !== (int) $metalId) { $product = $walletLine->product;
if (!$this->shouldShowInAssets($product)) {
continue; continue;
} }
if ((string) ($walletLine->product->group_type ?? '') === 'سکه') { if ((int) $product->metal_id !== (int) $metalId) {
continue;
}
if ((string) ($product->group_type ?? '') === 'سکه') {
continue; continue;
} }
$productGrams += Controller::productLineWeightToGrams( $productGrams += Controller::productLineWeightToGrams(
$walletLine->product, $product,
$walletLine->gold $walletLine->gold
); );
} }
$meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog); $meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog);
if (in_array((int) $metalId, $metalsWithProductWallets, true)) {
$meltedGrams = 0.0;
}
$productWeight = round(Controller::gramsToTradeUnit($productGrams), 3); $productWeight = round(Controller::gramsToTradeUnit($productGrams), 3);
$meltedWeight = round(Controller::gramsToTradeUnit($meltedGrams), 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;
}
$totals[] = [ $totals[] = [
'metal_id' => (int) $metalId, 'metal_id' => (int) $metalId,
@@ -296,7 +314,7 @@ class AssetSummaryService
'label' => 'کل دارایی ' . (self::METAL_LABELS[$metalId] ?? 'فلز'), 'label' => 'کل دارایی ' . (self::METAL_LABELS[$metalId] ?? 'فلز'),
'product_weight' => $productWeight, 'product_weight' => $productWeight,
'melted_weight' => $meltedWeight, 'melted_weight' => $meltedWeight,
'total_weight' => round($productWeight + $meltedWeight, 3), 'total_weight' => $totalWeight,
'unit' => $weightUnit, 'unit' => $weightUnit,
]; ];
} }
@@ -304,6 +322,21 @@ class AssetSummaryService
return $totals; 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( private function meltedGramsForMetal(
float $mainWalletGrams, float $mainWalletGrams,
int $metalId, int $metalId,
@@ -335,6 +368,10 @@ class AssetSummaryService
} }
$product = $walletLine->product; $product = $walletLine->product;
if (!$this->shouldShowInAssets($product)) {
continue;
}
$qty = (float) $walletLine->gold; $qty = (float) $walletLine->gold;
$metalId = (int) ($product->metal_id ?? 0); $metalId = (int) ($product->metal_id ?? 0);
@@ -350,11 +387,11 @@ class AssetSummaryService
private function shouldShowInAssets(Product $product): bool private function shouldShowInAssets(Product $product): bool
{ {
if (Product::hasShowInAssetsColumn()) { if (!Product::hasShowInAssetsColumn()) {
return (bool) ($product->show_in_assets ?? false); return false;
} }
return true; return (bool) ($product->show_in_assets ?? false);
} }
private function isCountBasedProduct(Product $product): bool private function isCountBasedProduct(Product $product): bool