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