Treat mesghal pasted into per-gram price as gram on admin orders.
Prevents the order preview and submit from turning a rial amount into under-weighted soot. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -201,13 +201,26 @@ function toOrderNumber(val: unknown): number | undefined {
|
||||
return Number.isFinite(n) ? n : undefined
|
||||
}
|
||||
|
||||
const MESGHAL_FACTOR = 4.3318
|
||||
|
||||
/** اگر قیمت واحد حدود مثقال باشد (گرم زنده × ضریب)، به قیمت هر گرم برگردان. */
|
||||
function gramUnitPrice(price: number): number {
|
||||
const live = toOrderNumber(form.value.direction === 1 ? dataLast.value?.buy : dataLast.value?.sell)
|
||||
if (!live || live <= 0 || !price) return price
|
||||
const ratio = price / live
|
||||
if (ratio >= MESGHAL_FACTOR * 0.92 && ratio <= MESGHAL_FACTOR * 1.08) {
|
||||
return price / MESGHAL_FACTOR
|
||||
}
|
||||
return price
|
||||
}
|
||||
|
||||
/** آخرین فیلدی که کاربر ویرایش کرده — برای همگامسازی هنگام تغییر قیمت هر گرم */
|
||||
const amountInputLock = ref<'rial' | 'gram' | null>(null)
|
||||
|
||||
function calculateCash(val: unknown) {
|
||||
amountInputLock.value = 'gram'
|
||||
const grams = toOrderNumber(val)
|
||||
const price = toOrderNumber(form.value.price)
|
||||
const price = gramUnitPrice(toOrderNumber(form.value.price) ?? 0)
|
||||
if (grams === undefined) {
|
||||
form.value.goldCash = undefined
|
||||
amountInputLock.value = null
|
||||
@@ -221,7 +234,7 @@ function calculateCash(val: unknown) {
|
||||
function calculateGold(val: unknown) {
|
||||
amountInputLock.value = 'rial'
|
||||
const rial = toOrderNumber(val)
|
||||
const price = toOrderNumber(form.value.price)
|
||||
const price = gramUnitPrice(toOrderNumber(form.value.price) ?? 0)
|
||||
if (rial === undefined) {
|
||||
form.value.goldValue = undefined
|
||||
amountInputLock.value = null
|
||||
@@ -311,6 +324,7 @@ async function ok() {
|
||||
loading.value = true
|
||||
try {
|
||||
const payload: Order = { ...form.value }
|
||||
payload.price = gramUnitPrice(toOrderNumber(form.value.price) ?? 0)
|
||||
payload.amount = toOrderNumber(form.value.goldValue)
|
||||
|
||||
if (amountInputLock.value === 'rial' && form.value.goldCash != null) {
|
||||
|
||||
Reference in New Issue
Block a user