fixed amount

This commit is contained in:
2026-07-04 12:06:02 +03:30
parent 0bcd9ce551
commit 212bfb1cc1
5 changed files with 51 additions and 50 deletions
+4 -2
View File
@@ -1,5 +1,5 @@
import type { Product } from '@/models/products'
import { formatWeightAmount, usePriceWithoutType } from '@/utils/usePrice'
import { formatGoldAmount, formatWeightAmount, usePriceWithoutType } from '@/utils/usePrice'
import type { UserWalletLine } from '@/models/wallet'
import type { ProductFile } from '@/utils/file'
import type { WalletInventoryCard, WalletMetalTotal } from '@/models/walletAssets'
@@ -587,8 +587,10 @@ export function formatProductWalletBalanceText(
const quantity = card ? Number(card.quantity ?? 0) : Number(mainWalletGold ?? 0)
const unit = card?.unit ?? getProductQuantityUnit(product)
const title = product?.title?.trim() || 'محصول'
const formattedQty =
unit === 'عدد' ? usePriceWithoutType(quantity, false) : formatGoldAmount(quantity)
return `موجودی کیف ${title}: ${quantity} ${unit}`
return `موجودی کیف ${title}: ${formattedQty} ${unit}`
}
export function isSingleMetalCatalog(metalIds: number[]): boolean {
+2 -3
View File
@@ -13,6 +13,7 @@ import {
usesQuantityTrade,
} from '@/utils/productCategories'
import { formatInventoryCardValue } from '@/utils/productAssets'
import { TRADE_GRAM_DISPLAY_DECIMALS } from '@/utils/serviceFee'
import { formatGoldAmount } from '@/utils/usePrice'
/** @deprecated از isMeltedTradeGroup استفاده کنید */
@@ -636,8 +637,6 @@ export function resolveTransactionTradeProduct(
return item.wallet?.product ?? null
}
const MELTED_GOLD_TRANSACTION_DECIMALS = 5
/** معامله آبشده / کیف طلای اصلی — نه سکه و محصول تعدادی */
export function isMeltedGoldTransaction(
item?: (TransactionTradeProductSource & { type?: number | null; title?: string | null }) | null,
@@ -685,7 +684,7 @@ export function formatTransactionGoldAmount(
item?: (TransactionTradeProductSource & { type?: number | null; title?: string | null }) | null,
amount?: number | null,
): string {
const decimals = isMeltedGoldTransaction(item) ? MELTED_GOLD_TRANSACTION_DECIMALS : 3
const decimals = isMeltedGoldTransaction(item) ? TRADE_GRAM_DISPLAY_DECIMALS : 3
return formatGoldAmount(amount, decimals)
}
+31 -10
View File
@@ -9,6 +9,24 @@ export const feeDeductFromGold = ref(false)
/** آخرین فیلدی که کاربر مستقیم ویرایش کرده — مبنای به‌روزرسانی هنگام تغییر قیمت */
export type TradeInputLock = 'gram' | 'rial' | null
/** حداکثر اعشار ذخیره/ارسال به سرور (مطابق orders.amount) */
export const TRADE_GRAM_STORAGE_DECIMALS = 10
/** حداکثر اعشار نمایش به کاربر در اپ */
export const TRADE_GRAM_DISPLAY_DECIMALS = 5
export function normalizeOrderGoldAmount(grams?: number | null): number {
const value = Number(grams ?? 0)
if (!Number.isFinite(value) || value === 0) return 0
return parseFloat(value.toFixed(TRADE_GRAM_STORAGE_DECIMALS))
}
export function formatTradeGramDisplay(grams?: number | null): number {
const value = Number(grams ?? 0)
if (!Number.isFinite(value)) return 0
return parseFloat(value.toFixed(TRADE_GRAM_DISPLAY_DECIMALS))
}
function feeRate() {
return Number(serviceFeePercentage.value)
}
@@ -46,7 +64,7 @@ export function calcFeeInGrams(goldValue?: number | null) {
/** گرم معادل برای دریافت دقیق مبلغ ریالی در فروش */
export function calcGoldAmountFromTargetRial(rialAmount: number, unitPriceRial: number) {
if (!rialAmount || !unitPriceRial) return 0
return parseFloat((rialAmount / unitPriceRial).toFixed(6))
return normalizeOrderGoldAmount(rialAmount / unitPriceRial)
}
/** وقتی کاربر گرم را قفل کرده — ریال نمایشی/قابل پرداخت از همان گرم */
@@ -78,16 +96,15 @@ export function calcGramFromLockedRial(rialAmount: number, unitPriceRial: number
if (isBuy) {
if (feeDeductFromGold.value) {
return parseFloat((rial / (unit * (1 + fee))).toFixed(6))
return rial / (unit * (1 + fee))
}
const grossGrams = rial / unit
return parseFloat((grossGrams * (1 - fee)).toFixed(6))
return (rial / unit) * (1 - fee)
}
if (feeDeductFromGold.value) {
return parseFloat((rial / unit).toFixed(6))
return rial / unit
}
return parseFloat((rial / (unit * (1 - fee))).toFixed(6))
return rial / (unit * (1 - fee))
}
/** مبلغ ریالی قفل‌شده توسط کاربر — برای ارسال به سرور */
@@ -131,8 +148,12 @@ export function calcOrderGoldAmount(
const cash = Number(goldCash ?? 0)
const unit = Number(unitPriceRial ?? 0)
if (isBuy && inputLock === 'rial' && !feeDeductFromGold.value && cash > 0 && unit > 0) {
return parseFloat((cash / unit).toFixed(6))
if (isBuy && inputLock === 'rial' && cash > 0 && unit > 0) {
if (feeDeductFromGold.value) {
const fee = feeRate()
return normalizeOrderGoldAmount(cash / (unit * (1 + fee)))
}
return normalizeOrderGoldAmount(cash / unit)
}
if (!isBuy && feeDeductFromGold.value) {
@@ -140,7 +161,7 @@ export function calcOrderGoldAmount(
return calcGoldAmountFromTargetRial(cash, unit)
}
if (inputLock !== 'rial') {
return parseFloat(gold.toFixed(6))
return normalizeOrderGoldAmount(gold)
}
const targetCash = calcSellTargetCash(goldCash, goldValue, unitPriceRial, inputLock)
if (targetCash != null && unit > 0) {
@@ -148,7 +169,7 @@ export function calcOrderGoldAmount(
}
}
return parseFloat(gold.toFixed(6))
return normalizeOrderGoldAmount(gold)
}
/** مبلغ ریالی دقیق دریافتی در فروش (فقط وقتی کاربر با ریال وارد کرده) */
+1 -1
View File
@@ -143,7 +143,7 @@ export function getTransactionGoldAmount(item: TransactionPriceSource): number {
return gold
}
export function formatGoldAmount(amount?: number | null, decimals = 3): string {
export function formatGoldAmount(amount?: number | null, decimals = 5): string {
const num = Number(amount)
if (!Number.isFinite(num)) return '0'
return formatGroupedPrice(Number(num.toFixed(decimals)), false)