price calulator changed
This commit is contained in:
+77
-10
@@ -6,6 +6,13 @@ export const serviceFeePercentage = ref<number>(0.01)
|
||||
/** true = مبالغ رند: فروش → ریال دقیق (کارمزد از طلا)؛ خرید → وزن دقیق (کارمزد از ریال) */
|
||||
export const feeDeductFromGold = ref(false)
|
||||
|
||||
/** آخرین فیلدی که کاربر مستقیم ویرایش کرده — مبنای بهروزرسانی هنگام تغییر قیمت */
|
||||
export type TradeInputLock = 'gram' | 'rial' | null
|
||||
|
||||
function feeRate() {
|
||||
return Number(serviceFeePercentage.value)
|
||||
}
|
||||
|
||||
export async function loadServiceFeeFromSettings() {
|
||||
try {
|
||||
const settings = await getSettings()
|
||||
@@ -42,6 +49,47 @@ export function calcGoldAmountFromTargetRial(rialAmount: number, unitPriceRial:
|
||||
return parseFloat((rialAmount / unitPriceRial).toFixed(6))
|
||||
}
|
||||
|
||||
/** وقتی کاربر گرم را قفل کرده — ریال نمایشی/قابل پرداخت از همان گرم */
|
||||
export function calcRialFromLockedGram(displayGram: number, unitPriceRial: number, isBuy: boolean) {
|
||||
const grams = Number(displayGram)
|
||||
const unit = Number(unitPriceRial)
|
||||
if (!grams || !unit) return 0
|
||||
const fee = feeRate()
|
||||
|
||||
if (isBuy) {
|
||||
if (feeDeductFromGold.value) {
|
||||
return Math.round(grams * unit * (1 + fee))
|
||||
}
|
||||
return Math.round(grams * unit)
|
||||
}
|
||||
|
||||
if (feeDeductFromGold.value) {
|
||||
return Math.round(grams * unit)
|
||||
}
|
||||
return Math.round(grams * unit * (1 - fee))
|
||||
}
|
||||
|
||||
/** وقتی کاربر ریال را قفل کرده — گرم نمایشی از همان مبلغ */
|
||||
export function calcGramFromLockedRial(rialAmount: number, unitPriceRial: number, isBuy: boolean) {
|
||||
const rial = Number(rialAmount)
|
||||
const unit = Number(unitPriceRial)
|
||||
if (!rial || !unit) return 0
|
||||
const fee = feeRate()
|
||||
|
||||
if (isBuy) {
|
||||
if (feeDeductFromGold.value) {
|
||||
return parseFloat((rial / (unit * (1 + fee))).toFixed(6))
|
||||
}
|
||||
const grossGrams = rial / unit
|
||||
return parseFloat((grossGrams * (1 - fee)).toFixed(6))
|
||||
}
|
||||
|
||||
if (feeDeductFromGold.value) {
|
||||
return parseFloat((rial / unit).toFixed(6))
|
||||
}
|
||||
return parseFloat((rial / (unit * (1 - fee))).toFixed(6))
|
||||
}
|
||||
|
||||
/** مبلغ نهایی قابل پرداخت (خرید) یا دریافت (فروش) به ریال */
|
||||
export function calcPayablePriceRial(
|
||||
finalPriceRial: number,
|
||||
@@ -57,22 +105,35 @@ export function calcPayablePriceRial(
|
||||
return isBuy ? finalPriceRial : Math.max(0, finalPriceRial - feeInRial)
|
||||
}
|
||||
|
||||
/** وزن ارسالی به سرور — فروش رند: از مبلغ ریالی هدف */
|
||||
/** وزن ارسالی به سرور */
|
||||
export function calcOrderGoldAmount(
|
||||
goldValue?: number | null,
|
||||
goldCash?: number | null,
|
||||
unitPriceRial?: number,
|
||||
isBuy?: boolean
|
||||
isBuy?: boolean,
|
||||
inputLock?: TradeInputLock
|
||||
) {
|
||||
const gold = Number(goldValue ?? 0)
|
||||
if (isBuy || !feeDeductFromGold.value) {
|
||||
return parseFloat(gold.toFixed(6))
|
||||
}
|
||||
const targetCash = calcSellTargetCash(goldCash, goldValue, unitPriceRial)
|
||||
const cash = Number(goldCash ?? 0)
|
||||
const unit = Number(unitPriceRial ?? 0)
|
||||
if (targetCash != null && unit > 0) {
|
||||
return calcGoldAmountFromTargetRial(targetCash, unit)
|
||||
|
||||
if (isBuy && inputLock === 'rial' && !feeDeductFromGold.value && cash > 0 && unit > 0) {
|
||||
return parseFloat((cash / unit).toFixed(6))
|
||||
}
|
||||
|
||||
if (!isBuy && feeDeductFromGold.value) {
|
||||
if (inputLock === 'rial' && cash > 0 && unit > 0) {
|
||||
return calcGoldAmountFromTargetRial(cash, unit)
|
||||
}
|
||||
if (inputLock !== 'rial') {
|
||||
return parseFloat(gold.toFixed(6))
|
||||
}
|
||||
const targetCash = calcSellTargetCash(goldCash, goldValue, unitPriceRial, inputLock)
|
||||
if (targetCash != null && unit > 0) {
|
||||
return calcGoldAmountFromTargetRial(targetCash, unit)
|
||||
}
|
||||
}
|
||||
|
||||
return parseFloat(gold.toFixed(6))
|
||||
}
|
||||
|
||||
@@ -80,10 +141,16 @@ export function calcOrderGoldAmount(
|
||||
export function calcSellTargetCash(
|
||||
goldCash?: number | null,
|
||||
goldValue?: number | null,
|
||||
unitPriceRial?: number
|
||||
unitPriceRial?: number,
|
||||
inputLock?: TradeInputLock
|
||||
) {
|
||||
if (!feeDeductFromGold.value) return undefined
|
||||
if (inputLock === 'gram') return undefined
|
||||
|
||||
const cash = Number(goldCash ?? 0)
|
||||
if (!feeDeductFromGold.value || cash <= 0) return undefined
|
||||
if (cash <= 0) return undefined
|
||||
if (inputLock === 'rial') return Math.round(cash)
|
||||
|
||||
const gold = Number(goldValue ?? 0)
|
||||
const unit = Number(unitPriceRial ?? 0)
|
||||
if (gold > 0 && unit > 0) {
|
||||
|
||||
Reference in New Issue
Block a user