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

1012 lines
28 KiB
TypeScript
Raw Normal View History

2024-08-06 16:34:20 +07:00
import { ref, toRaw, watch } from 'vue';
2024-08-02 13:58:44 +07:00
import { defineStore } from 'pinia';
import { CustomerBranchCreate, CustomerCreate } from 'stores/customer/types';
2024-08-09 15:08:25 +07:00
import { Employee, EmployeeCreate } from 'stores/employee/types';
2024-08-27 16:19:18 +07:00
import { useI18n } from 'vue-i18n';
2024-08-05 16:53:19 +07:00
2024-08-09 15:08:25 +07:00
import useMyBranch from 'stores/my-branch';
import useCustomerStore from 'stores/customer';
import useEmployeeStore from 'stores/employee';
import useFlowStore from 'stores/flow';
2024-08-02 13:58:44 +07:00
2024-09-11 16:53:08 +07:00
import { baseUrl } from 'src/stores/utils';
2024-08-02 13:58:44 +07:00
export const useCustomerForm = defineStore('form-customer', () => {
2024-08-27 16:19:18 +07:00
const { t } = useI18n();
2024-08-05 16:11:17 +07:00
2024-08-02 13:58:44 +07:00
const customerStore = useCustomerStore();
const branchStore = useMyBranch();
2024-08-06 03:14:31 +00:00
const defaultFormData: CustomerCreate = {
2024-09-16 14:38:04 +07:00
// code: '',
// namePrefix: '',
// firstName: '',
// lastName: '',
// firstNameEN: '',
// lastNameEN: '',
// gender: '',
// birthDate: new Date(),
customerBranch: [],
selectedImage: '',
2024-08-02 13:58:44 +07:00
status: 'CREATED',
customerType: 'CORP',
registeredBranchId: branchStore.currentMyBranch?.id || '',
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-26 11:03:32 +07:00
drawerModal: boolean;
2024-08-02 13:58:44 +07:00
branchIndex: number;
2024-08-05 15:09:36 +07:00
customerImageUrl: string;
2024-08-15 11:02:10 +07:00
defaultCustomerImageUrl: string;
2024-08-05 15:09:36 +07:00
imageDialog: boolean;
imageEdit: boolean;
2024-08-07 17:56:59 +07:00
readonly: boolean;
2024-08-05 10:25:09 +07:00
editCustomerId?: string;
2024-08-08 09:46:51 +07:00
editCustomerCode?: string;
2024-08-05 10:25:09 +07:00
editCustomerBranchId?: string;
2024-08-27 19:37:30 +07:00
treeFile: { label: string; file: { label: string }[] }[];
formDataOcr: Record<string, any>;
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
isImageEdit: boolean;
2024-08-02 13:58:44 +07:00
}>({
dialogType: 'info',
dialogOpen: false,
2024-08-05 10:14:54 +07:00
dialogModal: false,
2024-08-26 11:03:32 +07:00
drawerModal: false,
2024-08-05 15:09:36 +07:00
imageDialog: false,
2024-08-07 17:56:59 +07:00
branchIndex: -1,
readonly: true,
2024-08-05 15:09:36 +07:00
imageEdit: false,
customerImageUrl: '',
2024-08-05 10:25:09 +07:00
editCustomerId: '',
editCustomerBranchId: '',
2024-08-15 11:02:10 +07:00
defaultCustomerImageUrl: '',
2024-08-27 16:19:18 +07:00
treeFile: [],
2024-08-27 19:37:30 +07:00
formDataOcr: {},
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
isImageEdit: false,
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-28 17:36:19 +07:00
async function deleteAttachment(
id: { branchId: string; customerId: string },
filename: string,
) {
const res = await customerStore.deleteAttachment(id.branchId, filename);
if (res) {
assignFormData(id.customerId);
}
}
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) {
2024-08-07 17:56:59 +07:00
state.value.branchIndex = -1;
2024-08-05 15:29:11 +07:00
if (clean) {
defaultFormData.customerType = currentFormData.value.customerType;
currentFormData.value = structuredClone(defaultFormData);
currentFormData.value.registeredBranchId =
branchStore.currentMyBranch?.id || '';
2024-08-06 09:37:32 +07:00
resetFormData = structuredClone(defaultFormData);
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
2024-08-06 11:51:51 +07:00
state.value.editCustomerId = '';
2024-08-27 16:19:18 +07:00
state.value.treeFile = [];
2024-08-05 15:29:11 +07:00
return;
}
2024-08-05 15:09:36 +07:00
if (!resetFormData.registeredBranchId) {
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
}
2024-08-06 11:51:51 +07:00
if (state.value.dialogType === 'create') {
state.value.editCustomerId = '';
}
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
const currentImg = currentFormData.value.selectedImage;
2024-08-05 15:09:36 +07:00
currentFormData.value = structuredClone(resetFormData);
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
currentFormData.value.selectedImage = currentImg;
2024-08-02 13:58:44 +07:00
}
2024-08-05 15:29:11 +07:00
async function assignFormData(id?: string) {
2024-08-08 17:10:48 +07:00
state.value.readonly = true;
2024-08-05 15:29:11 +07:00
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-08 10:33:45 +07:00
state.value.dialogType = 'edit';
2024-08-08 09:46:51 +07:00
state.value.editCustomerId = id;
state.value.editCustomerCode = data.code;
2024-09-11 16:53:08 +07:00
state.value.customerImageUrl = `${baseUrl}/customer/${id}/image/${data.selectedImage}`;
state.value.defaultCustomerImageUrl = `${baseUrl}/customer/${id}/image/${data.selectedImage}`;
2024-08-05 16:11:17 +07:00
2024-08-05 15:09:36 +07:00
resetFormData.registeredBranchId = data.registeredBranchId;
resetFormData.status = data.status;
resetFormData.customerType = data.customerType;
2024-09-16 14:38:04 +07:00
resetFormData.code = data.code || '';
2024-08-20 17:59:09 +07:00
resetFormData.namePrefix = data.namePrefix;
resetFormData.firstName = data.firstName;
resetFormData.lastName = data.lastName;
resetFormData.firstNameEN = data.firstNameEN;
resetFormData.lastNameEN = data.lastNameEN;
resetFormData.gender = data.gender;
resetFormData.birthDate = new Date(data.birthDate);
2024-08-05 15:09:36 +07:00
resetFormData.image = null;
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
resetFormData.selectedImage = data.selectedImage;
2024-08-06 11:51:51 +07:00
2024-08-27 12:55:11 +07:00
resetFormData.customerBranch = await Promise.all(
data.branch.map(async (v) => ({
id: v.id,
code: v.code || '',
customerCode: '',
provinceId: v.provinceId,
districtId: v.districtId,
subDistrictId: v.subDistrictId,
wageRate: v.wageRate,
2024-09-16 14:38:04 +07:00
payDate: v.payDate, // Convert the string to a Date object
payDateEN: v.payDateEN,
2024-08-27 12:55:11 +07:00
saleEmployee: v.saleEmployee,
jobDescription: v.jobDescription,
jobPositionEN: v.jobPositionEN,
jobPosition: v.jobPosition,
businessTypeEN: v.businessTypeEN,
businessType: v.businessType,
employmentOffice: v.employmentOffice,
telephoneNo: v.telephoneNo,
email: v.email,
addressEN: v.addressEN,
address: v.address,
workplaceEN: v.workplaceEN,
workplace: v.workplace,
status: v.status,
customerId: v.customerId,
citizenId: v.citizenId || '',
authorizedCapital: v.authorizedCapital || '',
registerDate: new Date(v.registerDate), // Convert the string to a Date object
registerNameEN: v.registerNameEN || '',
registerName: v.registerName || '',
legalPersonNo: v.legalPersonNo || '',
registerCompanyName: '',
statusSave: true,
contactName: v.contactName || '',
file: await customerStore.listAttachment(v.id).then(async (r) => {
if (r) {
return await Promise.all(
2024-08-28 17:36:30 +07:00
r.map(async (item) => {
const fragment = item.split('-');
const group = fragment.length === 1 ? 'other' : fragment.at(0);
return {
url: await customerStore.getAttachment(v.id, item),
name: item,
group: group,
};
}),
2024-08-27 12:55:11 +07:00
);
}
return [];
}),
})),
);
2024-08-02 13:58:44 +07:00
2024-08-05 15:09:36 +07:00
currentFormData.value = structuredClone(resetFormData);
}
2024-09-16 14:38:04 +07:00
async function addCurrentCustomerBranch() {
2024-08-06 16:34:20 +07:00
if (currentFormData.value.customerBranch?.some((v) => !v.id)) return;
2024-08-05 15:09:36 +07:00
currentFormData.value.customerBranch?.push({
id: '',
2024-08-21 14:42:56 +07:00
customerId: '',
2024-09-16 14:38:04 +07:00
branchCode:
2024-08-21 15:13:28 +07:00
currentFormData.value.customerBranch.length !== 0
2024-09-16 14:38:04 +07:00
? currentFormData.value.customerBranch?.[0].branchCode === null
2024-08-21 15:13:28 +07:00
? ''
2024-09-16 14:38:04 +07:00
: currentFormData.value.customerBranch?.[0].branchCode
2024-08-21 15:13:28 +07:00
: '',
2024-09-16 14:38:04 +07:00
customerCode: '',
2024-08-21 14:42:56 +07:00
legalPersonNo:
2024-08-21 15:13:28 +07:00
currentFormData.value.customerBranch.length !== 0
? currentFormData.value.customerBranch?.[0].legalPersonNo === null
? ''
: currentFormData.value.customerBranch?.[0].legalPersonNo
: '',
2024-09-16 14:38:04 +07:00
citizenId:
currentFormData.value.customerBranch.length !== 0
? currentFormData.value.customerBranch?.[0].citizenId === null
? ''
: currentFormData.value.customerBranch?.[0].citizenId
: '',
namePrefix: '',
firstName: '',
lastName: '',
firstNameEN: '',
lastNameEN: '',
telephoneNo: '',
gender: '',
birthDate: '',
businessType: '',
jobPosition: '',
jobDescription: '',
payDate: '',
payDateEN: '',
wageRate: 0,
wageRateText: '',
homeCode: '',
employmentOffice: '',
employmentOfficeEN: '',
address: '',
addressEN: '',
street: '',
streetEN: '',
moo: '',
mooEN: '',
soi: '',
soiEN: '',
provinceId: '',
districtId: '',
subDistrictId: '',
2024-08-21 17:10:02 +07:00
contactName: '',
2024-09-16 14:38:04 +07:00
email: '',
contactTel: '',
officeTel: '',
agent: '',
status: 'CREATED',
customerName: '',
registerName: '',
registerNameEN: '',
registerDate: null,
authorizedCapital: '',
authorizedName: '',
authorizedNameEN: '',
2024-08-29 14:54:06 +07:00
file: [],
2024-08-05 15:09:36 +07:00
});
2024-08-07 17:56:59 +07:00
state.value.branchIndex =
(currentFormData.value.customerBranch?.length || 0) - 1;
2024-08-05 15:09:36 +07:00
}
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
async function submitFormCustomer(imgList?: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
}) {
2024-08-05 15:09:36 +07:00
if (state.value.dialogType === 'info') return;
if (state.value.dialogType === 'create') {
Squashed commit of the following: commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:29:44 2024 +0700 refactor: no img close to default on create commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 11:04:04 2024 +0700 refactor/feat: save => apply, disabled selected img, no img close to default commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3 Author: puriphatt <puriphat@frappet.com> Date: Wed Sep 11 09:31:32 2024 +0700 fix(01): use submit function commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:45:19 2024 +0700 fix: imgList error commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2 Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 17:18:03 2024 +0700 refactor(03): customer new upload img dialog commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:40:39 2024 +0700 refactor(02): new upload dialog commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb Author: puriphatt <puriphat@frappet.com> Date: Tue Sep 10 15:25:21 2024 +0700 refactor(01): new upload dialog, confirm remove, individual action commit 61caf1919168bc5635568d7ca246574fdc43cd04 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(01): branch new img upload commit e791b7316d001d839c8afb1950f7331c62d9e81a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 refactor(02): personnel new img upload commit af4d11312b9cb666338901efa9971117cb7738c4 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:42 2024 +0700 feat(02): new image upload commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8 Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock select image function commit 5ab3f045b9c7d2c821920c12114da15eed09655a Author: puriphatt <puriphat@frappet.com> Date: Mon Sep 9 17:08:41 2024 +0700 refactor: mock new image preview
2024-09-11 16:43:41 +07:00
const _data = await customerStore.create(currentFormData.value, imgList);
2024-08-13 12:52:25 +07:00
if (_data) await assignFormData(_data.id);
return;
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-13 12:52:25 +07:00
const { code: _, ...payload } = currentFormData.value;
2024-08-07 17:56:59 +07:00
const _data = await customerStore.editById(state.value.editCustomerId, {
2024-08-13 12:52:25 +07:00
...payload,
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-07 17:56:59 +07:00
if (_data) {
await assignFormData(_data.id);
state.value.dialogType = 'edit';
state.value.readonly = true;
state.value.editCustomerId = _data.id;
}
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-07 17:56:59 +07:00
submitFormCustomer,
2024-08-05 15:09:36 +07:00
addCurrentCustomerBranch,
2024-08-28 17:36:30 +07:00
deleteAttachment,
2024-08-02 13:58:44 +07:00
};
});
2024-08-09 15:09:46 +07:00
export const useCustomerBranchForm = defineStore('form-customer-branch', () => {
const customerStore = useCustomerStore();
2024-08-28 08:48:19 +07:00
const customerFormStore = useCustomerForm();
2024-08-09 15:09:46 +07:00
2024-08-21 14:42:56 +07:00
const defaultFormData: CustomerBranchCreate & { id?: string } = {
2024-08-09 15:09:46 +07:00
code: '',
2024-08-21 14:42:56 +07:00
customerCode: '',
2024-08-09 15:09:46 +07:00
provinceId: '',
districtId: '',
subDistrictId: '',
2024-08-21 14:42:56 +07:00
wageRate: 0,
2024-09-16 14:38:04 +07:00
payDate: '',
payDateEN: '',
2024-08-21 14:42:56 +07:00
saleEmployee: '',
jobDescription: '',
jobPositionEN: '',
jobPosition: '',
businessTypeEN: '',
businessType: '',
employmentOffice: '',
2024-08-09 15:09:46 +07:00
telephoneNo: '',
2024-08-21 14:42:56 +07:00
email: '',
addressEN: '',
address: '',
workplaceEN: '',
workplace: '',
2024-08-09 15:09:46 +07:00
status: 'CREATED',
2024-08-21 14:42:56 +07:00
customerId: '',
citizenId: '',
2024-08-09 15:09:46 +07:00
authorizedCapital: '',
2024-08-21 14:42:56 +07:00
registerDate: new Date(), // Convert the string to a Date object
registerNameEN: '',
registerName: '',
legalPersonNo: '',
registerCompanyName: '',
statusSave: false,
2024-08-21 17:10:02 +07:00
contactName: '',
2024-08-29 14:54:06 +07:00
file: [],
2024-08-09 15:09:46 +07:00
};
let resetFormData = structuredClone(defaultFormData);
2024-08-21 14:42:56 +07:00
const currentFormData = ref<CustomerBranchCreate & { id?: string }>(
2024-08-09 15:09:46 +07:00
structuredClone(defaultFormData),
);
const state = ref<{
dialogType: 'info' | 'create' | 'edit';
dialogOpen: boolean;
dialogModal: boolean;
currentCustomerId: string;
}>({
dialogType: 'info',
dialogOpen: false,
dialogModal: false,
currentCustomerId: '',
});
async function initForm(form: 'create'): Promise<void>;
async function initForm(form: 'info' | 'edit', id: string): Promise<void>;
async function initForm(form: 'info' | 'create' | 'edit', id?: string) {
state.value.dialogType = form;
resetFormData = structuredClone(defaultFormData);
if (!id) return;
const _data = await customerStore.getBranchById(id);
if (!_data) return;
resetFormData = {
code: _data.code,
2024-08-21 14:42:56 +07:00
customerCode: '',
2024-08-09 15:09:46 +07:00
provinceId: _data.provinceId,
districtId: _data.districtId,
subDistrictId: _data.subDistrictId,
2024-08-21 14:42:56 +07:00
wageRate: _data.wageRate,
2024-09-16 14:38:04 +07:00
payDate: _data.payDate, // Convert the string to a Date object
payDateEN: _data.payDateEN,
2024-08-21 14:42:56 +07:00
saleEmployee: _data.saleEmployee,
jobDescription: _data.jobDescription,
jobPositionEN: _data.jobPositionEN,
jobPosition: _data.jobPosition,
businessTypeEN: _data.businessTypeEN,
businessType: _data.businessType,
employmentOffice: _data.employmentOffice,
2024-08-09 15:09:46 +07:00
telephoneNo: _data.telephoneNo,
2024-08-21 14:42:56 +07:00
email: _data.email,
addressEN: _data.addressEN,
address: _data.address,
workplaceEN: _data.workplaceEN,
workplace: _data.workplace,
status: 'CREATED',
customerId: _data.customerId,
citizenId: _data.citizenId,
2024-08-09 15:09:46 +07:00
authorizedCapital: _data.authorizedCapital,
2024-08-21 14:42:56 +07:00
registerDate: new Date(), // Convert the string to a Date object
registerNameEN: _data.registerNameEN,
registerName: _data.registerName,
legalPersonNo: _data.legalPersonNo,
2024-08-21 17:10:02 +07:00
contactName: _data.contactName,
2024-08-21 14:42:56 +07:00
registerCompanyName: '',
statusSave: false,
2024-08-29 14:54:06 +07:00
file: [],
2024-08-09 15:09:46 +07:00
};
currentFormData.value = structuredClone(resetFormData);
}
function isFormDataDifferent() {
return (
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
);
}
2024-08-09 17:57:19 +07:00
watch(
() => state.value.dialogType,
() => {
if (state.value.dialogType === 'create') {
currentFormData.value = structuredClone(defaultFormData);
}
},
);
2024-08-09 15:09:46 +07:00
async function submitForm() {
if (!state.value.currentCustomerId) {
throw new Error(
'Employer id cannot be found. Did you properly set employer id?',
);
}
if (!currentFormData.value.id) {
2024-08-14 09:44:27 +07:00
const res = await customerStore.createBranch({
2024-08-09 15:09:46 +07:00
...currentFormData.value,
2024-08-28 08:48:19 +07:00
citizenId:
customerFormStore.currentFormData.customerType === 'CORP'
? undefined
: currentFormData.value.citizenId,
2024-08-09 15:09:46 +07:00
customerId: state.value.currentCustomerId,
});
2024-08-14 09:44:27 +07:00
if (res) return res;
2024-08-09 15:09:46 +07:00
} else {
2024-08-14 09:44:27 +07:00
const res = await customerStore.editBranchById(currentFormData.value.id, {
2024-08-09 15:09:46 +07:00
...currentFormData.value,
id: undefined,
});
2024-08-14 09:44:27 +07:00
if (res) return res;
2024-08-09 15:09:46 +07:00
}
}
return {
state,
currentFormData,
2024-08-28 17:36:30 +07:00
initForm,
2024-08-09 15:09:46 +07:00
isFormDataDifferent,
submitForm,
};
});
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';
2024-08-06 07:55:05 +00:00
imageDialog: boolean;
2024-08-06 03:14:31 +00:00
currentTab: string;
dialogModal: boolean;
2024-08-05 16:53:19 +07:00
drawerModal: boolean;
2024-09-11 16:53:08 +07:00
isImageEdit: boolean;
2024-08-13 13:31:29 +07:00
2024-08-05 16:53:19 +07:00
currentEmployeeCode: string;
currentEmployee: Employee | null;
currentIndex: number;
2024-08-05 16:53:19 +07:00
profileUrl: string;
isEmployeeEdit: boolean;
profileSubmit: boolean;
formDataEmployeeSameAddr: boolean;
editReadonly: boolean;
2024-08-07 13:30:05 +07:00
statusSavePersonal: boolean;
2024-08-05 16:53:19 +07:00
infoEmployeePersonCard: {
id: string;
2024-08-07 13:30:05 +07:00
img?: string;
2024-08-05 16:53:19 +07:00
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;
2024-08-28 16:41:50 +07:00
ocr: boolean;
2024-08-05 16:53:19 +07:00
}>({
2024-09-11 16:53:08 +07:00
isImageEdit: false,
2024-08-13 13:31:29 +07:00
currentIndex: -1,
2024-08-07 13:30:05 +07:00
statusSavePersonal: false,
2024-08-05 16:53:19 +07:00
drawerModal: false,
2024-08-06 07:55:05 +00:00
imageDialog: false,
2024-08-06 03:14:31 +00:00
currentTab: 'personalInfo',
dialogModal: false,
2024-08-05 16:53:19 +07:00
dialogType: 'info',
currentEmployeeCode: '',
currentEmployee: null,
profileUrl: '',
isEmployeeEdit: false,
profileSubmit: false,
2024-08-06 07:55:05 +00:00
formDataEmployeeSameAddr: true,
editReadonly: false,
2024-08-05 16:53:19 +07:00
infoEmployeePersonCard: [],
formDataEmployeeOwner: undefined,
2024-08-28 16:41:50 +07:00
ocr: false,
2024-08-05 16:53:19 +07:00
});
2024-08-06 03:14:31 +00:00
const defaultFormData: EmployeeCreate = {
2024-08-09 15:27:16 +07:00
id: '',
2024-08-05 16:53:19 +07:00
code: '',
customerBranchId: '',
nrcNo: '',
dateOfBirth: null,
gender: '',
nationality: '',
2024-08-06 07:55:05 +00:00
status: 'CREATED',
2024-08-05 16:53:19 +07:00
2024-08-27 15:03:56 +07:00
namePrefix: '',
2024-08-05 16:53:19 +07:00
firstName: '',
firstNameEN: '',
lastName: '',
lastNameEN: '',
2024-08-27 15:03:56 +07:00
middleName: '',
middleNameEN: '',
2024-08-05 16:53:19 +07:00
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: '',
},
2024-09-11 16:53:08 +07:00
image: null,
2024-08-05 16:53:19 +07:00
};
2024-08-07 13:30:05 +07:00
let resetEmployeeData = structuredClone(defaultFormData);
2024-08-05 16:53:19 +07:00
const currentFromDataEmployee = ref<EmployeeCreate>(
structuredClone(defaultFormData),
);
function isFormDataDifferent() {
return (
JSON.stringify(resetEmployeeData) !==
JSON.stringify(currentFromDataEmployee.value)
);
}
2024-08-07 13:30:05 +07:00
// function resetFormDataEmployee(cb?: (...args: any[]) => unknown) {
// state.value.dialogType = 'create';
// currentFromDataEmployee.value = structuredClone(resetEmployeeData);
// cb?.();
// }
function resetFormDataEmployee(clean = false) {
if (clean) {
state.value.currentTab = 'personalInfo';
2024-08-07 13:30:05 +07:00
state.value.formDataEmployeeOwner = undefined;
resetEmployeeData = structuredClone(defaultFormData);
state.value.statusSavePersonal = false;
2024-08-28 14:13:22 +07:00
state.value.profileUrl = '';
2024-09-11 16:53:08 +07:00
} else {
resetEmployeeData.selectedImage =
currentFromDataEmployee.value.selectedImage;
2024-08-07 13:30:05 +07:00
}
2024-08-05 16:53:19 +07:00
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
}
2024-08-09 15:27:16 +07:00
async function submitOther() {
if (!currentFromDataEmployee.value.employeeOtherInfo) return;
if (!currentFromDataEmployee.value.employeeOtherInfo.id) {
const res = await employeeStore.createEmployeeOtherInfo(
currentFromDataEmployee.value?.id || '',
currentFromDataEmployee.value.employeeOtherInfo,
);
if (res) {
currentFromDataEmployee.value.employeeOtherInfo.id = res.id;
currentFromDataEmployee.value.employeeOtherInfo.statusSave = true;
}
} else {
const res = await employeeStore.editByIdEmployeeOtherInfo(
currentFromDataEmployee.value?.id || '',
currentFromDataEmployee.value.employeeOtherInfo,
);
if (res) {
currentFromDataEmployee.value.employeeOtherInfo.statusSave = true;
}
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
async function deleteWorkHistory() {
if (!currentFromDataEmployee.value.employeeWork) return;
const res = await employeeStore.deleteByIdWork({
employeeId: currentFromDataEmployee.value.id || '',
workId:
currentFromDataEmployee.value.employeeWork[state.value.currentIndex]
?.id,
});
if (res) {
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
}
async function deleteHealthCheck() {
if (!currentFromDataEmployee.value.employeeCheckup) return;
const res = await employeeStore.deleteByIdCheckUp({
employeeId: currentFromDataEmployee.value.id || '',
checkUpId:
currentFromDataEmployee.value.employeeCheckup[state.value.currentIndex]
?.id,
});
if (res) {
currentFromDataEmployee.value.employeeCheckup.splice(
state.value.currentIndex,
1,
);
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
async function submitWorkHistory() {
if (!currentFromDataEmployee.value.employeeWork) return;
if (
!currentFromDataEmployee.value.employeeWork[state.value.currentIndex].id
) {
const res = await employeeStore.createEmployeeWork(
currentFromDataEmployee.value?.id || '',
currentFromDataEmployee.value.employeeWork[state.value.currentIndex],
);
if (res) {
currentFromDataEmployee.value.employeeWork[
state.value.currentIndex
].id = res.id;
currentFromDataEmployee.value.employeeWork[
state.value.currentIndex
].statusSave = true;
}
} else {
2024-08-09 15:27:16 +07:00
const data =
currentFromDataEmployee.value?.employeeWork[state.value.currentIndex];
const res = await employeeStore.editByIdEmployeeWork(
currentFromDataEmployee.value?.id || '',
2024-08-09 15:27:16 +07:00
data,
);
if (res) {
currentFromDataEmployee.value.employeeWork[
state.value.currentIndex
].statusSave = true;
}
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
async function submitHealthCheck() {
if (!currentFromDataEmployee.value.employeeCheckup) return;
if (
!currentFromDataEmployee.value.employeeCheckup[state.value.currentIndex]
.id
) {
const res = await employeeStore.createEmployeeCheckup(
state.value.currentEmployee?.id || '',
currentFromDataEmployee.value.employeeCheckup[state.value.currentIndex],
);
if (res) {
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndex
].id = res.id;
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndex
].statusSave = true;
}
} else {
2024-08-09 02:10:06 +00:00
const data =
currentFromDataEmployee.value.employeeCheckup[state.value.currentIndex];
const res = await employeeStore.editByIdEmployeeCheckup(
state.value.currentEmployee?.id || '',
2024-08-09 02:10:06 +00:00
data,
);
if (res) {
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndex
].statusSave = true;
}
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
2024-09-11 16:53:08 +07:00
async function submitPersonal(imgList?: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
}) {
2024-08-06 16:33:17 +07:00
if (state.value.dialogType === 'create') {
2024-09-11 16:53:08 +07:00
const res = await employeeStore.create(
{
...currentFromDataEmployee.value,
customerBranchId: state.value.formDataEmployeeOwner?.id || '',
2024-08-06 16:33:17 +07:00
2024-09-11 16:53:08 +07:00
employeeWork: [],
employeeCheckup: [],
employeeOtherInfo: undefined,
},
imgList,
);
2024-08-07 13:30:05 +07:00
if (res) {
await assignFormDataEmployee(res.id);
currentFromDataEmployee.value.id = res.id;
2024-08-07 13:30:05 +07:00
state.value.statusSavePersonal = true;
}
2024-08-06 16:33:17 +07:00
}
if (state.value.dialogType === 'edit') {
2024-08-07 13:30:05 +07:00
const res = await employeeStore.editById(
state.value.currentEmployee?.id || '',
{
...currentFromDataEmployee.value,
status:
state.value.currentEmployee?.status === 'CREATED'
? 'ACTIVE'
: state.value.currentEmployee?.status,
customerBranchId: state.value.formDataEmployeeOwner?.id || '',
employeeWork: [],
employeeCheckup: [],
employeeOtherInfo: undefined,
},
);
if (res) {
await assignFormDataEmployee(res.id);
state.value.statusSavePersonal = true;
}
2024-08-06 16:33:17 +07:00
}
}
2024-08-07 13:30:05 +07:00
async function assignFormDataEmployee(id?: string) {
if (!id) {
resetEmployeeData = structuredClone(defaultFormData);
return;
}
const _data = await employeeStore.fetchById(id);
2024-08-05 16:53:19 +07:00
if (_data) {
const _attach = await employeeStore.listAttachment(_data.id);
state.value.currentEmployee = _data;
2024-08-05 16:53:19 +07:00
const {
2024-08-07 13:30:05 +07:00
createdAt,
createdByUserId,
statusOrder,
2024-08-05 16:53:19 +07:00
province,
district,
subDistrict,
2024-08-07 13:30:05 +07:00
updatedAt,
updatedByUserId,
2024-08-05 16:53:19 +07:00
createdBy,
updatedBy,
profileImageUrl,
...payload
} = _data;
2024-08-05 16:53:19 +07:00
2024-08-07 13:30:05 +07:00
resetEmployeeData = {
...payload,
2024-08-05 16:53:19 +07:00
provinceId: province?.id,
districtId: district?.id,
subDistrictId: subDistrict?.id,
employeeCheckup: structuredClone(
payload.employeeCheckup?.length === 0
? defaultFormData.employeeCheckup
: payload.employeeCheckup?.map((item) => ({
...item,
statusSave: true,
})),
),
2024-08-09 15:27:16 +07:00
employeeOtherInfo: structuredClone(
{
...payload.employeeOtherInfo,
statusSave: !!payload.employeeOtherInfo?.id ? true : false,
2024-08-09 15:27:16 +07:00
} || {},
),
employeeWork: structuredClone(
payload.employeeWork?.length === 0
? defaultFormData.employeeWork
: payload.employeeWork?.map((item) => ({
2024-08-09 15:27:16 +07:00
...item,
statusSave: true,
})),
),
file: _attach
? await Promise.all(
2024-08-28 14:43:51 +07:00
_attach.map(async (name) => {
const fragment = name.split('-');
const group = fragment.length === 1 ? 'other' : fragment.at(0);
return {
2024-08-28 14:43:51 +07:00
url: await employeeStore.getAttachment(_data.id, name),
name,
group,
};
}),
)
: [],
2024-08-05 16:53:19 +07:00
};
2024-08-07 13:30:05 +07:00
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
2024-08-05 16:53:19 +07:00
const foundBranch = await customerStore.fetchListCustomeBranchById(
payload.customerBranchId,
2024-08-05 16:53:19 +07:00
);
state.value.currentEmployeeCode = payload.code;
2024-08-05 16:53:19 +07:00
2024-09-11 16:53:08 +07:00
state.value.profileUrl =
`${baseUrl}/employee/${id}/image/${_data.selectedImage}` || '';
2024-08-05 16:53:19 +07:00
profileImageUrl
? (state.value.profileSubmit = true)
: (state.value.profileSubmit = false);
state.value.formDataEmployeeOwner = { ...foundBranch };
2024-08-22 09:33:17 +07:00
if (
foundBranch.address === payload.address &&
foundBranch.zipCode === payload.zipCode
2024-08-22 09:33:17 +07:00
) {
2024-08-05 16:53:19 +07:00
state.value.formDataEmployeeSameAddr = true;
} else {
state.value.formDataEmployeeSameAddr = false;
}
2024-08-13 16:43:50 +07:00
if (
state.value.infoEmployeePersonCard &&
Array.isArray(state.value.infoEmployeePersonCard) &&
state.value.infoEmployeePersonCard.length > 0 &&
profileImageUrl !== null
) {
if (typeof state.value.infoEmployeePersonCard[0] === 'object') {
state.value.infoEmployeePersonCard[0].img = profileImageUrl;
}
2024-08-05 16:53:19 +07:00
}
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,
2024-08-09 15:27:16 +07:00
submitOther,
submitWorkHistory,
2024-08-06 16:33:17 +07:00
submitPersonal,
submitHealthCheck,
2024-08-06 16:33:17 +07:00
deleteWorkHistory,
deleteHealthCheck,
2024-08-05 16:53:19 +07:00
resetFormDataEmployee,
assignFormDataEmployee,
employeeFilterOwnerBranch,
isFormDataDifferent,
2024-08-05 16:53:19 +07:00
};
2024-08-02 13:58:44 +07:00
});