Files
ghatreh_user/src/utils/productAssets.ts
T

115 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-06-29 22:43:28 +03:30
import type { Product } from '@/models/products'
import type { WalletInventoryCard, WalletMetalTotal } from '@/models/walletAssets'
import { resolveProductImageSrc } from '@/utils/productImage'
export function fundPageInventoryCards<T extends { quantity?: number }>(cards: T[]): T[] {
return cards.filter((card) => Number(card.quantity ?? 0) > 0)
}
export function resolveSummaryMetalWeight(
metal: { total_weight: number },
_cards?: Array<{ metal_id?: number; quantity?: number }>
): number {
return Number(metal.total_weight ?? 0)
}
export function enrichInventoryCardsWithCatalog<T extends WalletInventoryCard>(
cards: T[],
catalog: Product[]
): T[] {
return cards.map((card) => {
const fromCatalog = catalog.find((product) => product.id === card.product_id)
if (!fromCatalog) {
return card
}
return {
...card,
file: card.file ?? fromCatalog.file ?? null,
}
})
}
export function getProductDisplayImage(product?: {
group_type?: string | null
title?: string | null
file?: { src?: string | null } | null
img?: string | null
} | null): string {
return resolveProductImageSrc(product)
}
2026-07-18 15:46:38 +03:30
import { formatGoldAmount, usePriceWithoutType } from '@/utils/usePrice'
2026-07-05 20:49:01 +03:30
2026-06-29 22:43:28 +03:30
export function formatMetalTotal(
metal: WalletMetalTotal,
inventoryCards: WalletInventoryCard[]
): string {
2026-07-05 20:49:01 +03:30
return `${formatGoldAmount(resolveSummaryMetalWeight(metal, inventoryCards))}${metal.unit}`
2026-06-29 22:43:28 +03:30
}
export function resolveAssetsRialValue(
summary: {
total_rial_value?: number
assets_rial_value?: number
cash_wallet?: number
} | null | undefined,
liveGoldPrice = 0
): number {
const total = Number(summary?.total_rial_value ?? 0)
if (total > 0) {
return total
}
const cash = Number(summary?.cash_wallet ?? 0)
const assetsRial = Number(summary?.assets_rial_value ?? 0)
if (assetsRial > 0) {
return cash + assetsRial
}
const metalWeight = (summary as { metal_totals?: Array<{ total_weight?: number }> } | null)
?.metal_totals?.[0]?.total_weight
return cash + Number(metalWeight ?? 0) * liveGoldPrice
}
2026-07-18 15:46:38 +03:30
export function getProductInventoryQuantity(
cards: WalletInventoryCard[],
productId?: number | null,
mainWalletGold?: number | null,
): number {
const card = cards.find((item) => Number(item.product_id) === Number(productId))
return card ? Number(card.quantity ?? 0) : Number(mainWalletGold ?? 0)
}
export function formatProductWalletBalanceText(
product:
| { id?: number; title?: string | null; unit?: string | null; group_type?: string | null }
| null,
cards: WalletInventoryCard[] = [],
mainWalletGold?: number | null,
): string {
const card = cards.find((item) => Number(item.product_id) === Number(product?.id))
const quantity = card ? Number(card.quantity ?? 0) : Number(mainWalletGold ?? 0)
const unit = card?.unit ?? product?.unit?.trim() ?? 'گرم'
const title = product?.title?.trim() || 'محصول'
const formatted = unit === 'عدد'
? usePriceWithoutType(quantity, false)
: formatGoldAmount(quantity)
return `موجودی کیف ${title}: ${formatted} ${unit}`
}
export function getCashWalletBalance(
walletInfo?: Record<string, unknown> | null,
wallets?: unknown,
): number {
const direct = Number(walletInfo?.cash ?? walletInfo?.count_cash_wallets ?? 0)
if (direct) return direct
if (Array.isArray(wallets)) {
return wallets.reduce((sum, wallet) => sum + Number(wallet?.cash ?? 0), 0)
}
return Number((wallets as { cash?: number } | null)?.cash ?? 0)
}