2024-10-01 15:02:18 +07:00
|
|
|
import { defineStore } from 'pinia';
|
2024-10-02 14:05:36 +07:00
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
2024-10-03 09:47:43 +07:00
|
|
|
// NOTE: Import types
|
2024-10-02 14:05:36 +07:00
|
|
|
import { QuotationPayload, EmployeeWorker } from 'src/stores/quotations/types';
|
2024-10-01 15:02:18 +07:00
|
|
|
|
2024-10-03 09:47:43 +07:00
|
|
|
// NOTE: Import stores
|
2024-10-03 09:41:06 +07:00
|
|
|
import { useQuotationStore } from 'stores/quotations';
|
|
|
|
|
|
2024-10-03 14:57:14 +07:00
|
|
|
const DEFAULT_DATA: QuotationPayload = {
|
2024-10-03 09:56:50 +07:00
|
|
|
productServiceList: [],
|
|
|
|
|
urgent: false,
|
|
|
|
|
customerBranchId: '',
|
|
|
|
|
worker: [],
|
|
|
|
|
workerCount: 0,
|
|
|
|
|
payBillDate: new Date(),
|
|
|
|
|
paySplit: [],
|
|
|
|
|
paySplitCount: 0,
|
|
|
|
|
payCondition: 'Full',
|
|
|
|
|
dueDate: new Date(),
|
|
|
|
|
documentReceivePoint: '',
|
|
|
|
|
contactTel: '',
|
|
|
|
|
contactName: '',
|
|
|
|
|
workName: '',
|
|
|
|
|
actorName: '',
|
|
|
|
|
status: 'CREATED',
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-01 15:02:18 +07:00
|
|
|
export const useQuotationForm = defineStore('form-quotation', () => {
|
2024-10-03 09:42:42 +07:00
|
|
|
const quotationStore = useQuotationStore();
|
|
|
|
|
|
2024-10-03 09:56:50 +07:00
|
|
|
let resetFormData = structuredClone(DEFAULT_DATA);
|
2024-10-02 14:05:36 +07:00
|
|
|
|
2024-10-03 14:45:39 +07:00
|
|
|
const currentTreeData = ref();
|
2024-10-03 14:57:14 +07:00
|
|
|
const currentFormData = ref<QuotationPayload & { id?: string }>(
|
|
|
|
|
structuredClone(resetFormData),
|
|
|
|
|
);
|
2024-10-03 09:51:58 +07:00
|
|
|
const currentFormState = ref<{
|
|
|
|
|
mode: null | 'info' | 'create' | 'edit';
|
|
|
|
|
}>({
|
|
|
|
|
mode: null,
|
|
|
|
|
});
|
2024-10-02 14:05:36 +07:00
|
|
|
|
|
|
|
|
function isFormDataDifferent() {
|
|
|
|
|
const { ...resetData } = resetFormData;
|
|
|
|
|
const { ...currData } = currentFormData.value;
|
|
|
|
|
|
|
|
|
|
return JSON.stringify(resetData) !== JSON.stringify(currData);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 09:41:06 +07:00
|
|
|
function resetForm(clean = false) {
|
|
|
|
|
if (clean) {
|
2024-10-03 09:56:50 +07:00
|
|
|
currentFormData.value = structuredClone(DEFAULT_DATA);
|
|
|
|
|
resetFormData = structuredClone(DEFAULT_DATA);
|
2024-10-03 09:41:06 +07:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentFormData.value = structuredClone(resetFormData);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 14:43:41 +07:00
|
|
|
async function assignFormData(id: string, mode: 'info' | 'edit' = 'info') {
|
2024-10-03 09:47:43 +07:00
|
|
|
const data = await quotationStore.getQuotation(id);
|
|
|
|
|
|
|
|
|
|
if (!data) return; // NOTE: Error should be handled globally by axios instance
|
2024-10-03 09:51:58 +07:00
|
|
|
|
2024-10-03 14:43:41 +07:00
|
|
|
resetFormData = Object.assign(data, {
|
|
|
|
|
dueDate: new Date(data.dueDate),
|
|
|
|
|
payBillDate: data.payBillDate ? new Date(data.payBillDate) : undefined,
|
|
|
|
|
worker: data.worker.map((v) =>
|
|
|
|
|
Object.assign(v.employee, {
|
|
|
|
|
dateOfBirth: new Date(v.employee.dateOfBirth),
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
currentFormData.value = structuredClone(resetFormData);
|
|
|
|
|
|
|
|
|
|
currentFormState.value.mode = mode;
|
2024-10-03 09:47:43 +07:00
|
|
|
}
|
2024-10-03 09:41:06 +07:00
|
|
|
|
2024-10-03 14:57:14 +07:00
|
|
|
async function submitQuotation() {
|
|
|
|
|
if (currentFormState.value.mode === 'create') {
|
2024-10-03 14:57:46 +07:00
|
|
|
await quotationStore.createQuotation(currentFormData.value);
|
2024-10-03 14:57:14 +07:00
|
|
|
}
|
|
|
|
|
if (currentFormState.value.mode === 'edit' && currentFormData.value.id) {
|
2024-10-03 14:57:46 +07:00
|
|
|
await quotationStore.editQuotation({
|
|
|
|
|
...currentFormData.value,
|
2024-10-03 14:58:29 +07:00
|
|
|
id: currentFormData.value.id,
|
2024-10-03 14:57:46 +07:00
|
|
|
});
|
2024-10-03 14:57:14 +07:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 09:51:58 +07:00
|
|
|
currentFormState.value.mode = 'info';
|
|
|
|
|
}
|
2024-10-03 09:41:06 +07:00
|
|
|
|
2024-10-03 09:28:47 +07:00
|
|
|
function injectNewEmployee(data: EmployeeWorker) {
|
2024-10-02 14:05:36 +07:00
|
|
|
currentFormData.value.worker.push({
|
|
|
|
|
alienReferencNumber: data.alienReferencNumber,
|
|
|
|
|
documentExpireDate: data.documentExpireDate,
|
|
|
|
|
lastNameEN: data.lastNameEN,
|
|
|
|
|
lastName: data.lastName,
|
|
|
|
|
middleNameEN: data.middleNameEN,
|
|
|
|
|
middleName: data.middleName,
|
|
|
|
|
firstNameEN: data.firstNameEN,
|
|
|
|
|
firstName: data.firstName,
|
|
|
|
|
namePrefix: data.namePrefix,
|
|
|
|
|
nationality: data.nationality,
|
|
|
|
|
gender: data.gender,
|
|
|
|
|
dateOfBirth: data.dateOfBirth,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
currentFormData,
|
2024-10-03 14:43:41 +07:00
|
|
|
currentFormState,
|
2024-10-02 14:05:36 +07:00
|
|
|
|
2024-10-03 09:28:47 +07:00
|
|
|
injectNewEmployee,
|
2024-10-03 09:41:06 +07:00
|
|
|
isFormDataDifferent,
|
|
|
|
|
resetForm,
|
|
|
|
|
assignFormData,
|
|
|
|
|
|
2024-10-03 14:45:39 +07:00
|
|
|
submitQuotation,
|
2024-10-02 14:05:36 +07:00
|
|
|
};
|
2024-10-01 15:02:18 +07:00
|
|
|
});
|