2026-08-03 11:52:44 +03:30
|
|
|
|
import type { Fund } from '@/models/fund'
|
|
|
|
|
|
|
|
|
|
|
|
export const PIGGY_PRESETS = [
|
|
|
|
|
|
'پس انداز',
|
|
|
|
|
|
'هدیه',
|
|
|
|
|
|
'ازدواج',
|
|
|
|
|
|
'تعمیرات',
|
|
|
|
|
|
'گردش',
|
|
|
|
|
|
'خرید لوازم منزل',
|
|
|
|
|
|
] as const
|
|
|
|
|
|
|
2026-08-03 22:36:23 +03:30
|
|
|
|
/** آیکون vuesax برای عنوان قلک — بدون تصویر سهبعدی گیجکننده */
|
|
|
|
|
|
export function piggyIconName(title?: string | null): string {
|
2026-08-03 11:52:44 +03:30
|
|
|
|
switch ((title ?? '').trim()) {
|
|
|
|
|
|
case 'خرید لوازم منزل':
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:house-2'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
case 'هدیه':
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:gift'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
case 'گردش':
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:airplane'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
case 'ازدواج':
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:heart'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
case 'تعمیرات':
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:setting-2'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
case 'پس انداز':
|
|
|
|
|
|
default:
|
2026-08-03 22:36:23 +03:30
|
|
|
|
return 'vuesax-linear:safe-home'
|
2026-08-03 11:52:44 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function fundWalletGold(item?: Fund | null): number {
|
|
|
|
|
|
if (!item) return 0
|
|
|
|
|
|
const wallets = Array.isArray(item.wallet) ? item.wallet : item.wallet ? [item.wallet] : []
|
|
|
|
|
|
return wallets.reduce((acc, w) => acc + Number((w as { gold?: number })?.gold ?? 0), 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function fundProductLabel(item?: Fund | null): string {
|
|
|
|
|
|
if (!item) return '—'
|
|
|
|
|
|
return item.product?.title || (item as { product_title?: string }).product_title || '—'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-03 22:36:23 +03:30
|
|
|
|
export function fundProductId(item?: Fund | null): number {
|
|
|
|
|
|
return Number(item?.product_id ?? item?.product?.id ?? 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-03 11:52:44 +03:30
|
|
|
|
export function formatPiggyAmount(value: number): string {
|
|
|
|
|
|
const n = Number(value) || 0
|
|
|
|
|
|
if (Math.abs(n) < 0.0005) return '۰'
|
|
|
|
|
|
return n.toLocaleString('fa-IR', { maximumFractionDigits: 3 })
|
|
|
|
|
|
}
|
2026-08-03 22:36:23 +03:30
|
|
|
|
|
|
|
|
|
|
export type PiggyProductGroup = {
|
|
|
|
|
|
productId: number
|
|
|
|
|
|
productTitle: string
|
|
|
|
|
|
totalGold: number
|
|
|
|
|
|
funds: Fund[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function groupFundsByProduct(funds: Fund[]): PiggyProductGroup[] {
|
|
|
|
|
|
const map = new Map<number, PiggyProductGroup>()
|
|
|
|
|
|
for (const fund of funds) {
|
|
|
|
|
|
const productId = fundProductId(fund) || -1
|
|
|
|
|
|
const productTitle = fundProductLabel(fund)
|
|
|
|
|
|
let group = map.get(productId)
|
|
|
|
|
|
if (!group) {
|
|
|
|
|
|
group = { productId, productTitle, totalGold: 0, funds: [] }
|
|
|
|
|
|
map.set(productId, group)
|
|
|
|
|
|
}
|
|
|
|
|
|
group.funds.push(fund)
|
|
|
|
|
|
group.totalGold += fundWalletGold(fund)
|
|
|
|
|
|
}
|
|
|
|
|
|
return [...map.values()].sort((a, b) => a.productTitle.localeCompare(b.productTitle, 'fa'))
|
|
|
|
|
|
}
|