125 lines
3.8 KiB
JavaScript
125 lines
3.8 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import utils from '@/utils/utils.js'
|
|
|
|
export const useAttendanceStore = defineStore('attendanceStore', () => {
|
|
const employee = ref('')
|
|
const year = ref(null)
|
|
const month = ref('')
|
|
const daysInMonth = ref(0)
|
|
const workingDays = ref(0)
|
|
const workingHours = ref(0)
|
|
const days = ref([])
|
|
|
|
// const lastThreeMonth = ['lipiec', 'czerwiec', 'maj']
|
|
// const lastMonths = ref(utils.getLastMonthsWithYear(5))
|
|
const lastMonths = utils.getLastMonthsWithYear(5)
|
|
const sumOfHours = computed(() => days.value[days.value.length - 1]?.accumulatedHours || 0)
|
|
const workedHours = computed(() => sumOfHours.value - holidayHours.value - sickHours.value)
|
|
const overtimeHours = computed(() => days.value[days.value.length - 1]?.balanceHours || 0)
|
|
const holidayHours = computed(() => holidayCount.value * 8)
|
|
const sickHours = computed(() => sickCount.value * 6.4)
|
|
const toGoHours = computed(() => leaveDayCount.value * 8)
|
|
const sickCount = computed(() => days.value.filter((d) => d.isSickLeave)?.length || 0)
|
|
const holidayCount = computed(() => days.value.filter((d) => d.isHolidayLeave)?.length || 0)
|
|
|
|
const leaveDayCount = computed(() => {
|
|
// console.log('wywylanie leaveFayCount')
|
|
return (
|
|
workingDays.value -
|
|
days.value.filter(
|
|
(day) =>
|
|
utils.isValidWorkDay(day) &&
|
|
(day.workedHours > 0 || day.isSickLeave || day.isHolidayLeave),
|
|
).length
|
|
)
|
|
})
|
|
|
|
function updateDay(day) {
|
|
utils.calculateMonthFromDay(day, days.value)
|
|
}
|
|
|
|
function loadFromResponse(response) {
|
|
employee.value = response.employee
|
|
year.value = response.year
|
|
month.value = response.month
|
|
daysInMonth.value = response.days_in_month
|
|
workingDays.value = response.working_days
|
|
workingHours.value = workingDays.value * 8
|
|
|
|
let workingDaysCount = 0
|
|
let accumulated = 0
|
|
let balance = 0
|
|
|
|
days.value = []
|
|
|
|
for (let d = 1; d <= daysInMonth.value; d++) {
|
|
const dateStr = `${year.value}-${month.value.toString().padStart(2, '0')}-${d.toString().padStart(2, '0')}`
|
|
const dayEntries = response.days.filter(
|
|
(day) => day.check_in && day.check_in.startsWith(dateStr),
|
|
)
|
|
|
|
// Sort entries by check-in time to ensure correct order
|
|
dayEntries.sort((a, b) => new Date(a.check_in) - new Date(b.check_in))
|
|
|
|
const entryTimes = dayEntries.map((entry) => utils.extractTimeFromDateString(entry.check_in))
|
|
const exitTimes = dayEntries.map((entry) => utils.extractTimeFromDateString(entry.check_out))
|
|
const attendance_reason_ids = dayEntries.flatMap((entry) => entry.attendance_reason_ids || [])
|
|
|
|
const isPublicHoliday = response.public_holidays.some((h) => h.date === dateStr)
|
|
const alert = attendance_reason_ids.length > 0
|
|
|
|
const day = {
|
|
dayOfMonth: d,
|
|
dayOfWeek: utils.getDayOfWeek(dateStr),
|
|
entryTime: entryTimes,
|
|
exitTime: exitTimes,
|
|
isSickLeave: false,
|
|
isHolidayLeave: false,
|
|
isPublicHoliday: isPublicHoliday,
|
|
workedHours: 0,
|
|
overtime: 0,
|
|
accumulatedHours: 0,
|
|
balanceHours: 0,
|
|
alert: alert,
|
|
}
|
|
|
|
if (utils.isValidWorkDay(day)) {
|
|
workingDaysCount++
|
|
}
|
|
|
|
accumulated += utils.calculateDay(day)
|
|
balance = parseFloat((accumulated - workingDaysCount * 8).toFixed(9))
|
|
|
|
day.accumulatedHours = parseFloat(accumulated.toFixed(9))
|
|
day.balanceHours = balance
|
|
|
|
days.value.push(day)
|
|
}
|
|
}
|
|
|
|
return {
|
|
// state
|
|
employee,
|
|
year,
|
|
month,
|
|
daysInMonth,
|
|
workingDays,
|
|
workingHours,
|
|
days,
|
|
sickCount,
|
|
holidayCount,
|
|
leaveDayCount,
|
|
sumOfHours,
|
|
workedHours,
|
|
overtimeHours,
|
|
holidayHours,
|
|
sickHours,
|
|
toGoHours,
|
|
lastMonths,
|
|
// actions
|
|
loadFromResponse,
|
|
updateDay,
|
|
}
|
|
})
|