fixed amount 2
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
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
|
||||
}
|
||||
|
||||
/** 5 => 5% ، 0.05 => 5% (اعشاری) */
|
||||
export function resolveFeePercentRatio(percentValue?: number | null) {
|
||||
const offset = Number(percentValue ?? 0)
|
||||
if (!offset) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if (Math.abs(offset) > 0 && Math.abs(offset) < 1) {
|
||||
return offset
|
||||
}
|
||||
|
||||
return offset / 100
|
||||
}
|
||||
|
||||
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 * resolveFeePercentRatio(raw)))
|
||||
}
|
||||
|
||||
export function resolveProductCashFeeBuyRial(product: ProductCashFeeSource, basePriceRial: number) {
|
||||
return resolveProductCashFeeRial(
|
||||
product.cash_fee_buy,
|
||||
product.cash_fee_buy_is_percent,
|
||||
basePriceRial,
|
||||
)
|
||||
}
|
||||
|
||||
export function resolveProductCashFeeSellRial(product: ProductCashFeeSource, basePriceRial: number) {
|
||||
return resolveProductCashFeeRial(
|
||||
product.cash_fee_sell,
|
||||
product.cash_fee_sell_is_percent,
|
||||
basePriceRial,
|
||||
)
|
||||
}
|
||||
|
||||
export function formatProductCashFeeLabel(
|
||||
feeValue: number | null | undefined,
|
||||
isPercent?: boolean | null,
|
||||
) {
|
||||
const raw = Number(feeValue ?? 0)
|
||||
if (!raw) {
|
||||
return '۰'
|
||||
}
|
||||
|
||||
if (isPercent) {
|
||||
return `${raw}%`
|
||||
}
|
||||
|
||||
return `${raw} ریال`
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
usesQuantityTrade,
|
||||
} from '@/utils/productCategories'
|
||||
import { formatInventoryCardValue } from '@/utils/productAssets'
|
||||
import { resolveProductCashFeeBuyRial, resolveProductCashFeeSellRial } from '@/utils/productCashFee'
|
||||
import { TRADE_GRAM_DISPLAY_DECIMALS } from '@/utils/serviceFee'
|
||||
import { formatGoldAmount } from '@/utils/usePrice'
|
||||
|
||||
@@ -371,7 +372,7 @@ export function resolveProductTradeBuyUnitPrice(product?: Product | null) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const basePrice = (product.price ?? 0) + (product.cash_fee_buy ?? 0)
|
||||
const basePrice = (product.price ?? 0) + resolveProductCashFeeBuyRial(product, product.price ?? 0)
|
||||
return basePrice * productPriceAmountFactor(product)
|
||||
}
|
||||
|
||||
@@ -380,7 +381,7 @@ export function resolveProductTradeSellUnitPrice(product?: Product | null) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const basePrice = (product.price ?? 0) - (product.cash_fee_sell ?? 0)
|
||||
const basePrice = (product.price ?? 0) - resolveProductCashFeeSellRial(product, product.price ?? 0)
|
||||
return basePrice * productPriceAmountFactor(product)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Product } from '@/models/products'
|
||||
import { getTradeBuyPrice, getTradeSellPrice, lastPrice } from '@/utils/price'
|
||||
import { resolveProductCashFeeBuyRial, resolveProductCashFeeSellRial } from '@/utils/productCashFee'
|
||||
import { isMetalCommodityGroup, isMeltedProductUsingAppPrice, isMeltedTradeGroup, usesQuantityTrade } from '@/utils/productCategories'
|
||||
import { productPriceAmountFactor } from '@/utils/productDealMethod'
|
||||
|
||||
@@ -104,7 +105,7 @@ export function getProductBuyPrice(product: Product): number {
|
||||
return 0
|
||||
}
|
||||
|
||||
const fee = Number(product.cash_fee_buy ?? 0)
|
||||
const fee = resolveProductCashFeeBuyRial(product, base)
|
||||
|
||||
if (
|
||||
isMeltedProductUsingAppPrice(product)
|
||||
@@ -159,7 +160,7 @@ export function getProductSellPrice(product: Product): number {
|
||||
}
|
||||
|
||||
if (isMeltedProductUsingAppPrice(product) || isMeltedTradeGroup(product.group_type) || usesQuantityTrade(product)) {
|
||||
return base - Number(product.cash_fee_sell ?? 0)
|
||||
return base - resolveProductCashFeeSellRial(product, base)
|
||||
}
|
||||
|
||||
return base
|
||||
|
||||
Reference in New Issue
Block a user