jws-frontend/src/pages/03_customer-management/form.ts

438 lines
11 KiB
TypeScript
Raw Normal View History

2024-08-05 15:29:11 +07:00
import { ref, watch } from 'vue';
2024-08-02 13:58:44 +07:00
import { defineStore } from 'pinia';
import { CustomerCreate } from 'src/stores/customer/types';
2024-08-05 16:53:19 +07:00
import { Employee, EmployeeCreate } from 'src/stores/employee/types';
2024-08-02 13:58:44 +07:00
import useMyBranch from 'src/stores/my-branch';
import useCustomerStore from 'src/stores/customer';
2024-08-05 16:53:19 +07:00
import useEmployeeStore from 'src/stores/employee';
import useFlowStore from 'src/stores/flow';
2024-08-02 13:58:44 +07:00
export const useCustomerForm = defineStore('form-customer', () => {
2024-08-05 16:11:17 +07:00
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;
2024-08-02 13:58:44 +07:00
const customerStore = useCustomerStore();
const branchStore = useMyBranch();
let defaultFormData: CustomerCreate = {
status: 'CREATED',
personName: '',
customerType: 'CORP',
customerName: '',
customerNameEN: '',
taxNo: '',
registeredBranchId: branchStore.currentMyBranch?.id || '',
2024-08-05 15:09:36 +07:00
customerBranch: [],
2024-08-02 13:58:44 +07:00
image: null,
};
2024-08-05 15:09:36 +07:00
let resetFormData = structuredClone(defaultFormData);
2024-08-02 13:58:44 +07:00
const currentFormData = ref<CustomerCreate>(structuredClone(defaultFormData));
const state = ref<{
2024-08-02 16:13:07 +07:00
dialogType: 'info' | 'create' | 'edit';
2024-08-02 13:58:44 +07:00
dialogOpen: boolean;
2024-08-05 10:14:54 +07:00
dialogModal: boolean;
2024-08-02 13:58:44 +07:00
branchIndex: number;
2024-08-05 10:14:54 +07:00
saveMode: 'customer' | 'branch';
2024-08-05 15:09:36 +07:00
customerImageUrl: string;
imageDialog: boolean;
imageEdit: boolean;
2024-08-05 15:45:26 +07:00
editReadonly: boolean;
2024-08-05 10:25:09 +07:00
editCustomerId?: string;
editCustomerBranchId?: string;
2024-08-02 13:58:44 +07:00
}>({
dialogType: 'info',
dialogOpen: false,
2024-08-05 10:14:54 +07:00
dialogModal: false,
2024-08-05 15:09:36 +07:00
imageDialog: false,
2024-08-02 13:58:44 +07:00
branchIndex: 0,
2024-08-05 15:45:26 +07:00
editReadonly: true,
2024-08-05 15:09:36 +07:00
imageEdit: false,
2024-08-05 10:14:54 +07:00
saveMode: 'customer',
2024-08-05 15:09:36 +07:00
customerImageUrl: '',
2024-08-05 10:25:09 +07:00
editCustomerId: '',
editCustomerBranchId: '',
2024-08-02 13:58:44 +07:00
});
2024-08-05 15:29:11 +07:00
watch(
currentFormData,
(v) => (defaultFormData.customerType = v.customerType),
);
2024-08-02 13:58:44 +07:00
function isFormDataDifferent() {
return (
2024-08-05 15:09:36 +07:00
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
2024-08-02 13:58:44 +07:00
);
}
2024-08-05 15:29:11 +07:00
function resetForm(clean = false) {
if (clean) {
defaultFormData.customerType = currentFormData.value.customerType;
currentFormData.value = structuredClone(defaultFormData);
currentFormData.value.registeredBranchId =
branchStore.currentMyBranch?.id || '';
return;
}
2024-08-05 15:09:36 +07:00
if (!resetFormData.registeredBranchId) {
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
}
currentFormData.value = structuredClone(resetFormData);
2024-08-02 13:58:44 +07:00
}
2024-08-05 15:29:11 +07:00
async function assignFormData(id?: string) {
if (!id) {
resetFormData = structuredClone(defaultFormData);
return;
}
2024-08-02 13:58:44 +07:00
const data = await customerStore.fetchById(id);
if (!data) return;
2024-08-05 16:11:17 +07:00
state.value.customerImageUrl = `${apiBaseUrl}/customer/${id}/image`;
2024-08-05 15:09:36 +07:00
resetFormData.registeredBranchId = data.registeredBranchId;
resetFormData.status = data.status;
resetFormData.customerType = data.customerType;
resetFormData.customerName = data.customerName;
resetFormData.customerNameEN = data.customerNameEN;
resetFormData.personName = data.personName;
resetFormData.taxNo = data.taxNo;
resetFormData.image = null;
resetFormData.customerBranch = data.branch.map((v) => ({
2024-08-02 13:58:44 +07:00
id: v.id,
code: v.code,
branchNo: v.branchNo,
address: v.address,
addressEN: v.addressEN,
provinceId: v.province.id,
districtId: v.district.id,
subDistrictId: v.subDistrict.id,
zipCode: v.zipCode,
email: v.email,
telephoneNo: v.telephoneNo,
name: v.name,
status: undefined,
taxNo: v.taxNo,
nameEN: v.nameEN,
legalPersonNo: v.legalPersonNo,
registerName: v.registerName,
registerDate: new Date(v.registerDate),
authorizedCapital: v.authorizedCapital,
employmentOffice: v.employmentOffice,
bussinessType: v.bussinessType,
bussinessTypeEN: v.bussinessTypeEN,
jobPosition: v.jobPosition,
jobPositionEN: v.jobPositionEN,
jobDescription: v.jobDescription,
saleEmployee: v.saleEmployee,
payDate: new Date(v.payDate),
wageRate: v.wageRate,
}));
2024-08-05 15:09:36 +07:00
currentFormData.value = structuredClone(resetFormData);
}
function addCurrentCustomerBranch() {
currentFormData.value.customerBranch?.push({
id: '',
code: '',
2024-08-06 08:57:07 +07:00
branchNo:
(currentFormData.value.customerBranch?.at(-1)?.branchNo || 0) + 1,
2024-08-05 15:09:36 +07:00
address: '',
addressEN: '',
provinceId: '',
districtId: '',
subDistrictId: '',
zipCode: '',
email: '',
telephoneNo: '',
name: '',
status: 'CREATED',
taxNo: '',
nameEN: '',
legalPersonNo: '',
registerName: '',
registerDate: new Date(),
authorizedCapital: '',
employmentOffice: '',
bussinessType: '',
bussinessTypeEN: '',
jobPosition: '',
jobPositionEN: '',
jobDescription: '',
saleEmployee: '',
payDate: new Date(),
wageRate: 0,
});
2024-08-02 13:58:44 +07:00
}
2024-08-05 10:14:54 +07:00
async function submitForm() {
2024-08-05 15:57:53 +07:00
if (state.value.saveMode === 'customer') {
await submitFormCustomer().then(() => {
state.value.dialogModal = false;
});
}
2024-08-05 15:09:36 +07:00
}
async function submitFormCustomer() {
if (state.value.dialogType === 'info') return;
if (state.value.dialogType === 'create') {
return await customerStore.create(currentFormData.value);
2024-08-05 10:14:54 +07:00
}
2024-08-05 15:09:36 +07:00
if (!state.value.editCustomerId) {
throw new Error(
'Form mode is set to edit but no ID is provided. Make sure to set customer ID.',
);
2024-08-05 10:14:54 +07:00
}
2024-08-05 15:09:36 +07:00
await customerStore.editById(state.value.editCustomerId, {
...currentFormData.value,
status:
currentFormData.value.status !== 'CREATED'
? currentFormData.value.status
: undefined,
2024-08-05 15:09:36 +07:00
image: currentFormData.value.image || undefined,
customerBranch: undefined,
});
2024-08-05 10:14:54 +07:00
}
2024-08-02 13:58:44 +07:00
return {
state,
2024-08-05 15:09:36 +07:00
resetFormData,
2024-08-02 13:58:44 +07:00
currentFormData,
isFormDataDifferent,
2024-08-05 15:09:36 +07:00
resetForm,
2024-08-02 13:58:44 +07:00
assignFormData,
2024-08-05 10:14:54 +07:00
submitForm,
2024-08-05 15:09:36 +07:00
addCurrentCustomerBranch,
2024-08-02 13:58:44 +07:00
};
});
export const useEmployeeForm = defineStore('form-employee', () => {
2024-08-05 16:53:19 +07:00
const customerStore = useCustomerStore();
const employeeStore = useEmployeeStore();
const flowStore = useFlowStore();
const branchStore = useMyBranch();
const state = ref<{
dialogType: 'info' | 'create' | 'edit';
drawerModal: boolean;
currentEmployeeCode: string;
currentEmployee: Employee | null;
profileUrl: string;
isEmployeeEdit: boolean;
profileSubmit: boolean;
formDataEmployeeSameAddr: boolean;
infoEmployeePersonCard: {
id: string;
img: string;
name: string;
male: boolean;
female: boolean;
badge: string;
disabled: boolean;
}[];
formDataEmployeeOwner:
| {
id: string;
address: string;
addressEN: string;
provinceId: string;
districtId: string;
subDistrictId: string;
zipCode: string;
}
| undefined;
}>({
drawerModal: false,
dialogType: 'info',
currentEmployeeCode: '',
currentEmployee: null,
profileUrl: '',
isEmployeeEdit: false,
profileSubmit: false,
formDataEmployeeSameAddr: false,
infoEmployeePersonCard: [],
formDataEmployeeOwner: undefined,
});
let defaultFormData: EmployeeCreate = {
code: '',
image: null,
customerBranchId: '',
nrcNo: '',
dateOfBirth: null,
gender: '',
nationality: '',
firstName: '',
firstNameEN: '',
lastName: '',
lastNameEN: '',
addressEN: '',
address: '',
zipCode: '',
passportType: '',
passportNumber: '',
passportIssueDate: null,
passportExpiryDate: null,
passportIssuingCountry: '',
passportIssuingPlace: '',
previousPassportReference: '',
visaType: '',
visaNumber: '',
visaIssueDate: null,
visaExpiryDate: null,
visaIssuingPlace: '',
visaStayUntilDate: null,
tm6Number: '',
entryDate: null,
workerStatus: '',
subDistrictId: '',
districtId: '',
provinceId: '',
employeeWork: [
{
workEndDate: null,
workPermitExpireDate: null,
workPermitIssuDate: null,
workPermitNo: '',
workplace: '',
jobType: '',
positionName: '',
ownerName: '',
remark: '',
},
],
employeeCheckup: [
{
coverageExpireDate: null,
coverageStartDate: null,
insuranceCompany: '',
medicalBenefitScheme: '',
remark: '',
hospitalName: '',
provinceId: '',
checkupResult: '',
checkupType: '',
},
],
employeeOtherInfo: {
citizenId: '',
fatherFirstName: '',
fatherLastName: '',
fatherFirstNameEN: '',
fatherLastNameEN: '',
fatherBirthPlace: '',
motherFirstName: '',
motherLastName: '',
motherFirstNameEN: '',
motherLastNameEN: '',
motherBirthPlace: '',
},
};
let resetEmployeeData = structuredClone(defaultFormData);
const currentFromDataEmployee = ref<EmployeeCreate>(
structuredClone(defaultFormData),
);
function resetFormDataEmployee(cb?: (...args: any[]) => unknown) {
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
cb?.();
}
async function assignFormDataEmployee(id: string) {
const res = await employeeStore.fetchById(id);
if (res) {
state.value.currentEmployee = res;
const {
province,
district,
subDistrict,
createdBy,
updatedBy,
profileImageUrl,
...playlond
} = res;
currentFromDataEmployee.value = {
...playlond,
provinceId: province?.id,
districtId: district?.id,
subDistrictId: subDistrict?.id,
employeeCheckup: structuredClone(playlond.employeeCheckup),
employeeOtherInfo: structuredClone(playlond.employeeOtherInfo),
employeeWork: structuredClone(playlond.employeeWork),
image: null,
};
const foundBranch = await customerStore.fetchListCustomeBranchById(
playlond.customerBranchId,
);
state.value.currentEmployeeCode = playlond.code;
state.value.profileUrl = profileImageUrl || '';
profileImageUrl
? (state.value.profileSubmit = true)
: (state.value.profileSubmit = false);
state.value.isEmployeeEdit = true;
state.value.formDataEmployeeOwner = { ...foundBranch };
if (foundBranch.address === playlond.address) {
state.value.formDataEmployeeSameAddr = true;
} else {
state.value.formDataEmployeeSameAddr = false;
}
if (state.value.infoEmployeePersonCard) {
state.value.infoEmployeePersonCard[0].img = profileImageUrl || '';
}
flowStore.rotate();
}
}
async function employeeFilterOwnerBranch(
val: string,
update: (...args: unknown[]) => void,
) {
update(async () => {
const result = await customerStore.fetchListCustomeBranch({
includeCustomer: true,
query: val,
pageSize: 30,
});
if (result) employeeStore.ownerOption = result.result;
});
}
return {
state,
currentFromDataEmployee,
resetEmployeeData,
resetFormDataEmployee,
assignFormDataEmployee,
employeeFilterOwnerBranch,
};
2024-08-02 13:58:44 +07:00
});