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

1416 lines
39 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-10-02 14:04:46 +07:00
editCustomerId: undefined,
2024-08-05 10:25:09 +07:00
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() {
2024-09-17 18:01:13 +07:00
const { status: resetStatus, ...resetData } = resetFormData;
const { status: currStatus, ...currData } = currentFormData.value;
return JSON.stringify(resetData) !== JSON.stringify(currData);
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;
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) => ({
2024-09-17 10:00:35 +07:00
firstName: v.firstName,
firstNameEN: v.firstNameEN,
lastName: v.lastName,
lastNameEN: v.lastNameEN,
gender: v.gender,
birthDate: v.birthDate,
namePrefix: v.namePrefix,
2024-08-27 12:55:11 +07:00
wageRate: v.wageRate,
2024-09-17 10:00:35 +07:00
wageRateText: v.wageRateText,
payDate: v.payDate,
2024-08-27 12:55:11 +07:00
jobDescription: v.jobDescription,
jobPosition: v.jobPosition,
businessType: v.businessType,
employmentOffice: v.employmentOffice,
2024-09-17 10:00:35 +07:00
employmentOfficeEN: v.employmentOfficeEN,
2024-08-27 12:55:11 +07:00
telephoneNo: v.telephoneNo,
2024-09-17 10:00:35 +07:00
contactName: v.contactName,
2024-08-27 12:55:11 +07:00
email: v.email,
2024-09-17 10:00:35 +07:00
subDistrictId: v.subDistrictId,
districtId: v.districtId,
provinceId: v.provinceId,
streetEN: v.streetEN,
street: v.street,
mooEN: v.mooEN,
moo: v.moo,
soiEN: v.soiEN,
soi: v.soi,
2024-08-27 12:55:11 +07:00
addressEN: v.addressEN,
address: v.address,
2024-09-17 10:00:35 +07:00
authorizedCapital: v.authorizedCapital,
registerDate: v.registerDate,
registerNameEN: v.registerNameEN,
registerName: v.registerName,
legalPersonNo: v.legalPersonNo,
citizenId: v.citizenId,
codeCustomer: v.codeCustomer,
updatedByUserId: v.updatedByUserId,
updatedAt: v.updatedAt,
createdByUserId: v.createdByUserId,
createdAt: v.createdAt,
code: v.code,
statusOrder: v.statusOrder,
2024-08-27 12:55:11 +07:00
status: v.status,
customerId: v.customerId,
2024-09-17 10:00:35 +07:00
id: v.id,
homeCode: v.homeCode,
contactTel: v.contactTel,
officeTel: v.officeTel,
2024-10-28 19:59:17 +07:00
agentUserId: v.agentUserId || undefined,
2024-09-17 10:00:35 +07:00
customerName: v.customerName,
authorizedName: v.authorizedName,
authorizedNameEN: v.authorizedNameEN,
payDateEN: v.payDateEN,
2024-08-27 12:55:11 +07:00
statusSave: true,
2024-11-12 16:12:18 +07:00
file: await customerStore
.listAttachment({ parentId: v.id })
.then(async (r) => {
if (r) {
return await Promise.all(
r.map(async (item) => {
const fragment = item.split('-');
const group =
fragment.length === 1 ? 'other' : fragment.at(0);
return {
url: await customerStore.getAttachment({
parentId: v.id,
name: item,
}),
name: item,
group: group,
};
}),
);
}
return [];
}),
2024-08-27 12:55:11 +07:00
})),
);
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-11-11 17:47:22 +07:00
codeCustomer: undefined,
2024-09-16 14:38:04 +07:00
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
: '',
2024-09-26 10:09:53 +07:00
namePrefix: currentFormData.value.customerBranch?.at(0)?.namePrefix || '',
firstName: currentFormData.value.customerBranch?.at(0)?.firstName || '',
lastName: currentFormData.value.customerBranch?.at(0)?.lastName || '',
firstNameEN:
currentFormData.value.customerBranch?.at(0)?.firstNameEN || '',
lastNameEN: currentFormData.value.customerBranch?.at(0)?.lastNameEN || '',
telephoneNo:
currentFormData.value.customerBranch?.at(0)?.telephoneNo || '',
gender: currentFormData.value.customerBranch?.at(0)?.gender || '',
birthDate: currentFormData.value.customerBranch?.at(0)?.birthDate || '',
businessType:
currentFormData.value.customerBranch?.at(0)?.businessType || '',
jobPosition:
currentFormData.value.customerBranch?.at(0)?.jobPosition || '',
jobDescription:
currentFormData.value.customerBranch?.at(0)?.jobDescription || '',
payDate: currentFormData.value.customerBranch?.at(0)?.payDate || '',
payDateEN: currentFormData.value.customerBranch?.at(0)?.payDateEN || '',
wageRate: currentFormData.value.customerBranch?.at(0)?.wageRate || 0,
wageRateText:
currentFormData.value.customerBranch?.at(0)?.wageRateText || '',
homeCode: currentFormData.value.customerBranch?.at(0)?.homeCode || '',
employmentOffice:
currentFormData.value.customerBranch?.at(0)?.employmentOffice || '',
employmentOfficeEN:
currentFormData.value.customerBranch?.at(0)?.employmentOfficeEN || '',
2024-09-16 14:38:04 +07:00
address: '',
addressEN: '',
street: '',
streetEN: '',
moo: '',
mooEN: '',
soi: '',
soiEN: '',
provinceId: '',
districtId: '',
subDistrictId: '',
2024-09-26 10:09:53 +07:00
contactName:
currentFormData.value.customerBranch?.at(0)?.contactTel || '',
email: currentFormData.value.customerBranch?.at(0)?.email || '',
contactTel: currentFormData.value.customerBranch?.at(0)?.contactTel || '',
officeTel: currentFormData.value.customerBranch?.at(0)?.officeTel || '',
2024-10-28 13:01:46 +07:00
agentUserId:
2024-10-28 19:59:17 +07:00
currentFormData.value.customerBranch?.at(0)?.agentUserId || undefined,
2024-09-16 14:38:04 +07:00
status: 'CREATED',
2024-09-26 10:09:53 +07:00
customerName:
currentFormData.value.customerBranch?.at(0)?.customerName || '',
registerName:
currentFormData.value.customerBranch?.at(0)?.registerName || '',
registerNameEN:
currentFormData.value.customerBranch?.at(0)?.registerNameEN || '',
registerDate:
currentFormData.value.customerBranch?.at(0)?.registerDate || null,
authorizedCapital:
currentFormData.value.customerBranch?.at(0)?.authorizedCapital || '',
authorizedName:
currentFormData.value.customerBranch?.at(0)?.authorizedName || '',
authorizedNameEN:
currentFormData.value.customerBranch?.at(0)?.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-09-17 18:01:13 +07:00
const defaultFormData: CustomerBranchCreate & {
id?: string;
codeCustomer?: string;
} = {
2024-11-20 14:41:55 +07:00
id: undefined,
2024-09-17 18:01:13 +07:00
customerId: '',
// branchCode: '',
// codeCustomer: '',
legalPersonNo: '',
citizenId: '',
namePrefix: '',
firstName: '',
lastName: '',
firstNameEN: '',
lastNameEN: '',
telephoneNo: '',
gender: '',
birthDate: '',
businessType: '',
jobPosition: '',
jobDescription: '',
2024-09-16 14:38:04 +07:00
payDate: '',
payDateEN: '',
2024-09-17 18:01:13 +07:00
wageRate: 0,
wageRateText: '',
homeCode: '',
2024-08-21 14:42:56 +07:00
employmentOffice: '',
2024-09-17 18:01:13 +07:00
employmentOfficeEN: '',
2024-08-21 14:42:56 +07:00
address: '',
2024-09-17 18:01:13 +07:00
addressEN: '',
street: '',
streetEN: '',
moo: '',
mooEN: '',
soi: '',
soiEN: '',
provinceId: '',
districtId: '',
subDistrictId: '',
contactName: '',
email: '',
contactTel: '',
officeTel: '',
2024-10-28 13:01:46 +07:00
agentUserId: undefined,
2024-08-09 15:09:46 +07:00
status: 'CREATED',
2024-09-17 18:01:13 +07:00
customerName: '',
2024-08-21 14:42:56 +07:00
registerName: '',
2024-09-17 18:01:13 +07:00
registerNameEN: '',
registerDate: null,
authorizedCapital: '',
authorizedName: '',
authorizedNameEN: '',
2024-08-29 14:54:06 +07:00
file: [],
2024-08-09 15:09:46 +07:00
};
let resetFormData = structuredClone(defaultFormData);
2024-11-20 17:35:33 +07:00
const currentFormData = ref<
CustomerBranchCreate & { id?: string; codeCustomer?: string }
>(structuredClone(defaultFormData));
2024-08-09 15:09:46 +07:00
const state = ref<{
dialogType: 'info' | 'create' | 'edit';
dialogOpen: boolean;
dialogModal: boolean;
currentCustomerId: string;
2024-11-20 17:35:33 +07:00
customerType?: 'CORP' | 'PERS';
2024-08-09 15:09:46 +07:00
}>({
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 = {
2024-09-17 18:01:13 +07:00
id: _data.id,
2024-08-09 15:09:46 +07:00
code: _data.code,
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
jobDescription: _data.jobDescription,
jobPosition: _data.jobPosition,
businessType: _data.businessType,
employmentOffice: _data.employmentOffice,
2024-09-17 18:01:13 +07:00
employmentOfficeEN: _data.employmentOfficeEN,
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,
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-09-17 18:01:13 +07:00
namePrefix: _data.namePrefix,
firstName: _data.firstName,
firstNameEN: _data.firstNameEN,
lastName: _data.lastName,
lastNameEN: _data.lastNameEN,
gender: _data.gender,
birthDate: _data.birthDate,
moo: _data.moo,
mooEN: _data.mooEN,
soi: _data.soi,
soiEN: _data.soiEN,
street: _data.street,
streetEN: _data.streetEN,
wageRateText: _data.wageRateText,
contactTel: _data.contactTel,
officeTel: _data.officeTel,
2024-10-28 19:59:17 +07:00
agentUserId: _data.agentUserId || undefined,
2024-09-17 18:01:13 +07:00
codeCustomer: _data.codeCustomer,
customerName: _data.customerName,
homeCode: _data.homeCode,
authorizedName: _data.authorizedName,
authorizedNameEN: _data.authorizedNameEN,
2024-08-29 14:54:06 +07:00
file: [],
2024-08-09 15:09:46 +07:00
};
currentFormData.value = structuredClone(resetFormData);
}
2024-11-20 14:41:42 +07:00
function resetForm() {
resetFormData = structuredClone(defaultFormData);
currentFormData.value = structuredClone(resetFormData);
}
2024-08-09 15:09:46 +07:00
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:
2024-11-20 17:35:33 +07:00
(state.value.customerType ||
customerFormStore.currentFormData.customerType) === 'CORP'
2024-08-28 08:48:19 +07:00
? 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-11-20 14:41:42 +07:00
resetForm,
2024-08-09 15:09:46 +07:00
};
});
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;
2024-10-03 17:35:56 +07:00
currentIndexPassport: number;
currentIndexVisa: number;
currentIndexCheckup: number;
currentIndexWorkHistory: 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;
}
| 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-10-03 17:35:56 +07:00
currentIndexPassport: -1,
currentIndexVisa: -1,
currentIndexCheckup: -1,
currentIndexWorkHistory: -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: '',
2024-08-05 16:53:19 +07:00
dateOfBirth: null,
gender: '',
nationality: '',
2024-08-06 07:55:05 +00:00
status: 'CREATED',
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: '',
street: '',
streetEN: '',
moo: '',
mooEN: '',
soi: '',
soiEN: '',
2024-08-05 16:53:19 +07:00
subDistrictId: '',
districtId: '',
provinceId: '',
employeeCheckup: [
{
coverageExpireDate: null,
coverageStartDate: null,
insuranceCompany: '',
medicalBenefitScheme: '',
remark: '',
hospitalName: '',
provinceId: '',
checkupResult: '',
checkupType: '',
},
],
2024-08-05 16:53:19 +07:00
employeeWork: [
{
workPermitExpireDate: null,
2024-10-30 09:04:55 +07:00
workPermitIssueDate: null,
2024-08-05 16:53:19 +07:00
workPermitNo: '',
workplace: '',
jobType: '',
positionName: '',
ownerName: '',
},
],
employeeInCountryNotice: [
2024-08-05 16:53:19 +07:00
{
noticeNumber: '',
noticeDate: '',
nextNoticeDate: new Date(),
tmNumber: '',
entryDate: new Date(),
travelBy: '',
travelFrom: '',
},
],
employeeVisa: [
{
arrivalAt: '',
arrivalTMNo: '',
arrivalTM: '',
mrz: undefined,
entryCount: 0,
issuePlace: '',
issueCountry: '',
issueDate: new Date(),
type: '',
expireDate: new Date(),
remark: undefined,
workerType: '',
number: '',
},
],
employeePassport: [
{
birthCountry: '',
previousPassportRef: '',
issuePlace: '',
issueCountry: '',
issueDate: new Date(),
type: '',
expireDate: new Date(),
birthDate: new Date(),
workerStatus: '',
nationality: '',
gender: '',
lastNameEN: '',
lastName: '',
middleNameEN: '',
middleName: '',
firstNameEN: '',
firstName: '',
namePrefix: '',
number: '',
},
],
2024-08-05 16:53:19 +07:00
employeeOtherInfo: {
citizenId: '',
fatherFirstName: '',
fatherLastName: '',
fatherFirstNameEN: '',
fatherLastNameEN: '',
fatherBirthPlace: '',
motherFirstName: '',
motherLastName: '',
motherFirstNameEN: '',
motherLastNameEN: '',
motherBirthPlace: '',
},
};
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) {
2024-10-03 17:35:56 +07:00
state.value.currentIndexPassport = -1;
state.value.currentIndexVisa = -1;
2024-08-07 13:30:05 +07:00
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-10-03 17:35:56 +07:00
async function submitPassport() {
if (
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
] === undefined
)
return;
if (
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
].id === undefined
) {
const res = await employeeStore.postMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'passport',
meta: currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
],
});
if (res) {
currentFromDataEmployee.value.employeePassport[
state.value.currentIndexPassport
].id = res.id;
}
}
if (
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
].id !== undefined
) {
2024-11-11 14:59:00 +07:00
const { id, employeeId, updatedAt, createdAt, ...payload } =
2024-10-03 17:35:56 +07:00
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
];
await employeeStore.putMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'passport',
metaId:
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
].id || '',
meta: payload,
});
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
state.value.currentIndexPassport = -1;
}
async function submitVisa() {
if (
currentFromDataEmployee.value.employeeVisa?.[
state.value.currentIndexVisa
] === undefined
)
return;
if (
currentFromDataEmployee.value.employeeVisa?.[state.value.currentIndexVisa]
.id === undefined
) {
const res = await employeeStore.postMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'visa',
meta: currentFromDataEmployee.value.employeeVisa?.[
state.value.currentIndexVisa
],
});
if (res) {
currentFromDataEmployee.value.employeeVisa[
state.value.currentIndexVisa
].id = res.id;
}
}
if (
currentFromDataEmployee.value.employeeVisa?.[state.value.currentIndexVisa]
.id !== undefined
) {
const { id, updatedAt, createdAt, employeeId, ...payload } =
currentFromDataEmployee.value.employeeVisa?.[
state.value.currentIndexVisa
];
await employeeStore.putMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'visa',
metaId:
currentFromDataEmployee.value.employeeVisa?.[
state.value.currentIndexVisa
].id || '',
meta: payload,
});
}
await assignFormDataEmployee(currentFromDataEmployee.value.id);
state.value.currentIndexVisa = -1;
}
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);
}
2024-11-12 16:33:58 +07:00
async function deletePassport(index: number) {
2024-10-03 17:35:56 +07:00
const res = await employeeStore.delMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'passport',
2024-11-12 16:33:58 +07:00
metaId: currentFromDataEmployee.value.employeePassport?.[index].id || '',
2024-10-03 17:35:56 +07:00
});
if (res) {
2024-11-12 16:33:58 +07:00
currentFromDataEmployee.value.employeePassport?.splice(index, 1);
2024-10-03 17:35:56 +07:00
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
}
2024-11-12 16:33:58 +07:00
async function deleteVisa(index: number) {
const res = await employeeStore.delMeta({
parentId: currentFromDataEmployee.value.id || '',
group: 'visa',
2024-11-12 16:33:58 +07:00
metaId: currentFromDataEmployee.value.employeeVisa?.[index].id || '',
});
if (res) {
2024-11-12 16:33:58 +07:00
currentFromDataEmployee.value.employeeVisa?.splice(index, 1);
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
}
2024-11-12 16:33:58 +07:00
async function deleteWorkHistory(index: number) {
if (!currentFromDataEmployee.value.employeeWork) return;
const res = await employeeStore.deleteByIdWork({
employeeId: currentFromDataEmployee.value.id || '',
2024-11-12 16:33:58 +07:00
workId: currentFromDataEmployee.value.employeeWork[index]?.id,
});
if (res) {
state.value.currentIndexWorkHistory = -1;
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
}
2024-11-12 16:33:58 +07:00
async function deleteHealthCheck(index: number) {
if (!currentFromDataEmployee.value.employeeCheckup) return;
const res = await employeeStore.deleteByIdCheckUp({
employeeId: currentFromDataEmployee.value.id || '',
2024-11-12 16:33:58 +07:00
checkUpId: currentFromDataEmployee.value.employeeCheckup[index]?.id,
});
if (res) {
state.value.currentIndexCheckup = -1;
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
}
async function submitWorkHistory() {
if (!currentFromDataEmployee.value.employeeWork) return;
if (
!currentFromDataEmployee.value.employeeWork[
state.value.currentIndexWorkHistory
].id
) {
const res = await employeeStore.createEmployeeWork(
currentFromDataEmployee.value?.id || '',
currentFromDataEmployee.value.employeeWork[
state.value.currentIndexWorkHistory
],
);
if (res) {
currentFromDataEmployee.value.employeeWork[
state.value.currentIndexWorkHistory
].id = res.id;
currentFromDataEmployee.value.employeeWork[
state.value.currentIndexWorkHistory
].statusSave = true;
}
} else {
2024-08-09 15:27:16 +07:00
const data =
currentFromDataEmployee.value?.employeeWork[
state.value.currentIndexWorkHistory
];
2024-08-09 15:27:16 +07:00
const res = await employeeStore.editByIdEmployeeWork(
currentFromDataEmployee.value?.id || '',
2024-08-09 15:27:16 +07:00
data,
);
if (res) {
currentFromDataEmployee.value.employeeWork[
state.value.currentIndexWorkHistory
].statusSave = true;
}
}
state.value.currentIndexWorkHistory = -1;
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
async function submitHealthCheck() {
if (!currentFromDataEmployee.value.employeeCheckup) return;
if (
!currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndexCheckup
].id
) {
const res = await employeeStore.createEmployeeCheckup(
state.value.currentEmployee?.id || '',
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndexCheckup
],
);
if (res) {
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndexCheckup
].id = res.id;
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndexCheckup
].statusSave = true;
}
} else {
2024-08-09 02:10:06 +00:00
const data =
currentFromDataEmployee.value.employeeCheckup[
state.value.currentIndexCheckup
];
2024-08-09 02:10:06 +00:00
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.currentIndexCheckup
].statusSave = true;
}
}
state.value.currentIndexCheckup = -1;
await assignFormDataEmployee(currentFromDataEmployee.value.id);
}
2024-09-26 14:54:47 +07:00
async function submitPersonal(imgList: {
2024-09-11 16:53:08 +07:00
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) {
2024-11-12 16:09:24 +07:00
const _attach = await employeeStore.listAttachment({
parentId: _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,
...payload
} = _data;
2024-08-05 16:53:19 +07:00
2024-08-07 13:30:05 +07:00
resetEmployeeData = {
...payload,
2024-10-02 14:04:46 +07:00
provinceId: province?.id || '',
districtId: district?.id || '',
subDistrictId: subDistrict?.id || '',
2024-10-03 17:35:56 +07:00
employeePassport: structuredClone(
payload.employeePassport?.length === 0
? state.value.dialogModal
? defaultFormData.employeePassport
: []
2024-11-11 14:59:00 +07:00
: payload.employeePassport,
2024-10-03 17:35:56 +07:00
),
employeeVisa: structuredClone(
payload.employeeVisa?.length === 0
? state.value.dialogModal
? defaultFormData.employeeVisa
: []
: payload.employeeVisa,
),
employeeCheckup: structuredClone(
payload.employeeCheckup?.length === 0
? state.value.dialogModal
? 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
? state.value.dialogModal
? 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 {
url: await employeeStore.getAttachment({
parentId: _data.id,
name,
}),
2024-08-28 14:43:51 +07:00
name,
group,
};
}),
)
: [],
2024-08-05 16:53:19 +07:00
};
2024-08-07 13:30:05 +07:00
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
2024-10-03 17:35:56 +07:00
state.value.currentIndexPassport =
(currentFromDataEmployee.value.employeePassport?.length || 0) - 1;
state.value.currentIndexVisa =
(currentFromDataEmployee.value.employeeVisa?.length || 0) - 1;
2024-10-03 17:35:56 +07:00
if (
2024-11-12 16:10:12 +07:00
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
] &&
2024-10-03 17:35:56 +07:00
currentFromDataEmployee.value.employeePassport?.[
state.value.currentIndexPassport
].id !== undefined
) {
state.value.currentIndexPassport = -1;
}
if (
currentFromDataEmployee.value.employeeVisa?.[
2024-11-12 16:10:12 +07:00
state.value.currentIndexVisa
] &&
currentFromDataEmployee.value.employeeVisa?.[
state.value.currentIndexVisa
].id !== undefined
) {
state.value.currentIndexVisa = -1;
}
2024-10-11 16:08:40 +07:00
const foundBranch = await customerStore.fetchListCustomerBranchById(
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
state.value.formDataEmployeeOwner = { ...foundBranch };
2024-08-22 09:33:17 +07:00
if (
foundBranch.address === payload.address &&
foundBranch.subDistrict.id === payload.subDistrictId
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
2024-08-13 16:43:50 +07:00
) {
if (typeof state.value.infoEmployeePersonCard[0] === 'object') {
}
2024-08-05 16:53:19 +07:00
}
flowStore.rotate();
}
}
async function employeeFilterOwnerBranch(
val: string,
update: (...args: unknown[]) => void,
) {
update(async () => {
2024-10-11 16:08:40 +07:00
const result = await customerStore.fetchListCustomerBranch({
2024-08-05 16:53:19 +07:00
includeCustomer: true,
query: val,
pageSize: 30,
});
if (result) employeeStore.ownerOption = result.result;
});
}
2024-10-03 17:35:56 +07:00
function addPassport() {
currentFromDataEmployee.value.employeePassport?.push({
birthCountry: '',
previousPassportRef: '',
issuePlace: '',
issueCountry: '',
2024-10-03 17:35:56 +07:00
issueDate: new Date(),
type: '',
2024-10-03 17:35:56 +07:00
expireDate: new Date(),
birthDate: new Date(),
workerStatus: '',
nationality: '',
gender: '',
lastNameEN: '',
lastName: '',
middleNameEN: '',
middleName: '',
firstNameEN: '',
firstName: '',
namePrefix: '',
number: '',
2024-10-03 17:35:56 +07:00
});
state.value.currentIndexPassport =
(currentFromDataEmployee.value.employeePassport?.length || 0) - 1;
}
function addVisa() {
currentFromDataEmployee.value.employeeVisa?.push({
arrivalAt: '',
arrivalTMNo: '',
arrivalTM: '',
mrz: undefined,
entryCount: 0,
issuePlace: '',
issueCountry: '',
issueDate: new Date(),
type: '',
expireDate: new Date(),
remark: undefined,
workerType: '',
number: '',
});
state.value.currentIndexVisa =
(currentFromDataEmployee.value.employeeVisa?.length || 0) - 1;
}
function addCheckup() {
currentFromDataEmployee.value.employeeCheckup?.push({
coverageExpireDate: null,
coverageStartDate: null,
insuranceCompany: '',
medicalBenefitScheme: '',
remark: '',
hospitalName: '',
provinceId: '',
checkupResult: '',
checkupType: '',
});
state.value.currentIndexCheckup =
(currentFromDataEmployee.value.employeeCheckup?.length || 0) - 1;
}
function addWorkHistory() {
currentFromDataEmployee.value.employeeWork?.push({
workPermitExpireDate: null,
workPermitIssueDate: null,
workPermitNo: '',
workplace: '',
jobType: '',
positionName: '',
ownerName: '',
});
state.value.currentIndexWorkHistory =
(currentFromDataEmployee.value.employeeWork?.length || 0) - 1;
}
2024-08-05 16:53:19 +07:00
return {
state,
currentFromDataEmployee,
resetEmployeeData,
2024-10-03 17:35:56 +07:00
addPassport,
addVisa,
addCheckup,
addWorkHistory,
2024-10-03 17:35:56 +07:00
submitPassport,
submitVisa,
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
2024-10-03 17:35:56 +07:00
deletePassport,
deleteVisa,
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
});