Files
boursecu_user/src/utils/productPrice.ts
T

95 lines
3.1 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
2026-06-11 21:21:15 +03:30
/** قیمت پایه بدون کارمزد — اگر صفر باشد نباید فقط cash_fee به‌عنوان قیمت نمایش داده شود */
export function getProductBaseBuyPrice(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)
2026-06-11 21:21:15 +03:30
if (tradeBuy > 0) {
return applyPriceOffsetPercent(tradeBuy, product.price_offset_percent)
}
// provider صفر — آخرین قیمت ذخیره‌شده از API (وقتی use_last_price_if_zero فعال است)
return Number(product.price ?? 0)
2026-05-23 13:27:30 +03:30
}
2026-06-09 21:04:22 +03:30
if (isMeltedTradeGroup(product.group_type)) {
2026-06-11 21:21:15 +03:30
return Number(product.price ?? 0)
2026-06-09 21:04:22 +03:30
}
if (isMetalCommodityGroup(product.group_type)) {
2026-06-11 21:21:15 +03:30
return Number(product.price_profit ?? 0) + Number(product.price ?? 0)
2026-05-23 13:27:30 +03:30
}
2026-06-11 21:21:15 +03:30
return Number(product.price ?? 0)
2026-05-23 13:27:30 +03:30
}
2026-06-11 21:21:15 +03:30
/** قیمت خرید نمایشی محصول (همان مقداری که در لیست و اکشن‌شیت نشان داده می‌شود) */
export function getProductBuyPrice(product: Product): number {
const base = getProductBaseBuyPrice(product)
if (base <= 0) {
return 0
}
const fee = Number(product.cash_fee_buy ?? 0)
if (
isMeltedProductUsingAppPrice(product)
|| isMeltedTradeGroup(product.group_type)
|| isMetalCommodityGroup(product.group_type)
) {
return base + fee
}
const withFee = base + fee
return product.direct_price === 0
? withFee * Number(product.amount ?? 0)
: withFee
}
function getProductBaseSellPrice(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)
2026-06-11 21:21:15 +03:30
if (tradeSell > 0) {
return applyPriceOffsetPercent(tradeSell, product.price_offset_percent)
}
// provider صفر — همان آخرین قیمت ذخیره‌شده (فروش از همان مبنا)
return Number(product.price ?? 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 21:21:15 +03:30
return Number(product.price ?? 0)
2026-06-09 21:04:22 +03:30
}
2026-06-06 15:48:27 +03:30
return getTradeSellPrice(lastPrice.value)
}
2026-06-11 21:21:15 +03:30
/** قیمت فروش نمایشی محصول آبشده‌ای */
export function getProductSellPrice(product: Product): number {
const base = getProductBaseSellPrice(product)
if (base <= 0) {
return 0
}
if (isMeltedProductUsingAppPrice(product) || isMeltedTradeGroup(product.group_type)) {
return base - Number(product.cash_fee_sell ?? 0)
}
return base
}
2026-05-23 13:27:30 +03:30
export function isProductPriceInactive(product: Product): boolean {
2026-06-11 21:21:15 +03:30
return getProductBaseBuyPrice(product) <= 0
2026-05-23 13:27:30 +03:30
}
2026-06-09 21:04:22 +03:30
export { isMeltedTradeGroup, isMeltedProductUsingAppPrice } from '@/utils/productCategories'