fix withdraw and desposit ui
This commit is contained in:
@@ -5,11 +5,104 @@ export function getWallets(wallet?: Wallet | Wallet[]): Wallet[] {
|
||||
return Array.isArray(wallet) ? wallet : [wallet]
|
||||
}
|
||||
|
||||
export function getCashBalance(wallet?: Wallet | Wallet[]): number {
|
||||
return getWallets(wallet)[0]?.cash ?? 0
|
||||
/** کیف اصلی مشتری — بدون fund و بدون product */
|
||||
export function getMainWallet(wallet?: Wallet | Wallet[]): Wallet | undefined {
|
||||
const wallets = getWallets(wallet)
|
||||
return wallets.find((w) => w.fund_id == null && w.product_id == null)
|
||||
}
|
||||
|
||||
export function getGoldBalance(wallet?: Wallet | Wallet[]): string {
|
||||
const sum = getWallets(wallet).reduce((total, item) => total + (item.gold ?? 0), 0)
|
||||
/** پارس setting `main_wallet_products` — id با کاما یا JSON */
|
||||
export function parseMainWalletProductIds(raw?: string | null): number[] {
|
||||
const value = (raw ?? '').trim()
|
||||
if (!value) return []
|
||||
|
||||
try {
|
||||
const decoded = JSON.parse(value)
|
||||
if (Array.isArray(decoded)) {
|
||||
return [...new Set(decoded.map((id) => Number(id)).filter((id) => id > 0))]
|
||||
}
|
||||
} catch {
|
||||
// کاماجدا
|
||||
}
|
||||
|
||||
return [
|
||||
...new Set(
|
||||
value
|
||||
.split(',')
|
||||
.map((part) => Number(part.trim()))
|
||||
.filter((id) => id > 0)
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
let cachedMainWalletProductIds: number[] | null = null
|
||||
|
||||
export function setCachedMainWalletProductIds(ids: number[]): void {
|
||||
cachedMainWalletProductIds = ids
|
||||
}
|
||||
|
||||
/** بارگذاری یکباره از API تنظیمات */
|
||||
export async function initMainWalletProductIds(): Promise<number[]> {
|
||||
if (cachedMainWalletProductIds !== null) {
|
||||
return cachedMainWalletProductIds
|
||||
}
|
||||
|
||||
const { initWalletSettingsFromApi } = await import('@/utils/config')
|
||||
await initWalletSettingsFromApi()
|
||||
|
||||
return cachedMainWalletProductIds ?? []
|
||||
}
|
||||
|
||||
export function getMainWalletProductIds(): number[] {
|
||||
return cachedMainWalletProductIds ?? []
|
||||
}
|
||||
|
||||
/**
|
||||
* موجودی ریالی کیف اصلی.
|
||||
* با main_wallet_products ممکن است کیف legacy محصول (مثلاً id=13) cash=0 داشته باشد — فقط کیف اصلی معتبر است.
|
||||
*/
|
||||
export function getCashBalance(
|
||||
wallet?: Wallet | Wallet[],
|
||||
options?: { walletCash?: number | null }
|
||||
): number {
|
||||
if (options?.walletCash != null) {
|
||||
return Number(options.walletCash)
|
||||
}
|
||||
return getMainWallet(wallet)?.cash ?? 0
|
||||
}
|
||||
|
||||
/**
|
||||
* جمع طلای قابلنمایش — همراستا با WalletBalanceService سرور.
|
||||
* کیف legacy محصولات main_wallet_products در جمع نمیآید.
|
||||
*/
|
||||
export function getGoldBalance(
|
||||
wallet?: Wallet | Wallet[],
|
||||
options?: { walletGold?: number | null; mainWalletProductIds?: number[] }
|
||||
): string {
|
||||
if (options?.walletGold != null) {
|
||||
return Number(options.walletGold).toFixed(3)
|
||||
}
|
||||
|
||||
const mainWalletProductIds = options?.mainWalletProductIds ?? getMainWalletProductIds()
|
||||
const sum = getWallets(wallet).reduce((total, line) => {
|
||||
const gold = Number(line.gold ?? 0)
|
||||
if (gold <= 0) return total
|
||||
|
||||
if (line.fund_id != null) {
|
||||
return total + gold
|
||||
}
|
||||
|
||||
if (line.product_id == null) {
|
||||
return total + gold
|
||||
}
|
||||
|
||||
const productId = Number(line.product_id)
|
||||
if (mainWalletProductIds.length > 0 && mainWalletProductIds.includes(productId)) {
|
||||
return total
|
||||
}
|
||||
|
||||
return total + gold
|
||||
}, 0)
|
||||
|
||||
return sum.toFixed(3)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user