From 13d2ce88052bb6eedf6770296bda21e68240f9bd Mon Sep 17 00:00:00 2001 From: adonis Date: Thu, 11 Jun 2026 21:49:46 +0330 Subject: [PATCH] fix bug 3 --- src/models/products.ts | 1 + src/utils/productPrice.ts | 54 +++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/models/products.ts b/src/models/products.ts index 22cc688..c743fe0 100644 --- a/src/models/products.ts +++ b/src/models/products.ts @@ -25,6 +25,7 @@ export interface Product { max_order_cash?: number | null direct_price: number | undefined price_offset_percent?: number | null + use_last_price_if_zero?: boolean reading_method: any created_at: any updated_at: any diff --git a/src/utils/productPrice.ts b/src/utils/productPrice.ts index 11d2112..d8d2669 100644 --- a/src/utils/productPrice.ts +++ b/src/utils/productPrice.ts @@ -2,6 +2,10 @@ import type { Product } from '@/models/products' import { getTradeBuyPrice, getTradeSellPrice, lastPrice } from '@/utils/price' import { isMetalCommodityGroup, isMeltedProductUsingAppPrice, isMeltedTradeGroup } from '@/utils/productCategories' +function productUsesLastPriceIfZero(product: Product): boolean { + return Boolean(product.use_last_price_if_zero) +} + function applyPriceOffsetPercent(basePrice: number, offsetPercent?: number | null) { const offset = Number(offsetPercent ?? 0) if (!offset) { @@ -14,12 +18,27 @@ function applyPriceOffsetPercent(basePrice: number, offsetPercent?: number | nul /** قیمت پایه بدون کارمزد — اگر صفر باشد نباید فقط cash_fee به‌عنوان قیمت نمایش داده شود */ export function getProductBaseBuyPrice(product: Product): number { if (isMeltedProductUsingAppPrice(product)) { - const tradeBuy = getTradeBuyPrice(lastPrice.value) - if (tradeBuy > 0) { - return applyPriceOffsetPercent(tradeBuy, product.price_offset_percent) + 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 } - // provider صفر — آخرین قیمت ذخیره‌شده از API (وقتی use_last_price_if_zero فعال است) - return Number(product.price ?? 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)) { @@ -58,12 +77,27 @@ export function getProductBuyPrice(product: Product): number { function getProductBaseSellPrice(product: Product): number { if (isMeltedProductUsingAppPrice(product)) { - const tradeSell = getTradeSellPrice(lastPrice.value) - if (tradeSell > 0) { - return applyPriceOffsetPercent(tradeSell, product.price_offset_percent) + 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 } - // provider صفر — همان آخرین قیمت ذخیره‌شده (فروش از همان مبنا) - return Number(product.price ?? 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)) {