hrms-user/src/modules/05_leave/store.ts

478 lines
16 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import type { QTableProps } from "quasar";
import type {
OptionData,
TypeLeave,
} from "@/modules/05_leave/interface/index/main";
import type {
ListLeave,
ListLeaveTable,
} from "@/modules/05_leave/interface/response/leave";
import type { DataOption } from "../14_IDP/interface/Main";
const mixin = useCounterMixin();
const { date2Thai, showLoader, hideLoader } = mixin;
const type = ref<string>("00000000-0000-0000-0000-000000000000");
const status = ref<string>("ALL");
export const useLeaveStore = defineStore("Leave", () => {
const tabValue = ref<string>("calendar");
const typeLeave = ref<string | undefined>("");
const LeaveType = ref<string | null>("0");
const LeaveStatus = ref<string | null>("0");
const fiscalYearyear = ref<Number | null>(new Date().getFullYear());
const rows = ref<ListLeaveTable[]>([]);
const leaveTypeData = ref<TypeLeave[]>([]);
/**
* function เรียกข้อมูลรายการลา Table
* @param data ข้อมูลรายการลา Table
*/
async function fetchListLeave(data: ListLeave[]) {
let datalist: ListLeaveTable[] = data.map((e: ListLeave) => ({
id: e.id,
leaveTypeName: e.leaveTypeName,
leaveTypeId: e.leaveTypeId,
fullName: e.fullName,
dateSendLeave: e.dateSendLeave && date2Thai(e.dateSendLeave, false, true),
dateLeave:
e.leaveStartDate && e.leaveEndDate
? e.leaveStartDate === e.leaveEndDate
? date2Thai(e.leaveEndDate)
: `${date2Thai(e.leaveStartDate)}${
e.leaveRange !== "ALL" ? ` (${converstType(e.leaveRange)})` : ""
} - ${date2Thai(e.leaveEndDate)}${
e.leaveRangeEnd !== "ALL"
? ` (${converstType(e.leaveRangeEnd)})`
: ""
}`
: "-",
status: e.status,
hajjDayStatus: e.hajjDayStatus,
leaveRange: e.leaveRange,
leaveRangeEnd: e.leaveRangeEnd,
leaveSubTypeName: e.leaveSubTypeName,
statusConvert: convertStatud(e.status),
isDelete: e.isDelete,
}));
rows.value = datalist;
}
//ฟังก์ชั่นแปลง Status
function convertStatud(val: string) {
switch (val) {
case "DRAFT":
return "แบบร่าง";
case "NEW":
return "ใหม่";
case "PENDING":
return "กำลังดำเนินการ";
case "APPROVE":
return "อนุมัติ";
case "REJECT":
return "ไม่อนุมัติ";
case "DELETING":
return "กำลังดำเนินการยกเลิก";
case "DELETE":
return "ยกเลิกสำเร็จ";
}
}
/** ประเภทการลา */
const typeOptions = ref<OptionData[]>([]);
const typeId = ref<string | undefined>("");
const typeOptionsMain = ref<OptionData[]>([]);
const typeOptionsAdd = ref<OptionData[]>([]);
/** รายการข้อมูลประเภทใบลา */
const options = ref<OptionData[]>([]);
/**
* function เรียกข้อมูลประเภทการลา
* @param data ประเภทการลา
*/
async function fetchLeaveType(data: TypeLeave[]) {
typeOptionsMain.value = [
{
id: "00000000-0000-0000-0000-000000000000",
name: "ทั้งหมด",
code: "LV-000",
},
];
const optionType = data.map((e: TypeLeave) => ({
id: e.id,
name: e.name,
code: e.code,
}));
typeOptionsMain.value.push(...optionType);
typeOptions.value = typeOptionsMain.value;
typeOptionsAdd.value = [];
typeOptionsAdd.value.push(...optionType);
options.value = typeOptionsAdd.value;
}
/** สถานะของการลา */
const statusOptionsMain = ref<any[]>([
{ id: "ALL", name: "ทั้งหมด" },
{ id: "DRAFT", name: "แบบร่าง" },
{ id: "NEW", name: "ใหม่" },
{ id: "PENDING", name: "กำลังดำเนินการ" },
{ id: "APPROVE", name: "อนุญาต " },
{ id: "REJECT", name: "ไม่อนุญาต" },
{ id: "DELETING", name: "กำลังดำเนินการยกเลิก" },
{ id: "DELETE", name: "ยกเลิกสำเร็จ" },
]);
const statusOptions = ref<any[]>(statusOptionsMain.value);
/**
* function ต้นหาข้อมูลของ Option
* @param val ค่าที่ต้องการฟิลเตอร์
* @param update อัพเดทค่า
* @param refData ดาต้าที่ต้องการฟิลเตอร์
*/
function filterOption(val: string, update: Function, refData: string) {
switch (refData) {
case "LeaveTypeOption":
update(() => {
typeOptions.value = typeOptionsMain.value.filter(
(v: any) => v.name.indexOf(val) > -1
);
});
break;
case "LeaveStatusOption":
update(() => {
statusOptions.value = statusOptionsMain.value.filter(
(v: any) => v.name.indexOf(val) > -1
);
});
break;
default:
break;
}
}
const rangeOptions = ref<DataOption[]>([
{
id: "ALL",
name: "เต็มวัน",
},
{
id: "MORNING",
name: "ครึ่งวันเช้า",
},
{
id: "AFTERNOON",
name: "ครึ่งวันบ่าย",
},
]);
function converstType(val: string) {
switch (val) {
case "ALL":
return "";
case "MORNING":
return "ครึ่งวันเช้า";
case "AFTERNOON":
return "ครึ่งวันบ่าย";
}
}
/** รายการประเภทการลาของ ลาไปศึกษา ฝึกอบรม ปฎิบัติการวิจัย หรือดูงาน*/
const optionsSpecific = ref<string[]>([
"ศึกษาต่อ",
"ฝึกอบรม",
"ปฎิบัติการวิจัย",
"ดูงาน",
]);
/** รายการประเภทการลาของ ลาอุปสมบทหรือลาประกอบพิธีฮัจญ์*/
const optionsOrdination = ref<string[]>(["ลาอุปสมบท", "ลาประกอบพิธีฮัจญ์"]);
/** data table filter & column ของรายการลา */
const visibleColumns = ref<string[]>([
"no",
"leaveTypeName",
"dateSendLeave",
"status",
"dateLeave",
]);
const columns = ref<QTableProps["columns"]>([
{
name: "no",
align: "left",
label: "ลำดับ",
sortable: true,
field: "no",
headerStyle: "font-size: 14px",
style: "font-size: 14px; width:5%;",
},
{
name: "leaveTypeName",
align: "left",
label: "ประเภทการลา",
sortable: true,
field: "leaveTypeName",
headerStyle: "font-size: 14px",
style: "font-size: 14px; width:15%;",
},
{
name: "dateLeave",
align: "left",
label: "วันที่ลา",
sortable: true,
field: "dateLeave",
headerStyle: "font-size: 14px",
style: "font-size: 14px; width:15%;",
},
{
name: "dateSendLeave",
align: "left",
label: "วันที่ยื่นใบลา",
sortable: true,
field: "dateSendLeave",
headerStyle: "font-size: 14px",
style: "font-size: 14px; width:15%;",
},
{
name: "status",
align: "left",
label: "สถานะ",
sortable: true,
field: "status",
headerStyle: "font-size: 14px",
style: "font-size: 14px; width:10%;",
},
]);
/**
*ฟังก์ชั่นแปลงประเภทแบบฟอร์มลา
* @param item ประเภทแบบฟอร์ม
* @param subitem ประเภทแบบฟอร์มย่อย
*/
function typeConvert(item: string, subitem: any) {
typeLeave.value = convertSubtitle(item);
// }
typeId.value = convertId(item);
}
/**
*ฟังก์ชั่นแปลงประเภทแบบฟอร์มลา ลาอุปสมบท/ลาประกอบพิธีฮัจญ์
* @param val ค่า string
* @returns ส่งค่าที่แปลงแล้ว
*/
function convertSubtitle(val: string) {
return options.value.find((x) => x.code == val)?.name;
}
/**
*ฟังก์ชั่นหา id จาก api
* @param val ค่า string
* @returns ส่งค่าที่แปลงแล้ว
*/
function convertId(val: string) {
return options.value.find((x) => x.code == val)?.id;
}
/**
*ตัวแปร profile ที่จะส่งออก
*/
const dateSendLeave = ref<Date>(); //วันที่ยื่นใบลา
const leaveTypeName = ref<string>(""); //Name ประเภทการลา
const dear = ref<string>(""); //เรียน
const commanderPosition = ref<string>(""); //ตำแหน่งผู้บังคับบัญชา
const fullName = ref<string>(""); //คำนำหน้า ชื่อ นามสกุล ผู้ยื่นขอ
const positionName = ref<string>(""); //ตำแหน่งผู้ยื่นขอ
const positionLevelName = ref<string>(""); //ระดับผู้ยื่นขอ
const organizationName = ref<string>(""); //สังกัดผู้ยื่นขอ
const leaveLimit = ref<number>(0); //โควต้าลา(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
const leaveTotal = ref<number>(0); //ลาไปแล้ว(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
const leaveRemain = ref<number>(0); //คงเหลือโควต้า(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
const restDayTotalOld = ref<number>(0); //จำนวนวันลาพักผ่อนสะสม จากปีที่ผ่านมา
const birthDate = ref<Date>(); //วันเกิด
const dateAppoint = ref<Date>(); //วันที่เข้ารับราชการ
const salary = ref<number>(0); //เงินเดือนปัจจุบัน
const salaryText = ref<string>(""); //เงินเดือนปัจจุบัน(ภาษาไทย)
const leaveLast = ref<any>();
const restDayCurrentTotal = ref<number>(0);
const telephoneNumber = ref<string>("");
const currentAddress = ref<string>("");
//ดึงข้อมูล profile จาก API
async function fetchProfile() {
showLoader();
await http
.post(config.API.leaveProfile(), { type: typeId.value })
.then((res: any) => {
const data = res.data.result;
dateSendLeave.value = data.dateSendLeave;
leaveTypeName.value = data.leaveTypeName;
fullName.value = data.fullName;
positionName.value = data.positionName;
positionLevelName.value = data.positionLevelName;
organizationName.value = data.organizationName;
leaveLimit.value = data.leaveLimit;
leaveTotal.value = data.leaveTotal;
leaveRemain.value = data.leaveRemain;
restDayTotalOld.value = data.restDayTotalOld;
birthDate.value = data.birthDate;
dateAppoint.value = data.dateAppoint;
telephoneNumber.value =
data.telephoneNumber && data.telephoneNumber !== "-"
? data.telephoneNumber
: null;
currentAddress.value = data.currentSubDistrict
? `${data.currentAddress} ${data.currentSubDistrict} ${data.currentDistrict} ${data.currentProvince} ${data.currentZipCode}`
: "";
salary.value = data.salary ? data.salary.toLocaleString("th-TH") : "";
salaryText.value = data.salaryText;
leaveLast.value =
data.leaveLast != "0001-01-01T00:00:00"
? date2Thai(data.leaveLast)
: "-";
restDayCurrentTotal.value =
Number(data.leaveLimit) - Number(data.restDayTotalOld);
})
.catch((e: any) => {
console.log(e);
})
.finally(() => {
hideLoader();
});
}
async function fetchProfileOld(data: any) {
await http
.post(config.API.leaveProfile(), { type: typeId.value })
.then((res: any) => {
const data = res.data.result;
leaveLimit.value = data.leaveLimit;
leaveTotal.value = data.leaveTotal;
leaveRemain.value = data.leaveRemain;
restDayTotalOld.value = data.restDayTotalOld; //จำนวนวันลาพักผ่อนสะสม จากปีที่ผ่านมา
birthDate.value = data.birthDate; // วันเกิด
dateAppoint.value = data.dateAppoint; // วันที่รับราชการ
salary.value = data.salary ? data.salary.toLocaleString("th-TH") : "";
salaryText.value = data.salaryText;
restDayCurrentTotal.value =
Number(data.leaveLimit) - Number(data.restDayTotalOld);
})
.catch((e: any) => {
console.log(e);
});
dateSendLeave.value = data.dateSendLeave;
typeLeave.value = data.leaveTypeName;
dear.value = data.dear;
commanderPosition.value = data.commanderPosition;
fullName.value = data.fullName;
positionName.value = data.positionName;
positionLevelName.value = data.positionLevelName;
organizationName.value = data.organizationName;
leaveLast.value =
data.leaveLast != "0001-01-01T00:00:00" ? date2Thai(data.leaveLast) : "-";
restDayCurrentTotal.value = data.restDayCurrentTotal;
}
function resetForm2() {
dateSendLeave.value = undefined;
leaveTypeName.value = "";
dear.value = "";
commanderPosition.value = "";
fullName.value = "";
positionName.value = "";
positionLevelName.value = "";
organizationName.value = "";
leaveLimit.value = 0;
leaveTotal.value = 0;
leaveRemain.value = 0;
restDayTotalOld.value = 0;
birthDate.value = undefined;
dateAppoint.value = undefined;
salary.value = 0;
salaryText.value = "";
leaveLast.value = undefined;
restDayCurrentTotal.value = 0;
}
/** ฟังก์ชันเรียกข้อมูลประเภทการลา */
async function fetchLeaveTypeData() {
if (leaveTypeData.value.length > 0) {
return leaveTypeData.value;
} else {
try {
const res = await http.get(config.API.leaveType());
leaveTypeData.value = res.data.result;
return leaveTypeData.value;
} catch (error) {
console.error("Fetch leave type error:", error);
}
}
}
return {
tabValue,
typeOptions,
optionsSpecific,
statusOptions,
visibleColumns,
columns,
rows,
LeaveType,
LeaveStatus,
fiscalYearyear,
options,
optionsOrdination,
typeConvert,
typeLeave,
typeId,
fetchListLeave,
fetchLeaveType,
filterOption,
fetchProfile,
//ส่งออกตัวแปร profileที่ได้จาก Api
dateSendLeave,
leaveTypeName,
dear,
commanderPosition,
fullName,
positionName,
positionLevelName,
organizationName,
leaveLimit, //โควต้าลา(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
leaveTotal, //ลาไปแล้ว(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
leaveRemain, //คงเหลือโควต้า(แต่ละประเภท)หน่วยเป็นวัน(ภายในปีนั้น)
restDayTotalOld,
birthDate,
dateAppoint,
salary,
salaryText,
leaveLast,
restDayCurrentTotal,
convertStatud,
resetForm2,
fetchProfileOld,
type,
typeOptionsMain,
status,
statusOptionsMain,
telephoneNumber,
currentAddress,
rangeOptions,
converstType,
fetchLeaveTypeData,
};
});