155 lines
4.4 KiB
TypeScript
155 lines
4.4 KiB
TypeScript
import type { Product } from '@/models/products'
|
||
import { getTradeBuyPrice, getTradeSellPrice, lastPrice } from '@/utils/price'
|
||
import { isMetalCommodityGroup, isMeltedProductUsingAppPrice, isMeltedTradeGroup, usesQuantityTrade } from '@/utils/productCategories'
|
||
import { productPriceAmountFactor } from '@/utils/productDealMethod'
|
||
|
||
function productUsesLastPriceIfZero(product: Product): boolean {
|
||
return Boolean(product.use_last_price_if_zero)
|
||
}
|
||
|
||
export function resolvePriceOffsetRatio(offsetPercent?: number | null) {
|
||
const offset = Number(offsetPercent ?? 0)
|
||
if (!offset) {
|
||
return 0
|
||
}
|
||
|
||
// 5 => 5% ، 0.05 => 5% (اعشاری)
|
||
if (Math.abs(offset) > 0 && Math.abs(offset) < 1) {
|
||
return offset
|
||
}
|
||
|
||
return offset / 100
|
||
}
|
||
|
||
export function applyPriceOffsetPercent(basePrice: number, offsetPercent?: number | null) {
|
||
const ratio = resolvePriceOffsetRatio(offsetPercent)
|
||
if (!ratio) {
|
||
return basePrice
|
||
}
|
||
|
||
return Math.round(basePrice * (1 + ratio))
|
||
}
|
||
|
||
/** boursecu chart: قیمت + (قیمت × offset) */
|
||
export function applyChartPriceOffset(basePrice: number, offsetPercent?: number | null) {
|
||
const ratio = resolvePriceOffsetRatio(offsetPercent)
|
||
if (!ratio) {
|
||
return basePrice
|
||
}
|
||
|
||
return Math.round(basePrice + basePrice * ratio)
|
||
}
|
||
|
||
/** قیمت پایه بدون کارمزد — اگر صفر باشد نباید فقط cash_fee بهعنوان قیمت نمایش داده شود */
|
||
export function getProductBaseBuyPrice(product: Product): number {
|
||
if (isMeltedProductUsingAppPrice(product)) {
|
||
const apiPrice = Number(product.price ?? 0)
|
||
const useLast = productUsesLastPriceIfZero(product)
|
||
|
||
if (!useLast) {
|
||
const liveBuy = getTradeBuyPrice(lastPrice.value)
|
||
if (liveBuy > 0) {
|
||
return applyPriceOffsetPercent(liveBuy, product.price_offset_percent)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
if (apiPrice > 0) {
|
||
return apiPrice
|
||
}
|
||
|
||
const liveBuy = getTradeBuyPrice(lastPrice.value)
|
||
if (liveBuy > 0) {
|
||
return applyPriceOffsetPercent(liveBuy, product.price_offset_percent)
|
||
}
|
||
|
||
return 0
|
||
}
|
||
|
||
if (isMeltedTradeGroup(product.group_type)) {
|
||
return Number(product.price ?? 0)
|
||
}
|
||
|
||
if (isMetalCommodityGroup(product.group_type)) {
|
||
return Number(product.price_profit ?? 0) + Number(product.price ?? 0)
|
||
}
|
||
|
||
return Number(product.price ?? 0)
|
||
}
|
||
|
||
/** قیمت خرید نمایشی محصول (همان مقداری که در لیست و اکشنشیت نشان داده میشود) */
|
||
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)
|
||
|| usesQuantityTrade(product)
|
||
) {
|
||
return base + fee
|
||
}
|
||
|
||
const withFee = base + fee
|
||
return product.direct_price === 0
|
||
? withFee * productPriceAmountFactor(product)
|
||
: withFee
|
||
}
|
||
|
||
function getProductBaseSellPrice(product: Product): number {
|
||
if (isMeltedProductUsingAppPrice(product)) {
|
||
const apiPrice = Number(product.price ?? 0)
|
||
const useLast = productUsesLastPriceIfZero(product)
|
||
|
||
if (!useLast) {
|
||
const liveSell = getTradeSellPrice(lastPrice.value)
|
||
if (liveSell > 0) {
|
||
return applyPriceOffsetPercent(liveSell, product.price_offset_percent)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
if (apiPrice > 0) {
|
||
return apiPrice
|
||
}
|
||
|
||
const liveSell = getTradeSellPrice(lastPrice.value)
|
||
if (liveSell > 0) {
|
||
return applyPriceOffsetPercent(liveSell, product.price_offset_percent)
|
||
}
|
||
|
||
return 0
|
||
}
|
||
|
||
if (isMeltedTradeGroup(product.group_type)) {
|
||
return Number(product.price ?? 0)
|
||
}
|
||
|
||
return getTradeSellPrice(lastPrice.value)
|
||
}
|
||
|
||
/** قیمت فروش نمایشی محصول آبشدهای */
|
||
export function getProductSellPrice(product: Product): number {
|
||
const base = getProductBaseSellPrice(product)
|
||
if (base <= 0) {
|
||
return 0
|
||
}
|
||
|
||
if (isMeltedProductUsingAppPrice(product) || isMeltedTradeGroup(product.group_type) || usesQuantityTrade(product)) {
|
||
return base - Number(product.cash_fee_sell ?? 0)
|
||
}
|
||
|
||
return base
|
||
}
|
||
|
||
export function isProductPriceInactive(product: Product): boolean {
|
||
return getProductBaseBuyPrice(product) <= 0
|
||
}
|
||
|
||
export { isMeltedTradeGroup, isMeltedProductUsingAppPrice } from '@/utils/productCategories'
|