ready to deployment
This commit is contained in:
234
frontend/src/utils/utils.js
Normal file
234
frontend/src/utils/utils.js
Normal file
@@ -0,0 +1,234 @@
|
||||
const polishMonths = {
|
||||
styczeń: 1,
|
||||
luty: 2,
|
||||
marzec: 3,
|
||||
kwiecień: 4,
|
||||
maj: 5,
|
||||
czerwiec: 6,
|
||||
lipiec: 7,
|
||||
sierpień: 8,
|
||||
wrzesień: 9,
|
||||
październik: 10,
|
||||
listopad: 11,
|
||||
grudzień: 12,
|
||||
}
|
||||
|
||||
const months = [
|
||||
'styczeń',
|
||||
'luty',
|
||||
'marzec',
|
||||
'kwiecień',
|
||||
'maj',
|
||||
'czerwiec',
|
||||
'lipiec',
|
||||
'sierpień',
|
||||
'wrzesień',
|
||||
'październik',
|
||||
'listopad',
|
||||
'grudzień',
|
||||
]
|
||||
|
||||
const daysOfWeek = ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So']
|
||||
|
||||
// function getLastThreeMonth(monthName) {
|
||||
// const months = Object.keys(polishMonths) // ["styczeń", "luty", ..., "grudzień"]
|
||||
// const index = months.indexOf(monthName)
|
||||
// if (index === -1) return [] // jeśli nie znaleziono miesiąca
|
||||
|
||||
// return [months[(index - 2 + 12) % 12], months[(index - 1 + 12) % 12], months[index]]
|
||||
// }
|
||||
|
||||
// function getLastThreeMonth(monthName) {
|
||||
// const index = months.indexOf(monthName)
|
||||
// if (index === -1) return [] // jeśli nie znaleziono miesiąca
|
||||
|
||||
// return [months[(index - 2 + 12) % 12], months[(index - 1 + 12) % 12], months[index]]
|
||||
// }
|
||||
function getLastMonths(startMonth, count) {
|
||||
const result = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
// Obliczamy numer miesiąca (1–12), uwzględniając cofanie się w roku
|
||||
let month = ((startMonth - i - 1 + 12) % 12) + 1
|
||||
result.push(month)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function getLastThreeMonth(month) {
|
||||
if (!month || typeof month !== 'number' || month < 1 || month > 12) {
|
||||
return null
|
||||
}
|
||||
return [((month - 3 + 12) % 12) + 1, ((month - 2 + 12) % 12) + 1, month]
|
||||
}
|
||||
|
||||
function getMonthName(month) {
|
||||
if (!month || typeof month !== 'number' || month < 1 || month > 12) {
|
||||
return null
|
||||
}
|
||||
return months[month - 1]
|
||||
}
|
||||
|
||||
function getMonthNumber(monthName) {
|
||||
const monthNum = polishMonths[monthName.toLowerCase()]
|
||||
if (!monthNum) {
|
||||
console.error('❌ Unknown month:', monthName)
|
||||
return null
|
||||
}
|
||||
return String(monthNum).padStart(2, '0')
|
||||
}
|
||||
|
||||
function getDayOfWeek(dateStr) {
|
||||
const date = new Date(dateStr)
|
||||
return daysOfWeek[date.getDay()]
|
||||
}
|
||||
|
||||
function extractTimeFromDateString(isoString) {
|
||||
if (!isoString || typeof isoString !== 'string') return ''
|
||||
const date = new Date(isoString + 'Z')
|
||||
if (isNaN(date.getTime())) return ''
|
||||
return date.toTimeString().slice(0, 5) // 'HH:MM:SS'
|
||||
}
|
||||
|
||||
function isValidWorkDay(day) {
|
||||
if (!day || typeof day !== 'object') return false
|
||||
if (!('dayOfWeek' in day) || !('isPublicHoliday' in day)) return false
|
||||
return day.dayOfWeek !== 'So' && day.dayOfWeek !== 'Nd' && !day.isPublicHoliday
|
||||
}
|
||||
|
||||
function calculateWorkedTime(entryTime, exitTime) {
|
||||
if (!entryTime || !exitTime || typeof entryTime !== 'string' || typeof exitTime !== 'string') {
|
||||
return 0
|
||||
}
|
||||
const [eh, em] = entryTime.split(':').map(Number)
|
||||
const [xh, xm] = exitTime.split(':').map(Number)
|
||||
|
||||
if ([eh, em, xh, xm].some((v) => typeof v !== 'number' || isNaN(v))) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const start = eh * 60 + em
|
||||
const end = xh * 60 + xm
|
||||
|
||||
let worked = (end - start) / 60
|
||||
return parseFloat(worked.toFixed(9))
|
||||
}
|
||||
|
||||
function floatHoursToHHMM(decimalHours) {
|
||||
if (typeof decimalHours !== 'number' || isNaN(decimalHours)) return ''
|
||||
// if (decimalHours === 0) return ''
|
||||
const negative = decimalHours < 0
|
||||
const abs = Math.abs(decimalHours)
|
||||
|
||||
const m_total = Math.round(abs * 60)
|
||||
const h = Math.floor(m_total / 60)
|
||||
const m = m_total % 60
|
||||
|
||||
const hh = String(h).padStart(2, '0')
|
||||
const mm = String(m).padStart(2, '0')
|
||||
|
||||
return `${negative ? '-' : ''}${hh}:${mm}`
|
||||
}
|
||||
|
||||
function calculateDay(day) {
|
||||
let exitTime = day.exitTime
|
||||
|
||||
if (!day.exitTime || typeof day.exitTime !== 'string' || day.exitTime.trim() === '') {
|
||||
// const now = new Date().toLocaleString('pl-PL', { timeZone: 'Europe/Warsaw' })
|
||||
const now = new Date()
|
||||
const hh = String(now.getHours()).padStart(2, '0')
|
||||
const mm = String(now.getMinutes()).padStart(2, '0')
|
||||
const ss = String(now.getSeconds()).padStart(2, '0')
|
||||
exitTime = `${hh}:${mm}:${ss}`
|
||||
}
|
||||
|
||||
if (!day.entryTime || typeof day.entryTime !== 'string' || day.entryTime.trim() === '') {
|
||||
day.workedHours = 0
|
||||
} else {
|
||||
day.workedHours = calculateWorkedTime(day.entryTime, exitTime) - 0.25
|
||||
day.workedHours = parseFloat(day.workedHours.toFixed(9))
|
||||
day.exitTime = exitTime
|
||||
}
|
||||
|
||||
if (isValidWorkDay(day) && !day.isHolidayLeave && !day.isSickLeave) {
|
||||
day.overtime = day.workedHours - 8
|
||||
} else {
|
||||
day.overtime = day.workedHours
|
||||
}
|
||||
day.overtime = parseFloat(day.overtime.toFixed(9))
|
||||
|
||||
let sumHours = day.workedHours
|
||||
if (day.isHolidayLeave) {
|
||||
sumHours += 8
|
||||
}
|
||||
if (day.isSickLeave) {
|
||||
sumHours += 6.4
|
||||
}
|
||||
|
||||
return parseFloat(sumHours.toFixed(9))
|
||||
}
|
||||
|
||||
function calculateMonth(days) {
|
||||
let accumulated = 0
|
||||
let workingDaysCount = 0
|
||||
let balance = 0
|
||||
|
||||
for (let i = 0; i < days.length; i++) {
|
||||
const day = days[i]
|
||||
const workedHours = calculateDay(day)
|
||||
|
||||
accumulated += workedHours
|
||||
if (isValidWorkDay(day)) {
|
||||
workingDaysCount++
|
||||
}
|
||||
|
||||
balance = parseFloat((accumulated - workingDaysCount * 8).toFixed(9))
|
||||
day.accumulatedHours = parseFloat(accumulated.toFixed(9))
|
||||
day.balanceHours = balance
|
||||
}
|
||||
}
|
||||
|
||||
function calculateMonthFromDay(startDay, days) {
|
||||
let startIndex = days.indexOf(startDay)
|
||||
if (startIndex === -1) return
|
||||
|
||||
let accumulated = 0
|
||||
let workingDaysCount = 0
|
||||
let balance = 0
|
||||
|
||||
if (startIndex > 0) {
|
||||
const previousDay = days[startIndex - 1]
|
||||
accumulated = previousDay.accumulatedHours || 0
|
||||
balance = previousDay.balanceHours || 0
|
||||
workingDaysCount = days.slice(0, startIndex).filter(isValidWorkDay).length
|
||||
}
|
||||
|
||||
for (let i = startIndex; i < days.length; i++) {
|
||||
const day = days[i]
|
||||
const workedHours = calculateDay(day)
|
||||
|
||||
accumulated += workedHours
|
||||
if (isValidWorkDay(day)) {
|
||||
workingDaysCount++
|
||||
}
|
||||
|
||||
balance = parseFloat((accumulated - workingDaysCount * 8).toFixed(9))
|
||||
day.accumulatedHours = parseFloat(accumulated.toFixed(9))
|
||||
day.balanceHours = balance
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getDayOfWeek,
|
||||
extractTimeFromDateString,
|
||||
isValidWorkDay,
|
||||
calculateWorkedTime,
|
||||
floatHoursToHHMM,
|
||||
calculateDay,
|
||||
calculateMonth,
|
||||
calculateMonthFromDay,
|
||||
getLastThreeMonth,
|
||||
getLastMonths,
|
||||
getMonthNumber,
|
||||
getMonthName,
|
||||
daysOfWeek,
|
||||
}
|
||||
Reference in New Issue
Block a user