added merge wallet
This commit is contained in:
@@ -42,11 +42,15 @@ class AssetSummaryService
|
||||
|
||||
$catalogById = $catalog->keyBy('id');
|
||||
|
||||
// محصولات متصل به کیف اصلی (main_wallet_products) — موجودیشان = gold کیف اصلی
|
||||
$mainWalletProductIds = $this->configService->mainWalletProductIds();
|
||||
|
||||
$metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds);
|
||||
$inventoryCards = array_merge(
|
||||
$this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds),
|
||||
$this->buildInventoryCards($userWallets, $catalogById)
|
||||
$this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds),
|
||||
$this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds)
|
||||
);
|
||||
$metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById);
|
||||
$metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds);
|
||||
$cashWallet = (float) ($mainWallet->cash ?? 0);
|
||||
$assetsRialValue = $this->sumAssetsRialValue($userWallets, $catalogById);
|
||||
$totalRialValue = $cashWallet + $assetsRialValue;
|
||||
@@ -97,15 +101,51 @@ class AssetSummaryService
|
||||
Collection $catalog,
|
||||
Collection $catalogById,
|
||||
?Wallet $mainWallet,
|
||||
array $metalIds
|
||||
array $metalIds,
|
||||
array $metalsWithProductWallet = [],
|
||||
array $mainWalletProductIds = []
|
||||
): array {
|
||||
$cards = [];
|
||||
$mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0));
|
||||
$assignedMetalIds = [];
|
||||
$mainProductId = $this->configService->mainProductId();
|
||||
|
||||
// حالت صریح: محصولات متصل به کیف اصلی — یک کارت واحد (نماینده = اولین محصول لیست)
|
||||
if ($mainWalletProductIds !== []) {
|
||||
$representative = $this->resolveMainWalletRepresentative($catalogById, $mainWalletProductIds);
|
||||
if (!$representative) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$mainWalletWeight = (float) ($mainWallet->gold ?? 0);
|
||||
if ($mainWalletWeight <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$productPayload = $this->productPayload($representative);
|
||||
|
||||
return [[
|
||||
'product_id' => (int) $representative->id,
|
||||
'title' => (string) $representative->title,
|
||||
'group_type' => $this->meltedGroupTypeLabel($representative),
|
||||
'metal_id' => (int) ($representative->metal_id ?? 0),
|
||||
'quantity' => round($mainWalletWeight, 3),
|
||||
'unit' => Controller::productQuantityUnit($productPayload),
|
||||
'display_type' => 'weight',
|
||||
'unit_weight' => $this->productUnitWeightLabel($representative),
|
||||
'show_in_assets' => $this->shouldShowInAssets($representative),
|
||||
'is_melted' => true,
|
||||
'is_main_wallet' => true,
|
||||
'file' => $this->filePayload($representative),
|
||||
]];
|
||||
}
|
||||
|
||||
foreach ($metalIds as $metalId) {
|
||||
if ($metalId <= 0 || in_array($metalId, $assignedMetalIds, true)) {
|
||||
if (
|
||||
$metalId <= 0
|
||||
|| in_array($metalId, $assignedMetalIds, true)
|
||||
|| in_array((int) $metalId, $metalsWithProductWallet, true)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -186,6 +226,48 @@ class AssetSummaryService
|
||||
return $group !== '' ? $group : 'آبشده';
|
||||
}
|
||||
|
||||
/** اولین محصولِ لیست main_wallet_products که در کاتالوگ فعال است. */
|
||||
private function resolveMainWalletRepresentative(Collection $catalogById, array $mainWalletProductIds): ?Product
|
||||
{
|
||||
foreach ($mainWalletProductIds as $productId) {
|
||||
$product = $catalogById->get((int) $productId);
|
||||
if ($product) {
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** فلزهایی که کاربر روی کیف محصول موجودی دارد — برای جلوگیری از کارت آبشدهٔ تکراری. */
|
||||
private function metalIdsWithProductWallet(Collection $userWallets, Collection $catalogById, array $mainWalletProductIds = []): array
|
||||
{
|
||||
$metalIds = [];
|
||||
|
||||
foreach ($userWallets as $walletLine) {
|
||||
if ($walletLine->product_id === null || (float) $walletLine->gold <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// کیف محصولاتِ متصل به کیف اصلی نادیده گرفته میشود
|
||||
if (in_array((int) $walletLine->product_id, $mainWalletProductIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product = $walletLine->product;
|
||||
if (!$product) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$metalId = (int) ($this->catalogProduct($product, $catalogById)->metal_id ?? 0);
|
||||
if ($metalId > 0) {
|
||||
$metalIds[] = $metalId;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($metalIds));
|
||||
}
|
||||
|
||||
/** محصول نمایشی برای موجودی کیف اصلی هر فلز. */
|
||||
private function resolveDisplayProductForMetal(
|
||||
Collection $catalog,
|
||||
@@ -208,7 +290,7 @@ class AssetSummaryService
|
||||
return $catalog->first(fn (Product $product) => (int) ($product->metal_id ?? 0) === $metalId);
|
||||
}
|
||||
|
||||
private function buildInventoryCards(Collection $userWallets, Collection $catalogById): array
|
||||
private function buildInventoryCards(Collection $userWallets, Collection $catalogById, array $mainWalletProductIds = []): array
|
||||
{
|
||||
$cards = [];
|
||||
|
||||
@@ -217,6 +299,11 @@ class AssetSummaryService
|
||||
continue;
|
||||
}
|
||||
|
||||
// محصول متصل به کیف اصلی دارایی جدا ندارد — کارتش از کیف اصلی ساخته میشود
|
||||
if (in_array((int) $walletLine->product_id, $mainWalletProductIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product = $walletLine->product;
|
||||
if (!$product) {
|
||||
continue;
|
||||
@@ -248,7 +335,9 @@ class AssetSummaryService
|
||||
?Wallet $mainWallet,
|
||||
array $metalIds,
|
||||
string $weightUnit,
|
||||
Collection $catalogById
|
||||
Collection $catalogById,
|
||||
array $metalsWithProductWallet = [],
|
||||
array $mainWalletProductIds = []
|
||||
): array {
|
||||
if ($metalIds === []) {
|
||||
return [];
|
||||
@@ -257,6 +346,13 @@ class AssetSummaryService
|
||||
$mainWalletGrams = Controller::weightToGrams((float) ($mainWallet->gold ?? 0));
|
||||
$totals = [];
|
||||
|
||||
// فلز نمایندهٔ کیف اصلی در حالت main_wallet_products
|
||||
$mainWalletMetalId = null;
|
||||
if ($mainWalletProductIds !== []) {
|
||||
$representative = $this->resolveMainWalletRepresentative($catalogById, $mainWalletProductIds);
|
||||
$mainWalletMetalId = $representative ? (int) ($representative->metal_id ?? 0) : null;
|
||||
}
|
||||
|
||||
foreach ($metalIds as $metalId) {
|
||||
$productGrams = 0.0;
|
||||
|
||||
@@ -265,6 +361,11 @@ class AssetSummaryService
|
||||
continue;
|
||||
}
|
||||
|
||||
// کیف محصولاتِ متصل به کیف اصلی جداگانه شمرده نمیشود
|
||||
if (in_array((int) $walletLine->product_id, $mainWalletProductIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$catalogProduct = $this->catalogProduct($walletLine->product, $catalogById);
|
||||
if ((int) ($catalogProduct->metal_id ?? 0) !== (int) $metalId) {
|
||||
continue;
|
||||
@@ -280,7 +381,17 @@ class AssetSummaryService
|
||||
);
|
||||
}
|
||||
|
||||
$meltedGrams = $this->meltedGramsForMetalTotal($mainWalletGrams, $metalId, $metalIds);
|
||||
if ($mainWalletProductIds !== []) {
|
||||
// حالت صریح: کیف اصلی فقط یکبار، زیر فلزِ محصول نماینده
|
||||
$meltedGrams = ($mainWalletMetalId !== null && (int) $metalId === $mainWalletMetalId)
|
||||
? $mainWalletGrams
|
||||
: 0.0;
|
||||
} else {
|
||||
// کیف اصلی فقط برای فلزی که هیچ کیف محصولی ندارد به مجموع اضافه میشود.
|
||||
$meltedGrams = in_array((int) $metalId, $metalsWithProductWallet, true)
|
||||
? 0.0
|
||||
: $this->meltedGramsForMetalTotal($mainWalletGrams, $metalId, $metalIds);
|
||||
}
|
||||
$productWeight = round(Controller::gramsToTradeUnit($productGrams), 3);
|
||||
$meltedWeight = round(Controller::gramsToTradeUnit($meltedGrams), 3);
|
||||
$totalWeight = round($productWeight + $meltedWeight, 3);
|
||||
|
||||
Reference in New Issue
Block a user