refactor: เพิ่ม สาขาต่อสร้าง

This commit is contained in:
Net 2024-07-03 17:55:12 +07:00
parent 11f90445f8
commit 0651f374ac
2 changed files with 71 additions and 19 deletions

View file

@ -1,8 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, watch } from 'vue'; import { ref, onMounted, watch } from 'vue';
import { Status } from 'src/stores/types';
import useCustomerStore from 'src/stores/customer'; import useCustomerStore from 'src/stores/customer';
import useFlowStore from 'src/stores/flow';
import { Status } from 'src/stores/types';
import { CustomerBranch, CustomerType } from 'src/stores/customer/types'; import { CustomerBranch, CustomerType } from 'src/stores/customer/types';
import AppBox from 'components/app/AppBox.vue'; import AppBox from 'components/app/AppBox.vue';
@ -10,8 +12,10 @@ import BranchCardCustomer from 'components/03_customer-management/BranchCardCust
import PaginationComponent from 'src/components/PaginationComponent.vue'; import PaginationComponent from 'src/components/PaginationComponent.vue';
import NoData from 'components/NoData.vue'; import NoData from 'components/NoData.vue';
const flowStore = useFlowStore();
const userCustomer = useCustomerStore(); const userCustomer = useCustomerStore();
const { fetchListBranch } = userCustomer; const { fetchListCustomeBranch } = userCustomer;
const inputSearch = ref<string>(''); const inputSearch = ref<string>('');
@ -52,7 +56,7 @@ onMounted(async () => {
}); });
async function fetchList() { async function fetchList() {
const result = await fetchListBranch({ const result = await fetchListCustomeBranch({
customerId: prop.customerId, customerId: prop.customerId,
query: !!inputSearch.value ? inputSearch.value : undefined, query: !!inputSearch.value ? inputSearch.value : undefined,
page: currentPageBranch.value, page: currentPageBranch.value,

View file

@ -4,6 +4,7 @@ import { computed, ref, watch, toRaw } from 'vue';
import useCustomerStore from 'src/stores/customer'; import useCustomerStore from 'src/stores/customer';
import useEmployeeStore from 'src/stores/employee'; import useEmployeeStore from 'src/stores/employee';
import useOptionStore from 'src/stores/options'; import useOptionStore from 'src/stores/options';
import useBranchStore from 'src/stores/branch';
import { Status } from 'src/stores/types'; import { Status } from 'src/stores/types';
@ -62,6 +63,10 @@ import useFlowStore from 'src/stores/flow';
const { t, locale } = useI18n(); const { t, locale } = useI18n();
const utilsStore = useUtilsStore(); const utilsStore = useUtilsStore();
const userCustomer = useCustomerStore(); const userCustomer = useCustomerStore();
const userBranch = useBranchStore();
const { fetchList: fetchListBranch } = userBranch;
const { const {
create, create,
getStatsCustomer, getStatsCustomer,
@ -70,8 +75,8 @@ const {
fetchListById, fetchListById,
createBranch, createBranch,
editBranchById, editBranchById,
fetchListBranch, fetchListCustomeBranch,
fetchListBranchById, fetchListCustomeBranchById,
} = userCustomer; } = userCustomer;
const flowStore = useFlowStore(); const flowStore = useFlowStore();
const employeeStore = useEmployeeStore(); const employeeStore = useEmployeeStore();
@ -83,8 +88,11 @@ const formData = ref<CustomerCreate>({
customerName: '', customerName: '',
customerNameEN: '', customerNameEN: '',
taxNo: '', taxNo: '',
registeredBranchId: '',
customerBranch: [ customerBranch: [
{ {
code: '',
branchNo: 1,
address: '', address: '',
addressEN: '', addressEN: '',
provinceId: '', provinceId: '',
@ -234,6 +242,7 @@ const formDataEmployee = ref<EmployeeCreate>({
}, },
}); });
const allBranch = ref<{ id: string; name: string }[]>();
const indexTab = ref<number>(0); const indexTab = ref<number>(0);
const statusToggle = ref<boolean>(false); const statusToggle = ref<boolean>(false);
const profileSubmit = ref<boolean>(false); const profileSubmit = ref<boolean>(false);
@ -379,8 +388,9 @@ const customerType = ref<CustomerType>('CORP');
const employeeHistoryDialog = ref(false); const employeeHistoryDialog = ref(false);
const employeeHistory = ref<EmployeeHistory[]>(); const employeeHistory = ref<EmployeeHistory[]>();
function triggerCreate(type: CustomerType) { async function triggerCreate(type: CustomerType) {
customerType.value = type; customerType.value = type;
await fetchListOfBranch();
openDialogInputForm(); openDialogInputForm();
dialogCustomerType.value = false; dialogCustomerType.value = false;
} }
@ -402,7 +412,10 @@ async function openDialogInputForm(
) { ) {
if (action === 'INFO') { if (action === 'INFO') {
if (!id) return; if (!id) return;
if (selectorLabel.value === 'EMPLOYER') infoDrawer.value = true; if (selectorLabel.value === 'EMPLOYER') {
await fetchListOfBranch();
infoDrawer.value = true;
}
if (selectorLabel.value === 'EMPLOYEE') { if (selectorLabel.value === 'EMPLOYEE') {
await assignFormDataEmployee(id); await assignFormDataEmployee(id);
infoDrawerEmployee.value = true; infoDrawerEmployee.value = true;
@ -426,7 +439,7 @@ async function openDialogInputForm(
: []; : [];
const code = currentEmployee.value?.code.split('-')[0]; const code = currentEmployee.value?.code.split('-')[0];
const result = await fetchListBranch({ const result = await fetchListCustomeBranch({
includeCustomer: true, includeCustomer: true,
query: code, query: code,
pageSize: 30, pageSize: 30,
@ -434,13 +447,16 @@ async function openDialogInputForm(
if (result) employeeStore.ownerOption = result.result; if (result) employeeStore.ownerOption = result.result;
} }
} else { } else {
if (selectorLabel.value === 'EMPLOYER') dialogInputForm.value = true; if (selectorLabel.value === 'EMPLOYER') {
await fetchListOfBranch();
dialogInputForm.value = true;
}
if (selectorLabel.value === 'EMPLOYEE') { if (selectorLabel.value === 'EMPLOYEE') {
if (!id) return; if (!id) return;
await assignFormDataEmployee(id); await assignFormDataEmployee(id);
const code = currentEmployee.value?.code.split('-')[0]; const code = currentEmployee.value?.code.split('-')[0];
const result = await fetchListBranch({ const result = await fetchListCustomeBranch({
includeCustomer: true, includeCustomer: true,
query: code, query: code,
pageSize: 30, pageSize: 30,
@ -476,8 +492,11 @@ function clearForm() {
customerNameEN: '', customerNameEN: '',
personName: '', personName: '',
taxNo: '', taxNo: '',
registeredBranchId: '',
customerBranch: [ customerBranch: [
{ {
code: '',
branchNo: 1,
address: '', address: '',
addressEN: '', addressEN: '',
provinceId: '', provinceId: '',
@ -692,6 +711,14 @@ async function onSubmitCustomerBranch() {
flowStore.rotate(); flowStore.rotate();
} }
async function fetchListOfBranch() {
const res = await fetchListBranch({ pageSize: 999 });
if (res) allBranch.value = res.result;
console.log(allBranch.value);
}
async function fetchListCustomer() { async function fetchListCustomer() {
const resultList = await fetchList({ const resultList = await fetchList({
includeBranch: true, includeBranch: true,
@ -754,6 +781,7 @@ async function onSubmitEdit(id: string) {
if (!formData.value) return; if (!formData.value) return;
const tempValue: CustomerUpdate = { const tempValue: CustomerUpdate = {
registeredBranchId: formData.value.registeredBranchId,
status: formData.value.status, status: formData.value.status,
customerType: formData.value.customerType, customerType: formData.value.customerType,
customerName: formData.value.customerName, customerName: formData.value.customerName,
@ -855,7 +883,7 @@ async function employeeFilterOwnerBranch(
update: (...args: unknown[]) => void, update: (...args: unknown[]) => void,
) { ) {
update(async () => { update(async () => {
const result = await fetchListBranch({ const result = await fetchListCustomeBranch({
includeCustomer: true, includeCustomer: true,
query: val, query: val,
pageSize: 30, pageSize: 30,
@ -873,11 +901,21 @@ const prevCustomer = ref<CustomerCreate>({
taxNo: '', taxNo: '',
customerBranch: [], customerBranch: [],
image: null, image: null,
registeredBranchId: '',
}); });
async function assignFormData(data: Customer & { branch: CustomerBranch[] }) { async function assignFormData(customerId: string) {
let data;
const res = await fetchListById(customerId);
if (res) {
data = res;
}
if (!data) return;
prevCustomer.value = { prevCustomer.value = {
status: 'CREATED', registeredBranchId: data.registeredBranchId,
status: data.status,
customerType: data.customerType, customerType: data.customerType,
customerName: data.customerName, customerName: data.customerName,
customerNameEN: data.customerNameEN, customerNameEN: data.customerNameEN,
@ -889,6 +927,8 @@ async function assignFormData(data: Customer & { branch: CustomerBranch[] }) {
data.branch.forEach((v) => { data.branch.forEach((v) => {
prevCustomer.value.customerBranch?.push({ prevCustomer.value.customerBranch?.push({
code: v.code,
branchNo: v.branchNo,
id: v.id, id: v.id,
address: v.address, address: v.address,
addressEN: v.addressEN, addressEN: v.addressEN,
@ -926,6 +966,8 @@ function cloneData() {
...prevCustomer.value, ...prevCustomer.value,
customerBranch: structuredClone(toRaw(prevCustomer.value.customerBranch)), customerBranch: structuredClone(toRaw(prevCustomer.value.customerBranch)),
}; };
console.log(formData.value);
} }
async function assignFormDataEmployee(id: string) { async function assignFormDataEmployee(id: string) {
@ -989,7 +1031,7 @@ async function assignFormDataEmployee(id: string) {
: (profileSubmit.value = false); : (profileSubmit.value = false);
isEmployeeEdit.value = true; isEmployeeEdit.value = true;
const foundBranch = await fetchListBranchById( const foundBranch = await fetchListCustomeBranchById(
foundEmployee.customerBranchId, foundEmployee.customerBranchId,
); );
formDataEmployeeOwner.value = { formDataEmployeeOwner.value = {
@ -1543,7 +1585,7 @@ watch([inputSearch, currentStatus], async () => {
currentCustomerId = i.id; currentCustomerId = i.id;
assignFormData(i); assignFormData(i.id);
infoDrawerEdit = true; infoDrawerEdit = true;
openDialogInputForm('INFO', i.id); openDialogInputForm('INFO', i.id);
@ -1556,7 +1598,7 @@ watch([inputSearch, currentStatus], async () => {
currentCustomerId = i.id; currentCustomerId = i.id;
customerType = i.customerType; customerType = i.customerType;
assignFormData(i); assignFormData(i.id);
openDialogInputForm('INFO', i.id); openDialogInputForm('INFO', i.id);
} }
" "
@ -1725,9 +1767,8 @@ watch([inputSearch, currentStatus], async () => {
code: v[0].code, code: v[0].code,
}; };
if (!!currentCustomer && !!v) { if (currentCustomer) assignFormData(currentCustomer.id);
assignFormData({ ...currentCustomer, branch: v });
}
infoDrawerBranch = true; infoDrawerBranch = true;
} }
" "
@ -1795,6 +1836,8 @@ watch([inputSearch, currentStatus], async () => {
outlined outlined
separator separator
:type-customer="customerType" :type-customer="customerType"
:options-branch="allBranch"
v-model:registered-branch-id="formData.registeredBranchId"
v-model:customer-name="formData.customerName" v-model:customer-name="formData.customerName"
v-model:customer-name-en="formData.customerNameEN" v-model:customer-name-en="formData.customerNameEN"
v-model:person-name="formData.personName" v-model:person-name="formData.personName"
@ -1873,6 +1916,7 @@ watch([inputSearch, currentStatus], async () => {
indexTab !== undefined && formData.customerBranch?.[indexTab] indexTab !== undefined && formData.customerBranch?.[indexTab]
" "
:type-customer="customerType" :type-customer="customerType"
v-model:branch-no="formData.customerBranch[indexTab].branchNo"
v-model:tax-no="formData.customerBranch[indexTab].taxNo" v-model:tax-no="formData.customerBranch[indexTab].taxNo"
v-model:customer-name="formData.customerBranch[indexTab].name" v-model:customer-name="formData.customerBranch[indexTab].name"
v-model:customer-english-name=" v-model:customer-english-name="
@ -2391,6 +2435,8 @@ watch([inputSearch, currentStatus], async () => {
separator separator
:readonly="!infoDrawerEdit" :readonly="!infoDrawerEdit"
:type-customer="customerType" :type-customer="customerType"
:options-branch="allBranch"
v-model:registered-branch-id="formData.registeredBranchId"
v-model:customer-name="formData.customerName" v-model:customer-name="formData.customerName"
v-model:customer-name-en="formData.customerNameEN" v-model:customer-name-en="formData.customerNameEN"
v-model:person-name="formData.personName" v-model:person-name="formData.personName"
@ -2479,6 +2525,8 @@ watch([inputSearch, currentStatus], async () => {
formData.customerBranch?.[indexTab] formData.customerBranch?.[indexTab]
" "
:type-customer="customerType" :type-customer="customerType"
v-model:branch-code="formData.customerBranch[indexTab].code"
v-model:branch-no="formData.customerBranch[indexTab].branchNo"
v-model:customer-name="formData.customerBranch[indexTab].name" v-model:customer-name="formData.customerBranch[indexTab].name"
v-model:customer-english-name=" v-model:customer-english-name="
formData.customerBranch[indexTab].nameEN formData.customerBranch[indexTab].nameEN