2024-08-05 15:29:11 +07:00
|
|
|
import { ref, watch } from 'vue';
|
2024-08-02 13:58:44 +07:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
import { CustomerCreate } from 'src/stores/customer/types';
|
|
|
|
|
import useMyBranch from 'src/stores/my-branch';
|
|
|
|
|
import useCustomerStore from 'src/stores/customer';
|
|
|
|
|
|
|
|
|
|
export const useCustomerForm = defineStore('form-customer', () => {
|
|
|
|
|
const customerStore = useCustomerStore();
|
|
|
|
|
const branchStore = useMyBranch();
|
|
|
|
|
|
|
|
|
|
let defaultFormData: CustomerCreate = {
|
|
|
|
|
status: 'CREATED',
|
|
|
|
|
personName: '',
|
|
|
|
|
customerType: 'CORP',
|
|
|
|
|
customerName: '',
|
|
|
|
|
customerNameEN: '',
|
|
|
|
|
taxNo: '',
|
|
|
|
|
registeredBranchId: branchStore.currentMyBranch?.id || '',
|
2024-08-05 15:09:36 +07:00
|
|
|
customerBranch: [],
|
2024-08-02 13:58:44 +07:00
|
|
|
image: null,
|
|
|
|
|
};
|
2024-08-05 15:09:36 +07:00
|
|
|
let resetFormData = structuredClone(defaultFormData);
|
2024-08-02 13:58:44 +07:00
|
|
|
|
|
|
|
|
const currentFormData = ref<CustomerCreate>(structuredClone(defaultFormData));
|
|
|
|
|
const state = ref<{
|
2024-08-02 16:13:07 +07:00
|
|
|
dialogType: 'info' | 'create' | 'edit';
|
2024-08-02 13:58:44 +07:00
|
|
|
dialogOpen: boolean;
|
2024-08-05 10:14:54 +07:00
|
|
|
dialogModal: boolean;
|
2024-08-02 13:58:44 +07:00
|
|
|
branchIndex: number;
|
2024-08-05 10:14:54 +07:00
|
|
|
saveMode: 'customer' | 'branch';
|
2024-08-05 15:09:36 +07:00
|
|
|
customerImageUrl: string;
|
|
|
|
|
imageDialog: boolean;
|
|
|
|
|
imageEdit: boolean;
|
2024-08-05 10:25:09 +07:00
|
|
|
editCustomerId?: string;
|
|
|
|
|
editCustomerBranchId?: string;
|
2024-08-02 13:58:44 +07:00
|
|
|
}>({
|
|
|
|
|
dialogType: 'info',
|
|
|
|
|
dialogOpen: false,
|
2024-08-05 10:14:54 +07:00
|
|
|
dialogModal: false,
|
2024-08-05 15:09:36 +07:00
|
|
|
imageDialog: false,
|
2024-08-02 13:58:44 +07:00
|
|
|
branchIndex: 0,
|
2024-08-05 15:09:36 +07:00
|
|
|
imageEdit: false,
|
2024-08-05 10:14:54 +07:00
|
|
|
saveMode: 'customer',
|
2024-08-05 15:09:36 +07:00
|
|
|
customerImageUrl: '',
|
2024-08-05 10:25:09 +07:00
|
|
|
editCustomerId: '',
|
|
|
|
|
editCustomerBranchId: '',
|
2024-08-02 13:58:44 +07:00
|
|
|
});
|
|
|
|
|
|
2024-08-05 15:29:11 +07:00
|
|
|
watch(
|
|
|
|
|
currentFormData,
|
|
|
|
|
(v) => (defaultFormData.customerType = v.customerType),
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-02 13:58:44 +07:00
|
|
|
function isFormDataDifferent() {
|
|
|
|
|
return (
|
2024-08-05 15:09:36 +07:00
|
|
|
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
|
2024-08-02 13:58:44 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 15:29:11 +07:00
|
|
|
function resetForm(clean = false) {
|
|
|
|
|
if (clean) {
|
|
|
|
|
defaultFormData.customerType = currentFormData.value.customerType;
|
|
|
|
|
currentFormData.value = structuredClone(defaultFormData);
|
|
|
|
|
currentFormData.value.registeredBranchId =
|
|
|
|
|
branchStore.currentMyBranch?.id || '';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 15:09:36 +07:00
|
|
|
if (!resetFormData.registeredBranchId) {
|
|
|
|
|
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentFormData.value = structuredClone(resetFormData);
|
2024-08-02 13:58:44 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-05 15:29:11 +07:00
|
|
|
async function assignFormData(id?: string) {
|
|
|
|
|
if (!id) {
|
|
|
|
|
resetFormData = structuredClone(defaultFormData);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 13:58:44 +07:00
|
|
|
const data = await customerStore.fetchById(id);
|
|
|
|
|
|
|
|
|
|
if (!data) return;
|
|
|
|
|
|
2024-08-05 15:09:36 +07:00
|
|
|
resetFormData.registeredBranchId = data.registeredBranchId;
|
|
|
|
|
resetFormData.status = data.status;
|
|
|
|
|
resetFormData.customerType = data.customerType;
|
|
|
|
|
resetFormData.customerName = data.customerName;
|
|
|
|
|
resetFormData.customerNameEN = data.customerNameEN;
|
|
|
|
|
resetFormData.personName = data.personName;
|
|
|
|
|
resetFormData.taxNo = data.taxNo;
|
|
|
|
|
resetFormData.image = null;
|
|
|
|
|
resetFormData.customerBranch = data.branch.map((v) => ({
|
2024-08-02 13:58:44 +07:00
|
|
|
id: v.id,
|
|
|
|
|
code: v.code,
|
|
|
|
|
branchNo: v.branchNo,
|
|
|
|
|
address: v.address,
|
|
|
|
|
addressEN: v.addressEN,
|
|
|
|
|
provinceId: v.province.id,
|
|
|
|
|
districtId: v.district.id,
|
|
|
|
|
subDistrictId: v.subDistrict.id,
|
|
|
|
|
zipCode: v.zipCode,
|
|
|
|
|
email: v.email,
|
|
|
|
|
telephoneNo: v.telephoneNo,
|
|
|
|
|
name: v.name,
|
|
|
|
|
status: undefined,
|
|
|
|
|
taxNo: v.taxNo,
|
|
|
|
|
nameEN: v.nameEN,
|
|
|
|
|
legalPersonNo: v.legalPersonNo,
|
|
|
|
|
registerName: v.registerName,
|
|
|
|
|
registerDate: new Date(v.registerDate),
|
|
|
|
|
authorizedCapital: v.authorizedCapital,
|
|
|
|
|
employmentOffice: v.employmentOffice,
|
|
|
|
|
bussinessType: v.bussinessType,
|
|
|
|
|
bussinessTypeEN: v.bussinessTypeEN,
|
|
|
|
|
jobPosition: v.jobPosition,
|
|
|
|
|
jobPositionEN: v.jobPositionEN,
|
|
|
|
|
jobDescription: v.jobDescription,
|
|
|
|
|
saleEmployee: v.saleEmployee,
|
|
|
|
|
payDate: new Date(v.payDate),
|
|
|
|
|
wageRate: v.wageRate,
|
|
|
|
|
}));
|
|
|
|
|
|
2024-08-05 15:09:36 +07:00
|
|
|
currentFormData.value = structuredClone(resetFormData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addCurrentCustomerBranch() {
|
|
|
|
|
currentFormData.value.customerBranch?.push({
|
|
|
|
|
id: '',
|
|
|
|
|
code: '',
|
|
|
|
|
branchNo: 1,
|
|
|
|
|
address: '',
|
|
|
|
|
addressEN: '',
|
|
|
|
|
provinceId: '',
|
|
|
|
|
districtId: '',
|
|
|
|
|
subDistrictId: '',
|
|
|
|
|
zipCode: '',
|
|
|
|
|
email: '',
|
|
|
|
|
telephoneNo: '',
|
|
|
|
|
name: '',
|
|
|
|
|
status: 'CREATED',
|
|
|
|
|
taxNo: '',
|
|
|
|
|
nameEN: '',
|
|
|
|
|
legalPersonNo: '',
|
|
|
|
|
registerName: '',
|
|
|
|
|
registerDate: new Date(),
|
|
|
|
|
authorizedCapital: '',
|
|
|
|
|
employmentOffice: '',
|
|
|
|
|
bussinessType: '',
|
|
|
|
|
bussinessTypeEN: '',
|
|
|
|
|
jobPosition: '',
|
|
|
|
|
jobPositionEN: '',
|
|
|
|
|
jobDescription: '',
|
|
|
|
|
saleEmployee: '',
|
|
|
|
|
payDate: new Date(),
|
|
|
|
|
wageRate: 0,
|
|
|
|
|
});
|
2024-08-02 13:58:44 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-05 10:14:54 +07:00
|
|
|
async function submitForm() {
|
2024-08-05 15:09:36 +07:00
|
|
|
if (state.value.saveMode === 'customer') await submitFormCustomer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function submitFormCustomer() {
|
|
|
|
|
if (state.value.dialogType === 'info') return;
|
|
|
|
|
|
|
|
|
|
if (state.value.dialogType === 'create') {
|
|
|
|
|
return await customerStore.create(currentFormData.value);
|
2024-08-05 10:14:54 +07:00
|
|
|
}
|
2024-08-05 15:09:36 +07:00
|
|
|
|
|
|
|
|
if (!state.value.editCustomerId) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Form mode is set to edit but no ID is provided. Make sure to set customer ID.',
|
|
|
|
|
);
|
2024-08-05 10:14:54 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-05 15:09:36 +07:00
|
|
|
await customerStore.editById(state.value.editCustomerId, {
|
|
|
|
|
...currentFormData.value,
|
2024-08-05 15:42:00 +07:00
|
|
|
status:
|
|
|
|
|
currentFormData.value.status !== 'CREATED'
|
|
|
|
|
? currentFormData.value.status
|
|
|
|
|
: undefined,
|
2024-08-05 15:09:36 +07:00
|
|
|
image: currentFormData.value.image || undefined,
|
|
|
|
|
customerBranch: undefined,
|
|
|
|
|
});
|
2024-08-05 10:14:54 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-02 13:58:44 +07:00
|
|
|
return {
|
|
|
|
|
state,
|
2024-08-05 15:09:36 +07:00
|
|
|
resetFormData,
|
2024-08-02 13:58:44 +07:00
|
|
|
currentFormData,
|
|
|
|
|
isFormDataDifferent,
|
2024-08-05 15:09:36 +07:00
|
|
|
resetForm,
|
2024-08-02 13:58:44 +07:00
|
|
|
assignFormData,
|
2024-08-05 10:14:54 +07:00
|
|
|
submitForm,
|
2024-08-05 15:09:36 +07:00
|
|
|
addCurrentCustomerBranch,
|
2024-08-02 13:58:44 +07:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const useEmployeeForm = defineStore('form-employee', () => {
|
|
|
|
|
return {};
|
|
|
|
|
});
|