fix(assets): resolve accurate copper assets and profit during closed market and normalize ton pricing

This commit is contained in:
2026-11-19 11:01:03 +03:30
parent 1aceafd02d
commit 84d95b46dc
3 changed files with 78 additions and 14 deletions
+11 -2
View File
@@ -520,10 +520,14 @@ export function computeMetalRialValue(
} }
const metalId = Number(metal.metal_id ?? 0) const metalId = Number(metal.metal_id ?? 0)
const unitPrice = let unitPrice =
livePricesByMetal?.[metalId] ?? livePricesByMetal?.[metalId] ??
(metalId === METAL_COPPER || metalId === METAL_GOLD ? liveGoldPrice : 0) (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 return sum + weight * unitPrice
}, 0) }, 0)
} }
@@ -532,8 +536,13 @@ export function resolveAssetsRialValue(
summary: { assets_rial_value?: number; metal_totals?: Array<{ metal_id?: number; total_weight?: number }> } | null | undefined, summary: { assets_rial_value?: number; metal_totals?: Array<{ metal_id?: number; total_weight?: number }> } | null | undefined,
liveGoldPrice: number liveGoldPrice: number
): number { ): number {
const fromServer = Number(summary?.assets_rial_value ?? 0) let fromServer = Number(summary?.assets_rial_value ?? 0)
if (fromServer > 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 return fromServer
} }
+53 -11
View File
@@ -240,14 +240,32 @@ const { assetsSummary, loadWalletAssets } = useWalletAssets()
const inventoryCards = computed(() => assetsSummary.value?.inventory_cards ?? []) const inventoryCards = computed(() => assetsSummary.value?.inventory_cards ?? [])
const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? []) const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? [])
const cashWallet = computed(() => assetsSummary.value?.cash_wallet ?? 0) 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 metalRialValue = computed(() => {
const fromSummary = Number(assetsSummary.value?.assets_rial_value ?? 0) return resolveAssetsRialValue(assetsSummary.value, effectiveClosingPrice.value)
if (fromSummary > 0) return fromSummary
return resolveAssetsRialValue(assetsSummary.value, getTradeBuyPrice(lastPrice.value))
}) })
const totalRialValue = computed(() => { const totalRialValue = computed(() => {
const fromSummary = Number(assetsSummary.value?.total_rial_value ?? 0) let fromSummary = Number(assetsSummary.value?.total_rial_value ?? 0)
if (fromSummary > 0) return fromSummary 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 return cashWallet.value + metalRialValue.value
}) })
@@ -264,15 +282,37 @@ const {
} = useAssetsPortfolioChart(cashWallet, metalRialValue) } = useAssetsPortfolioChart(cashWallet, metalRialValue)
const portfolioInsight = computed(() => assetsSummary.value?.portfolio) const portfolioInsight = computed(() => assetsSummary.value?.portfolio)
const profitSheetLabel = computed(() => { const safeUnrealizedProfitRial = computed(() => {
const profit = portfolioInsight.value?.unrealized_profit_rial 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 '—' if (profit == null) return '—'
const pct = formatPortfolioProfitPercent(portfolioInsight.value?.unrealized_profit_percent) const pct = formatPortfolioProfitPercent(safeUnrealizedProfitPercent.value)
return `${formatPortfolioProfitText(profit)}${pct ? ` (${pct})` : ''}` return `${formatPortfolioProfitText(profit)}${pct ? ` (${pct})` : ''}`
}) })
const profitClass = computed(() => { const profitClass = computed(() => {
const profit = portfolioInsight.value?.unrealized_profit_rial const profit = safeUnrealizedProfitRial.value
if (profit == null || profit === 0) return '' if (profit == null || profit === 0) return ''
return profit > 0 ? 'is-profit' : 'is-loss' return profit > 0 ? 'is-profit' : 'is-loss'
}) })
@@ -290,17 +330,19 @@ const summaryItems = computed(() => {
{ label: 'ارزش ریالی مس', value: usePrice(metalRialValue.value) }, { label: 'ارزش ریالی مس', value: usePrice(metalRialValue.value) },
) )
if (portfolioInsight.value?.average_buy_price != null) { 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({ items.push({
label: 'میانگین خرید', label: 'میانگین خرید',
value: usePrice(portfolioInsight.value.average_buy_price ?? 0), value: usePrice(avg),
}) })
} }
return items return items
}) })
const assetRows = computed(() => { const assetRows = computed(() => {
const pct = formatPortfolioProfitPercent(portfolioInsight.value?.unrealized_profit_percent) const pct = formatPortfolioProfitPercent(safeUnrealizedProfitPercent.value)
const profit = portfolioInsight.value?.unrealized_profit_rial const profit = safeUnrealizedProfitRial.value
const changeClass = profit == null || profit === 0 ? '' : profit > 0 ? 'is-profit' : 'is-loss' const changeClass = profit == null || profit === 0 ? '' : profit > 0 ? 'is-profit' : 'is-loss'
const rows = metalTotals.value.flatMap((metal) => { const rows = metalTotals.value.flatMap((metal) => {
const unit = metal.metal_id === 3 ? resolveBoursecuCopperUnit(metal.unit) : (metal.unit || 'کیلوگرم') const unit = metal.metal_id === 3 ? resolveBoursecuCopperUnit(metal.unit) : (metal.unit || 'کیلوگرم')
@@ -66,8 +66,21 @@ const { assetsSummary } = useWalletAssets()
const rootRef = ref<HTMLElement | null>(null) const rootRef = ref<HTMLElement | null>(null)
const openRowId = ref<BoursecuHomeAssetRowId | null>(null) const openRowId = ref<BoursecuHomeAssetRowId | null>(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(() => const rows = computed(() =>
buildBoursecuHomeAssetRows(assetsSummary.value, getTradeBuyPrice(lastPrice.value)) buildBoursecuHomeAssetRows(assetsSummary.value, effectiveClosingPrice.value)
) )
function toggleRow(rowId: BoursecuHomeAssetRowId) { function toggleRow(rowId: BoursecuHomeAssetRowId) {