fix(wallet,assets): fix 3 extra zeros in wallet rial value and handle closed market in asset calculations
This commit is contained in:
@@ -185,13 +185,14 @@ class AssetSummaryService
|
|||||||
if ($totalBought > 0 && $weightedSum > 0) {
|
if ($totalBought > 0 && $weightedSum > 0) {
|
||||||
$averageBuyPrice = round($weightedSum / $totalBought, 0);
|
$averageBuyPrice = round($weightedSum / $totalBought, 0);
|
||||||
$costBasisRial = round($averageBuyPrice * $metalWeight, 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);
|
$unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2);
|
||||||
}
|
// سود وزنی: همان سود ریالی تبدیلشده به وزن فلز با قیمت زندهٔ امروز
|
||||||
// سود وزنی: همان سود ریالی تبدیلشده به وزن فلز با قیمت زندهٔ امروز
|
if ($livePrice > 0) {
|
||||||
if ($livePrice > 0) {
|
$unrealizedProfitGold = round($unrealizedProfitRial / $livePrice, 4);
|
||||||
$unrealizedProfitGold = round($unrealizedProfitRial / $livePrice, 4);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -806,6 +807,20 @@ class AssetSummaryService
|
|||||||
return $total;
|
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
|
private function resolveLivePricePerUnit(int $metalId): float
|
||||||
{
|
{
|
||||||
$prices = app(ProductDisplayPriceService::class);
|
$prices = app(ProductDisplayPriceService::class);
|
||||||
@@ -821,16 +836,27 @@ class AssetSummaryService
|
|||||||
if ($copperProduct) {
|
if ($copperProduct) {
|
||||||
$base = (float) $prices->resolveOrderBasePrice($copperProduct);
|
$base = (float) $prices->resolveOrderBasePrice($copperProduct);
|
||||||
if ($base > 0) {
|
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();
|
$live = (float) $prices->resolveAppBasePrice();
|
||||||
if ($live > 0) {
|
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) {
|
if ($metalId === 1) {
|
||||||
@@ -839,6 +865,11 @@ class AssetSummaryService
|
|||||||
return (float) $live;
|
return (float) $live;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$historical = (float) $prices->resolveHistoricalBasePrice();
|
||||||
|
if ($historical > 0) {
|
||||||
|
return $historical;
|
||||||
|
}
|
||||||
|
|
||||||
return (float) (Price::latest('id')->value('price') ?? 0);
|
return (float) (Price::latest('id')->value('price') ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -855,13 +886,16 @@ class AssetSummaryService
|
|||||||
|
|
||||||
$base = (float) $prices->resolveOrderBasePrice($product);
|
$base = (float) $prices->resolveOrderBasePrice($product);
|
||||||
if ($base > 0) {
|
if ($base > 0) {
|
||||||
return $base;
|
return $this->normalizeCopperUnitPrice($base, $metalId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$unitPrice = $this->productDisplayUnitPrice($product);
|
$unitPrice = $this->productDisplayUnitPrice($product);
|
||||||
$amount = (float) ($product->amount ?? 1);
|
$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
|
private function productDisplayUnitPrice(Product $product): float
|
||||||
|
|||||||
@@ -47,7 +47,11 @@ class BalanceReportService
|
|||||||
|
|
||||||
public function gramsToMetalDisplay(float $grams, int $metalId): float
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -219,7 +219,11 @@ class ProductPriceSyncService
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($product->reading_method == 'مس' && $CP) {
|
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]);
|
Product::where('id', $product->id)->update(['show' => $show]);
|
||||||
$synced = true;
|
$synced = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -523,7 +523,7 @@ class WalletBalanceService
|
|||||||
'label' => $this->balanceReportService->metalLabel($metalId),
|
'label' => $this->balanceReportService->metalLabel($metalId),
|
||||||
'gold' => $displayGold,
|
'gold' => $displayGold,
|
||||||
'display_unit' => $this->balanceReportService->metalDisplayUnit($metalId),
|
'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);
|
$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 [
|
return [
|
||||||
'wallet_kind' => $kind,
|
'wallet_kind' => $kind,
|
||||||
@@ -627,19 +643,80 @@ class WalletBalanceService
|
|||||||
'wallet_metal_id' => $metalId,
|
'wallet_metal_id' => $metalId,
|
||||||
'wallet_metal_label' => $metalLabel,
|
'wallet_metal_label' => $metalLabel,
|
||||||
'gold_to_cash' => $goldToCash,
|
'gold_to_cash' => $goldToCash,
|
||||||
'gold_grams' => round($goldGrams, 3),
|
'gold_grams' => round($displayGold, 3),
|
||||||
'total_rial_value' => (int) round($cash + $goldToCash),
|
'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
|
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) {
|
if ($metalId === 1) {
|
||||||
$live = (int) app(ProductDisplayPriceService::class)->resolveAppBasePrice();
|
$live = (int) $prices->resolveAppBasePrice();
|
||||||
if ($live > 0) {
|
if ($live > 0) {
|
||||||
return (float) $live;
|
return (float) $live;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$historical = (float) $prices->resolveHistoricalBasePrice();
|
||||||
|
if ($historical > 0) {
|
||||||
|
return $historical;
|
||||||
|
}
|
||||||
|
|
||||||
return (float) (Price::latest('id')->value('price') ?? 0);
|
return (float) (Price::latest('id')->value('price') ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -656,8 +733,11 @@ class WalletBalanceService
|
|||||||
|
|
||||||
$unitPrice = (float) $product->price + (float) ($product->price_profit ?? 0);
|
$unitPrice = (float) $product->price + (float) ($product->price_profit ?? 0);
|
||||||
$amount = (float) ($product->amount ?? 1);
|
$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
|
private function lineProduct(mixed $line): ?object
|
||||||
|
|||||||
@@ -844,6 +844,9 @@ class ZaarboxPriceService
|
|||||||
|
|
||||||
if ($copper !== null && $dollar !== null) {
|
if ($copper !== null && $dollar !== null) {
|
||||||
$cp = (int) round(($copper / 1000000) * $dollar);
|
$cp = (int) round(($copper / 1000000) * $dollar);
|
||||||
|
if ($cp > 500_000_000) {
|
||||||
|
$cp = (int) round($cp / 1000);
|
||||||
|
}
|
||||||
$copperProducts = Product::where('metal_id', 3)->get();
|
$copperProducts = Product::where('metal_id', 3)->get();
|
||||||
foreach ($copperProducts as $copperProduct) {
|
foreach ($copperProducts as $copperProduct) {
|
||||||
ProductPriceService::syncFromReference($copperProduct, $cp, 0);
|
ProductPriceService::syncFromReference($copperProduct, $cp, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user