2026-07-16 15:21:53 +03:30
|
|
|
|
export type ProductCashFeeSource = {
|
|
|
|
|
|
cash_fee_buy?: number | null
|
|
|
|
|
|
cash_fee_sell?: number | null
|
|
|
|
|
|
cash_fee_buy_is_percent?: boolean | null
|
|
|
|
|
|
cash_fee_sell_is_percent?: boolean | null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-18 17:57:22 +03:30
|
|
|
|
/** 0.5 => 0.5% ، 5 => 5% — همیشه عدد درصد */
|
2026-07-16 15:21:53 +03:30
|
|
|
|
export function resolveCashFeePercentRatio(percentValue?: number | null) {
|
|
|
|
|
|
const offset = Number(percentValue ?? 0)
|
|
|
|
|
|
if (!offset || !Number.isFinite(offset)) {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return offset / 100
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-18 17:57:22 +03:30
|
|
|
|
/** @deprecated از resolveCashFeePercentRatio استفاده کنید */
|
|
|
|
|
|
export function resolveFeePercentRatio(percentValue?: number | null) {
|
|
|
|
|
|
return resolveCashFeePercentRatio(percentValue)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-16 15:21:53 +03:30
|
|
|
|
export function resolveProductCashFeeRial(
|
|
|
|
|
|
feeValue: number | null | undefined,
|
|
|
|
|
|
isPercent: boolean | null | undefined,
|
|
|
|
|
|
basePriceRial: number,
|
|
|
|
|
|
) {
|
|
|
|
|
|
const raw = Number(feeValue ?? 0)
|
|
|
|
|
|
if (!raw || !Number.isFinite(raw)) {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!isPercent) {
|
|
|
|
|
|
return Math.round(raw)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const base = Number(basePriceRial)
|
|
|
|
|
|
if (!base || base <= 0) {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Math.round(Math.abs(base * resolveCashFeePercentRatio(raw)))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function resolveProductCashFeeBuyRial(product: ProductCashFeeSource, basePriceRial: number) {
|
2026-08-18 17:57:22 +03:30
|
|
|
|
const baseFee = resolveProductCashFeeRial(
|
2026-07-16 15:21:53 +03:30
|
|
|
|
product.cash_fee_buy,
|
|
|
|
|
|
product.cash_fee_buy_is_percent,
|
|
|
|
|
|
basePriceRial,
|
|
|
|
|
|
)
|
2026-08-18 17:57:22 +03:30
|
|
|
|
const special = Number((product as any).special_profit_buy ?? 0)
|
|
|
|
|
|
return baseFee + (Number.isFinite(special) ? Math.round(special) : 0)
|
2026-07-16 15:21:53 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function resolveProductCashFeeSellRial(product: ProductCashFeeSource, basePriceRial: number) {
|
2026-08-18 17:57:22 +03:30
|
|
|
|
const baseFee = resolveProductCashFeeRial(
|
2026-07-16 15:21:53 +03:30
|
|
|
|
product.cash_fee_sell,
|
|
|
|
|
|
product.cash_fee_sell_is_percent,
|
|
|
|
|
|
basePriceRial,
|
|
|
|
|
|
)
|
2026-08-18 17:57:22 +03:30
|
|
|
|
const special = Number((product as any).special_profit_sell ?? 0)
|
|
|
|
|
|
return baseFee + (Number.isFinite(special) ? Math.round(special) : 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatProductCashFeeLabel(
|
|
|
|
|
|
feeValue: number | null | undefined,
|
|
|
|
|
|
isPercent?: boolean | null,
|
|
|
|
|
|
) {
|
|
|
|
|
|
const raw = Number(feeValue ?? 0)
|
|
|
|
|
|
if (!raw) {
|
|
|
|
|
|
return '۰'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isPercent) {
|
|
|
|
|
|
const text = Number.isInteger(raw) ? String(raw) : String(parseFloat(raw.toFixed(4)))
|
|
|
|
|
|
return `${text}%`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return `${raw} ریال`
|
2026-07-16 15:21:53 +03:30
|
|
|
|
}
|