diff --git a/src/utils/productAssets.ts b/src/utils/productAssets.ts index 32b5907..23d8ef0 100644 --- a/src/utils/productAssets.ts +++ b/src/utils/productAssets.ts @@ -520,10 +520,14 @@ export function computeMetalRialValue( } const metalId = Number(metal.metal_id ?? 0) - const unitPrice = + let unitPrice = livePricesByMetal?.[metalId] ?? (metalId === METAL_COPPER || metalId === METAL_GOLD ? liveGoldPrice : 0) + if ((metalId === METAL_COPPER || metalId === 0) && unitPrice > 500_000_000) { + unitPrice = unitPrice / 1000 + } + return sum + weight * unitPrice }, 0) } @@ -532,8 +536,13 @@ export function resolveAssetsRialValue( summary: { assets_rial_value?: number; metal_totals?: Array<{ metal_id?: number; total_weight?: number }> } | null | undefined, liveGoldPrice: number ): number { - const fromServer = Number(summary?.assets_rial_value ?? 0) + let fromServer = Number(summary?.assets_rial_value ?? 0) if (fromServer > 0) { + const copper = summary?.metal_totals?.find((m) => Number(m.metal_id ?? 0) === METAL_COPPER || Number(m.metal_id ?? 0) === 0) + const copperWeight = Number(copper?.total_weight ?? 0) + if (copperWeight > 0 && fromServer / copperWeight > 500_000_000) { + fromServer = fromServer / 1000 + } return fromServer } diff --git a/src/views/base/assets/index.vue b/src/views/base/assets/index.vue index 75ac684..bba3b92 100644 --- a/src/views/base/assets/index.vue +++ b/src/views/base/assets/index.vue @@ -240,14 +240,32 @@ const { assetsSummary, loadWalletAssets } = useWalletAssets() const inventoryCards = computed(() => assetsSummary.value?.inventory_cards ?? []) const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? []) const cashWallet = computed(() => assetsSummary.value?.cash_wallet ?? 0) +const effectiveClosingPrice = computed(() => { + const p = lastPrice.value as any + const buy = getTradeBuyPrice(p) + if (buy > 0) return buy > 500_000_000 ? buy / 1000 : buy + const kilo = Number(p?.copper_kilo ?? 0) + if (kilo > 0) return kilo > 500_000_000 ? kilo / 1000 : kilo + const ton = Number(p?.copper_ton ?? 0) + if (ton > 500_000_000) return ton / 1000 + const price = Number(p?.price_last ?? p?.price ?? 0) + if (price > 500_000_000) return price / 1000 + return price > 0 ? price : 0 +}) + const metalRialValue = computed(() => { - const fromSummary = Number(assetsSummary.value?.assets_rial_value ?? 0) - if (fromSummary > 0) return fromSummary - return resolveAssetsRialValue(assetsSummary.value, getTradeBuyPrice(lastPrice.value)) + return resolveAssetsRialValue(assetsSummary.value, effectiveClosingPrice.value) }) const totalRialValue = computed(() => { - const fromSummary = Number(assetsSummary.value?.total_rial_value ?? 0) - if (fromSummary > 0) return fromSummary + let fromSummary = Number(assetsSummary.value?.total_rial_value ?? 0) + const copper = metalTotals.value.find((m) => Number(m.metal_id ?? 0) === 3 || Number(m.metal_id ?? 0) === 0) + const copperWeight = Number(copper?.total_weight ?? 0) + if (copperWeight > 0 && fromSummary / copperWeight > 500_000_000) { + fromSummary = fromSummary / 1000 + } + if (fromSummary > 0 && Math.abs(fromSummary - (cashWallet.value + metalRialValue.value)) < 1000) { + return fromSummary + } return cashWallet.value + metalRialValue.value }) @@ -264,15 +282,37 @@ const { } = useAssetsPortfolioChart(cashWallet, metalRialValue) const portfolioInsight = computed(() => assetsSummary.value?.portfolio) -const profitSheetLabel = computed(() => { +const safeUnrealizedProfitRial = computed(() => { const profit = portfolioInsight.value?.unrealized_profit_rial + if (profit == null) return null + if (Math.abs(profit) > 100_000_000_000 && Number(portfolioInsight.value?.cost_basis_rial ?? 0) < 10_000_000_000) { + return Math.round(profit / 1000) + } + return profit +}) + +const safeUnrealizedProfitPercent = computed(() => { + const pct = portfolioInsight.value?.unrealized_profit_percent + if (pct == null) return null + if (Math.abs(pct) > 1000) { + const costBasis = Number(portfolioInsight.value?.cost_basis_rial ?? 0) + const profit = safeUnrealizedProfitRial.value + if (costBasis > 0 && profit != null) { + return Math.round((profit / costBasis) * 10000) / 100 + } + } + return pct +}) + +const profitSheetLabel = computed(() => { + const profit = safeUnrealizedProfitRial.value if (profit == null) return '—' - const pct = formatPortfolioProfitPercent(portfolioInsight.value?.unrealized_profit_percent) + const pct = formatPortfolioProfitPercent(safeUnrealizedProfitPercent.value) return `${formatPortfolioProfitText(profit)}${pct ? ` (${pct})` : ''}` }) const profitClass = computed(() => { - const profit = portfolioInsight.value?.unrealized_profit_rial + const profit = safeUnrealizedProfitRial.value if (profit == null || profit === 0) return '' return profit > 0 ? 'is-profit' : 'is-loss' }) @@ -290,17 +330,19 @@ const summaryItems = computed(() => { { label: 'ارزش ریالی مس', value: usePrice(metalRialValue.value) }, ) if (portfolioInsight.value?.average_buy_price != null) { + let avg = Number(portfolioInsight.value.average_buy_price ?? 0) + if (avg > 500_000_000) avg = Math.round(avg / 1000) items.push({ label: 'میانگین خرید', - value: usePrice(portfolioInsight.value.average_buy_price ?? 0), + value: usePrice(avg), }) } return items }) const assetRows = computed(() => { - const pct = formatPortfolioProfitPercent(portfolioInsight.value?.unrealized_profit_percent) - const profit = portfolioInsight.value?.unrealized_profit_rial + const pct = formatPortfolioProfitPercent(safeUnrealizedProfitPercent.value) + const profit = safeUnrealizedProfitRial.value const changeClass = profit == null || profit === 0 ? '' : profit > 0 ? 'is-profit' : 'is-loss' const rows = metalTotals.value.flatMap((metal) => { const unit = metal.metal_id === 3 ? resolveBoursecuCopperUnit(metal.unit) : (metal.unit || 'کیلوگرم') diff --git a/src/views/base/home/components/BoursecuAssetsSummary.vue b/src/views/base/home/components/BoursecuAssetsSummary.vue index ddd752d..e215e8a 100644 --- a/src/views/base/home/components/BoursecuAssetsSummary.vue +++ b/src/views/base/home/components/BoursecuAssetsSummary.vue @@ -66,8 +66,21 @@ const { assetsSummary } = useWalletAssets() const rootRef = ref(null) const openRowId = ref(null) +const effectiveClosingPrice = computed(() => { + const p = lastPrice.value as any + const buy = getTradeBuyPrice(p) + if (buy > 0) return buy > 500_000_000 ? buy / 1000 : buy + const kilo = Number(p?.copper_kilo ?? 0) + if (kilo > 0) return kilo > 500_000_000 ? kilo / 1000 : kilo + const ton = Number(p?.copper_ton ?? 0) + if (ton > 500_000_000) return ton / 1000 + const price = Number(p?.price_last ?? p?.price ?? 0) + if (price > 500_000_000) return price / 1000 + return price > 0 ? price : 0 +}) + const rows = computed(() => - buildBoursecuHomeAssetRows(assetsSummary.value, getTradeBuyPrice(lastPrice.value)) + buildBoursecuHomeAssetRows(assetsSummary.value, effectiveClosingPrice.value) ) function toggleRow(rowId: BoursecuHomeAssetRowId) {