diff --git a/Aberwyn/Controllers/BudgetController.cs b/Aberwyn/Controllers/BudgetController.cs index 017da7a..85f26ec 100644 --- a/Aberwyn/Controllers/BudgetController.cs +++ b/Aberwyn/Controllers/BudgetController.cs @@ -1,6 +1,8 @@ -using Aberwyn.Models; +using Aberwyn.Data; +using Aberwyn.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.Globalization; namespace Aberwyn.Controllers { @@ -12,6 +14,9 @@ namespace Aberwyn.Controllers { ViewBag.Year = year; ViewBag.Month = month; + DateTime payDate = SalaryDateService.GetSalaryPayDate(year, month -1); + ViewBag.Paydate = payDate.ToString("d MMMM", new CultureInfo("sv-SE")); + return View(); } [Route("budget/list")] @@ -39,9 +44,19 @@ namespace Aberwyn.Controllers public IActionResult Index() { var now = DateTime.Now; - return RedirectToAction("Index", new { year = now.Year, month = now.Month }); + return RedirectToAction("Index", new { year = now.Year, month = now.Month + 1 }); } + [HttpGet("api/budget/{year:int}/{month:int}/salary-date")] + public IActionResult SalaryDate(int year, int month) + { + var date = SalaryDateService.GetSalaryPayDate(year, month); + return Ok(new + { + iso = date.ToString("yyyy-MM-dd"), + display = date.ToString("d MMMM yyyy", new CultureInfo("sv-SE")) + }); + } } } diff --git a/Aberwyn/Data/PayoutService.cs b/Aberwyn/Data/PayoutService.cs new file mode 100644 index 0000000..50379c3 --- /dev/null +++ b/Aberwyn/Data/PayoutService.cs @@ -0,0 +1,96 @@ +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 20–26 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; + } +} \ No newline at end of file diff --git a/Aberwyn/Models/Budget.cs b/Aberwyn/Models/Budget.cs index d1361c1..c7474bc 100644 --- a/Aberwyn/Models/Budget.cs +++ b/Aberwyn/Models/Budget.cs @@ -127,4 +127,6 @@ namespace Aberwyn.Models public string Color { get; set; } // standardfärg, kan modifieras per period } + + } diff --git a/Aberwyn/Views/Budget/Index.cshtml b/Aberwyn/Views/Budget/Index.cshtml index 211f3dd..c5a550b 100644 --- a/Aberwyn/Views/Budget/Index.cshtml +++ b/Aberwyn/Views/Budget/Index.cshtml @@ -51,6 +51,7 @@

Sammanställning