API รายการผังบัญชีเงินเดือน

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-02-16 15:57:30 +07:00
parent 3b0dea6b52
commit 7ca7518c23
7 changed files with 461 additions and 129 deletions

View file

@ -1,11 +1,18 @@
<script setup lang="ts">
import { computed, ref, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import axios from "axios";
import http from "@/plugins/http";
import config from "@/app.config";
import type {
DataOption,
ObjectSalaryRef,
} from "@/modules/13_salary/interface/index/Main";
import type {
SalaryPosType,
SalaryPosLevel,
} from "@/modules/13_salary/interface/response/Main";
import Header from "@/components/DialogHeader.vue";
@ -19,6 +26,7 @@ const {
hideLoader,
messageError,
success,
dialogRemove,
} = useCounterMixin();
const modal = defineModel<boolean>("modal", { required: true });
@ -31,17 +39,22 @@ const props = defineProps({
type: Object,
defult: [],
},
fetchData: {
type: Function,
defult: () => {},
},
});
const salaryId = ref<string>("");
const formData = reactive({
salaryType: "", //* (OFFICER->"",EMPLOYEE->"")
posType: "", //*
posLevel: "", //*
posTypeId: "", //*
posLevelId: "", //*
isActive: false, //*
date: null, //
startDate: null, //
endDate: null, //
detail: "", //
details: "", //
});
/** ตัวแปร ref สำหรับแสดง validate */
@ -54,8 +67,8 @@ const endDateRef = ref<Object | null>(null);
const ObjectRef: ObjectSalaryRef = {
salaryType: salaryTypeRef,
posTyp: posTypeRef,
posLevel: posLevelRef,
posTypId: posTypeRef,
posLevelId: posLevelRef,
date: dateRef,
startDate: startDateRef,
endDate: endDateRef,
@ -66,6 +79,9 @@ const salaryTypeOption = ref<DataOption[]>([
{ id: "EMPLOYEE", name: "ลูกจ้างประจำกรุงเทพมหานคร" },
]);
const salaryPosTypeOption = ref<DataOption[]>([]);
const salaryPosLevelOption = ref<DataOption[]>([]);
const documentFile = ref<any>(null);
const itemsDocument = ref<any>([]);
@ -80,20 +96,88 @@ const title = computed(() => {
return name;
});
async function fetchPosType() {
await http
.get(config.API.salaryPosType)
.then((res) => {
const listOption = res.data.result.map((e: SalaryPosType) => ({
id: e.id,
name: e.posTypeName,
}));
salaryPosTypeOption.value = listOption;
})
.catch((err) => {
messageError($q, err);
});
}
async function fetchPosLevel() {
await http
.get(config.API.salaryPosLevel)
.then((res) => {
const listOption = res.data.result.map((e: SalaryPosLevel) => ({
id: e.id,
name: e.posLevelName,
}));
salaryPosLevelOption.value = listOption;
})
.catch((err) => {
messageError($q, err);
});
}
async function fetchSalaryDetail(id: string) {
showLoader();
await http
.get(config.API.salaryChartByid(id))
.then((res) => {
const data = res.data.result;
console.log(data);
formData.salaryType = data.salaryType;
formData.posTypeId = data.posTypeId;
formData.posLevelId = data.posLevelId;
formData.isActive = data.isActive;
formData.date = data.date;
formData.startDate = data.startDate;
formData.endDate = data.endDate;
formData.details = data.details;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
async function fetchDocumentFile(id: string) {
await http
.get(config.API.salaryChartFile(id))
.then((res) => {
const list = res.data.map((e: any) => ({ name: e.fileName }));
itemsDocument.value = list;
})
.catch((err) => {});
}
watch(
() => modal.value,
() => {
if (modal.value && props.typeAction === "edit") {
if (props.data) {
formData.salaryType = props.data.salaryType;
formData.posType = props.data.posType;
formData.posLevel = props.data.posLevel;
formData.isActive = props.data.isActive;
formData.date = props.data.date;
formData.startDate = props.data.startDate;
formData.endDate = props.data.endDate;
formData.detail = props.data.detail;
fetchDocumentFile(props.data.id);
async () => {
if (modal.value) {
if (salaryPosTypeOption.value.length === 0) {
await fetchPosType();
}
if (salaryPosLevelOption.value.length === 0) {
await fetchPosLevel();
}
if (props.typeAction === "edit") {
await showLoader();
if (props.data) {
salaryId.value = props.data.id;
await fetchDocumentFile(props.data.id);
await fetchSalaryDetail(props.data.id);
}
}
}
}
@ -106,17 +190,16 @@ function closeDialog() {
function clearFormData() {
formData.salaryType = "";
formData.posType = "";
formData.posLevel = "";
formData.posTypeId = "";
formData.posLevelId = "";
formData.isActive = false;
formData.date = null;
formData.startDate = null;
formData.endDate = null;
formData.detail = "";
formData.details = "";
documentFile.value = null;
itemsDocument.value = [];
}
function fetchDocumentFile(id: string) {}
function uploadDocumentFile() {}
function onClickSubmit() {
const hasError = [];
@ -135,13 +218,95 @@ function onClickSubmit() {
}
function createSalary() {
dialogConfirm($q, () => {
if (props.typeAction === "add") {
success($q, "add");
} else {
success($q, "edit");
dialogConfirm($q, async () => {
showLoader();
try {
const url =
props.typeAction === "add"
? config.API.salaryChart
: config.API.salaryChartByid(salaryId.value);
await http[props.typeAction === "add" ? "post" : "put"](url, formData);
success($q, "บันทีกข้อมูลสำเร็จ");
props.fetchData?.();
} catch (err) {
messageError($q, err);
} finally {
hideLoader();
closeDialog();
}
closeDialog();
});
}
async function uploadDocumentFile() {
const fileName = documentFile.value.name.replace(/\.(xlsx|docx|pdf)$/, "");
showLoader();
const formData = new FormData();
formData.append("file", documentFile.value);
const body = {
replace: false,
fileList: {
fileName: fileName,
},
};
await http
.post(config.API.salaryChartFile(salaryId.value), body)
.then((res) => {
console.log(res);
const foundKey: any = Object.keys(res.data).find(
(key) =>
res.data[key]?.fileName !== undefined &&
res.data[key]?.fileName !== ""
);
const link = res.data[foundKey]?.uploadUrl;
fileUpLoad(link);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* function ปโหลดไฟล
* @param url link ปโหลด
*/
function fileUpLoad(url: string) {
axios
.put(url, documentFile.value, {
headers: { "Content-Type": documentFile.value?.type },
onUploadProgress: (e) => console.log(e),
})
.then(() => {
success($q, "อัปโหลดไฟล์สำเร็จ");
documentFile.value = null;
fetchDocumentFile(salaryId.value);
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {});
}
function onClickDeleteFile(fileName: string) {
dialogRemove($q, async () => {
showLoader();
await http
.delete(config.API.salaryChartDelFile(salaryId.value, fileName))
.then((res) => {
setTimeout(() => {
fetchDocumentFile(salaryId.value);
success($q, "ลบไฟล์สำเร็จ");
}, 1000);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
setTimeout(() => {
hideLoader();
}, 1000);
});
});
}
@ -153,6 +318,50 @@ function checkEndDate() {
}
}
}
async function onClickDonwload(fileName: string) {
showLoader();
await http
.get(config.API.salaryChartDelFile(salaryId.value, fileName))
.then((res) => {
const data = res.data;
downloadFile(data.downloadUrl, data.fileType, fileName);
})
.catch((err) => {
messageError($q, err);
hideLoader();
});
}
/**
* function เรยกไฟล PDF
* @param url link PDF
* @param type ประเภทไฟล
* @param fileName อไฟล
*/
async function downloadFile(url: string, type: string, fileName: string) {
await axios
.get(url, {
method: "GET",
responseType: "blob",
headers: {
"Content-Type": "application/json",
Accept: type, //
},
})
.then(async (res) => {
const a = document.createElement("a");
a.href = window.URL.createObjectURL(res.data);
a.download = fileName;
a.click();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
</script>
<template>
@ -192,8 +401,8 @@ function checkEndDate() {
option-value="id"
emit-value
map-options
v-model="formData.posType"
:options="salaryTypeOption"
v-model="formData.posTypeId"
:options="salaryPosTypeOption"
label="ประเภทตำแหน่ง/กลุ่ม"
:rules="[(val) => !!val || 'กรุณาเลือกประเภทตำแหน่ง/กลุ่ม']"
lazy-rules
@ -210,8 +419,8 @@ function checkEndDate() {
option-value="id"
emit-value
map-options
v-model="formData.posLevel"
:options="salaryTypeOption"
v-model="formData.posLevelId"
:options="salaryPosLevelOption"
label="ระดับ"
:rules="[(val) => !!val || 'กรุณาเลือกระดับ']"
lazy-rules
@ -369,7 +578,7 @@ function checkEndDate() {
<div class="col-12">
<q-input
v-model="formData.detail"
v-model="formData.details"
outlined
dense
type="textarea"
@ -425,7 +634,7 @@ function checkEndDate() {
clickable
v-ripple
>
<q-item-section>{{ file.fileName }}</q-item-section>
<q-item-section>{{ file.name }}</q-item-section>
<q-item-section avatar>
<div class="row">
<div>
@ -436,6 +645,7 @@ function checkEndDate() {
size="12px"
color="blue"
icon="mdi-download-outline"
@click="onClickDonwload(file.name)"
>
<q-tooltip>ดาวนโหลดไฟล</q-tooltip>
</q-btn>
@ -448,6 +658,7 @@ function checkEndDate() {
size="12px"
color="red"
icon="mdi-delete-outline"
@click="onClickDeleteFile(file.name)"
><q-tooltip>ลบไฟล</q-tooltip></q-btn
>
</div>