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 qrCodeimageUrl = ref<string | null>('');
const formBranchCount = ref<number>(0);
const formLastSubBranch = ref<number>(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, () => {
<div class="col-md-10 col-12 q-pa-md q-gutter-y-xl">
<FormBranchInformation
id="form-information"
v-model:branchCount="formBranchCount"
v-model:branchCount="formLastSubBranch"
v-model:abbreviation="formData.code"
v-model:code="formData.codeHeadOffice"
v-model:code-sub-branch="currentEdit.code"
@ -1848,12 +1843,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)
"
>
<q-item-section avatar>