fixsome changes

This commit is contained in:
2026-08-16 14:03:11 +03:30
parent 2b27b16296
commit 27f3a6212a
7 changed files with 469 additions and 59 deletions
+57 -9
View File
@@ -12,12 +12,45 @@ export function findMethodPriceEntry(
): MethodePrices | undefined {
if (!readingMethod) return undefined
return prices.find(
(entry) =>
entry.key === readingMethod ||
entry.id === readingMethod ||
entry.name === readingMethod
)
const needle = String(readingMethod).trim()
if (!needle) return undefined
return prices.find((entry) => {
const key = String(entry.key ?? '').trim()
const id = String(entry.id ?? '').trim()
const name = String(entry.name ?? '').trim()
return key === needle || id === needle || name === needle
})
}
/** مقدار value سلکت مرجع — key خالی را با ?? نگه نمی‌دارد */
export function canonicalMethodPriceId(entry: MethodePrices): string {
return String(entry.key || entry.id || entry.name || '').trim()
}
/** reading_method ذخیره‌شده (name/key/id) را به id سلکت هم‌تراز می‌کند */
export function resolveReadingMethodForSelect(
raw: string | null | undefined,
prices: MethodePrices[]
): string | null | undefined {
if (raw == null) return raw
const trimmed = String(raw).trim()
if (!trimmed) return raw
const entry = findMethodPriceEntry(prices, trimmed)
if (!entry) return trimmed
return canonicalMethodPriceId(entry) || trimmed
}
/** مرجع روی محصول هست ولی در فید/لیست فعلی همکار پیدا نمی‌شود */
export function isOrphanReadingMethod(
raw: string | null | undefined,
prices: MethodePrices[]
): boolean {
const trimmed = String(raw ?? '').trim()
if (!trimmed || !prices.length) return false
return !findMethodPriceEntry(prices, trimmed)
}
export function normalizeReferenceBase(entry: MethodePrices): { base: number; profit: number } {
@@ -117,7 +150,12 @@ export function computeProductEditorPricePreview(input: {
}
const entry = findMethodPriceEntry(input.pricesData, input.form.reading_method)
if (!entry) {
return { status: 'empty', message: '' }
return {
status: 'empty',
message: input.pricesData.length
? 'مرجع در لیست قیمت یافت نشد'
: 'لیست قیمت مرجع خالی است — بروزرسانی را بزنید',
}
}
const { base } = normalizeReferenceBase(entry)
if (base <= 0) {
@@ -177,7 +215,12 @@ export function computeProductEditorPricePreview(input: {
}
const entry = findMethodPriceEntry(input.pricesData, input.form.reading_method)
if (!entry) {
return { status: 'empty', message: '' }
return {
status: 'empty',
message: input.pricesData.length
? 'مرجع در لیست قیمت یافت نشد'
: 'لیست قیمت مرجع خالی است — بروزرسانی را بزنید',
}
}
const { base, profit } = normalizeReferenceBase(entry)
const liveAmount = adjustedProductPrice(base, profit, offsetPercent)
@@ -232,7 +275,12 @@ export function computeProductEditorPricePreview(input: {
}
const entry = findMethodPriceEntry(input.pricesData, input.form.reading_method)
if (!entry) {
return { status: 'empty', message: '' }
return {
status: 'empty',
message: input.pricesData.length
? 'مرجع در لیست قیمت یافت نشد'
: 'لیست قیمت مرجع خالی است — بروزرسانی را بزنید',
}
}
const { base, profit } = normalizeReferenceBase(entry)
const liveAmount = adjustedProductPrice(base, profit, offsetPercent)