Sync Tahesab wallet balance on import and let admins resolve conflicts.

Add customer search on the Tahesab-only list, bulk/per-customer mande sync UI, and a decision modal when app balance differs from Tahesab.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-30 14:16:36 +03:30
co-authored by Cursor
parent 084b449177
commit 04cc03fe25
7 changed files with 761 additions and 8 deletions
@@ -26,8 +26,24 @@
<p class="m-0 text-xs" style="color: var(--admin-text-secondary)">
برای پاک کردن کد، فیلد را خالی بگذارید و ذخیره کنید.
</p>
<p
v-if="provider === 'tahesab'"
class="m-0 text-xs"
style="color: var(--admin-text-secondary)"
>
بعد از ذخیره کد ته‌حساب، اگر موجودی اپ با مانده ته‌حساب فرق داشته باشد از شما تصمیم گرفته می‌شود.
</p>
</div>
</BasicModal>
<TahesabMandeDecisionModal
v-model:visible="decisionVisible"
:app-gold="decision?.app_gold"
:app-cash="decision?.app_cash"
:tahesab-gold="decision?.tahesab_gold"
:tahesab-cash="decision?.tahesab_cash"
@decide="onDecide"
/>
</template>
<script setup lang="ts">
@@ -35,10 +51,15 @@ import { computed, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import BasicModal from '@/components/BasicModal.vue'
import { updateCustomer } from '@/api/customer'
import {
syncCustomerWalletFromTahesab,
type TahesabWalletSyncResult,
} from '@/api/tahesab'
import type { Customer } from '@/model/customer'
import type { AccountingProvider } from '@/utils/config'
import { displayAccountingCode, normalizeAccountingCode } from '@/utils/accountingCode'
import { isMobile } from '@core/utils/is'
import TahesabMandeDecisionModal from './TahesabMandeDecisionModal.vue'
const props = defineProps<{
visible: boolean
@@ -53,6 +74,9 @@ const emit = defineEmits<{
const loading = ref(false)
const code = ref('')
const decisionVisible = ref(false)
const decision = ref<TahesabWalletSyncResult['result'] | null>(null)
const savedCustomerId = ref<number | null>(null)
const visibleProxy = computed({
get: () => props.visible,
@@ -79,6 +103,50 @@ function close() {
visibleProxy.value = false
}
async function maybeSyncTahesabWallet(customerId: number, previousCode: string, nextCode: string) {
if (props.provider !== 'tahesab') return
if (!nextCode || nextCode === previousCode) return
try {
const res = await syncCustomerWalletFromTahesab(customerId, { ask_if_conflict: true })
if (res.needs_decision || res.result?.needs_decision) {
decision.value = res.result ?? null
savedCustomerId.value = customerId
decisionVisible.value = true
return
}
if (res.result?.applied) {
message.success(res.message || 'مانده ته‌حساب روی کیف اعمال شد')
} else if (res.message) {
message.info(res.message)
}
} catch (e) {
// خطای همگام‌سازی مانع ذخیره کد نیست
console.error(e)
}
}
async function onDecide(mode: 'overwrite' | 'keep') {
const customerId = savedCustomerId.value
decisionVisible.value = false
if (!customerId) return
try {
const res = await syncCustomerWalletFromTahesab(customerId, {
overwrite: mode === 'overwrite',
keep: mode === 'keep',
ask_if_conflict: false,
})
if (mode === 'keep') {
message.info(res.message || 'موجودی اپ حفظ شد')
} else {
message.success(res.message || 'مانده ته‌حساب روی کیف اعمال شد')
}
emit('success')
} catch (e) {
console.error(e)
}
}
async function submit() {
if (!props.customer?.id) return
if (props.provider !== 'pargar' && props.provider !== 'tahesab' && props.provider !== 'zarin') {
@@ -86,6 +154,7 @@ async function submit() {
return
}
const previousCode = normalizeAccountingCode(currentCodeOf(props.customer))
const normalized = normalizeAccountingCode(code.value)
const payload =
props.provider === 'pargar'
@@ -98,6 +167,7 @@ async function submit() {
message.success('کد حسابداری ذخیره شد.')
emit('success')
close()
await maybeSyncTahesabWallet(props.customer.id, previousCode, normalized)
} catch (e) {
console.error(e)
} finally {
@@ -0,0 +1,97 @@
<template>
<BasicModal
v-model:visible="visibleProxy"
:width="isMobile ? '92vw' : '28rem'"
title="تصمیم برای مانده حساب"
:custom-footer="true"
cancel-text="بعداً"
@close="close"
>
<div class="flex flex-col gap-3 text-sm" style="color: var(--admin-text)">
<p class="m-0" style="color: var(--admin-text-secondary)">
موجودی اپ با مانده ته‌حساب فرق دارد. کدام را اعمال کنیم؟
</p>
<div class="rounded-lg border border-base p-3 space-y-2" style="background: var(--admin-bg-muted)">
<div class="flex justify-between gap-2">
<span style="color: var(--admin-text-secondary)">موجودی اپ (طلا)</span>
<strong dir="ltr">{{ formatGold(appGold) }}</strong>
</div>
<div class="flex justify-between gap-2">
<span style="color: var(--admin-text-secondary)">مانده ته‌حساب (طلا)</span>
<strong dir="ltr">{{ formatGold(tahesabGold) }}</strong>
</div>
<div class="h-px" style="background: var(--admin-border)" />
<div class="flex justify-between gap-2">
<span style="color: var(--admin-text-secondary)">موجودی اپ (ریال)</span>
<strong dir="ltr">{{ formatCash(appCash) }}</strong>
</div>
<div class="flex justify-between gap-2">
<span style="color: var(--admin-text-secondary)">مانده ته‌حساب (ریال)</span>
<strong dir="ltr">{{ formatCash(tahesabCash) }}</strong>
</div>
</div>
<div class="flex flex-col gap-2">
<a-button type="primary" block :loading="loading === 'overwrite'" :disabled="!!loading" @click="choose('overwrite')">
اعمال مانده ته‌حساب روی کیف
</a-button>
<a-button block :loading="loading === 'keep'" :disabled="!!loading" @click="choose('keep')">
نگه داشتن موجودی فعلی اپ
</a-button>
<a-button type="link" block :disabled="!!loading" @click="close">بعداً</a-button>
</div>
</div>
<template #footer><span /></template>
</BasicModal>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import BasicModal from '@/components/BasicModal.vue'
import { isMobile } from '@core/utils/is'
const props = defineProps<{
visible: boolean
appGold?: number | null
appCash?: number | null
tahesabGold?: number | null
tahesabCash?: number | null
}>()
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
(e: 'decide', mode: 'overwrite' | 'keep'): void
}>()
const loading = ref<'overwrite' | 'keep' | null>(null)
const visibleProxy = computed({
get: () => props.visible,
set: (value: boolean) => emit('update:visible', value),
})
watch(
() => props.visible,
(v) => {
if (!v) loading.value = null
}
)
function formatGold(v?: number | null) {
if (v == null || Number.isNaN(Number(v))) return '—'
return Number(v).toLocaleString('fa-IR', { maximumFractionDigits: 3 })
}
function formatCash(v?: number | null) {
if (v == null || Number.isNaN(Number(v))) return '—'
return Math.round(Number(v)).toLocaleString('fa-IR')
}
function close() {
visibleProxy.value = false
}
function choose(mode: 'overwrite' | 'keep') {
loading.value = mode
emit('decide', mode)
}
</script>
@@ -0,0 +1,72 @@
<template>
<div class="tahesab-wallet-sync">
<div class="tahesab-wallet-sync__head">
<div class="tahesab-wallet-sync__icon" aria-hidden="true">
<Icon icon="vuesax-linear:refresh-circle" :size="18" color="var(--admin-primary, #10913A)" />
</div>
<div class="tahesab-wallet-sync__copy min-w-0 flex-1">
<p class="tahesab-wallet-sync__title">همگام‌سازی با ته‌حساب</p>
<p class="tahesab-wallet-sync__hint">
{{
canSync
? 'مانده طلا و ریال ته‌حساب را با کیف اپ مقایسه می‌کند؛ اگر تداخل باشد شما تصمیم می‌گیرید'
: 'ابتدا کد حسابداری ته‌حساب را برای این مشتری ثبت کنید'
}}
</p>
</div>
</div>
<a-button
type="primary"
block
size="small"
class="tahesab-wallet-sync__btn"
:loading="loading"
:disabled="!canSync || loading"
@click="$emit('sync', 'ask')"
>
بررسی و همگام‌سازی مانده
</a-button>
</div>
</template>
<script setup lang="ts">
import Icon from '@core/components/BaseIcon/src/Icon.vue'
defineProps<{
loading?: boolean
canSync?: boolean
}>()
defineEmits<{
(e: 'sync', mode: 'ask' | 'overwrite' | 'keep'): void
}>()
</script>
<style scoped>
.tahesab-wallet-sync {
@apply mb-2.5 rounded-xl border border-base p-2.5;
background: var(--admin-surface-muted, var(--admin-bg-muted, #f8fafc));
}
.tahesab-wallet-sync__head {
@apply mb-2.5 flex items-start gap-2;
}
.tahesab-wallet-sync__icon {
@apply flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-gray-50;
}
.tahesab-wallet-sync__title {
@apply m-0 text-sm font-700;
color: var(--admin-text, inherit);
}
.tahesab-wallet-sync__hint {
@apply m-0 mt-0.5 text-xs leading-5;
color: var(--admin-text-secondary, #6b7280);
}
.tahesab-wallet-sync__btn {
@apply font-600;
}
</style>
@@ -197,6 +197,12 @@
:can-sync="pargarWalletCanSync"
@sync="syncPargarWallet"
/>
<TahesabWalletSyncAction
v-if="tahesabWalletSyncVisible"
:loading="tahesabSyncLoading"
:can-sync="tahesabWalletCanSync"
@sync="syncTahesabWallet"
/>
<CustomerWalletBalancePanel
:key="`wallet-mobile-${walletPanelKey}`"
ref="walletPanelMobile"
@@ -218,6 +224,12 @@
:can-sync="pargarWalletCanSync"
@sync="syncPargarWallet"
/>
<TahesabWalletSyncAction
v-if="tahesabWalletSyncVisible"
:loading="tahesabSyncLoading"
:can-sync="tahesabWalletCanSync"
@sync="syncTahesabWallet"
/>
<CustomerWalletBalancePanel
:key="`wallet-desktop-${walletPanelKey}`"
ref="walletPanelDesktop"
@@ -275,6 +287,14 @@
:customer-id="chargeCustomerId"
@success="onWalletChargeSuccess"
/>
<TahesabMandeDecisionModal
v-model:visible="tahesabDecisionVisible"
:app-gold="tahesabDecision?.app_gold"
:app-cash="tahesabDecision?.app_cash"
:tahesab-gold="tahesabDecision?.tahesab_gold"
:tahesab-cash="tahesabDecision?.tahesab_cash"
@decide="onTahesabMandeDecide"
/>
<BasicInfoModal
v-model:visible="userInfoOrderVisible"
@close="userInfoOrderVisible = false"
@@ -338,7 +358,13 @@ import {
import { displayAccountingCode, isValidAccountingCode } from '@/utils/accountingCode'
import CustomerPresenceBadge from '@/pages/menuPages/customers/base/components/CustomerPresenceBadge.vue'
import { syncCustomerWalletFromPargar, type PargarSyncWalletResult } from '@/api/pargar'
import {
syncCustomerWalletFromTahesab,
type TahesabWalletSyncResult,
} from '@/api/tahesab'
import PargarWalletSyncAction from './components/PargarWalletSyncAction.vue'
import TahesabWalletSyncAction from './components/TahesabWalletSyncAction.vue'
import TahesabMandeDecisionModal from './components/TahesabMandeDecisionModal.vue'
const fnameHelp: DashboardHelp = {
description: 'نام کوچک مشتری که در اپ و پنل نمایش داده می‌شود.',
@@ -734,6 +760,9 @@ const accountingCodeValue = computed(() => {
})
const pargarSyncLoading = ref(false)
const tahesabSyncLoading = ref(false)
const tahesabDecisionVisible = ref(false)
const tahesabDecision = ref<TahesabWalletSyncResult['result'] | null>(null)
const walletPanelDesktop = ref<{ reload?: () => void | Promise<void> } | null>(null)
const walletPanelMobile = ref<{ reload?: () => void | Promise<void> } | null>(null)
const walletPanelKey = ref(0)
@@ -743,6 +772,10 @@ const pargarWalletSyncVisible = computed(
() => accountingProviderSetting.value === 'pargar'
)
const tahesabWalletSyncVisible = computed(
() => accountingProviderSetting.value === 'tahesab'
)
const tahesabDocsVisible = computed(() => accountingProviderSetting.value === 'tahesab')
const zarinDocsVisible = computed(() => accountingProviderSetting.value === 'zarin')
@@ -750,6 +783,10 @@ const pargarWalletCanSync = computed(() =>
isValidAccountingCode(userInfo.value?.pargar_account_id)
)
const tahesabWalletCanSync = computed(() =>
isValidAccountingCode(userInfo.value?.moshtari_code)
)
async function refreshWalletPanels() {
await nextTick()
walletPanelKey.value += 1
@@ -760,6 +797,58 @@ async function refreshWalletPanels() {
])
}
function openTahesabDecision(result?: TahesabWalletSyncResult['result']) {
tahesabDecision.value = result ?? null
tahesabDecisionVisible.value = true
}
async function syncTahesabWallet(mode: 'ask' | 'overwrite' | 'keep' = 'ask') {
if (!userInfo.value?.id || !tahesabWalletCanSync.value || tahesabSyncLoading.value) return
tahesabSyncLoading.value = true
try {
const res = await syncCustomerWalletFromTahesab(userInfo.value.id, {
ask_if_conflict: mode === 'ask',
overwrite: mode === 'overwrite',
keep: mode === 'keep',
})
if (res.needs_decision || res.result?.needs_decision) {
openTahesabDecision(res.result)
return
}
if (res.result?.applied || res.ok) {
message.success(res.message || 'مانده از ته‌حساب روی کیف اعمال شد')
} else {
message.info(res.message || 'تغییری برای اعمال نبود')
}
await refreshWalletPanels()
refreshTransactionTable()
refreshTable()
} catch (e: unknown) {
const data = (e as { response?: { data?: TahesabWalletSyncResult } })?.response?.data
const status = (e as { response?: { status?: number } })?.response?.status
await refreshWalletPanels()
refreshTransactionTable()
refreshTable()
if (data?.result?.applied || data?.ok) {
message.success(data?.message || 'مانده از ته‌حساب روی کیف اعمال شد')
} else if (status !== 422) {
message.warning(data?.message || 'اعمال مانده از ته‌حساب ناموفق بود')
}
} finally {
tahesabSyncLoading.value = false
}
}
async function onTahesabMandeDecide(mode: 'overwrite' | 'keep') {
tahesabDecisionVisible.value = false
await syncTahesabWallet(mode)
}
async function syncPargarWallet(force = false) {
if (!userInfo.value?.id || !pargarWalletCanSync.value || pargarSyncLoading.value) return
pargarSyncLoading.value = true