From 9f47a709c07283fcf970207d36a0111dca68f234 Mon Sep 17 00:00:00 2001 From: puriphatt Date: Thu, 29 Aug 2024 10:38:20 +0700 Subject: [PATCH] fix: subbranch code when create --- src/pages/01_branch-management/MainPage.vue | 52 +++++++++------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/pages/01_branch-management/MainPage.vue b/src/pages/01_branch-management/MainPage.vue index 3b52563f..523a39c9 100644 --- a/src/pages/01_branch-management/MainPage.vue +++ b/src/pages/01_branch-management/MainPage.vue @@ -131,7 +131,7 @@ const imageDialog = ref(false); const qrFile = ref(undefined); const qrCodeimageUrl = ref(''); -const formBranchCount = ref(0); +const formLastSubBranch = ref(0); const inputFile = (() => { const element = document.createElement('input'); @@ -297,11 +297,11 @@ const formDialogRef = ref(); const formType = ref<'create' | 'edit' | 'delete' | 'view'>('create'); const formTypeBranch = ref<'headOffice' | 'subBranch'>('headOffice'); -const currentHq = ref<{ id: string; code: string; count?: number }>({ +const currentHq = ref<{ id: string; code: string }>({ id: '', code: '', }); -const currentEdit = ref<{ id: string; code: string; count?: number }>({ +const currentEdit = ref<{ id: string; code: string }>({ id: '', code: '', }); @@ -394,7 +394,6 @@ function triggerCreate( type: 'headOffice' | 'subBranch', id?: string, code?: string, - count?: number, ) { clearData(); @@ -404,7 +403,6 @@ function triggerCreate( currentHq.value = { id: id ?? '', code: code ?? '', - count: count, }; } @@ -413,13 +411,24 @@ function triggerCreate( formData.value.headOfficeId = id; formData.value.code = code; formData.value.codeHeadOffice = code; - formBranchCount.value = count || 0; + formLastSubBranch.value = findLastSub(code); } - formType.value = 'create'; openDialog(); } +function findLastSub(code: string) { + const abbrev = code.slice(0, -5); + const filteredArray = branchData.value.result.filter((v) => + v.code.includes(abbrev), + ); + const maxCodeNum = filteredArray.reduce((max, v) => { + const codeNum = parseInt(v.code.slice(-5), 10); + return codeNum > max ? codeNum : max; + }, 0); + return maxCodeNum; +} + function drawerEdit() { isImageEdit.value = true; formType.value = 'edit'; @@ -458,7 +467,6 @@ async function triggerEdit( currentEdit.value = { id: currentRecord.id, code: currentRecord.code, - count: currentRecord._count, }; if (typeBranch === 'subBranch') { @@ -471,7 +479,6 @@ async function triggerEdit( if (currentRecordHead) { currentHq.value.id = currentRecordHead.id; currentHq.value.code = currentRecordHead.code; - currentHq.value.count = currentRecordHead._count.branch; } else { currentHq.value = currentEdit.value; } @@ -802,7 +809,6 @@ watch(currentHq, () => { 'subBranch', currentHq.id, currentHq.code, - currentHq.count, ); } } @@ -857,7 +863,6 @@ watch(currentHq, () => { currentHq = { id: v.id, code: v.code, - count: v._count.branch, }; beforeBranch = { id: '', @@ -866,15 +871,7 @@ watch(currentHq, () => { } } " - @create=" - (v) => - triggerCreate( - 'subBranch', - v.id, - v.code, - v._count.branch, - ) - " + @create="(v) => triggerCreate('subBranch', v.id, v.code)" @view=" (v) => { if (v.isHeadOffice) { @@ -1123,7 +1120,6 @@ watch(currentHq, () => { currentHq = { id: props.row.id, code: props.row.code, - count: props.row._count.branch, }; beforeBranch = { id: '', @@ -1287,7 +1283,6 @@ watch(currentHq, () => { currentHq = { id: props.row.id, code: props.row.code, - count: props.row._count.branch, }; beforeBranch = { id: '', @@ -1411,7 +1406,7 @@ watch(currentHq, () => { " :close=" () => ( - (formBranchCount = 0), + (formLastSubBranch = 0), (modal = false), (profileFile = undefined), (isImageEdit = false), @@ -1430,7 +1425,7 @@ watch(currentHq, () => { :caption=" formTypeBranch === 'headOffice' ? `${formData.code}00000` - : `${formData.code?.slice(0, -5)}${(formBranchCount + 1).toString().padStart(5, '0')}` + : `${formData.code?.slice(0, -5)}${(formLastSubBranch + 1).toString().padStart(5, '0')}` " v-model:toggle-status="formData.status" v-model:cover-url="imageUrl" @@ -1510,7 +1505,7 @@ watch(currentHq, () => {
{ v-ripple v-close-popup @click.stop=" - triggerCreate( - 'subBranch', - currentNode.id, - currentNode.code, - currentNode._count.branch, - ) + triggerCreate('subBranch', currentNode.id, currentNode.code) " >