fix(wallet,assets): fix 3 extra zeros in wallet rial value and handle closed market in asset calculations

This commit is contained in:
2026-11-19 11:00:41 +03:30
parent ce9d62ffaa
commit 070acd52e0
5 changed files with 143 additions and 18 deletions
+45 -11
View File
@@ -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