fix wallet 4
This commit is contained in:
@@ -40,13 +40,15 @@ class AssetSummaryService
|
|||||||
$hasCountBased = $this->catalogHasCountBased($catalog);
|
$hasCountBased = $this->catalogHasCountBased($catalog);
|
||||||
$hasWeightBased = $this->catalogHasWeightBased($catalog);
|
$hasWeightBased = $this->catalogHasWeightBased($catalog);
|
||||||
|
|
||||||
|
$catalogById = $catalog->keyBy('id');
|
||||||
|
|
||||||
$inventoryCards = array_merge(
|
$inventoryCards = array_merge(
|
||||||
$this->buildMeltedInventoryCards($catalog, $mainWallet, $metalIds, $userWallets),
|
$this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $userWallets),
|
||||||
$this->buildInventoryCards($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);
|
$cashWallet = (float) ($mainWallet->cash ?? 0);
|
||||||
$assetsRialValue = $this->sumAssetsRialValue($userWallets);
|
$assetsRialValue = $this->sumAssetsRialValue($userWallets, $catalogById);
|
||||||
$totalRialValue = $cashWallet + $assetsRialValue;
|
$totalRialValue = $cashWallet + $assetsRialValue;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -93,6 +95,7 @@ class AssetSummaryService
|
|||||||
|
|
||||||
private function buildMeltedInventoryCards(
|
private function buildMeltedInventoryCards(
|
||||||
Collection $catalog,
|
Collection $catalog,
|
||||||
|
Collection $catalogById,
|
||||||
?Wallet $mainWallet,
|
?Wallet $mainWallet,
|
||||||
array $metalIds,
|
array $metalIds,
|
||||||
Collection $userWallets
|
Collection $userWallets
|
||||||
@@ -100,10 +103,10 @@ class AssetSummaryService
|
|||||||
$cards = [];
|
$cards = [];
|
||||||
$mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0));
|
$mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0));
|
||||||
$assignedMetalIds = [];
|
$assignedMetalIds = [];
|
||||||
$metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets);
|
$metalsWithProductWallets = $this->metalIdsWithVisibleProductWallet($userWallets, $catalogById);
|
||||||
|
|
||||||
foreach ($this->resolveMeltedProducts($catalog) as $product) {
|
foreach ($this->resolveMeltedProducts($catalog) as $product) {
|
||||||
if (!$this->shouldShowInAssets($product)) {
|
if (!$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +119,7 @@ class AssetSummaryService
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog);
|
$meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog, $catalogById);
|
||||||
if ($meltedGrams <= 0) {
|
if ($meltedGrams <= 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -133,7 +136,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' => $this->shouldShowInAssets($product),
|
'show_in_assets' => true,
|
||||||
'is_melted' => true,
|
'is_melted' => true,
|
||||||
'file' => $this->filePayload($product),
|
'file' => $this->filePayload($product),
|
||||||
];
|
];
|
||||||
@@ -142,6 +145,11 @@ class AssetSummaryService
|
|||||||
return $cards;
|
return $cards;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function catalogProduct(Product $product, Collection $catalogById): Product
|
||||||
|
{
|
||||||
|
return $catalogById->get((int) $product->id) ?? $product;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* محصولات آبشده: دسته «آبشده»/«کاتد مس» + محصول اصلی تنظیمات (main_product).
|
* محصولات آبشده: دسته «آبشده»/«کاتد مس» + محصول اصلی تنظیمات (main_product).
|
||||||
*/
|
*/
|
||||||
@@ -183,16 +191,16 @@ class AssetSummaryService
|
|||||||
return $group !== '' ? $group : 'آبشده';
|
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(
|
return $this->resolveMeltedProducts($catalog)->contains(
|
||||||
fn (Product $product) => (int) ($product->metal_id ?? 0) === (int) $metalId
|
fn (Product $product) => (int) ($product->metal_id ?? 0) === (int) $metalId
|
||||||
&& $this->shouldShowInAssets($product)
|
&& $this->shouldShowInAssets($this->catalogProduct($product, $catalogById))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** فلزهایی که کاربر موجودی روی کیف محصول دارد — کارت آبشده جدا لازم نیست. */
|
/** فلزهایی که کاربر موجودی روی کیف محصول visible دارد — کارت آبشده جدا لازم نیست. */
|
||||||
private function metalIdsWithVisibleProductWallet(Collection $userWallets): array
|
private function metalIdsWithVisibleProductWallet(Collection $userWallets, Collection $catalogById): array
|
||||||
{
|
{
|
||||||
$metalIds = [];
|
$metalIds = [];
|
||||||
|
|
||||||
@@ -202,7 +210,7 @@ class AssetSummaryService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$product = $walletLine->product;
|
$product = $walletLine->product;
|
||||||
if (!$product || !$this->shouldShowInAssets($product)) {
|
if (!$product || !$this->shouldShowInAssets($this->catalogProduct($product, $catalogById))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +223,7 @@ class AssetSummaryService
|
|||||||
return array_values(array_unique($metalIds));
|
return array_values(array_unique($metalIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildInventoryCards(Collection $userWallets): array
|
private function buildInventoryCards(Collection $userWallets, Collection $catalogById): array
|
||||||
{
|
{
|
||||||
$cards = [];
|
$cards = [];
|
||||||
|
|
||||||
@@ -225,24 +233,29 @@ class AssetSummaryService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$product = $walletLine->product;
|
$product = $walletLine->product;
|
||||||
if (!$product || !$this->shouldShowInAssets($product)) {
|
if (!$product) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$productPayload = $this->productPayload($product);
|
$catalogProduct = $this->catalogProduct($product, $catalogById);
|
||||||
$isCountBased = $this->isCountBasedProduct($product);
|
if (!$this->shouldShowInAssets($catalogProduct)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$productPayload = $this->productPayload($catalogProduct);
|
||||||
|
$isCountBased = $this->isCountBasedProduct($catalogProduct);
|
||||||
|
|
||||||
$cards[] = [
|
$cards[] = [
|
||||||
'product_id' => (int) $product->id,
|
'product_id' => (int) $catalogProduct->id,
|
||||||
'title' => (string) $product->title,
|
'title' => (string) $catalogProduct->title,
|
||||||
'group_type' => (string) ($product->group_type ?? ''),
|
'group_type' => (string) ($catalogProduct->group_type ?? ''),
|
||||||
'metal_id' => (int) ($product->metal_id ?? 0),
|
'metal_id' => (int) ($catalogProduct->metal_id ?? 0),
|
||||||
'quantity' => round((float) $walletLine->gold, 3),
|
'quantity' => round((float) $walletLine->gold, 3),
|
||||||
'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($catalogProduct),
|
||||||
'show_in_assets' => $this->shouldShowInAssets($product),
|
'show_in_assets' => true,
|
||||||
'file' => $this->filePayload($product),
|
'file' => $this->filePayload($catalogProduct),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,15 +267,13 @@ class AssetSummaryService
|
|||||||
?Wallet $mainWallet,
|
?Wallet $mainWallet,
|
||||||
array $metalIds,
|
array $metalIds,
|
||||||
string $weightUnit,
|
string $weightUnit,
|
||||||
Collection $catalog,
|
Collection $catalogById
|
||||||
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) {
|
||||||
@@ -273,40 +284,25 @@ class AssetSummaryService
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$product = $walletLine->product;
|
$catalogProduct = $this->catalogProduct($walletLine->product, $catalogById);
|
||||||
if (!$this->shouldShowInAssets($product)) {
|
if ((int) ($catalogProduct->metal_id ?? 0) !== (int) $metalId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((int) $product->metal_id !== (int) $metalId) {
|
if ((string) ($catalogProduct->group_type ?? '') === 'سکه') {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((string) ($product->group_type ?? '') === 'سکه') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$productGrams += Controller::productLineWeightToGrams(
|
$productGrams += Controller::productLineWeightToGrams(
|
||||||
$product,
|
$catalogProduct,
|
||||||
$walletLine->gold
|
$walletLine->gold
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$meltedGrams = $this->meltedGramsForMetal($mainWalletGrams, $metalId, $metalIds, $catalog);
|
$meltedGrams = $this->meltedGramsForMetalTotal($mainWalletGrams, $metalId, $metalIds);
|
||||||
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 = round($productWeight + $meltedWeight, 3);
|
||||||
$totalWeight = $visibleCardWeight > 0
|
|
||||||
? $visibleCardWeight
|
|
||||||
: round($productWeight + $meltedWeight, 3);
|
|
||||||
|
|
||||||
if ($totalWeight <= 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$totals[] = [
|
$totals[] = [
|
||||||
'metal_id' => (int) $metalId,
|
'metal_id' => (int) $metalId,
|
||||||
@@ -322,35 +318,16 @@ class AssetSummaryService
|
|||||||
return $totals;
|
return $totals;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function visibleCardWeightForMetal(array $inventoryCards, int $metalId): float
|
/** جمع واقعی کیف اصلی برای هر فلز — مستقل از show_in_assets. */
|
||||||
{
|
private function meltedGramsForMetalTotal(
|
||||||
$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,
|
float $mainWalletGrams,
|
||||||
int $metalId,
|
int $metalId,
|
||||||
array $catalogMetalIds,
|
array $catalogMetalIds
|
||||||
Collection $catalog
|
|
||||||
): float {
|
): float {
|
||||||
if ($mainWalletGrams <= 0) {
|
if ($mainWalletGrams <= 0) {
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->hasVisibleMeltedProductForMetal($catalog, $metalId)) {
|
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($catalogMetalIds) === 1) {
|
if (count($catalogMetalIds) === 1) {
|
||||||
return (int) $catalogMetalIds[0] === (int) $metalId ? $mainWalletGrams : 0.0;
|
return (int) $catalogMetalIds[0] === (int) $metalId ? $mainWalletGrams : 0.0;
|
||||||
}
|
}
|
||||||
@@ -358,7 +335,29 @@ class AssetSummaryService
|
|||||||
return (int) $metalId === 1 ? $mainWalletGrams : 0.0;
|
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;
|
$total = 0.0;
|
||||||
|
|
||||||
@@ -367,18 +366,18 @@ class AssetSummaryService
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$product = $walletLine->product;
|
$catalogProduct = $this->catalogProduct($walletLine->product, $catalogById);
|
||||||
if (!$this->shouldShowInAssets($product)) {
|
if (!$this->shouldShowInAssets($catalogProduct)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$qty = (float) $walletLine->gold;
|
$qty = (float) $walletLine->gold;
|
||||||
$metalId = (int) ($product->metal_id ?? 0);
|
$metalId = (int) ($catalogProduct->metal_id ?? 0);
|
||||||
|
|
||||||
if ($metalId === 1) {
|
if ($metalId === 1) {
|
||||||
$total += (float) ($product->price ?? 0) * $qty;
|
$total += (float) ($catalogProduct->price ?? 0) * $qty;
|
||||||
} elseif ($metalId === 2 || $metalId === 3) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user