fix some bugs

This commit is contained in:
2026-07-14 13:53:20 +03:30
parent 76813df062
commit e83daaf28b
9 changed files with 119 additions and 28 deletions
+19 -5
View File
@@ -6,9 +6,18 @@ export function getWallets(wallet?: Wallet | Wallet[]): Wallet[] {
}
/** کیف اصلی مشتری — بدون fund و بدون product */
export function getMainWallet(wallet?: Wallet | Wallet[]): Wallet | undefined {
export function getMainWallet(
wallet?: Wallet | Wallet[],
ownerType?: number | null
): Wallet | undefined {
const wallets = getWallets(wallet)
return wallets.find((w) => w.fund_id == null && w.product_id == null)
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
})
}
/** پارس setting `main_wallet_products` — id با کاما یا JSON */
@@ -63,12 +72,12 @@ export function getMainWalletProductIds(): number[] {
*/
export function getCashBalance(
wallet?: Wallet | Wallet[],
options?: { walletCash?: number | null }
options?: { walletCash?: number | null; ownerType?: number | null }
): number {
if (options?.walletCash != null) {
return Number(options.walletCash)
}
return getMainWallet(wallet)?.cash ?? 0
return getMainWallet(wallet, options?.ownerType)?.cash ?? 0
}
/**
@@ -77,14 +86,19 @@ export function getCashBalance(
*/
export function getGoldBalance(
wallet?: Wallet | Wallet[],
options?: { walletGold?: number | null; mainWalletProductIds?: number[] }
options?: { walletGold?: number | null; mainWalletProductIds?: number[]; ownerType?: number | null }
): string {
if (options?.walletGold != null) {
return Number(options.walletGold).toFixed(3)
}
const mainWalletProductIds = options?.mainWalletProductIds ?? getMainWalletProductIds()
const ownerType = options?.ownerType ?? null
const sum = getWallets(wallet).reduce((total, line) => {
if (ownerType != null && line.owner_type != null && Number(line.owner_type) !== Number(ownerType)) {
return total
}
const gold = Number(line.gold ?? 0)
if (gold <= 0) return total