Files
boursecu_admin/src/utils/orderServiceFee.ts
T
2026-07-23 17:53:28 +03:30

44 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Order } from '@/model/order'
import { usePrice } from '@core/utils/usePrice'
import { getMainMetal } from '@/utils/metalLabels'
export type OrderServiceFeeDisplay = {
value: string
/** از ریال / از مس / از طلا … */
source: string
}
/**
* نمایش کارمزد سفارش با منبع کسر (fee_asset).
* gold → از کیف فلز (گرم + معادل ریالی)
* rial / خالی → از ریال
*/
export function formatOrderServiceFee(order?: Pick<
Order,
'fee_asset' | 'service_charge' | 'service_charge_price' | 'order_metal_label' | 'amount_unit'
> | null): OrderServiceFeeDisplay {
const grams = Number(order?.service_charge ?? 0)
const rial = Number(order?.service_charge_price ?? 0)
if ((!Number.isFinite(grams) || grams <= 0) && (!Number.isFinite(rial) || rial <= 0)) {
return { value: '۰', source: '' }
}
const fromMetal = String(order?.fee_asset ?? '').toLowerCase() === 'gold'
if (fromMetal) {
const metalName = (order?.order_metal_label || getMainMetal() || 'فلز').trim()
const unit = (order?.amount_unit || 'گرم').trim() || 'گرم'
const gramText = Number(grams || 0).toLocaleString('fa-IR', { maximumFractionDigits: 4 })
const rialText = usePrice(rial || 0, true, 'ریال')
return {
value: `${gramText} ${unit} ≈ ${rialText}`,
source: `از ${metalName}`,
}
}
return {
value: usePrice(rial || 0, true, 'ریال'),
source: 'از ریال',
}
}