feat: customer branch form

This commit is contained in:
Methapon2001 2024-08-07 17:56:59 +07:00
parent 779374b164
commit 09b4f2500a
6 changed files with 499 additions and 561 deletions

View file

@ -33,11 +33,10 @@ export const useCustomerForm = defineStore('form-customer', () => {
dialogOpen: boolean;
dialogModal: boolean;
branchIndex: number;
saveMode: 'customer' | 'branch';
customerImageUrl: string;
imageDialog: boolean;
imageEdit: boolean;
editReadonly: boolean;
readonly: boolean;
editCustomerId?: string;
editCustomerBranchId?: string;
}>({
@ -45,10 +44,9 @@ export const useCustomerForm = defineStore('form-customer', () => {
dialogOpen: false,
dialogModal: false,
imageDialog: false,
branchIndex: 0,
editReadonly: true,
branchIndex: -1,
readonly: true,
imageEdit: false,
saveMode: 'customer',
customerImageUrl: '',
editCustomerId: '',
editCustomerBranchId: '',
@ -60,12 +58,14 @@ export const useCustomerForm = defineStore('form-customer', () => {
);
function isFormDataDifferent() {
console.log(resetFormData, currentFormData.value);
return (
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
);
}
function resetForm(clean = false) {
state.value.branchIndex = -1;
if (clean) {
defaultFormData.customerType = currentFormData.value.customerType;
currentFormData.value = structuredClone(defaultFormData);
@ -177,143 +177,8 @@ export const useCustomerForm = defineStore('form-customer', () => {
payDate: new Date(),
wageRate: 0,
});
}
async function submitForm() {
if (state.value.saveMode === 'customer') {
const _data = await submitFormCustomer();
if (_data) {
await assignFormData(_data.id);
state.value.dialogType = 'edit';
state.value.editReadonly = true;
state.value.editCustomerId = _data.id;
}
}
if (state.value.saveMode === 'branch') {
const _form = currentFormData.value.customerBranch?.at(
state.value.branchIndex,
);
if (_form) {
if (_form.id) {
const _data = await customerStore.editBranchById(_form.id, {
..._form,
id: undefined,
});
if (
_data &&
currentFormData.value.customerBranch?.[state.value.branchIndex]
) {
currentFormData.value.customerBranch[state.value.branchIndex] = {
id: _data.id,
code: _data.code,
branchNo: _data.branchNo,
address: _data.address,
addressEN: _data.addressEN,
provinceId: _data.provinceId,
districtId: _data.districtId,
subDistrictId: _data.subDistrictId,
zipCode: _data.zipCode,
email: _data.email,
telephoneNo: _data.telephoneNo,
name: _data.name,
status: undefined,
taxNo: _data.taxNo,
nameEN: _data.nameEN,
legalPersonNo: _data.legalPersonNo,
registerName: _data.registerName,
registerDate: new Date(_data.registerDate),
authorizedCapital: _data.authorizedCapital,
employmentOffice: _data.employmentOffice,
bussinessType: _data.bussinessType,
bussinessTypeEN: _data.bussinessTypeEN,
jobPosition: _data.jobPosition,
jobPositionEN: _data.jobPositionEN,
jobDescription: _data.jobDescription,
saleEmployee: _data.saleEmployee,
payDate: new Date(_data.payDate),
wageRate: _data.wageRate,
};
if (resetFormData.customerBranch?.[state.value.branchIndex]) {
resetFormData.customerBranch[state.value.branchIndex] =
structuredClone(
toRaw(
currentFormData.value.customerBranch[
state.value.branchIndex
],
),
);
} else {
resetFormData.customerBranch?.push(
structuredClone(
toRaw(
currentFormData.value.customerBranch[
state.value.branchIndex
],
),
),
);
}
return;
}
}
if (!state.value.editCustomerId) {
throw new Error(
'Form mode is set to edit but no ID is provided. Make sure to set customer ID.',
);
}
const _data = await customerStore.createBranch({
..._form,
customerId: state.value.editCustomerId,
id: undefined,
});
if (
_data &&
currentFormData.value.customerBranch?.[state.value.branchIndex]
) {
currentFormData.value.customerBranch[state.value.branchIndex] = {
id: _data.id,
code: _data.code,
branchNo: _data.branchNo,
address: _data.address,
addressEN: _data.addressEN,
provinceId: _data.provinceId,
districtId: _data.districtId,
subDistrictId: _data.subDistrictId,
zipCode: _data.zipCode,
email: _data.email,
telephoneNo: _data.telephoneNo,
name: _data.name,
status: undefined,
taxNo: _data.taxNo,
nameEN: _data.nameEN,
legalPersonNo: _data.legalPersonNo,
registerName: _data.registerName,
registerDate: new Date(_data.registerDate),
authorizedCapital: _data.authorizedCapital,
employmentOffice: _data.employmentOffice,
bussinessType: _data.bussinessType,
bussinessTypeEN: _data.bussinessTypeEN,
jobPosition: _data.jobPosition,
jobPositionEN: _data.jobPositionEN,
jobDescription: _data.jobDescription,
saleEmployee: _data.saleEmployee,
payDate: new Date(_data.payDate),
wageRate: _data.wageRate,
};
if (resetFormData.customerBranch?.[state.value.branchIndex]) {
resetFormData.customerBranch[state.value.branchIndex] =
structuredClone(
toRaw(
currentFormData.value.customerBranch[state.value.branchIndex],
),
);
}
}
}
}
state.value.branchIndex =
(currentFormData.value.customerBranch?.length || 0) - 1;
}
async function submitFormCustomer() {
@ -329,7 +194,7 @@ export const useCustomerForm = defineStore('form-customer', () => {
);
}
await customerStore.editById(state.value.editCustomerId, {
const _data = await customerStore.editById(state.value.editCustomerId, {
...currentFormData.value,
status:
currentFormData.value.status !== 'CREATED'
@ -338,6 +203,13 @@ export const useCustomerForm = defineStore('form-customer', () => {
image: currentFormData.value.image || undefined,
customerBranch: undefined,
});
if (_data) {
await assignFormData(_data.id);
state.value.dialogType = 'edit';
state.value.readonly = true;
state.value.editCustomerId = _data.id;
}
}
return {
@ -347,7 +219,7 @@ export const useCustomerForm = defineStore('form-customer', () => {
isFormDataDifferent,
resetForm,
assignFormData,
submitForm,
submitFormCustomer,
addCurrentCustomerBranch,
};
});