diff --git a/src/utils/productAssets.ts b/src/utils/productAssets.ts index 6e66ec7..400a828 100644 --- a/src/utils/productAssets.ts +++ b/src/utils/productAssets.ts @@ -93,9 +93,47 @@ function formatRialNumber(value: number): string { return Math.round(value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') } -/** متن توضیح مودال دارایی ریالی — cash_wallet + assets_rial_value */ -export function formatRialAssetsInfoText(cashWallet: number, assetsRialValue: number): string { - return `مجموع کیف پول ریالی (${formatRialNumber(cashWallet)} ریال) + معادل ریالی محصولات (${formatRialNumber(assetsRialValue)} ریال)` +/** متن توضیح مودال دارایی ریالی */ +export function formatRialAssetsInfoText(cashWallet: number, metalRialValue: number): string { + const total = cashWallet + metalRialValue + return [ + `کیف پول ریالی: ${formatRialNumber(cashWallet)} ریال`, + `ارزش ریالی طلا (وزن × قیمت زنده): ${formatRialNumber(metalRialValue)} ریال`, + `مجموع: ${formatRialNumber(total)} ریال`, + ].join('\n') +} + +/** ارزش ریالی فلزات از وزن × قیمت زنده — fallback کلاینت قبل از deploy بک‌اند */ +export function computeMetalRialValue( + metalTotals: Array<{ metal_id?: number; total_weight?: number }>, + liveGoldPrice: number, + livePricesByMetal?: Partial> +): number { + return metalTotals.reduce((sum, metal) => { + const weight = Number(metal.total_weight ?? 0) + if (weight <= 0) { + return sum + } + + const metalId = Number(metal.metal_id ?? 0) + const unitPrice = + livePricesByMetal?.[metalId] ?? + (metalId === METAL_GOLD ? liveGoldPrice : 0) + + return sum + weight * unitPrice + }, 0) +} + +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) + if (fromServer > 0) { + return fromServer + } + + return computeMetalRialValue(summary?.metal_totals ?? [], liveGoldPrice) } export function formatInventoryCardValue(card: { diff --git a/src/views/base/fund/index.vue b/src/views/base/fund/index.vue index 5d3ce08..3056602 100644 --- a/src/views/base/fund/index.vue +++ b/src/views/base/fund/index.vue @@ -61,8 +61,8 @@
{{ usePriceWithoutType(cashWallet) }}ریال
- معادل ریالی مجموع دارایی ها: -
{{ usePriceWithoutType(totalRialValue) }}ریال
+ مجموع دارایی ریالی: +
{{ usePriceWithoutType(cashWallet + metalRialValue) }}ریال
@@ -108,8 +108,9 @@ import { usePriceWithoutType } from '@/utils/usePrice' import { useConfigStore } from '@/store/configStore' import { useWalletAssets } from '@/composables/useWalletAssets' import type { WalletMetalTotal } from '@/models/walletAssets' -import { resolveSummaryMetalWeight, fundPageInventoryCards } from '@/utils/productAssets' +import { resolveSummaryMetalWeight, fundPageInventoryCards, resolveAssetsRialValue } from '@/utils/productAssets' import { useWalletStore } from '@/store/walletStore' +import { getTradeBuyPrice, lastPrice } from '@/utils/price' import { getProjectFundEmptyImage, getProjectFundEmptyText, @@ -129,7 +130,9 @@ const inventoryCards = computed(() => fundPageInventoryCards(assetsSummary.value const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? []) const primaryMetalTotal = computed(() => metalTotals.value[0]) const cashWallet = computed(() => assetsSummary.value?.cash_wallet ?? 0) -const totalRialValue = computed(() => assetsSummary.value?.total_rial_value ?? 0) +const metalRialValue = computed(() => + resolveAssetsRialValue(assetsSummary.value, getTradeBuyPrice(lastPrice.value)) +) const countBasedInventory = computed(() => (assetsSummary.value?.count_products ?? []).reduce((sum, item) => sum + Number(item.quantity ?? 0), 0) ) @@ -137,7 +140,7 @@ const countBasedInventory = computed(() => onMounted(async () => { try { loading.value = true - const tasks = [loadWalletAssets(true)] + const tasks = [loadWalletAssets(true), getLastPrices()] if (isBazretalaProject()) { tasks.push(walletStore.getMyWallet()) } diff --git a/src/views/base/home/components/AssetsSection.vue b/src/views/base/home/components/AssetsSection.vue index d6ccc99..0517c48 100644 --- a/src/views/base/home/components/AssetsSection.vue +++ b/src/views/base/home/components/AssetsSection.vue @@ -35,7 +35,7 @@
دارایی ریالی: -
{{ usePriceWithoutType(totalRialValue) }} ریال
+
{{ usePriceWithoutType(cashWallet) }} ریال
@@ -59,7 +59,7 @@ defineEmits<{ const meta = computed(() => assetsSummary.value?.meta) const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? []) const primaryMetalTotal = computed(() => metalTotals.value[0]) -const totalRialValue = computed(() => assetsSummary.value?.total_rial_value ?? 0) +const cashWallet = computed(() => assetsSummary.value?.cash_wallet ?? 0) const inventoryCards = computed(() => assetsSummary.value?.inventory_cards ?? []) const countBasedInventory = computed(() => (assetsSummary.value?.count_products ?? []).reduce((sum, item) => sum + Number(item.quantity ?? 0), 0) diff --git a/src/views/base/home/components/BalanceSheet.vue b/src/views/base/home/components/BalanceSheet.vue index 5d2d179..704f476 100644 --- a/src/views/base/home/components/BalanceSheet.vue +++ b/src/views/base/home/components/BalanceSheet.vue @@ -31,9 +31,9 @@ assetsSummary.value?.inventory_cards ?? []) const metalTotals = computed(() => assetsSummary.value?.metal_totals ?? []) const cashWallet = computed(() => assetsSummary.value?.cash_wallet ?? 0) -const assetsRialValue = computed(() => assetsSummary.value?.assets_rial_value ?? 0) -const totalRialValue = computed(() => assetsSummary.value?.total_rial_value ?? 0) +const metalRialValue = computed(() => + resolveAssetsRialValue(assetsSummary.value, getTradeBuyPrice(lastPrice.value)) +) +const totalRialValue = computed(() => cashWallet.value + metalRialValue.value) onMounted(() => { void loadWalletAssets() diff --git a/src/views/base/home/index.vue b/src/views/base/home/index.vue index 8963b25..0d90b6d 100644 --- a/src/views/base/home/index.vue +++ b/src/views/base/home/index.vue @@ -84,11 +84,11 @@ import {useUserStore} from '@/store/userStore' import {useWalletStore} from '@/store/walletStore' import { useWalletAssets } from '@/composables/useWalletAssets' import type { WalletMetalTotal } from '@/models/walletAssets' -import { formatMetalAssetsInfoText, formatRialAssetsInfoText } from '@/utils/productAssets' +import { formatMetalAssetsInfoText, formatRialAssetsInfoText, resolveAssetsRialValue } from '@/utils/productAssets' +import {getLastPrices, getTradeBuyPrice, lastPrice, price, startLastPricePolling, stopLastPricePolling} from '@/utils/price' import {useConfigStore} from '@/store/configStore' import {useFundUserStore} from '@/store/fundStore' import {usePriceWithoutType} from '../../../utils/usePrice' -import {getLastPrices, lastPrice, price, startLastPricePolling, stopLastPricePolling} from '@/utils/price' import { getStoreClosedMessage, isStoreClosed } from '@/utils/storeHours' import {getConstByKey} from '@/api/const' import {message, Modal} from 'ant-design-vue' @@ -228,11 +228,11 @@ function openGoldModal(metal: WalletMetalTotal) { function openRialModal() { const summary = assetsSummary.value + const cash = summary?.cash_wallet ?? 0 + const metalRial = resolveAssetsRialValue(summary, getTradeBuyPrice(lastPrice.value)) Modal.confirm({ - title: formatRialAssetsInfoText( - summary?.cash_wallet ?? 0, - summary?.assets_rial_value ?? 0 - ), + title: 'دارایی ریالی', + content: formatRialAssetsInfoText(cash, metalRial), okText: 'متوجه شدم', centered: true, cancelButtonProps: {style: {display: 'none'}}