2026-07-19 15:49:52 +03:30
|
|
|
|
<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>
|
2026-08-30 14:16:36 +03:30
|
|
|
|
<p
|
|
|
|
|
|
v-if="provider === 'tahesab'"
|
|
|
|
|
|
class="m-0 text-xs"
|
|
|
|
|
|
style="color: var(--admin-text-secondary)"
|
|
|
|
|
|
>
|
|
|
|
|
|
بعد از ذخیره کد تهحساب، اگر موجودی اپ با مانده تهحساب فرق داشته باشد از شما تصمیم گرفته میشود.
|
|
|
|
|
|
</p>
|
2026-07-19 15:49:52 +03:30
|
|
|
|
</div>
|
|
|
|
|
|
</BasicModal>
|
2026-08-30 14:16:36 +03:30
|
|
|
|
|
|
|
|
|
|
<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"
|
|
|
|
|
|
/>
|
2026-07-19 15:49:52 +03:30
|
|
|
|
</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'
|
2026-08-30 14:16:36 +03:30
|
|
|
|
import {
|
|
|
|
|
|
syncCustomerWalletFromTahesab,
|
|
|
|
|
|
type TahesabWalletSyncResult,
|
|
|
|
|
|
} from '@/api/tahesab'
|
2026-07-19 15:49:52 +03:30
|
|
|
|
import type { Customer } from '@/model/customer'
|
|
|
|
|
|
import type { AccountingProvider } from '@/utils/config'
|
|
|
|
|
|
import { displayAccountingCode, normalizeAccountingCode } from '@/utils/accountingCode'
|
|
|
|
|
|
import { isMobile } from '@core/utils/is'
|
2026-08-30 14:16:36 +03:30
|
|
|
|
import TahesabMandeDecisionModal from './TahesabMandeDecisionModal.vue'
|
2026-07-19 15:49:52 +03:30
|
|
|
|
|
|
|
|
|
|
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('')
|
2026-08-30 14:16:36 +03:30
|
|
|
|
const decisionVisible = ref(false)
|
|
|
|
|
|
const decision = ref<TahesabWalletSyncResult['result'] | null>(null)
|
|
|
|
|
|
const savedCustomerId = ref<number | null>(null)
|
2026-07-19 15:49:52 +03:30
|
|
|
|
|
|
|
|
|
|
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)
|
2026-07-19 15:49:52 +03:30
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-30 14:16:36 +03:30
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-19 15:49:52 +03:30
|
|
|
|
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') {
|
2026-07-19 15:49:52 +03:30
|
|
|
|
message.error('حسابداری فعال نیست.')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-30 14:16:36 +03:30
|
|
|
|
const previousCode = normalizeAccountingCode(currentCodeOf(props.customer))
|
2026-07-19 15:49:52 +03:30
|
|
|
|
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()
|
2026-08-30 14:16:36 +03:30
|
|
|
|
await maybeSyncTahesabWallet(props.customer.id, previousCode, normalized)
|
2026-07-19 15:49:52 +03:30
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(e)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|