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