Files
boursecu_user/src/utils/productDealMethod.ts
T

295 lines
8.5 KiB
TypeScript
Raw Normal View History

2026-06-01 17:19:45 +03:30
import type { SelectItem } from '@/models/SelectItem'
import { isBoursecuProject } from '@/utils/config'
2026-06-03 22:04:45 +03:30
import { useSettingsStore } from '@/store/settingsStore'
2026-06-01 17:19:45 +03:30
2026-06-03 22:04:45 +03:30
/** شناسه group_type محصول در دیتابیس — نه برچسب نمایشی */
2026-06-01 17:19:45 +03:30
export const BOURSECU_GROUP_MAFTOL = 'مفتول مس'
2026-06-03 19:54:23 +03:30
export const BOURSECU_GROUP_KATOD = 'کاتد مس'
2026-06-01 17:19:45 +03:30
export const BOURSECU_GROUP_ABSHODE = 'آبشده'
2026-06-03 22:04:45 +03:30
export const BOURSECU_WALLET_LABEL_GENERIC = 'موجودی کیف'
2026-06-01 17:19:45 +03:30
export function normalizeProductGroupType(groupType?: string | null) {
return (groupType ?? '').trim()
}
export function isBoursecuDealProductGroup(groupType?: string | null) {
if (!isBoursecuProject()) {
return false
}
const group = normalizeProductGroupType(groupType)
return group === BOURSECU_GROUP_MAFTOL
2026-06-03 19:54:23 +03:30
|| group === BOURSECU_GROUP_KATOD
2026-06-01 17:19:45 +03:30
|| group === BOURSECU_GROUP_ABSHODE
}
export function isMaftolMesGroup(groupType?: string | null) {
return normalizeProductGroupType(groupType) === BOURSECU_GROUP_MAFTOL
}
2026-06-03 19:54:23 +03:30
export function isKatodMesGroup(groupType?: string | null) {
return normalizeProductGroupType(groupType) === BOURSECU_GROUP_KATOD
}
2026-06-01 17:19:45 +03:30
export function isGoldOnlyDealGroup(groupType?: string | null) {
2026-06-03 19:54:23 +03:30
return normalizeProductGroupType(groupType) === BOURSECU_GROUP_ABSHODE
2026-06-01 17:19:45 +03:30
}
2026-06-03 22:04:45 +03:30
export interface ProductUnitSource {
unit?: string | null
direct_price?: number | null
group_type?: string | null
amount?: number | null
}
const COUNT_BASED_GROUP_TYPES = ['سکه', 'گوی', 'مکعب', 'شمش', 'سکه پارسیان']
/** مطابق ادمین: وزن «دارد» یعنی amount مقدار واقعی است؛ «ندارد» یعنی amount = 1 */
export function hasProductUnitWeight(product?: ProductUnitSource | null) {
const amount = Number(product?.amount ?? 0)
return amount > 0 && amount !== 1
}
export function getProductWeightUnit(product?: ProductUnitSource | null) {
if (isBoursecuProject()) {
const group = normalizeProductGroupType(product?.group_type)
if (group === BOURSECU_GROUP_MAFTOL || group === BOURSECU_GROUP_KATOD || group === BOURSECU_GROUP_ABSHODE) {
return getWeightUnit()
}
}
if (!isCountBasedProduct(product) || isGoldOnlyDealGroup(product?.group_type)) {
return getWeightUnit()
}
return 'گرم'
}
export function formatProductWeightAmount(amount?: number | null, product?: ProductUnitSource | null) {
const value = Number(amount ?? 0)
const weightUnit = getProductWeightUnit(product)
if (weightUnit === 'گرم' && value > 0 && value < 1) {
return `${value * 1000} سوت`
}
return `${value} ${weightUnit}`
}
export function getProductUnitWeightLabel(product?: ProductUnitSource | null) {
if (!hasProductUnitWeight(product)) {
return null
}
return formatProductWeightAmount(product?.amount, product)
}
export function getProductTotalWeightLabel(count?: number | null, product?: ProductUnitSource | null) {
if (!hasProductUnitWeight(product)) {
return null
}
const quantity = Number(count ?? 0)
return formatProductWeightAmount(quantity * Number(product?.amount ?? 0), product)
}
export function shouldShowProductTotalWeight(product?: ProductUnitSource | null) {
if (!product) {
return false
}
if (!isCountBasedProduct(product)) {
return true
}
return hasProductUnitWeight(product)
}
export function formatProductInventoryBalance(product?: (ProductUnitSource & { wallet?: { gold?: number | null } }) | null) {
const count = product?.wallet?.gold ?? 0
const quantityPart = `${count} ${getProductQuantityUnit(product)}`
const unitWeight = getProductUnitWeightLabel(product)
if (!unitWeight) {
return quantityPart
}
return `${quantityPart} · هر عدد ${unitWeight}`
}
/** محصول تعدادی: موجودی wallet.gold = تعداد قلم، نه وزن */
export function isCountBasedProduct(product?: ProductUnitSource | null) {
if (!product) {
return false
}
if (product.direct_price != null && product.direct_price !== 0) {
return true
}
const group = normalizeProductGroupType(product.group_type)
if (group === BOURSECU_GROUP_MAFTOL || group === BOURSECU_GROUP_KATOD) {
return true
}
return COUNT_BASED_GROUP_TYPES.includes(group)
}
export function getProductQuantityUnit(product?: ProductUnitSource | null) {
if (isCountBasedProduct(product)) {
return 'عدد'
}
const unit = product?.unit?.trim()
if (unit) {
return unit
}
return getWeightUnit()
}
function getDefaultTypeDealOptions(): SelectItem[] {
const settingsStore = useSettingsStore()
return [
{ value: 'نقدی', key: 'cash' },
{ value: settingsStore.metalTypeLabel, key: 'gold' },
]
}
2026-06-01 17:19:45 +03:30
export function getProductDealMethodOptions(groupType?: string | null): SelectItem[] {
2026-06-03 22:04:45 +03:30
const settingsStore = useSettingsStore()
2026-06-01 17:19:45 +03:30
if (!isBoursecuProject()) {
2026-06-03 22:04:45 +03:30
return getDefaultTypeDealOptions()
2026-06-01 17:19:45 +03:30
}
if (isMaftolMesGroup(groupType)) {
2026-06-03 22:04:45 +03:30
return [
{ value: settingsStore.metalTypeLabel, key: 'katod' },
{ value: 'نقدی', key: 'cash' },
]
2026-06-01 17:19:45 +03:30
}
2026-06-03 19:54:23 +03:30
if (isKatodMesGroup(groupType)) {
2026-06-03 22:04:45 +03:30
return [{ value: 'نقدی', key: 'cash' }]
2026-06-03 19:54:23 +03:30
}
2026-06-01 17:19:45 +03:30
if (isGoldOnlyDealGroup(groupType)) {
2026-06-03 22:04:45 +03:30
return [{ value: settingsStore.meltedGoldLabel, key: 'gold' }]
2026-06-01 17:19:45 +03:30
}
2026-06-03 22:04:45 +03:30
return getDefaultTypeDealOptions()
2026-06-01 17:19:45 +03:30
}
export function getDefaultProductDealMethod(groupType?: string | null) {
return getProductDealMethodOptions(groupType)[0]?.key ?? 'cash'
}
export function isCashLikeDealMethod(method?: string | null) {
2026-06-03 22:04:45 +03:30
return method === 'cash'
2026-06-01 17:19:45 +03:30
}
2026-06-03 19:54:23 +03:30
2026-06-03 22:04:45 +03:30
export function isMainMetalWalletDealMethod(method?: string | null) {
return method === 'gold' || method === 'katod'
}
/** برچسب کیف فلز اصلی (main wallet gold) — از setting metal_type */
export function getMainMetalWalletLabel() {
return `موجودی کیف ${useSettingsStore().metalTypeLabel}`
}
/** برچسب کیف آبشده — از setting metal_type */
export function getMeltedGoldWalletLabel() {
return `موجودی کیف ${useSettingsStore().meltedGoldLabel}`
}
2026-06-03 19:54:23 +03:30
export function getGoldWalletLabel(groupType?: string | null) {
2026-06-03 22:04:45 +03:30
const settingsStore = useSettingsStore()
if (!isBoursecuProject()) {
return getMainMetalWalletLabel()
}
2026-06-03 19:54:23 +03:30
const group = normalizeProductGroupType(groupType)
2026-06-03 22:04:45 +03:30
if (group === BOURSECU_GROUP_MAFTOL) {
return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
}
if (group === BOURSECU_GROUP_KATOD || group === BOURSECU_GROUP_ABSHODE || !group) {
return group === BOURSECU_GROUP_ABSHODE
? getMeltedGoldWalletLabel()
: getMainMetalWalletLabel()
}
2026-06-03 19:54:23 +03:30
return BOURSECU_WALLET_LABEL_GENERIC
}
export function getCashWalletLabel(groupType?: string | null) {
2026-06-03 22:04:45 +03:30
if (!isBoursecuProject()) {
return 'موجودی کیف پول'
}
2026-06-03 19:54:23 +03:30
const group = normalizeProductGroupType(groupType)
2026-06-03 22:04:45 +03:30
if (group === BOURSECU_GROUP_MAFTOL) {
return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
}
2026-06-03 19:54:23 +03:30
2026-06-03 22:04:45 +03:30
return 'موجودی کیف پول'
}
export function getMetalAssetLabel() {
return `دارایی ${useSettingsStore().metalTypeLabel}`
}
export function getMeltedGoldAssetLabel() {
return `دارایی ${useSettingsStore().meltedGoldLabel}`
}
export function getMaftolAssetLabel() {
return `دارایی ${BOURSECU_GROUP_MAFTOL}`
2026-06-03 19:54:23 +03:30
}
export function getWeightUnit() {
return isBoursecuProject() ? 'کیلوگرم' : 'گرم'
}
export function formatBoursecuAssetAmount(amount?: number | null) {
return `${amount ?? 0}${getWeightUnit()}`
}
2026-06-03 22:04:45 +03:30
export function formatMainMetalWalletBalanceText(gold: number) {
return `${getMainMetalWalletLabel()}: ${gold} ${getWeightUnit()}`
}
2026-06-03 19:54:23 +03:30
export function formatGoldWalletBalanceText(gold: number, groupType?: string | null) {
return `${getGoldWalletLabel(groupType)}: ${gold} ${getWeightUnit()}`
}
export function formatCashWalletBalanceText(cashFormatted: string | number, groupType?: string | null) {
return `${getCashWalletLabel(groupType)}: ${cashFormatted} ریال`
}
export function getInsufficientGoldWalletMessage(groupType?: string | null) {
return `${getGoldWalletLabel(groupType)} ناکافی است`
}
export function getInsufficientCashWalletMessage(groupType?: string | null) {
return `${getCashWalletLabel(groupType)} ناکافی است`
}
2026-06-03 22:04:45 +03:30
export function getInsufficientMainMetalWalletMessage() {
return `${getMainMetalWalletLabel()} ناکافی است`
}
export function getMeltedGoldTradeLabel() {
return useSettingsStore().meltedGoldLabel
}
export function getMetalTypeTradeLabel() {
return useSettingsStore().metalTypeLabel
}
export function getTradePriceUnitLabel() {
return useSettingsStore().metalTypeLabel
}