fix some bugs

This commit is contained in:
2026-06-23 22:31:21 +03:30
parent 71764f7e9a
commit 69da635247
9 changed files with 156 additions and 239 deletions
+1 -10
View File
@@ -1,19 +1,10 @@
import type { TableInput } from '@core/type/baseTable' import type { TableInput } from '@core/type/baseTable'
import useAxios from '@core/utils/useAxios' import useAxios from '@core/utils/useAxios'
// import { Customer } from '@/model/customer' import type { Charge, SendWeight, Wallet, Withdraw } from '@/model/wallet'
import type { Charge, SendWeight, Wallet, WalletChargeTarget, Withdraw } from '@/model/wallet'
export const getWalletTable = async (params?: TableInput) => export const getWalletTable = async (params?: TableInput) =>
await useAxios.post<Array<Wallet>>('/admin/wallet/table', params) await useAxios.post<Array<Wallet>>('/admin/wallet/table', params)
export const getWalletChargeTargets = async () =>
await useAxios.get<{ targets: WalletChargeTarget[] }>('/admin/wallet/charge-targets')
/** شارژ یکپارچه — نقدی یا محصول (product_id=null یعنی ریالی) */
export const chargeWalletUnified = async (params: Charge) =>
await useAxios.post<boolean>('/admin/wallet/charge_wallet', params)
/** @deprecated از chargeWalletUnified استفاده کنید */
export const chargeWallet = async (params: Charge, type: 'gold' | 'money') => export const chargeWallet = async (params: Charge, type: 'gold' | 'money') =>
type === 'money' type === 'money'
? await useAxios.post<boolean>('/admin/wallet/charge', params) ? await useAxios.post<boolean>('/admin/wallet/charge', params)
-16
View File
@@ -54,20 +54,6 @@ export interface Withdraw {
export type WalletChargeDirection = 'charge' | 'discharge' export type WalletChargeDirection = 'charge' | 'discharge'
export type WalletChargeTargetKind = 'money' | 'metal'
export interface WalletChargeTarget {
key: string
kind: WalletChargeTargetKind
label: string
unit: string
product_id?: number | null
metal_id?: number
uses_main_wallet?: boolean
trade_by_quantity?: boolean
group_type?: string
}
export interface Charge { export interface Charge {
value?: number value?: number
note?: string note?: string
@@ -75,8 +61,6 @@ export interface Charge {
talabBedehi?: boolean talabBedehi?: boolean
direction?: WalletChargeDirection direction?: WalletChargeDirection
amountAbs?: number | string amountAbs?: number | string
chargeTargetKey?: string | null
product_id?: number | null
} }
export interface WithdrawStatus { export interface WithdrawStatus {
@@ -2,7 +2,7 @@
<BasicActionSheet <BasicActionSheet
v-model:visible="visibleProxy" v-model:visible="visibleProxy"
:title="modalTitle" :title="modalTitle"
modal-width="24rem" modal-width="22rem"
drawer-height="auto" drawer-height="auto"
compact compact
:loading="loading" :loading="loading"
@@ -24,18 +24,33 @@
/> />
</div> </div>
<div v-if="selectedCustomer" class="dashboard-wallet-modal__balances"> <div
v-if="selectedCustomer"
class="dashboard-wallet-modal__balances"
>
<div>
<span class="text-slate-500">{{ metalLabel.goldWalletShort() }}</span>
<p class="font-semibold text-slate-800 dark:text-slate-100">{{ formatGold(selectedCustomer) }}</p>
</div>
<div> <div>
<span class="text-slate-500">ریالی</span> <span class="text-slate-500">ریالی</span>
<p class="font-semibold text-slate-800 dark:text-slate-100">{{ formatCash(selectedCustomer) }}</p> <p class="font-semibold text-slate-800 dark:text-slate-100">{{ formatCash(selectedCustomer) }}</p>
</div> </div>
</div> </div>
<div class="dashboard-wallet-modal__section">
<label class="dashboard-wallet-modal__label">نوع کیف پول</label>
<a-radio-group v-model:value="walletVariant" button-style="solid" class="flex w-full">
<a-radio-button value="money" class="w-1/2 text-center">ریالی</a-radio-button>
<a-radio-button value="gold" class="w-1/2 text-center">{{ metalLabel.goldWalletShort() }}</a-radio-button>
</a-radio-group>
</div>
<WalletChargeForm <WalletChargeForm
ref="chargeFormRef" ref="chargeFormRef"
class="dashboard-wallet-modal__form" class="dashboard-wallet-modal__form"
:model-value="formCharge" :model-value="formCharge"
:targets="chargeTargets" :variant="walletVariant"
/> />
<div class="dashboard-wallet-modal__footer"> <div class="dashboard-wallet-modal__footer">
@@ -63,19 +78,18 @@ import { message } from 'ant-design-vue'
import BasicActionSheet from '@/components/BasicActionSheet.vue' import BasicActionSheet from '@/components/BasicActionSheet.vue'
import WalletChargeForm from '@/pages/menuPages/customers/base/components/WalletChargeForm.vue' import WalletChargeForm from '@/pages/menuPages/customers/base/components/WalletChargeForm.vue'
import { import {
applyChargeTarget,
buildChargePayload, buildChargePayload,
createEmptyChargeForm, createEmptyChargeForm,
resolveChargeTarget,
walletChargeOkText, walletChargeOkText,
walletChargeSuccessMessage, walletChargeSuccessMessage,
walletChargeTitle, walletChargeTitle,
} from '@/pages/menuPages/customers/base/utils/walletChargeForm' } from '@/pages/menuPages/customers/base/utils/walletChargeForm'
import { getCustomersList } from '@/api/customer' import { getCustomersList } from '@/api/customer'
import { chargeWalletUnified, getWalletChargeTargets } from '@/api/wallet' import { chargeWallet } from '@/api/wallet'
import type { Customer } from '@/model/customer' import type { Customer } from '@/model/customer'
import type { Charge, Wallet, WalletChargeTarget } from '@/model/wallet' import type { Charge, Wallet } from '@/model/wallet'
import { usePrice } from '@core/utils/usePrice' import { usePrice } from '@core/utils/usePrice'
import { metalLabel } from '@/utils/adminMetalLabels'
const props = defineProps<{ visible: boolean }>() const props = defineProps<{ visible: boolean }>()
const emit = defineEmits<{ (e: 'update:visible', value: boolean): void }>() const emit = defineEmits<{ (e: 'update:visible', value: boolean): void }>()
@@ -88,7 +102,7 @@ const visibleProxy = computed({
const loading = ref(false) const loading = ref(false)
const submitting = ref(false) const submitting = ref(false)
const customers = ref<Customer[]>([]) const customers = ref<Customer[]>([])
const chargeTargets = ref<WalletChargeTarget[]>([]) const walletVariant = ref<'money' | 'gold'>('money')
const formCharge = ref<Charge>(createEmptyChargeForm()) const formCharge = ref<Charge>(createEmptyChargeForm())
const chargeFormRef = ref<InstanceType<typeof WalletChargeForm>>() const chargeFormRef = ref<InstanceType<typeof WalletChargeForm>>()
@@ -110,12 +124,8 @@ const selectedCustomer = computed(() =>
customers.value.find((customer) => customer.id === formCharge.value.customer_id), customers.value.find((customer) => customer.id === formCharge.value.customer_id),
) )
const selectedTarget = computed(() =>
resolveChargeTarget(chargeTargets.value, formCharge.value.chargeTargetKey),
)
const modalTitle = computed(() => const modalTitle = computed(() =>
walletChargeTitle(selectedTarget.value, formCharge.value.direction), walletChargeTitle(walletVariant.value, formCharge.value.direction),
) )
const okText = computed(() => walletChargeOkText(formCharge.value.direction)) const okText = computed(() => walletChargeOkText(formCharge.value.direction))
@@ -129,25 +139,20 @@ function customerWallet(customer: Customer): Wallet | undefined {
return Array.isArray(wallet) ? wallet[0] : wallet return Array.isArray(wallet) ? wallet[0] : wallet
} }
function formatGold(customer: Customer) {
const gold = customerWallet(customer)?.gold ?? customer.wallet_gold ?? customer.gold
return `${Number(gold ?? 0).toLocaleString('fa-IR', { maximumFractionDigits: 3 })} گرم`
}
function formatCash(customer: Customer) { function formatCash(customer: Customer) {
const cash = customerWallet(customer)?.cash ?? customer.wallet_cash const cash = customerWallet(customer)?.cash ?? customer.wallet_cash
return usePrice(Number(cash ?? 0), true, '') return usePrice(Number(cash ?? 0), true, '')
} }
async function loadChargeTargets() {
const data = await getWalletChargeTargets()
chargeTargets.value = data?.targets ?? []
const firstTarget = chargeTargets.value[0]
if (firstTarget) {
Object.assign(formCharge.value, applyChargeTarget(formCharge.value, firstTarget))
}
}
async function loadCustomers() { async function loadCustomers() {
loading.value = true loading.value = true
try { try {
const [customerList] = await Promise.all([getCustomersList(), loadChargeTargets()]) customers.value = (await getCustomersList()) ?? []
customers.value = customerList ?? []
} catch (e) { } catch (e) {
console.error(e) console.error(e)
message.error('خطا در بارگذاری لیست مشتریان') message.error('خطا در بارگذاری لیست مشتریان')
@@ -157,11 +162,8 @@ async function loadCustomers() {
} }
function resetForm() { function resetForm() {
walletVariant.value = 'money'
formCharge.value = createEmptyChargeForm() formCharge.value = createEmptyChargeForm()
const firstTarget = chargeTargets.value[0]
if (firstTarget) {
Object.assign(formCharge.value, applyChargeTarget(formCharge.value, firstTarget))
}
} }
function handleClose() { function handleClose() {
@@ -187,7 +189,7 @@ async function submit() {
submitting.value = true submitting.value = true
try { try {
await chargeWalletUnified(buildChargePayload(formCharge.value)) await chargeWallet(buildChargePayload(formCharge.value), walletVariant.value)
message.success(walletChargeSuccessMessage(formCharge.value.direction)) message.success(walletChargeSuccessMessage(formCharge.value.direction))
closeModal() closeModal()
} catch (e) { } catch (e) {
@@ -230,7 +232,7 @@ watch(
} }
.dashboard-wallet-modal__balances { .dashboard-wallet-modal__balances {
@apply grid grid-cols-1 gap-3 rounded-lg border border-slate-200 bg-slate-50 px-3 py-2.5 text-xs dark:border-slate-600 dark:bg-slate-800/60; @apply grid grid-cols-2 gap-3 rounded-lg border border-slate-200 bg-slate-50 px-3 py-2.5 text-xs dark:border-slate-600 dark:bg-slate-800/60;
} }
.dashboard-wallet-modal__footer { .dashboard-wallet-modal__footer {
@@ -26,7 +26,8 @@
@edit="forwardAndClose('edit', $event)" @edit="forwardAndClose('edit', $event)"
@archive="forwardAndClose('archive', $event)" @archive="forwardAndClose('archive', $event)"
@toggleActive="forward('toggleActive', $event)" @toggleActive="forward('toggleActive', $event)"
@chargeWallet="forwardAndClose('chargeWallet', $event)" @chargeMoney="forwardAndClose('chargeMoney', $event)"
@chargeGold="forwardAndClose('chargeGold', $event)"
@toggleTrade="forward('toggleTrade', $event)" @toggleTrade="forward('toggleTrade', $event)"
@confirmUser="forwardAndClose('confirmUser', $event)" @confirmUser="forwardAndClose('confirmUser', $event)"
/> />
@@ -55,7 +56,8 @@ const emit = defineEmits<{
(e: 'edit', item: Customer): void (e: 'edit', item: Customer): void
(e: 'archive', item: Customer): void (e: 'archive', item: Customer): void
(e: 'toggleActive', item: Customer): void (e: 'toggleActive', item: Customer): void
(e: 'chargeWallet', item: Customer): void (e: 'chargeMoney', item: Customer): void
(e: 'chargeGold', item: Customer): void
(e: 'toggleTrade', item: Customer): void (e: 'toggleTrade', item: Customer): void
(e: 'confirmUser', item: Customer): void (e: 'confirmUser', item: Customer): void
}>() }>()
@@ -67,7 +69,8 @@ type SheetAction =
| 'edit' | 'edit'
| 'archive' | 'archive'
| 'toggleActive' | 'toggleActive'
| 'chargeWallet' | 'chargeMoney'
| 'chargeGold'
| 'toggleTrade' | 'toggleTrade'
| 'confirmUser' | 'confirmUser'
@@ -4,15 +4,12 @@
<h2 class="text-base font-700 leading-relaxed text-gray-900">{{ row.name || '-' }}</h2> <h2 class="text-base font-700 leading-relaxed text-gray-900">{{ row.name || '-' }}</h2>
<p v-if="row.mobile" class="text-sm text-gray-500" dir="ltr">{{ row.mobile }}</p> <p v-if="row.mobile" class="text-sm text-gray-500" dir="ltr">{{ row.mobile }}</p>
<div class="grid grid-cols-1 gap-2"> <div class="grid grid-cols-2 gap-2">
<BaseButton <BaseButton class="w-full" size="middle" type="primary" icon="vuesax-linear:wallet-add" @click="closeAndEmit('chargeMoney', row)">
class="w-full" شارژ ریالی
size="middle" </BaseButton>
type="primary" <BaseButton class="w-full" size="middle" type="primary" icon="vuesax-linear:wallet-money" @click="closeAndEmit('chargeGold', row)">
icon="vuesax-linear:wallet-add" {{ metalLabel.goldChargeShort() }}
@click="closeAndEmit('chargeWallet', row)"
>
شارژ / دشارژ کیف پول
</BaseButton> </BaseButton>
<BaseButton <BaseButton
v-if="!row.is_legal" v-if="!row.is_legal"
@@ -110,7 +107,7 @@ import MobileInfoGrid from '@/components/mobile/MobileInfoGrid.vue'
import { getCashBalance } from '@/components/mobile/utils/wallet' import { getCashBalance } from '@/components/mobile/utils/wallet'
import { toJalali } from '@core/utils/useDataTime' import { toJalali } from '@core/utils/useDataTime'
import { usePrice } from '@core/utils/usePrice' import { usePrice } from '@core/utils/usePrice'
import { customerWalletMetalBalanceTitle } from '@/utils/adminMetalLabels' import { customerWalletMetalBalanceTitle, metalLabel } from '@/utils/adminMetalLabels'
import type { CustomerMetalColumn } from '../hooks/customerColumns' import type { CustomerMetalColumn } from '../hooks/customerColumns'
import { formatMetalDisplayValue, metalCellValue, metalDisplayUnit } from '@/utils/metalWeight' import { formatMetalDisplayValue, metalCellValue, metalDisplayUnit } from '@/utils/metalWeight'
@@ -126,7 +123,8 @@ const emit = defineEmits<{
(e: 'edit', item: Customer): void (e: 'edit', item: Customer): void
(e: 'archive', item: Customer): void (e: 'archive', item: Customer): void
(e: 'toggleActive', item: Customer): void (e: 'toggleActive', item: Customer): void
(e: 'chargeWallet', item: Customer): void (e: 'chargeMoney', item: Customer): void
(e: 'chargeGold', item: Customer): void
(e: 'toggleTrade', item: Customer): void (e: 'toggleTrade', item: Customer): void
(e: 'confirmUser', item: Customer): void (e: 'confirmUser', item: Customer): void
}>() }>()
@@ -136,7 +134,7 @@ const visibleProxy = computed({
set: (value: boolean) => emit('update:visible', value) set: (value: boolean) => emit('update:visible', value)
}) })
type SheetEmit = 'archive' | 'chargeWallet' | 'info' | 'confirmUser' | 'edit' type SheetEmit = 'archive' | 'chargeMoney' | 'chargeGold' | 'info' | 'confirmUser' | 'edit'
function closeAndEmit(event: SheetEmit, row: Customer) { function closeAndEmit(event: SheetEmit, row: Customer) {
visibleProxy.value = false visibleProxy.value = false
@@ -6,18 +6,6 @@
class="wallet-charge-form" class="wallet-charge-form"
:class="formClass" :class="formClass"
> >
<a-form-item label="کیف پول / محصول" name="chargeTargetKey" :rules="targetRules">
<a-select
v-model:value="modelValue.chargeTargetKey"
show-search
placeholder="انتخاب کنید"
class="w-full"
:options="targetOptions"
:filter-option="filterTarget"
@change="onTargetChange"
/>
</a-form-item>
<a-form-item label="نوع عملیات" name="direction"> <a-form-item label="نوع عملیات" name="direction">
<a-radio-group <a-radio-group
v-model:value="modelValue.direction" v-model:value="modelValue.direction"
@@ -48,10 +36,10 @@
<BasicInput <BasicInput
v-model:value="modelValue.amountAbs" v-model:value="modelValue.amountAbs"
:addon-before="unit" :addon-before="unit"
:type="isMoney ? 'price' : 'number'" :type="variant === 'money' ? 'price' : 'number'"
:place-holder="amountPlaceholder" :place-holder="amountPlaceholder"
/> />
<div v-if="isMoney && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint"> <div v-if="variant === 'money' && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint">
معادل {{ usePrice(Number(modelValue.amountAbs) / 10, true, 'تومان') }} معادل {{ usePrice(Number(modelValue.amountAbs) / 10, true, 'تومان') }}
</div> </div>
</a-form-item> </a-form-item>
@@ -63,46 +51,23 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, ref, watch } from 'vue' import { computed, ref } from 'vue'
import type { FormInstance } from 'ant-design-vue' import type { FormInstance } from 'ant-design-vue'
import type { Charge, WalletChargeTarget } from '@/model/wallet' import type { Charge } from '@/model/wallet'
import { inputRules } from '@/utils/inputRules' import { inputRules } from '@/utils/inputRules'
import { usePrice } from '@core/utils/usePrice' import { usePrice } from '@core/utils/usePrice'
import { applyChargeTarget, resolveChargeTarget, walletPreviewLabel } from '../utils/walletChargeForm' import { m } from '@/utils/metalLabels'
const props = defineProps<{ const props = defineProps<{
variant: 'money' | 'gold'
modelValue: Charge modelValue: Charge
targets: WalletChargeTarget[]
}>() }>()
const formRef = ref<FormInstance>() const formRef = ref<FormInstance>()
const selectedTarget = computed(() => const unit = computed(() => (props.variant === 'money' ? 'ریال' : 'گرم'))
resolveChargeTarget(props.targets, props.modelValue.chargeTargetKey)
)
const isMoney = computed(() => selectedTarget.value?.kind === 'money')
const unit = computed(() => selectedTarget.value?.unit ?? 'ریال')
const isDischarge = computed(() => props.modelValue.direction === 'discharge') const isDischarge = computed(() => props.modelValue.direction === 'discharge')
const targetOptions = computed(() =>
props.targets.map((target) => ({
value: target.key,
label: target.label
}))
)
const targetRules = [
{
validator: (_rule: unknown, value: string | undefined) => {
if (!value || !resolveChargeTarget(props.targets, value)) {
return Promise.reject('کیف پول یا محصول را انتخاب کنید')
}
return Promise.resolve()
}
}
]
const formClass = computed(() => const formClass = computed(() =>
isDischarge.value ? 'wallet-charge-form--discharge' : 'wallet-charge-form--charge' isDischarge.value ? 'wallet-charge-form--discharge' : 'wallet-charge-form--charge'
) )
@@ -117,7 +82,8 @@ const previewSign = computed(() => (isDischarge.value ? '−' : '+'))
const previewText = computed(() => { const previewText = computed(() => {
const action = isDischarge.value ? 'کسر از' : 'افزایش' const action = isDischarge.value ? 'کسر از' : 'افزایش'
return `${action} ${walletPreviewLabel(selectedTarget.value)}` const wallet = props.variant === 'money' ? 'موجودی ریالی' : m('موجودی طلایی')
return `${action} ${wallet} کاربر`
}) })
const amountLabel = computed(() => const amountLabel = computed(() =>
@@ -127,7 +93,7 @@ const amountLabel = computed(() =>
const noteLabel = computed(() => (isDischarge.value ? 'علت دشارژ' : 'علت شارژ')) const noteLabel = computed(() => (isDischarge.value ? 'علت دشارژ' : 'علت شارژ'))
const amountPlaceholder = computed(() => const amountPlaceholder = computed(() =>
isMoney.value ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵' props.variant === 'money' ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵'
) )
const notePlaceholder = computed(() => const notePlaceholder = computed(() =>
@@ -147,28 +113,6 @@ const amountRules = [
} }
] ]
function filterTarget(input: string, option?: { label?: string }) {
return String(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
}
function onTargetChange(key: string) {
const target = resolveChargeTarget(props.targets, key)
if (!target) return
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, target))
}
watch(
() => props.targets,
(targets) => {
if (!targets.length) return
const current = resolveChargeTarget(targets, props.modelValue.chargeTargetKey)
if (!current) {
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, targets[0]))
}
},
{ immediate: true }
)
defineExpose({ defineExpose({
validateFields: () => formRef.value?.validateFields() validateFields: () => formRef.value?.validateFields()
}) })
@@ -49,10 +49,14 @@
:color="record.active ? '#54AD06' : '#ad0627'" :color="record.active ? '#54AD06' : '#ad0627'"
/> />
</a-tooltip> </a-tooltip>
<a-tooltip placement="bottom" class="cursor-pointer" @click="emits('chargeWallet', record)"> <a-tooltip placement="bottom" class="cursor-pointer" @click="emits('chargeMoney', record)">
<template #title><span>شارژ / دشارژ کیف پول</span></template> <template #title><span>شارژ کیف پول ریالی</span></template>
<Icon icon="vuesax-linear:wallet-add" color="#9333ea" /> <Icon icon="vuesax-linear:wallet-add" color="#9333ea" />
</a-tooltip> </a-tooltip>
<a-tooltip placement="bottom" class="cursor-pointer" @click="emits('chargeGold', record)">
<template #title><span>{{ metalLabel.goldWalletCharge() }}</span></template>
<Icon icon="vuesax-linear:wallet-money" color="#ea580c" />
</a-tooltip>
<a-tooltip placement="bottom" class="cursor-pointer" @click="emits('info', record)"> <a-tooltip placement="bottom" class="cursor-pointer" @click="emits('info', record)">
<template #title><span>مشخصات</span></template> <template #title><span>مشخصات</span></template>
<Icon icon="vuesax-linear:stickynote" color="#10913A" /> <Icon icon="vuesax-linear:stickynote" color="#10913A" />
@@ -96,7 +100,8 @@
@edit="emits('edit', $event)" @edit="emits('edit', $event)"
@archive="handleArchive" @archive="handleArchive"
@toggleActive="(item) => changeCustomerActivationFunction(item.id)" @toggleActive="(item) => changeCustomerActivationFunction(item.id)"
@chargeWallet="emits('chargeWallet', $event)" @chargeMoney="emits('chargeMoney', $event)"
@chargeGold="emits('chargeGold', $event)"
@toggleTrade="(item) => changeCustomerOrderActivationFunction(item.id)" @toggleTrade="(item) => changeCustomerOrderActivationFunction(item.id)"
@confirmUser="openConfirmUser" @confirmUser="openConfirmUser"
/> />
@@ -148,9 +153,10 @@ import { toJalaliNoTime } from '@core/utils/useDataTime'
import type { Customer } from '@/model/customer' import type { Customer } from '@/model/customer'
import { MODAL_ABOVE_ACTION_SHEET_Z_INDEX } from '@/constants/overlayZIndex' import { MODAL_ABOVE_ACTION_SHEET_Z_INDEX } from '@/constants/overlayZIndex'
import { isMobile } from '@core/utils/is' import { isMobile } from '@core/utils/is'
import { metalLabel } from '@/utils/adminMetalLabels'
const [submit, reload] = useBaseTable() const [submit, reload] = useBaseTable()
const emits = defineEmits(['delete', 'edit', 'add', 'info', 'chargeWallet']) const emits = defineEmits(['delete', 'edit', 'add', 'info', 'chargeMoney', 'chargeGold'])
const loading = ref(false) const loading = ref(false)
const confirmVisible = ref(false) const confirmVisible = ref(false)
+80 -65
View File
@@ -6,7 +6,8 @@
@info="info" @info="info"
@edit="edit" @edit="edit"
@add="add" @add="add"
@chargeWallet="walletCharge" @chargeMoney="walletMoneyCharge"
@chargeGold="walletGoldCharge"
/> />
<!-- <BasicMenu :menu="menu" width="13rem" @onChange="tabSelected" /> <!-- <BasicMenu :menu="menu" width="13rem" @onChange="tabSelected" />
@@ -213,39 +214,60 @@
</BasicActionSheet> </BasicActionSheet>
<BasicActionSheet <BasicActionSheet
v-if="isMobile" v-if="isMobile"
v-model:visible="showWalletCharge" v-model:visible="showMoneyWalletCharge"
:title="walletChargeModalTitle" :title="moneyChargeTitle"
modal-width="26rem" modal-width="24rem"
:loading="loading" :loading="loading"
:show-button="true" :show-button="true"
:show-cancel="true" :show-cancel="true"
:show-ok="true" :show-ok="true"
:ok-text="walletChargeOkLabel" :ok-text="moneyChargeOkText"
cancel-text="انصراف" cancel-text="انصراف"
@ok="submitWalletCharge" @ok="chargeMoneyWallet"
@close="closeWalletCharge" @close="showMoneyWalletCharge = false"
> >
<WalletChargeForm <WalletChargeForm ref="moneyChargeFormRef" :model-value="formCharge" variant="money" />
ref="walletChargeFormRef"
:model-value="formCharge"
:targets="chargeTargets"
/>
</BasicActionSheet> </BasicActionSheet>
<BasicModal <BasicModal
v-else v-else
v-model:visible="showWalletCharge" v-model:visible="showMoneyWalletCharge"
width="24rem" width="22rem"
:title="walletChargeModalTitle" :title="moneyChargeTitle"
:ok-text="walletChargeOkLabel" :ok-text="moneyChargeOkText"
@ok="submitWalletCharge" @ok="chargeMoneyWallet"
:loading="loading" :loading="loading"
@close="closeWalletCharge" @close="showMoneyWalletCharge = false"
> >
<WalletChargeForm <WalletChargeForm ref="moneyChargeFormRef" :model-value="formCharge" variant="money" />
ref="walletChargeFormRef" </BasicModal>
:model-value="formCharge"
:targets="chargeTargets" <BasicActionSheet
/> v-if="isMobile"
v-model:visible="showGoldWalletCharge"
:title="goldChargeTitle"
modal-width="24rem"
:loading="loading"
:show-button="true"
:show-cancel="true"
:show-ok="true"
:ok-text="goldChargeOkText"
cancel-text="انصراف"
@ok="chargeGoldWallet"
@close="showGoldWalletCharge = false"
>
<WalletChargeForm ref="goldChargeFormRef" :model-value="formCharge" variant="gold" />
</BasicActionSheet>
<BasicModal
v-else
v-model:visible="showGoldWalletCharge"
width="22rem"
:title="goldChargeTitle"
:ok-text="goldChargeOkText"
@ok="chargeGoldWallet"
:loading="loading"
@close="showGoldWalletCharge = false"
>
<WalletChargeForm ref="goldChargeFormRef" :model-value="formCharge" variant="gold" />
</BasicModal> </BasicModal>
<BasicInfoModal <BasicInfoModal
v-model:visible="userInfoOrderVisible" v-model:visible="userInfoOrderVisible"
@@ -277,9 +299,9 @@ import { cloneDeep } from 'lodash'
import type { Item } from '@/model/Item' import type { Item } from '@/model/Item'
import type { Customer } from '@/model/customer' import type { Customer } from '@/model/customer'
import { createCustomer, deleteCustomer, updateCustomer } from '@/api/customer' import { createCustomer, deleteCustomer, updateCustomer } from '@/api/customer'
import type { Charge, WalletChargeTarget } from '@/model/wallet' import type { Charge } from '@/model/wallet'
import customers from '@/router/routes/customers' import customers from '@/router/routes/customers'
import { chargeWalletUnified, getWalletChargeTargets } from '@/api/wallet' import { chargeWallet } from '@/api/wallet'
import { usePrice } from '@core/utils/usePrice' import { usePrice } from '@core/utils/usePrice'
import jalaliday from 'jalaliday' import jalaliday from 'jalaliday'
import ordersTbl from '@/pages/menuPages/orders/components/table.vue' import ordersTbl from '@/pages/menuPages/orders/components/table.vue'
@@ -292,10 +314,8 @@ import Icon from '@core/components/BaseIcon/src/Icon.vue'
import { isMobile } from '@core/utils/is' import { isMobile } from '@core/utils/is'
import WalletChargeForm from './components/WalletChargeForm.vue' import WalletChargeForm from './components/WalletChargeForm.vue'
import { import {
applyChargeTarget,
buildChargePayload, buildChargePayload,
createEmptyChargeForm, createEmptyChargeForm,
resolveChargeTarget,
walletChargeOkText, walletChargeOkText,
walletChargeSuccessMessage, walletChargeSuccessMessage,
walletChargeTitle walletChargeTitle
@@ -354,23 +374,21 @@ const customerItems: Array<Item> = [
] ]
//wallet //wallet
const showWalletCharge = ref<boolean>(false) const showMoneyWalletCharge = ref<boolean>(false)
const chargeTargets = ref<WalletChargeTarget[]>([]) const showGoldWalletCharge = ref<boolean>(false)
const userInfoOrderVisible = ref<boolean>(false) const userInfoOrderVisible = ref<boolean>(false)
//form //form
const form = ref<Partial<Customer>>({}) const form = ref<Partial<Customer>>({})
const formRef = ref<FormInstance>() const formRef = ref<FormInstance>()
const formCharge = ref<Charge>({}) const formCharge = ref<Charge>({})
const walletChargeFormRef = ref<InstanceType<typeof WalletChargeForm>>() const moneyChargeFormRef = ref<InstanceType<typeof WalletChargeForm>>()
const goldChargeFormRef = ref<InstanceType<typeof WalletChargeForm>>()
const selectedChargeTarget = computed(() => const moneyChargeTitle = computed(() => walletChargeTitle('money', formCharge.value.direction))
resolveChargeTarget(chargeTargets.value, formCharge.value.chargeTargetKey) const goldChargeTitle = computed(() => walletChargeTitle('gold', formCharge.value.direction))
) const moneyChargeOkText = computed(() => walletChargeOkText(formCharge.value.direction))
const walletChargeModalTitle = computed(() => const goldChargeOkText = computed(() => walletChargeOkText(formCharge.value.direction))
walletChargeTitle(selectedChargeTarget.value, formCharge.value.direction)
)
const walletChargeOkLabel = computed(() => walletChargeOkText(formCharge.value.direction))
//info //info
let infoParams = reactive<Info>({}) let infoParams = reactive<Info>({})
@@ -491,43 +509,40 @@ async function add() {
function resetFormCharge(customerId: number) { function resetFormCharge(customerId: number) {
formCharge.value = createEmptyChargeForm(customerId) formCharge.value = createEmptyChargeForm(customerId)
const firstTarget = chargeTargets.value[0]
if (firstTarget) {
Object.assign(formCharge.value, applyChargeTarget(formCharge.value, firstTarget))
}
} }
async function loadChargeTargets() { async function walletMoneyCharge(val: Customer) {
if (chargeTargets.value.length) return resetFormCharge(val.id)
const data = await getWalletChargeTargets() showMoneyWalletCharge.value = true
chargeTargets.value = data?.targets ?? []
} }
async function walletCharge(val: Customer) { async function walletGoldCharge(val: Customer) {
try { resetFormCharge(val.id)
loading.value = true showGoldWalletCharge.value = true
await loadChargeTargets()
resetFormCharge(val.id)
showWalletCharge.value = true
} catch (e) {
console.error(e)
message.error('خطا در بارگذاری گزینه‌های شارژ')
} finally {
loading.value = false
}
} }
function closeWalletCharge() { async function chargeMoneyWallet() {
showWalletCharge.value = false moneyChargeFormRef.value?.validateFields()?.then(async () => {
}
async function submitWalletCharge() {
walletChargeFormRef.value?.validateFields()?.then(async () => {
try { try {
loading.value = true loading.value = true
await chargeWalletUnified(buildChargePayload(formCharge.value)) await chargeWallet(buildChargePayload(formCharge.value), 'money')
message.success(walletChargeSuccessMessage(formCharge.value.direction)) message.success(walletChargeSuccessMessage(formCharge.value.direction))
showWalletCharge.value = false showMoneyWalletCharge.value = false
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
})
}
async function chargeGoldWallet() {
goldChargeFormRef.value?.validateFields()?.then(async () => {
try {
loading.value = true
await chargeWallet(buildChargePayload(formCharge.value), 'gold')
message.success(walletChargeSuccessMessage(formCharge.value.direction))
showGoldWalletCharge.value = false
} catch (e) { } catch (e) {
console.log(e) console.log(e)
} finally { } finally {
@@ -1,4 +1,5 @@
import type { Charge, WalletChargeDirection, WalletChargeTarget } from '@/model/wallet' import type { Charge, WalletChargeDirection } from '@/model/wallet'
import { m } from '@/utils/metalLabels'
export function createEmptyChargeForm(customerId?: number): Charge { export function createEmptyChargeForm(customerId?: number): Charge {
return { return {
@@ -7,9 +8,7 @@ export function createEmptyChargeForm(customerId?: number): Charge {
note: undefined, note: undefined,
talabBedehi: false, talabBedehi: false,
direction: 'charge', direction: 'charge',
amountAbs: undefined, amountAbs: undefined
chargeTargetKey: 'money',
product_id: null
} }
} }
@@ -20,35 +19,17 @@ export function buildChargePayload(form: Charge): Charge {
value: signed, value: signed,
note: form.note, note: form.note,
customer_id: form.customer_id, customer_id: form.customer_id,
talabBedehi: form.talabBedehi === true, talabBedehi: form.talabBedehi === true
product_id: form.product_id ?? null
}
}
export function resolveChargeTarget(
targets: WalletChargeTarget[],
key?: string | null
): WalletChargeTarget | undefined {
if (!key) return targets[0]
return targets.find((target) => target.key === key)
}
export function applyChargeTarget(form: Charge, target?: WalletChargeTarget): Charge {
if (!target) return form
return {
...form,
chargeTargetKey: target.key,
product_id: target.kind === 'metal' ? target.product_id ?? null : null
} }
} }
export function walletChargeTitle( export function walletChargeTitle(
target: WalletChargeTarget | undefined, variant: 'money' | 'gold',
direction: WalletChargeDirection = 'charge' direction: WalletChargeDirection = 'charge'
) { ) {
const wallet = variant === 'money' ? 'ریالی' : m('طلایی')
const action = direction === 'discharge' ? 'دشارژ' : 'شارژ' const action = direction === 'discharge' ? 'دشارژ' : 'شارژ'
const wallet = target?.label ?? 'کیف پول' return `${action} کیف پول ${wallet}`
return `${action} ${wallet}`
} }
export function walletChargeOkText(direction: WalletChargeDirection = 'charge') { export function walletChargeOkText(direction: WalletChargeDirection = 'charge') {
@@ -58,10 +39,3 @@ export function walletChargeOkText(direction: WalletChargeDirection = 'charge')
export function walletChargeSuccessMessage(direction: WalletChargeDirection = 'charge') { export function walletChargeSuccessMessage(direction: WalletChargeDirection = 'charge') {
return direction === 'discharge' ? 'دشارژ با موفقیت انجام شد' : 'شارژ با موفقیت انجام شد' return direction === 'discharge' ? 'دشارژ با موفقیت انجام شد' : 'شارژ با موفقیت انجام شد'
} }
export function walletPreviewLabel(target: WalletChargeTarget | undefined): string {
if (!target) return 'موجودی کاربر'
if (target.kind === 'money') return 'موجودی ریالی'
if (target.uses_main_wallet) return `موجودی ${target.label.replace(/\s*\(کیف اصلی\)$/, '')} (کیف اصلی)`
return `موجودی ${target.label}`
}