diff --git a/src/pages/03_customer-management/form.ts b/src/pages/03_customer-management/form.ts index d326cb44..e2fff4fe 100644 --- a/src/pages/03_customer-management/form.ts +++ b/src/pages/03_customer-management/form.ts @@ -1,4 +1,4 @@ -import { ref, watch } from 'vue'; +import { ref, toRaw, watch } from 'vue'; import { defineStore } from 'pinia'; import { CustomerCreate } from 'src/stores/customer/types'; import { Employee, EmployeeCreate } from 'src/stores/employee/types'; @@ -115,9 +115,9 @@ export const useCustomerForm = defineStore('form-customer', () => { branchNo: v.branchNo, address: v.address, addressEN: v.addressEN, - provinceId: v.province.id, - districtId: v.district.id, - subDistrictId: v.subDistrict.id, + provinceId: v.provinceId, + districtId: v.districtId, + subDistrictId: v.subDistrictId, zipCode: v.zipCode, email: v.email, telephoneNo: v.telephoneNo, @@ -144,6 +144,8 @@ export const useCustomerForm = defineStore('form-customer', () => { } function addCurrentCustomerBranch() { + if (currentFormData.value.customerBranch?.some((v) => !v.id)) return; + currentFormData.value.customerBranch?.push({ id: '', code: '', @@ -187,6 +189,131 @@ export const useCustomerForm = defineStore('form-customer', () => { 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], + ), + ); + } + } + } + } } async function submitFormCustomer() {