Files
boursecu_admin/src/pages/menuPages/customers/base/components/CustomerAccountingCodeModal.vue
T

178 lines
5.5 KiB
Vue
Raw Normal View History

<template>
<BasicModal
v-model:visible="visibleProxy"
:width="isMobile ? '92vw' : '28rem'"
title="ویرایش کد حسابداری"
ok-text="ذخیره"
:loading="loading"
@ok="submit"
@close="close"
>
<div class="flex flex-col gap-3">
<p class="m-0 text-sm" style="color: var(--admin-text-secondary)">
مشتری:
<strong class="text-[var(--admin-text)]">{{ customer?.name || '—' }}</strong>
</p>
<a-form layout="vertical">
<a-form-item label="کد حسابداری" class="mb-0">
<BasicInput
v-model:value="code"
placeholder="کد حسابداری را وارد کنید"
dir="ltr"
class="text-left"
/>
</a-form-item>
</a-form>
<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">
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
customer: Customer | null
provider: AccountingProvider
}>()
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
(e: 'success'): void
}>()
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,
set: (value: boolean) => emit('update:visible', value),
})
function currentCodeOf(customer: Customer | null): string {
if (!customer) return ''
if (props.provider === 'pargar') return displayAccountingCode(customer.pargar_account_id)
2026-07-23 22:24:26 +03:30
if (props.provider === 'tahesab' || props.provider === 'zarin') return displayAccountingCode(customer.moshtari_code)
return ''
}
watch(
() => [props.visible, props.customer, props.provider] as const,
([visible]) => {
if (!visible) return
const shown = currentCodeOf(props.customer)
code.value = shown === '-' ? '' : shown
}
)
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
2026-07-23 22:24:26 +03:30
if (props.provider !== 'pargar' && props.provider !== 'tahesab' && props.provider !== 'zarin') {
message.error('حسابداری فعال نیست.')
return
}
const previousCode = normalizeAccountingCode(currentCodeOf(props.customer))
const normalized = normalizeAccountingCode(code.value)
const payload =
props.provider === 'pargar'
? { pargar_account_id: normalized }
: { moshtari_code: normalized }
try {
loading.value = true
await updateCustomer(props.customer.id, payload as Customer)
message.success('کد حسابداری ذخیره شد.')
emit('success')
close()
await maybeSyncTahesabWallet(props.customer.id, previousCode, normalized)
} catch (e) {
console.error(e)
} finally {
loading.value = false
}
}
</script>