Files
boursecu_admin/src/components/mobile/utils/wallet.ts
T

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-05-31 11:51:52 +03:30
import type { Wallet } from '@/model/wallet'
export function getWallets(wallet?: Wallet | Wallet[]): Wallet[] {
if (!wallet) return []
return Array.isArray(wallet) ? wallet : [wallet]
}
2026-06-15 15:48:10 +03:30
/** کیف اصلی مشتری — بدون fund و بدون product */
2026-07-14 13:53:20 +03:30
export function getMainWallet(
wallet?: Wallet | Wallet[],
ownerType?: number | null
): Wallet | undefined {
2026-06-15 15:48:10 +03:30
const wallets = getWallets(wallet)
2026-07-14 13:53:20 +03:30
return wallets.find((w) => {
if (w.fund_id != null || w.product_id != null) return false
if (ownerType != null && w.owner_type != null && Number(w.owner_type) !== Number(ownerType)) {
return false
}
return true
})
2026-05-31 11:51:52 +03:30
}
2026-06-15 15:48:10 +03:30
/** پارس 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[],
2026-07-14 13:53:20 +03:30
options?: { walletCash?: number | null; ownerType?: number | null }
2026-06-15 15:48:10 +03:30
): number {
if (options?.walletCash != null) {
return Number(options.walletCash)
}
2026-07-14 13:53:20 +03:30
return getMainWallet(wallet, options?.ownerType)?.cash ?? 0
2026-06-15 15:48:10 +03:30
}
/**
* جمع طلای قابل‌نمایش — هم‌راستا با WalletBalanceService سرور.
* کیف legacy محصولات main_wallet_products در جمع نمی‌آید.
*/
export function getGoldBalance(
wallet?: Wallet | Wallet[],
2026-07-14 13:53:20 +03:30
options?: { walletGold?: number | null; mainWalletProductIds?: number[]; ownerType?: number | null }
2026-06-15 15:48:10 +03:30
): string {
if (options?.walletGold != null) {
return Number(options.walletGold).toFixed(3)
}
const mainWalletProductIds = options?.mainWalletProductIds ?? getMainWalletProductIds()
2026-07-14 13:53:20 +03:30
const ownerType = options?.ownerType ?? null
2026-06-15 15:48:10 +03:30
const sum = getWallets(wallet).reduce((total, line) => {
2026-07-14 13:53:20 +03:30
if (ownerType != null && line.owner_type != null && Number(line.owner_type) !== Number(ownerType)) {
return total
}
2026-06-15 15:48:10 +03:30
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)
2026-05-31 11:51:52 +03:30
return sum.toFixed(3)
}