Files
boursecu_user/src/utils/productPrice.ts
T

61 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-05-23 13:27:30 +03:30
import type { Product } from '@/models/products'
2026-06-06 15:48:27 +03:30
import { getTradeBuyPrice, getTradeSellPrice, lastPrice } from '@/utils/price'
2026-06-09 21:04:22 +03:30
import { isMetalCommodityGroup, isMeltedProductUsingAppPrice, isMeltedTradeGroup } from '@/utils/productCategories'
function applyPriceOffsetPercent(basePrice: number, offsetPercent?: number | null) {
const offset = Number(offsetPercent ?? 0)
if (!offset) {
return basePrice
}
return Math.round(basePrice * (1 + offset / 100))
}
2026-05-23 13:27:30 +03:30
/** قیمت خرید نمایشی محصول (همان مقداری که در لیست و اکشن‌شیت نشان داده می‌شود) */
export function getProductBuyPrice(product: Product): number {
2026-06-09 21:04:22 +03:30
if (isMeltedProductUsingAppPrice(product)) {
2026-06-11 16:30:08 +03:30
const tradeBuy = getTradeBuyPrice(lastPrice.value)
const base = tradeBuy > 0
? applyPriceOffsetPercent(tradeBuy, product.price_offset_percent)
: (product.price ?? 0)
2026-06-09 21:04:22 +03:30
return base + (product.cash_fee_buy ?? 0)
2026-05-23 13:27:30 +03:30
}
2026-06-09 21:04:22 +03:30
if (isMeltedTradeGroup(product.group_type)) {
return (product.price ?? 0) + (product.cash_fee_buy ?? 0)
}
if (isMetalCommodityGroup(product.group_type)) {
2026-05-23 13:27:30 +03:30
const base = (product.price_profit ?? 0) + (product.price ?? 0)
return base + (product.cash_fee_buy ?? 0)
}
const basePrice = (product.price ?? 0) + (product.cash_fee_buy ?? 0)
return product.direct_price === 0
? basePrice * (product.amount ?? 0)
: basePrice
}
2026-06-09 21:04:22 +03:30
/** قیمت فروش نمایشی محصول آبشده‌ای */
2026-06-06 15:48:27 +03:30
export function getProductSellPrice(product: Product): number {
2026-06-09 21:04:22 +03:30
if (isMeltedProductUsingAppPrice(product)) {
2026-06-11 16:30:08 +03:30
const tradeSell = getTradeSellPrice(lastPrice.value)
const base = tradeSell > 0
? applyPriceOffsetPercent(tradeSell, product.price_offset_percent)
: (product.price ?? 0)
2026-06-09 21:04:22 +03:30
return base - (product.cash_fee_sell ?? 0)
2026-06-06 15:48:27 +03:30
}
2026-06-09 21:04:22 +03:30
if (isMeltedTradeGroup(product.group_type)) {
2026-06-11 15:17:14 +03:30
return (product.price ?? 0) - (product.cash_fee_sell ?? 0)
2026-06-09 21:04:22 +03:30
}
2026-06-06 15:48:27 +03:30
return getTradeSellPrice(lastPrice.value)
}
2026-05-23 13:27:30 +03:30
export function isProductPriceInactive(product: Product): boolean {
return getProductBuyPrice(product) <= 0
}
2026-06-09 21:04:22 +03:30
export { isMeltedTradeGroup, isMeltedProductUsingAppPrice } from '@/utils/productCategories'