From 070acd52e05d79bc199e0bac2c9926e4ab02e313 Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Thu, 19 Nov 2026 11:00:41 +0330 Subject: [PATCH] fix(wallet,assets): fix 3 extra zeros in wallet rial value and handle closed market in asset calculations --- app/Services/AssetSummaryService.php | 56 ++++++++++++--- app/Services/BalanceReportService.php | 6 +- app/Services/ProductPriceSyncService.php | 6 +- app/Services/WalletBalanceService.php | 90 ++++++++++++++++++++++-- app/Services/ZaarboxPriceService.php | 3 + 5 files changed, 143 insertions(+), 18 deletions(-) diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 7310c96..9e54905 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -185,13 +185,14 @@ class AssetSummaryService if ($totalBought > 0 && $weightedSum > 0) { $averageBuyPrice = round($weightedSum / $totalBought, 0); $costBasisRial = round($averageBuyPrice * $metalWeight, 0); - $unrealizedProfitRial = round($metalRialValue - $costBasisRial, 0); - if ($costBasisRial > 0) { + + if ($metalRialValue > 0 && $costBasisRial > 0) { + $unrealizedProfitRial = round($metalRialValue - $costBasisRial, 0); $unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2); - } - // سود وزنی: همان سود ریالی تبدیل‌شده به وزن فلز با قیمت زندهٔ امروز - if ($livePrice > 0) { - $unrealizedProfitGold = round($unrealizedProfitRial / $livePrice, 4); + // سود وزنی: همان سود ریالی تبدیل‌شده به وزن فلز با قیمت زندهٔ امروز + if ($livePrice > 0) { + $unrealizedProfitGold = round($unrealizedProfitRial / $livePrice, 4); + } } } } @@ -806,6 +807,20 @@ class AssetSummaryService return $total; } + public function normalizeCopperUnitPrice(float $price, int $metalId = 3): float + { + if ($price <= 0) { + return 0.0; + } + + // اگر فلز مس باشد و قیمت بر حسب تن (بیشتر از ۵۰۰ میلیون ریال) باشد، به کیلوگرم تبدیل می‌شود + if (($metalId === 3 || $metalId === 0) && $price > 500_000_000) { + return $price / 1000.0; + } + + return $price; + } + private function resolveLivePricePerUnit(int $metalId): float { $prices = app(ProductDisplayPriceService::class); @@ -821,16 +836,27 @@ class AssetSummaryService if ($copperProduct) { $base = (float) $prices->resolveOrderBasePrice($copperProduct); if ($base > 0) { - return $base; + return $this->normalizeCopperUnitPrice($base, $metalId); + } + + $pUnit = (float) $copperProduct->price + (float) ($copperProduct->price_profit ?? 0); + if ($pUnit > 0) { + return $this->normalizeCopperUnitPrice($pUnit, $metalId); } } $live = (float) $prices->resolveAppBasePrice(); if ($live > 0) { - return $live; + return $this->normalizeCopperUnitPrice($live, $metalId); } - return (float) (Price::latest('id')->value('price') ?? 0); + $historical = (float) $prices->resolveHistoricalBasePrice(); + if ($historical > 0) { + return $this->normalizeCopperUnitPrice($historical, $metalId); + } + + $latestPrice = (float) (Price::latest('id')->value('price') ?? 0); + return $this->normalizeCopperUnitPrice($latestPrice, $metalId); } if ($metalId === 1) { @@ -839,6 +865,11 @@ class AssetSummaryService return (float) $live; } + $historical = (float) $prices->resolveHistoricalBasePrice(); + if ($historical > 0) { + return $historical; + } + return (float) (Price::latest('id')->value('price') ?? 0); } @@ -855,13 +886,16 @@ class AssetSummaryService $base = (float) $prices->resolveOrderBasePrice($product); if ($base > 0) { - return $base; + return $this->normalizeCopperUnitPrice($base, $metalId); } $unitPrice = $this->productDisplayUnitPrice($product); $amount = (float) ($product->amount ?? 1); + if (Controller::isWeightBasedProductUnit($product->unit ?? null)) { + $amount = 1; + } - return $amount > 0 ? $unitPrice * $amount : $unitPrice; + return $this->normalizeCopperUnitPrice($amount > 0 ? $unitPrice * $amount : $unitPrice, $metalId); } private function productDisplayUnitPrice(Product $product): float diff --git a/app/Services/BalanceReportService.php b/app/Services/BalanceReportService.php index 33c6473..2895139 100644 --- a/app/Services/BalanceReportService.php +++ b/app/Services/BalanceReportService.php @@ -47,7 +47,11 @@ class BalanceReportService public function gramsToMetalDisplay(float $grams, int $metalId): float { - return (int) $metalId === 3 ? $grams / 1000.0 : $grams; + if ((int) $metalId === 3 || (int) $metalId === 0 || $this->metalDisplayUnit($metalId) === 'کیلوگرم') { + return $grams / 1000.0; + } + + return $grams; } /** diff --git a/app/Services/ProductPriceSyncService.php b/app/Services/ProductPriceSyncService.php index 7b9c016..3e26087 100644 --- a/app/Services/ProductPriceSyncService.php +++ b/app/Services/ProductPriceSyncService.php @@ -219,7 +219,11 @@ class ProductPriceSyncService } if ($product->reading_method == 'مس' && $CP) { - ProductPriceService::syncFromReference($product, (int) $CP, 0); + $unitCp = (int) $CP; + if ($unitCp > 500_000_000) { + $unitCp = (int) round($unitCp / 1000); + } + ProductPriceService::syncFromReference($product, $unitCp, 0); Product::where('id', $product->id)->update(['show' => $show]); $synced = true; } diff --git a/app/Services/WalletBalanceService.php b/app/Services/WalletBalanceService.php index 1b4193e..bc7f244 100644 --- a/app/Services/WalletBalanceService.php +++ b/app/Services/WalletBalanceService.php @@ -523,7 +523,7 @@ class WalletBalanceService 'label' => $this->balanceReportService->metalLabel($metalId), 'gold' => $displayGold, 'display_unit' => $this->balanceReportService->metalDisplayUnit($metalId), - 'goldToCash' => round($goldGrams * $price, 2), + 'goldToCash' => round($displayGold * $price, 2), ]; } @@ -613,7 +613,23 @@ class WalletBalanceService } $goldGrams = $this->walletLineGoldGrams($row); - $goldToCash = $goldGrams > 0 ? (int) round($goldGrams * $this->livePricePerGram($metalId)) : 0; + $displayGold = $this->balanceReportService->gramsToMetalDisplay($goldGrams, $metalId); + + $unitPrice = 0.0; + if ($product !== null && (float) ($product->price ?? 0) > 0) { + $pPrice = (float) $product->price + (float) ($product->price_profit ?? 0); + $amount = (float) ($product->amount ?? 1); + if (Controller::isWeightBasedProductUnit($product->unit ?? null)) { + $amount = 1; + } + $effective = $amount > 0 ? $pPrice * $amount : $pPrice; + $unitPrice = $this->normalizeMetalUnitPrice($effective, $metalId); + } + if ($unitPrice <= 0) { + $unitPrice = $this->livePricePerGram($metalId); + } + + $goldToCash = $displayGold > 0 ? (int) round($displayGold * $unitPrice) : 0; return [ 'wallet_kind' => $kind, @@ -627,19 +643,80 @@ class WalletBalanceService 'wallet_metal_id' => $metalId, 'wallet_metal_label' => $metalLabel, 'gold_to_cash' => $goldToCash, - 'gold_grams' => round($goldGrams, 3), + 'gold_grams' => round($displayGold, 3), 'total_rial_value' => (int) round($cash + $goldToCash), ]; } + public function normalizeMetalUnitPrice(float $price, int $metalId = 3): float + { + if ($price <= 0) { + return 0.0; + } + + // اگر فلز مس باشد و قیمت بر حسب تن (بیشتر از ۵۰۰ میلیون ریال) باشد، به کیلوگرم تبدیل می‌شود + if (($metalId === 3 || $metalId === 0) && $price > 500_000_000) { + return $price / 1000.0; + } + + return $price; + } + private function livePricePerGram(int $metalId): float { + $prices = app(ProductDisplayPriceService::class); + $mainMetalId = $this->resolveMainWalletMetalId(); + + if ($metalId === 3 || $metalId === $mainMetalId) { + $product = Product::query() + ->where('active', 1) + ->where('metal_id', $metalId) + ->where('price', '>', 0) + ->orderBy('id') + ->first(); + + if ($product) { + $base = (float) $prices->resolveOrderBasePrice($product); + if ($base > 0) { + return $this->normalizeMetalUnitPrice($base, $metalId); + } + + $unitPrice = (float) $product->price + (float) ($product->price_profit ?? 0); + $amount = (float) ($product->amount ?? 1); + if (Controller::isWeightBasedProductUnit($product->unit ?? null)) { + $amount = 1; + } + $effective = $amount > 0 ? $unitPrice * $amount : $unitPrice; + if ($effective > 0) { + return $this->normalizeMetalUnitPrice($effective, $metalId); + } + } + + $live = (float) $prices->resolveAppBasePrice(); + if ($live > 0) { + return $this->normalizeMetalUnitPrice($live, $metalId); + } + + $historical = (float) $prices->resolveHistoricalBasePrice(); + if ($historical > 0) { + return $this->normalizeMetalUnitPrice($historical, $metalId); + } + + $latestPrice = (float) (Price::latest('id')->value('price') ?? 0); + return $this->normalizeMetalUnitPrice($latestPrice, $metalId); + } + if ($metalId === 1) { - $live = (int) app(ProductDisplayPriceService::class)->resolveAppBasePrice(); + $live = (int) $prices->resolveAppBasePrice(); if ($live > 0) { return (float) $live; } + $historical = (float) $prices->resolveHistoricalBasePrice(); + if ($historical > 0) { + return $historical; + } + return (float) (Price::latest('id')->value('price') ?? 0); } @@ -656,8 +733,11 @@ class WalletBalanceService $unitPrice = (float) $product->price + (float) ($product->price_profit ?? 0); $amount = (float) ($product->amount ?? 1); + if (Controller::isWeightBasedProductUnit($product->unit ?? null)) { + $amount = 1; + } - return $amount > 0 ? $unitPrice * $amount : $unitPrice; + return $this->normalizeMetalUnitPrice($amount > 0 ? $unitPrice * $amount : $unitPrice, $metalId); } private function lineProduct(mixed $line): ?object diff --git a/app/Services/ZaarboxPriceService.php b/app/Services/ZaarboxPriceService.php index eb2a0fa..392077f 100644 --- a/app/Services/ZaarboxPriceService.php +++ b/app/Services/ZaarboxPriceService.php @@ -844,6 +844,9 @@ class ZaarboxPriceService if ($copper !== null && $dollar !== null) { $cp = (int) round(($copper / 1000000) * $dollar); + if ($cp > 500_000_000) { + $cp = (int) round($cp / 1000); + } $copperProducts = Product::where('metal_id', 3)->get(); foreach ($copperProducts as $copperProduct) { ProductPriceService::syncFromReference($copperProduct, $cp, 0);