Files
Aberwyn/Aberwyn/Data/PayoutService.cs
Elias Jansson 7b3c0998a0
All checks were successful
continuous-integration/drone/push Build is passing
Same same
2026-02-11 19:56:31 +01:00

97 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Aberwyn.Data;
namespace Aberwyn.Data
{
public class PayoutService
{
}
public static class SwedishHolidays
{
public static bool IsRedDay(DateTime date)
{
var year = date.Year;
var fixedHolidays = new[]
{
new DateTime(year, 1, 1), // Nyårsdagen
new DateTime(year, 1, 6), // Trettondedag jul
new DateTime(year, 5, 1), // Första maj
new DateTime(year, 6, 6), // Nationaldagen
new DateTime(year, 12, 25), // Juldagen
new DateTime(year, 12, 26), // Annandag jul
};
if (fixedHolidays.Contains(date.Date))
return true;
// Rörliga helgdagar
var easter = GetEasterSunday(year);
var movable = new[]
{
easter.AddDays(-2), // Långfredagen
easter, // Påskdagen
easter.AddDays(1), // Annandag påsk
easter.AddDays(39), // Kristi himmelsfärd
easter.AddDays(49), // Pingstdagen
GetMidsummerDay(year),
};
return movable.Contains(date.Date);
}
private static DateTime GetMidsummerDay(int year)
{
// Lördag mellan 2026 juni
for (int day = 20; day <= 26; day++)
{
var d = new DateTime(year, 6, day);
if (d.DayOfWeek == DayOfWeek.Saturday)
return d;
}
throw new Exception("Midsummer not found");
}
private static DateTime GetEasterSunday(int year)
{
// Meeus/Jones/Butcher
int a = year % 19;
int b = year / 100;
int c = year % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int l = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * l) / 451;
int month = (h + l - 7 * m + 114) / 31;
int day = ((h + l - 7 * m + 114) % 31) + 1;
return new DateTime(year, month, day);
}
}
}
public static class SalaryDateService
{
public static DateTime GetSalaryPayDate(int year, int month)
{
var payDate = new DateTime(year, month, 25);
while (payDate.DayOfWeek == DayOfWeek.Saturday ||
payDate.DayOfWeek == DayOfWeek.Sunday ||
SwedishHolidays.IsRedDay(payDate))
{
payDate = payDate.AddDays(-1);
}
return payDate;
}
}