hrms-mgt/src/modules/13_salary/components/SalaryChart/DialogFormMain.vue
2024-03-01 14:09:22 +07:00

535 lines
17 KiB
Vue

<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,
FormData,
} from "@/modules/13_salary/interface/index/Main";
import type {
SalaryPosType,
SalaryPosLevel,
} from "@/modules/13_salary/interface/response/Main";
import Header from "@/components/DialogHeader.vue";
import { useCounterMixin } from "@/stores/mixin";
const $q = useQuasar();
const {
date2Thai,
dialogConfirm,
showLoader,
hideLoader,
messageError,
success,
dialogRemove,
} = useCounterMixin();
const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
typeAction: {
type: String,
required: true,
},
data: {
type: Object,
defult: [],
},
fetchData: {
type: Function,
defult: () => {},
},
});
const salaryId = ref<string>("");
const formData = reactive<FormData>({
salaryType: "", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร")
posTypeId: "", //*ประเภทของตำแหน่ง
posLevelId: "", //*ระดับของตำแหน่ง
isActive: false, //*สถานะการใช้งาน
date: null, //ให้ไว้ ณ วันที่
startDate: null, //วันที่มีผลบังคับใช้
endDate: null, //วันที่สิ้นสุดบังคับใช้
details: "", //คำอธิบาย
isSpecial: false,
});
/** ตัวแปร ref สำหรับแสดง validate */
const salaryTypeRef = ref<Object | null>(null);
const posTypeRef = ref<Object | null>(null);
const posLevelRef = ref<Object | null>(null);
const dateRef = ref<any>(null);
const startDateRef = ref<any>();
const endDateRef = ref<any>();
const ObjectRef: ObjectSalaryRef = {
salaryType: salaryTypeRef,
posTypId: posTypeRef,
posLevelId: posLevelRef,
date: dateRef,
startDate: startDateRef,
endDate: endDateRef,
};
const salaryTypeOption = ref<DataOption[]>([
{ id: "OFFICER", name: "ข้าราชการกรุงเทพมหานครสามัญ" },
{ id: "EMPLOYEE", name: "ลูกจ้างประจำกรุงเทพมหานคร" },
]);
const posType = ref<SalaryPosType[]>([]);
const salaryPosTypeOption = ref<DataOption[]>([]);
const salaryPosLevelOption = ref<DataOption[]>([]);
const documentFile = ref<any>(null);
const itemsDocument = ref<any>([]);
const title = computed(() => {
const name =
props.typeAction === "add"
? "เพิ่มผังบัญชีเงินเดือน"
: props.typeAction === "edit"
? "แก้ไขผังบัญชีเงินเดือน"
: "บัญชีเงินเดือน";
return name;
});
async function fetchPosType() {
await http
.get(config.API.salaryPosType)
.then((res) => {
posType.value = res.data.result;
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(id: string) {
const filterLevel = posType.value.find((e: SalaryPosType) => e.id === id);
const listOption =
filterLevel?.posLevels.map((e: SalaryPosLevel) => ({
id: e.id,
name: e.posLevelName,
})) || [];
salaryPosLevelOption.value = listOption;
if (!listOption.some((e: DataOption) => e.id === formData.posLevelId)) {
formData.posLevelId = "";
}
}
async function fetchSalaryDetail(id: string) {
showLoader();
await http
.get(config.API.salaryChartByid(id))
.then((res) => {
const data = res.data.result;
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,
async () => {
if (modal.value) {
if (salaryPosTypeOption.value.length === 0) {
await fetchPosType();
}
if (props.typeAction === "add") {
formData.date = new Date();
formData.startDate = new Date();
formData.endDate = new Date();
}
if (props.typeAction === "edit") {
await showLoader();
if (props.data) {
salaryId.value = props.data.id;
await fetchDocumentFile(props.data.id);
await fetchSalaryDetail(props.data.id);
await fetchPosLevel(props.data.posTypeId);
}
}
}
}
);
function closeDialog() {
modal.value = !modal.value;
clearFormData();
}
function clearFormData() {
formData.salaryType = "";
formData.posTypeId = "";
formData.posLevelId = "";
formData.isSpecial = false;
formData.isActive = false;
formData.date = null;
formData.startDate = null;
formData.endDate = null;
formData.details = "";
documentFile.value = null;
itemsDocument.value = [];
}
function onClickSubmit() {
const hasError = [];
for (const key in ObjectRef) {
if (Object.prototype.hasOwnProperty.call(ObjectRef, key)) {
const property = ObjectRef[key];
if (property.value && typeof property.value.validate === "function") {
const isValid = property.value.validate();
hasError.push(isValid);
}
}
}
if (hasError.every((result) => result === true)) {
createSalary();
}
}
function createSalary() {
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();
}
});
}
/** function checkEndDate*/
function checkEndDate() {
if (formData.endDate !== null && formData.startDate !== null) {
if (formData.endDate < formData.startDate) {
formData.endDate = null;
}
}
}
watch(
() => formData.date,
() => {
if (formData.date) {
dateRef.value.resetValidation();
}
}
);
watch(
() => formData.startDate,
() => {
if (formData.startDate) {
startDateRef.value.resetValidation();
}
}
);
watch(
() => formData.endDate,
() => {
if (formData.endDate) {
endDateRef.value.resetValidation();
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card class="col-12" style="width: 80%">
<form @submit.prevent.stop="onClickSubmit">
<Header :tittle="title" :close="closeDialog" />
<q-separator />
<q-card-section class="scroll" style="max-height: 70vh">
<div class="row col-12 q-col-gutter-sm">
<div class="col-md-12">
<div class="row col-12 q-col-gutter-sm">
<div class="col-xs-12 col-md-3">
<q-select
ref="salaryTypeRef"
dense
hide-bottom-space
outlined
option-label="name"
option-value="id"
emit-value
map-options
v-model="formData.salaryType"
:options="salaryTypeOption"
label="ประเภทผังบัญชีเงินเดือน"
:rules="[
(val) => !!val || 'กรุณาเลือกประเภทผังบัญชีเงินเดือน',
]"
lazy-rules
/>
</div>
<div class="col-xs-12 col-md-3">
<q-select
ref="posTypeRef"
dense
hide-bottom-space
outlined
option-label="name"
option-value="id"
emit-value
map-options
v-model="formData.posTypeId"
:options="salaryPosTypeOption"
label="ประเภทตำแหน่ง/กลุ่ม"
:rules="[(val) => !!val || 'กรุณาเลือกประเภทตำแหน่ง/กลุ่ม']"
lazy-rules
@update:model-value="fetchPosLevel"
/>
</div>
<div class="col-xs-12 col-md-3">
<q-select
ref="posLevelRef"
dense
hide-bottom-space
outlined
option-label="name"
option-value="id"
emit-value
map-options
v-model="formData.posLevelId"
:options="salaryPosLevelOption"
label="ระดับ"
:rules="[(val) => !!val || 'กรุณาเลือกระดับ']"
lazy-rules
:disable="formData.posTypeId === ''"
/>
</div>
<div class="col-xs-12 col-md-1">
<q-checkbox
size="md"
v-model="formData.isSpecial"
label="ฉ"
/>
</div>
<div
class="col-xs-12 col-md-2"
style="display: flex; justify-content: flex-start"
>
<q-toggle
color="primary"
label="สถานะการใช้งาน"
v-model="formData.isActive"
/>
</div>
<div class="col-xs-12 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.date"
:locale="'th'"
autoApply
borderless
:enableTimePicker="false"
week-start="0"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
ref="dateRef"
outlined
dense
hide-bottom-space
:model-value="
formData.date != null
? date2Thai(formData.date)
: null
"
label="ให้ไว้ ณ วันที่"
:rules="[
(val) =>
!!val || `${'กรุณาเลือกวันที่ ให้ไว ณ วันที่'}`,
]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
<div class="col-xs-12 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.startDate"
:locale="'th'"
autoApply
borderless
:enableTimePicker="false"
week-start="0"
@update:model-value="checkEndDate"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
ref="startDateRef"
outlined
dense
hide-bottom-space
:model-value="
formData.startDate != null
? date2Thai(formData.startDate)
: null
"
label="วันที่มีผลบังคับใช้"
:rules="[
(val) =>
!!val || `${'กรุณาเลือกวันที่มีผลบังคับใช้'}`,
]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
<div class="col-xs-12 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.endDate"
:locale="'th'"
autoApply
borderless
:enableTimePicker="false"
week-start="0"
:min-date="formData.startDate"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
ref="endDateRef"
outlined
dense
hide-bottom-space
:model-value="
formData.endDate != null
? date2Thai(formData.endDate)
: null
"
label="วันที่สิ้นสุดบังคับใช้"
:rules="[
(val) =>
!!val || `${'กรุณาเลือกวันที่สิ้นสุดบังคับใช้'}`,
]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
<div class="col-12">
<q-input
v-model="formData.details"
outlined
dense
type="textarea"
label="คำอธิบาย"
/>
</div>
</div>
</div>
</div>
</q-card-section>
<q-separator />
<div class="text-right q-ma-sm">
<q-btn label="บันทึก" type="submit" color="secondary">
<q-tooltip>นทกขอม</q-tooltip>
</q-btn>
</div>
</form>
</q-card>
</q-dialog>
</template>
<style lang="scss" scoped></style>