fix: subbranch code when create

This commit is contained in:
puriphatt 2024-08-29 10:38:20 +07:00
parent a91d783b12
commit 9f47a709c0

View file

@ -131,7 +131,7 @@ const imageDialog = ref(false);
const qrFile = ref<File | undefined>(undefined); const qrFile = ref<File | undefined>(undefined);
const qrCodeimageUrl = ref<string | null>(''); const qrCodeimageUrl = ref<string | null>('');
const formBranchCount = ref<number>(0); const formLastSubBranch = ref<number>(0);
const inputFile = (() => { const inputFile = (() => {
const element = document.createElement('input'); const element = document.createElement('input');
@ -297,11 +297,11 @@ const formDialogRef = ref();
const formType = ref<'create' | 'edit' | 'delete' | 'view'>('create'); const formType = ref<'create' | 'edit' | 'delete' | 'view'>('create');
const formTypeBranch = ref<'headOffice' | 'subBranch'>('headOffice'); const formTypeBranch = ref<'headOffice' | 'subBranch'>('headOffice');
const currentHq = ref<{ id: string; code: string; count?: number }>({ const currentHq = ref<{ id: string; code: string }>({
id: '', id: '',
code: '', code: '',
}); });
const currentEdit = ref<{ id: string; code: string; count?: number }>({ const currentEdit = ref<{ id: string; code: string }>({
id: '', id: '',
code: '', code: '',
}); });
@ -394,7 +394,6 @@ function triggerCreate(
type: 'headOffice' | 'subBranch', type: 'headOffice' | 'subBranch',
id?: string, id?: string,
code?: string, code?: string,
count?: number,
) { ) {
clearData(); clearData();
@ -404,7 +403,6 @@ function triggerCreate(
currentHq.value = { currentHq.value = {
id: id ?? '', id: id ?? '',
code: code ?? '', code: code ?? '',
count: count,
}; };
} }
@ -413,13 +411,24 @@ function triggerCreate(
formData.value.headOfficeId = id; formData.value.headOfficeId = id;
formData.value.code = code; formData.value.code = code;
formData.value.codeHeadOffice = code; formData.value.codeHeadOffice = code;
formBranchCount.value = count || 0; formLastSubBranch.value = findLastSub(code);
} }
formType.value = 'create'; formType.value = 'create';
openDialog(); 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() { function drawerEdit() {
isImageEdit.value = true; isImageEdit.value = true;
formType.value = 'edit'; formType.value = 'edit';
@ -458,7 +467,6 @@ async function triggerEdit(
currentEdit.value = { currentEdit.value = {
id: currentRecord.id, id: currentRecord.id,
code: currentRecord.code, code: currentRecord.code,
count: currentRecord._count,
}; };
if (typeBranch === 'subBranch') { if (typeBranch === 'subBranch') {
@ -471,7 +479,6 @@ async function triggerEdit(
if (currentRecordHead) { if (currentRecordHead) {
currentHq.value.id = currentRecordHead.id; currentHq.value.id = currentRecordHead.id;
currentHq.value.code = currentRecordHead.code; currentHq.value.code = currentRecordHead.code;
currentHq.value.count = currentRecordHead._count.branch;
} else { } else {
currentHq.value = currentEdit.value; currentHq.value = currentEdit.value;
} }
@ -802,7 +809,6 @@ watch(currentHq, () => {
'subBranch', 'subBranch',
currentHq.id, currentHq.id,
currentHq.code, currentHq.code,
currentHq.count,
); );
} }
} }
@ -857,7 +863,6 @@ watch(currentHq, () => {
currentHq = { currentHq = {
id: v.id, id: v.id,
code: v.code, code: v.code,
count: v._count.branch,
}; };
beforeBranch = { beforeBranch = {
id: '', id: '',
@ -866,15 +871,7 @@ watch(currentHq, () => {
} }
} }
" "
@create=" @create="(v) => triggerCreate('subBranch', v.id, v.code)"
(v) =>
triggerCreate(
'subBranch',
v.id,
v.code,
v._count.branch,
)
"
@view=" @view="
(v) => { (v) => {
if (v.isHeadOffice) { if (v.isHeadOffice) {
@ -1123,7 +1120,6 @@ watch(currentHq, () => {
currentHq = { currentHq = {
id: props.row.id, id: props.row.id,
code: props.row.code, code: props.row.code,
count: props.row._count.branch,
}; };
beforeBranch = { beforeBranch = {
id: '', id: '',
@ -1287,7 +1283,6 @@ watch(currentHq, () => {
currentHq = { currentHq = {
id: props.row.id, id: props.row.id,
code: props.row.code, code: props.row.code,
count: props.row._count.branch,
}; };
beforeBranch = { beforeBranch = {
id: '', id: '',
@ -1411,7 +1406,7 @@ watch(currentHq, () => {
" "
:close=" :close="
() => ( () => (
(formBranchCount = 0), (formLastSubBranch = 0),
(modal = false), (modal = false),
(profileFile = undefined), (profileFile = undefined),
(isImageEdit = false), (isImageEdit = false),
@ -1430,7 +1425,7 @@ watch(currentHq, () => {
:caption=" :caption="
formTypeBranch === 'headOffice' formTypeBranch === 'headOffice'
? `${formData.code}00000` ? `${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:toggle-status="formData.status"
v-model:cover-url="imageUrl" v-model:cover-url="imageUrl"
@ -1510,7 +1505,7 @@ watch(currentHq, () => {
<div class="col-md-10 col-12 q-pa-md q-gutter-y-xl"> <div class="col-md-10 col-12 q-pa-md q-gutter-y-xl">
<FormBranchInformation <FormBranchInformation
id="form-information" id="form-information"
v-model:branchCount="formBranchCount" v-model:branchCount="formLastSubBranch"
v-model:abbreviation="formData.code" v-model:abbreviation="formData.code"
v-model:code="formData.codeHeadOffice" v-model:code="formData.codeHeadOffice"
v-model:code-sub-branch="currentEdit.code" v-model:code-sub-branch="currentEdit.code"
@ -1848,12 +1843,7 @@ watch(currentHq, () => {
v-ripple v-ripple
v-close-popup v-close-popup
@click.stop=" @click.stop="
triggerCreate( triggerCreate('subBranch', currentNode.id, currentNode.code)
'subBranch',
currentNode.id,
currentNode.code,
currentNode._count.branch,
)
" "
> >
<q-item-section avatar> <q-item-section avatar>