Add a nice cleanup of holidayUtils.js
This commit is contained in:
@@ -14,7 +14,6 @@
|
|||||||
const normalizedLocale = locale.toLowerCase() === 'us' ? 'en-US' : `en-${locale.toUpperCase()}`;
|
const normalizedLocale = locale.toLowerCase() === 'us' ? 'en-US' : `en-${locale.toUpperCase()}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to get firstDay from Intl.Locale weekInfo
|
|
||||||
// @ts-ignore .weekInfo exists on all browsers except Firefox
|
// @ts-ignore .weekInfo exists on all browsers except Firefox
|
||||||
const weekFirstDay = new Intl.Locale(normalizedLocale)?.weekInfo?.firstDay;
|
const weekFirstDay = new Intl.Locale(normalizedLocale)?.weekInfo?.firstDay;
|
||||||
if (weekFirstDay !== undefined) {
|
if (weekFirstDay !== undefined) {
|
||||||
@@ -101,10 +100,11 @@
|
|||||||
<div class="day"></div>
|
<div class="day"></div>
|
||||||
{/each}
|
{/each}
|
||||||
{#each Array.from({ length: daysInMonth }, (_, i) => i + 1) as day}
|
{#each Array.from({ length: daysInMonth }, (_, i) => i + 1) as day}
|
||||||
<div class="day {isWeekend(new Date(year, month, day)) ? 'weekend' : ''} {getHoliday(day) ? 'holiday' : ''} {isOptimizedDayOff(day) ? 'optimized' : ''} {isConsecutiveDayOff(day) ? 'consecutive-day' : ''}">
|
{@const holiday = getHoliday(day)}
|
||||||
{day}
|
<div class="day {isWeekend(new Date(year, month, day)) ? 'weekend' : ''} {holiday ? 'holiday' : ''} {isOptimizedDayOff(day) ? 'optimized' : ''} {isConsecutiveDayOff(day) ? 'consecutive-day' : ''}">
|
||||||
{#if getHoliday(day)}
|
<span class={holiday?.hidden ? 'strikethrough' : ''}>{day}</span>
|
||||||
<Tooltip text={getHoliday(day)?.name} />
|
{#if holiday}
|
||||||
|
<Tooltip text={holiday.name} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
@@ -197,4 +197,9 @@
|
|||||||
font-size: 0.8em;
|
font-size: 0.8em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.strikethrough {
|
||||||
|
text-decoration: line-through;
|
||||||
|
opacity: 0.5;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -1,336 +1,179 @@
|
|||||||
import Holidays from 'date-holidays';
|
import Holidays from 'date-holidays';
|
||||||
|
|
||||||
// Constants
|
|
||||||
const MS_IN_A_DAY = 86400000;
|
const MS_IN_A_DAY = 86400000;
|
||||||
const MAX_GAP_LENGTH = 5;
|
const MAX_GAP_LENGTH = 5;
|
||||||
|
|
||||||
// Helper function to check if a date is a weekend
|
// Core date helper functions
|
||||||
const isWeekend = (date: Date, weekendDays: number[]): boolean => weekendDays.includes(date.getDay());
|
|
||||||
|
|
||||||
// Helper function to check if two dates are the same day
|
|
||||||
const isSameDay = (date1: Date, date2: Date): boolean =>
|
|
||||||
date1.getFullYear() === date2.getFullYear() &&
|
|
||||||
date1.getMonth() === date2.getMonth() &&
|
|
||||||
date1.getDate() === date2.getDate();
|
|
||||||
|
|
||||||
// Helper function to generate a unique key for a date
|
|
||||||
const dateKey = (date: Date): string => `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
const dateKey = (date: Date): string => `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||||
|
const isWeekend = (date: Date, weekendDays: number[]): boolean => weekendDays.includes(date.getDay());
|
||||||
// Helper function to check if a date is a holiday
|
|
||||||
const isHoliday = (date: Date, holidays: { date: Date }[]): boolean => holidays.some(h => dateKey(h.date) === dateKey(date));
|
const isHoliday = (date: Date, holidays: { date: Date }[]): boolean => holidays.some(h => dateKey(h.date) === dateKey(date));
|
||||||
|
const daysBetween = (start: Date, end: Date): number => Math.round((end.getTime() - start.getTime()) / MS_IN_A_DAY);
|
||||||
|
|
||||||
// Helper function to check if a date is a day off
|
// Get holidays for a year, handling multi-day holidays and timezone differences
|
||||||
const isDayOff = (date: Date, allDaysOffSet: Set<string>): boolean => allDaysOffSet.has(dateKey(date));
|
|
||||||
|
|
||||||
// Helper function to calculate the number of days between two dates
|
|
||||||
const daysBetween = (startDate: Date, endDate: Date): number => Math.round((endDate.getTime() - startDate.getTime()) / MS_IN_A_DAY);
|
|
||||||
|
|
||||||
// Helper function to format a date
|
|
||||||
const formatDate = (date: Date): string => date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
|
||||||
|
|
||||||
// Get holidays for a specific year and country
|
|
||||||
export function getHolidaysForYear(countryCode: string, year: number, stateCode?: string): { date: Date; name: string }[] {
|
export function getHolidaysForYear(countryCode: string, year: number, stateCode?: string): { date: Date; name: string }[] {
|
||||||
// The date-holidays lib has translations for many holidays, but defaults to using the language of the country.
|
// Use browser's languages and timezone to get localized holiday names
|
||||||
// We can pass in the browser's preferred languages (though the lib doesn't fall back, e.g. from `de-AT` to `de`)
|
const opts = {
|
||||||
const languages = navigator.languages.map(lang => lang.split('-')[0]);
|
languages: navigator.languages.map(lang => lang.split('-')[0]),
|
||||||
// Start/end dates are returned in that country/state's time zone, so we need to provide our time zone to localise
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||||
const opts = { languages, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone };
|
};
|
||||||
const hd = stateCode ? new Holidays(countryCode, stateCode, opts) : new Holidays(countryCode, opts);
|
const hd = stateCode ? new Holidays(countryCode, stateCode, opts) : new Holidays(countryCode, opts);
|
||||||
console.log(hd.getHolidays(year));
|
|
||||||
return hd.getHolidays(year)
|
return hd.getHolidays(year)
|
||||||
.filter(holiday => holiday.type === 'public')
|
.filter(holiday => holiday.type === 'public')
|
||||||
.flatMap(holiday =>
|
.flatMap(holiday => Array.from(
|
||||||
// To handle single- and multi-day holidays, we generate a holiday entry for each day in the period
|
{ length: daysBetween(holiday.start, holiday.end) },
|
||||||
Array.from({ length: daysBetween(holiday.start, holiday.end) }, (_, i) => ({
|
(_, i) => ({
|
||||||
date: new Date(holiday.start.getFullYear(), holiday.start.getMonth(), holiday.start.getDate() + i),
|
date: new Date(holiday.start.getFullYear(), holiday.start.getMonth(), holiday.start.getDate() + i),
|
||||||
name: holiday.name,
|
name: holiday.name,
|
||||||
}))
|
})
|
||||||
)
|
))
|
||||||
.sort((holiday1, holiday2) => holiday1.date.getTime() - holiday2.date.getTime() || holiday1.name.localeCompare(holiday2.name));
|
.sort((a, b) => a.date.getTime() - b.date.getTime() || a.name.localeCompare(b.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize days off to create the longest possible chains
|
// Find optimal placement of PTO days to maximize consecutive time off
|
||||||
export function optimizeDaysOff(
|
export function optimizeDaysOff(holidays: { date: Date }[], year: number, daysOff: number, weekendDays: number[] = [0, 6]): Date[] {
|
||||||
holidays: { date: Date }[],
|
|
||||||
year: number,
|
|
||||||
daysOff: number,
|
|
||||||
weekendDays: number[] = [0, 6]
|
|
||||||
): Date[] {
|
|
||||||
const currentYearHolidays = holidays.filter(h => h.date.getFullYear() === year);
|
|
||||||
const weekends = getWeekends(year, weekendDays);
|
|
||||||
const allDaysOffSet = new Set([
|
|
||||||
...currentYearHolidays.map(h => dateKey(h.date)),
|
|
||||||
...weekends.map(d => dateKey(d))
|
|
||||||
]);
|
|
||||||
|
|
||||||
let rankedGaps = rankGapsByEfficiency(
|
|
||||||
findGaps(allDaysOffSet, year, weekendDays),
|
|
||||||
allDaysOffSet,
|
|
||||||
weekendDays
|
|
||||||
);
|
|
||||||
|
|
||||||
return selectDaysOff(rankedGaps, daysOff, allDaysOffSet, year, weekendDays);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate consecutive days off
|
|
||||||
export function calculateConsecutiveDaysOff(
|
|
||||||
holidays: Array<{ date: Date }>,
|
|
||||||
optimizedDaysOff: Date[],
|
|
||||||
year: number,
|
|
||||||
weekendDays: number[] = [0, 6]
|
|
||||||
) {
|
|
||||||
const allDaysOff = new Set([
|
const allDaysOff = new Set([
|
||||||
...holidays.map(h => h.date.toISOString()),
|
...holidays.filter(h => h.date.getFullYear() === year).map(h => dateKey(h.date)),
|
||||||
...optimizedDaysOff.map(d => d.toISOString())
|
...getWeekends(year, weekendDays).map(d => dateKey(d))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Add all weekend days for the year
|
const gaps = findGaps(allDaysOff, year, weekendDays);
|
||||||
const startDate = new Date(year, 0, 1);
|
return selectDaysOff(rankGapsByEfficiency(gaps, allDaysOff, weekendDays), daysOff, allDaysOff, weekendDays);
|
||||||
const endDate = new Date(year, 11, 31);
|
}
|
||||||
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
|
|
||||||
if (weekendDays.includes(d.getDay())) {
|
|
||||||
allDaysOff.add(new Date(d).toISOString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const consecutiveDaysOff: { startDate: Date; endDate: Date; usedDaysOff: number; totalDays: number }[] = [];
|
// Calculate periods of consecutive days off (weekends + holidays + PTO)
|
||||||
let currentGroup: Date[] = [];
|
export function calculateConsecutiveDaysOff(holidays: { date: Date }[], optimizedDaysOff: Date[], year: number, weekendDays: number[] = [0, 6]) {
|
||||||
|
const allDaysOff = new Set([
|
||||||
|
...holidays.map(h => dateKey(h.date)),
|
||||||
|
...optimizedDaysOff.map(d => dateKey(d)),
|
||||||
|
...getWeekends(year, weekendDays).map(d => dateKey(d))
|
||||||
|
]);
|
||||||
|
|
||||||
for (let month = 0; month < 12; month++) {
|
const consecutiveDaysOff = [];
|
||||||
for (let day = 1; day <= 31; day++) {
|
let currentGroup = [];
|
||||||
const date = new Date(year, month, day);
|
|
||||||
if (date.getMonth() !== month) break;
|
|
||||||
|
|
||||||
if (isWeekend(date, weekendDays) || isHoliday(date, holidays) || allDaysOff.has(date.toISOString())) {
|
for (let d = new Date(year, 0, 1); d <= new Date(year, 11, 31); d.setDate(d.getDate() + 1)) {
|
||||||
currentGroup.push(date);
|
if (isWeekend(d, weekendDays) || isHoliday(d, holidays) || allDaysOff.has(dateKey(d))) {
|
||||||
} else {
|
currentGroup.push(new Date(d));
|
||||||
if (isValidPeriod(currentGroup, weekendDays, holidays, optimizedDaysOff)) {
|
} else if (currentGroup.length > 0) {
|
||||||
const startDate = currentGroup[0];
|
if (hasWeekendAndNonWeekendHoliday(currentGroup, weekendDays, holidays, optimizedDaysOff)) {
|
||||||
const endDate = currentGroup[currentGroup.length - 1];
|
consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff));
|
||||||
const totalDays = daysBetween(startDate, endDate) + 1;
|
|
||||||
const usedDaysOff = currentGroup.filter(d =>
|
|
||||||
optimizedDaysOff.some(od =>
|
|
||||||
od.getFullYear() === d.getFullYear() &&
|
|
||||||
od.getMonth() === d.getMonth() &&
|
|
||||||
od.getDate() === d.getDate()
|
|
||||||
)
|
|
||||||
).length;
|
|
||||||
|
|
||||||
consecutiveDaysOff.push({
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
usedDaysOff,
|
|
||||||
totalDays
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
currentGroup = [];
|
currentGroup = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (isValidPeriod(currentGroup, weekendDays, holidays, optimizedDaysOff)) {
|
if (currentGroup.length > 0 && hasWeekendAndNonWeekendHoliday(currentGroup, weekendDays, holidays, optimizedDaysOff)) {
|
||||||
const startDate = currentGroup[0];
|
consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff));
|
||||||
const endDate = currentGroup[currentGroup.length - 1];
|
|
||||||
const totalDays = daysBetween(startDate, endDate) + 1;
|
|
||||||
const usedDaysOff = currentGroup.filter(d =>
|
|
||||||
optimizedDaysOff.some(od =>
|
|
||||||
od.getFullYear() === d.getFullYear() &&
|
|
||||||
od.getMonth() === d.getMonth() &&
|
|
||||||
od.getDate() === d.getDate()
|
|
||||||
)
|
|
||||||
).length;
|
|
||||||
|
|
||||||
consecutiveDaysOff.push({
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
usedDaysOff,
|
|
||||||
totalDays
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return consecutiveDaysOff;
|
return consecutiveDaysOff;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the isValidPeriod function
|
// Get all weekend days for a year
|
||||||
function isValidPeriod(
|
|
||||||
group: Date[],
|
|
||||||
weekendDays: number[],
|
|
||||||
holidays: Array<{ date: Date }>,
|
|
||||||
optimizedDaysOff: Date[]
|
|
||||||
): boolean {
|
|
||||||
if (group.length < 2) return false;
|
|
||||||
|
|
||||||
// Count weekend days
|
|
||||||
const weekendDates = group.filter(date => weekendDays.includes(date.getDay()));
|
|
||||||
if (weekendDates.length === 0) return false; // Must have at least one weekend day
|
|
||||||
|
|
||||||
// Count non-weekend days that are either holidays or PTO days
|
|
||||||
const nonWeekendHolidayOrPTO = group.some(date => {
|
|
||||||
// Skip if it's a weekend day
|
|
||||||
if (weekendDays.includes(date.getDay())) return false;
|
|
||||||
|
|
||||||
// Check if it's either a holiday or PTO day
|
|
||||||
return isHoliday(date, holidays) ||
|
|
||||||
optimizedDaysOff.some(od =>
|
|
||||||
od.getFullYear() === date.getFullYear() &&
|
|
||||||
od.getMonth() === date.getMonth() &&
|
|
||||||
od.getDate() === date.getDate()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Must have at least one weekend day AND one non-weekend holiday/PTO day
|
|
||||||
return nonWeekendHolidayOrPTO;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all weekends for a specific year
|
|
||||||
function getWeekends(year: number, weekendDays: number[]): Date[] {
|
function getWeekends(year: number, weekendDays: number[]): Date[] {
|
||||||
const weekends: Date[] = [];
|
const weekends = [];
|
||||||
for (let month = 0; month < 12; month++) {
|
for (let d = new Date(year, 0, 1); d <= new Date(year, 11, 31); d.setDate(d.getDate() + 1)) {
|
||||||
for (let day = 1; day <= 31; day++) {
|
if (d.getMonth() === d.getMonth() && isWeekend(d, weekendDays)) {
|
||||||
const date = new Date(year, month, day);
|
weekends.push(new Date(d));
|
||||||
if (date.getMonth() !== month) break;
|
|
||||||
if (isWeekend(date, weekendDays)) weekends.push(date);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return weekends;
|
return weekends;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find gaps between days off
|
// Find gaps between days off that could be filled with PTO
|
||||||
function findGaps(allDaysOffSet: Set<string>, year: number, weekendDays: number[]): { start: Date; end: Date; gapLength: number }[] {
|
function findGaps(allDaysOff: Set<string>, year: number, weekendDays: number[]) {
|
||||||
const gaps: { start: Date; end: Date; gapLength: number }[] = [];
|
const gaps = [];
|
||||||
let currentGapStart: Date | null = null;
|
let gapStart = null;
|
||||||
|
|
||||||
for (let month = 0; month < 12; month++) {
|
for (let d = new Date(year, 0, 1); d <= new Date(year, 11, 31); d.setDate(d.getDate() + 1)) {
|
||||||
for (let day = 1; day <= 31; day++) {
|
if (!allDaysOff.has(dateKey(d)) && !isWeekend(d, weekendDays)) {
|
||||||
const date = new Date(year, month, day);
|
if (!gapStart) gapStart = new Date(d);
|
||||||
if (date.getMonth() !== month) break;
|
} else if (gapStart) {
|
||||||
|
const gapLength = daysBetween(gapStart, d);
|
||||||
const isDayOff = allDaysOffSet.has(dateKey(date));
|
|
||||||
|
|
||||||
if (!isDayOff && !isWeekend(date, weekendDays)) {
|
|
||||||
if (!currentGapStart) currentGapStart = date;
|
|
||||||
} else if (currentGapStart) {
|
|
||||||
const gapLength = daysBetween(currentGapStart, date);
|
|
||||||
if (gapLength > 0 && gapLength <= MAX_GAP_LENGTH) {
|
if (gapLength > 0 && gapLength <= MAX_GAP_LENGTH) {
|
||||||
gaps.push({ start: currentGapStart, end: new Date(date.getTime() - MS_IN_A_DAY), gapLength });
|
gaps.push({ start: gapStart, end: new Date(d.getTime() - MS_IN_A_DAY), gapLength });
|
||||||
}
|
}
|
||||||
currentGapStart = null;
|
gapStart = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (currentGapStart) {
|
|
||||||
const lastDayOfYear = new Date(year, 11, 31);
|
|
||||||
const gapLength = daysBetween(currentGapStart, lastDayOfYear) + 1;
|
|
||||||
if (gapLength > 0 && gapLength <= MAX_GAP_LENGTH) {
|
|
||||||
gaps.push({ start: currentGapStart, end: lastDayOfYear, gapLength });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return gaps;
|
return gaps;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rank gaps by efficiency
|
// Rank gaps by how efficiently they can be used to create longer periods off
|
||||||
function rankGapsByEfficiency(
|
function rankGapsByEfficiency(gaps: any[], allDaysOff: Set<string>, weekendDays: number[]) {
|
||||||
gaps: { start: Date; end: Date; gapLength: number }[],
|
return gaps
|
||||||
allDaysOffSet: Set<string>,
|
.map(gap => {
|
||||||
weekendDays: number[] = [0, 6]
|
const [backward, forward] = ['backward', 'forward'].map(direction =>
|
||||||
): any[] {
|
calculateChain(direction === 'backward' ? gap.start : gap.end, gap.gapLength, allDaysOff, direction as 'backward' | 'forward', weekendDays)
|
||||||
return gaps.map(gap => {
|
);
|
||||||
const backward = calculateChain(gap.start, gap.gapLength, allDaysOffSet, 'backward', weekendDays);
|
|
||||||
const forward = calculateChain(gap.end, gap.gapLength, allDaysOffSet, 'forward', weekendDays);
|
|
||||||
|
|
||||||
return forward.chainLength > backward.chainLength ||
|
return forward.chainLength > backward.chainLength ||
|
||||||
(forward.chainLength === backward.chainLength && forward.usedDaysOff <= backward.usedDaysOff)
|
(forward.chainLength === backward.chainLength && forward.usedDaysOff <= backward.usedDaysOff)
|
||||||
? { ...gap, ...forward, fillFrom: 'end' }
|
? { ...gap, ...forward, fillFrom: 'end' }
|
||||||
: { ...gap, ...backward, fillFrom: 'start' };
|
: { ...gap, ...backward, fillFrom: 'start' };
|
||||||
}).sort((a, b) =>
|
})
|
||||||
a.gapLength - b.gapLength ||
|
.sort((a, b) => a.gapLength - b.gapLength || b.chainLength - a.chainLength || a.usedDaysOff - b.usedDaysOff);
|
||||||
b.chainLength - a.chainLength ||
|
|
||||||
a.usedDaysOff - b.usedDaysOff
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate potential chain length and days off used
|
// Calculate potential chain length in either direction from a gap
|
||||||
function calculateChain(
|
function calculateChain(date: Date, gapLength: number, allDaysOff: Set<string>, direction: 'backward' | 'forward', weekendDays: number[]) {
|
||||||
startDate: Date,
|
|
||||||
gapLength: number,
|
|
||||||
allDaysOffSet: Set<string>,
|
|
||||||
direction: 'backward' | 'forward',
|
|
||||||
weekendDays: number[] = [0, 6]
|
|
||||||
): { chainLength: number; usedDaysOff: number } {
|
|
||||||
let chainLength = gapLength;
|
|
||||||
let usedDaysOff = 0;
|
|
||||||
let currentDate = new Date(startDate);
|
|
||||||
|
|
||||||
const increment = direction === 'backward' ? -1 : 1;
|
const increment = direction === 'backward' ? -1 : 1;
|
||||||
const boundaryCheck = direction === 'backward' ? -MS_IN_A_DAY : MS_IN_A_DAY;
|
let chainLength = gapLength;
|
||||||
|
let currentDate = new Date(date);
|
||||||
|
|
||||||
while (allDaysOffSet.has(dateKey(new Date(currentDate.getTime() + boundaryCheck))) ||
|
while (allDaysOff.has(dateKey(new Date(currentDate.getTime() + MS_IN_A_DAY * increment))) ||
|
||||||
isWeekend(new Date(currentDate.getTime() + boundaryCheck), weekendDays)) {
|
isWeekend(new Date(currentDate.getTime() + MS_IN_A_DAY * increment), weekendDays)) {
|
||||||
chainLength++;
|
chainLength++;
|
||||||
currentDate.setDate(currentDate.getDate() + increment);
|
currentDate.setDate(currentDate.getDate() + increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < gapLength; i++) {
|
return {
|
||||||
const potentialDayOff = new Date(startDate);
|
chainLength,
|
||||||
potentialDayOff.setDate(potentialDayOff.getDate() + (i * increment));
|
usedDaysOff: Array.from({ length: gapLength }, (_, i) => {
|
||||||
if (!allDaysOffSet.has(dateKey(potentialDayOff)) && !isWeekend(potentialDayOff, weekendDays)) {
|
const d = new Date(date);
|
||||||
usedDaysOff++;
|
d.setDate(d.getDate() + i * increment);
|
||||||
}
|
return !allDaysOff.has(dateKey(d)) && !isWeekend(d, weekendDays);
|
||||||
}
|
}).filter(Boolean).length
|
||||||
|
};
|
||||||
return { chainLength, usedDaysOff };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select days off based on ranked gaps
|
// Select optimal days off based on ranked gaps
|
||||||
function selectDaysOff(rankedGaps: any[], daysOff: number, allDaysOffSet: Set<string>, year: number, weekendDays: number[]): Date[] {
|
function selectDaysOff(rankedGaps: any[], daysOff: number, allDaysOff: Set<string>, weekendDays: number[]): Date[] {
|
||||||
const selectedDays: Date[] = [];
|
const selectedDays = [];
|
||||||
|
let remainingDays = daysOff;
|
||||||
|
|
||||||
while (daysOff > 0 && rankedGaps.length > 0) {
|
for (const gap of rankedGaps) {
|
||||||
const gap = rankedGaps.shift();
|
if (remainingDays <= 0) break;
|
||||||
|
|
||||||
const increment = gap.fillFrom === 'start' ? 1 : -1;
|
const increment = gap.fillFrom === 'start' ? 1 : -1;
|
||||||
const startDate = gap.fillFrom === 'start' ? gap.start : gap.end;
|
const startDate = gap.fillFrom === 'start' ? gap.start : gap.end;
|
||||||
|
|
||||||
for (let i = 0; i < gap.gapLength && daysOff > 0; i++) {
|
for (let i = 0; i < gap.gapLength && remainingDays > 0; i++) {
|
||||||
const potentialDayOff = new Date(startDate);
|
const day = new Date(startDate);
|
||||||
potentialDayOff.setDate(potentialDayOff.getDate() + (i * increment));
|
day.setDate(day.getDate() + (i * increment));
|
||||||
|
|
||||||
if (!allDaysOffSet.has(dateKey(potentialDayOff)) && !isWeekend(potentialDayOff, weekendDays)) {
|
if (!allDaysOff.has(dateKey(day)) && !isWeekend(day, weekendDays)) {
|
||||||
selectedDays.push(potentialDayOff);
|
selectedDays.push(day);
|
||||||
allDaysOffSet.add(dateKey(potentialDayOff));
|
remainingDays--;
|
||||||
daysOff--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const newGaps = findGaps(allDaysOffSet, year, weekendDays);
|
|
||||||
rankedGaps = rankGapsByEfficiency(newGaps, allDaysOffSet, weekendDays);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return selectedDays;
|
return selectedDays;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add consecutive days off to the list
|
// Check if a group contains both a weekend day and a non-weekend holiday/PTO day
|
||||||
function addConsecutiveDaysOff(
|
function hasWeekendAndNonWeekendHoliday(group: Date[], weekendDays: number[], holidays: { date: Date }[], optimizedDaysOff: Date[]) {
|
||||||
consecutiveDaysOff: { startDate: Date; endDate: Date; usedDaysOff: number; totalDays: number }[],
|
return group.some(d => weekendDays.includes(d.getDay())) &&
|
||||||
currentGroup: Date[],
|
group.some(d => !weekendDays.includes(d.getDay()) && (isHoliday(d, holidays) || optimizedDaysOff.some(od => dateKey(od) === dateKey(d))));
|
||||||
optimizedDaysOff: Date[]
|
}
|
||||||
) {
|
|
||||||
const startDate = currentGroup[0];
|
// Create a period object from a group of consecutive days
|
||||||
const endDate = currentGroup[currentGroup.length - 1];
|
function createPeriod(group: Date[], optimizedDaysOff: Date[]) {
|
||||||
const totalDays = daysBetween(startDate, endDate) + 1;
|
return {
|
||||||
|
startDate: group[0],
|
||||||
// Create a Set of optimized days off for faster lookup
|
endDate: group[group.length - 1],
|
||||||
const optimizedDaysOffSet = new Set(optimizedDaysOff.map(d => dateKey(d)));
|
totalDays: daysBetween(group[0], group[group.length - 1]) + 1,
|
||||||
|
usedDaysOff: group.filter(d => optimizedDaysOff.some(od => dateKey(od) === dateKey(d))).length
|
||||||
// Count only the days that were actually used from our PTO days
|
};
|
||||||
const usedDaysOff = currentGroup.filter(d => optimizedDaysOffSet.has(dateKey(d))).length;
|
|
||||||
|
|
||||||
consecutiveDaysOff.push({
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
usedDaysOff,
|
|
||||||
totalDays
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
@@ -253,6 +253,7 @@
|
|||||||
$: visibleHolidaysCount = holidays.filter(h => !h.hidden).length;
|
$: visibleHolidaysCount = holidays.filter(h => !h.hidden).length;
|
||||||
|
|
||||||
function toggleWeekendDay(dayNumber: number) {
|
function toggleWeekendDay(dayNumber: number) {
|
||||||
|
console.log('Toggling weekend day:', dayNumber);
|
||||||
if (weekendDays.includes(dayNumber)) {
|
if (weekendDays.includes(dayNumber)) {
|
||||||
weekendDays = weekendDays.filter(d => d !== dayNumber);
|
weekendDays = weekendDays.filter(d => d !== dayNumber);
|
||||||
} else {
|
} else {
|
||||||
@@ -554,7 +555,6 @@
|
|||||||
.content-box li {
|
.content-box li {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.content-box button {
|
.content-box button {
|
||||||
@@ -603,7 +603,7 @@
|
|||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
gap: 10px;
|
gap: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.holidays-list li:hover {
|
.holidays-list li:hover {
|
||||||
@@ -710,15 +710,6 @@
|
|||||||
|
|
||||||
<div class="content-box" id="calendar">
|
<div class="content-box" id="calendar">
|
||||||
<div class="calendar-key">
|
<div class="calendar-key">
|
||||||
<div class="key-item">
|
|
||||||
<div class="key-label">
|
|
||||||
<span class="color-box weekend"></span>
|
|
||||||
<span>Weekend</span>
|
|
||||||
</div>
|
|
||||||
<a href="#" on:click|preventDefault={() => showWeekendSettings = !showWeekendSettings} class="edit-link">
|
|
||||||
(edit)
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
<div class="key-item">
|
<div class="key-item">
|
||||||
<div class="key-label">
|
<div class="key-label">
|
||||||
<span class="color-box optimized"></span>
|
<span class="color-box optimized"></span>
|
||||||
@@ -736,6 +727,15 @@
|
|||||||
</a>
|
</a>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
<div class="key-item">
|
||||||
|
<div class="key-label">
|
||||||
|
<span class="color-box weekend"></span>
|
||||||
|
<span>Weekend</span>
|
||||||
|
</div>
|
||||||
|
<a href="#" on:click|preventDefault={() => showWeekendSettings = !showWeekendSettings} class="edit-link">
|
||||||
|
(edit)
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if showHolidaysList || showWeekendSettings}
|
{#if showHolidaysList || showWeekendSettings}
|
||||||
|
|||||||
Reference in New Issue
Block a user