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
+85 -5
View File
@@ -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