Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week to month converter #570

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/Core/Weeks/Week.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private bool DateIsInWeek(DateOnly day)
return FromDateOnly(day).Equals(this);
}


public static bool operator ==(Week left, Week right)
{
if (ReferenceEquals(left, right)) return true;
Expand Down
49 changes: 49 additions & 0 deletions backend/Core/Weeks/WeekExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Core.Weeks;
public static class WeekExtensions
{

public struct MonthsOfWeek
{
public int Month { get; init; }
public int? SecondMonth { get; init; }
public int Distribution { get; init; }
}

public static MonthsOfWeek GetMonthOfWeek(this Week week)
{
int daysFromStartOfYear = 1 + (week.WeekNumber - 1) * 7;
var dayOfWeek = new DateOnly(week.Year, 1, 1).AddDays(daysFromStartOfYear - 1); // Adjust start of year to the calculated day
var monday = dayOfWeek.GetPreviousOrCurrentMonday();
int month = monday.Month;

double distribution = 100;
int? secondMonth = null;

for (int i = 1; i < 7; i++)
{
var addedDayDate = monday.AddDays(i);
if (addedDayDate.Month != month)
{
distribution = distribution / 7 * i;
secondMonth = addedDayDate.Month;
break;
}
}

return new MonthsOfWeek
{
Month = month,
SecondMonth = secondMonth,
Distribution = (int)distribution
};
}
public static DateOnly GetPreviousOrCurrentMonday(this DateOnly date)
{
if (date.DayOfWeek == DayOfWeek.Monday)
{
return date;
}
int daysToSubtract = (date.DayOfWeek - DayOfWeek.Monday + 7) % 7;
return date.AddDays(-daysToSubtract);
}
}
56 changes: 56 additions & 0 deletions frontend/src/app/[organisation]/kunder/weekToMonthConverter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { addDays, isMonday, previousMonday } from "date-fns";

export default async function WeekToMonth() {
type Week = {
week: number;
year: number;
};

type MonthDistributionOfWeek = {
week: number;
year: number;
month: number;
secondMonth?: number;
distribution: number;
};

function weekToWeekType(weekInput: number) {
const weekInputStr = weekInput.toString();
return {
week: Number(weekInputStr.slice(-2)),
year: Number(weekInputStr.slice(0, 4)),
} as Week;
}

function getMonthOfWeek(week: Week) {
const weekNumber = week.week;
const year = week.year;
var daysFromStartOfYear = 1 + (weekNumber - 1) * 7;

const dayOfSelectedWeek = new Date(year, 0, daysFromStartOfYear);
const mondayOfSelectedWeek = !isMonday(dayOfSelectedWeek)
? previousMonday(dayOfSelectedWeek)
: dayOfSelectedWeek;
const month = mondayOfSelectedWeek.getMonth();

var distribution = 100;
var endDate = null;
for (let i = 1; i < 7; i++) {
const addedDayDate = addDays(mondayOfSelectedWeek, i);
if (addedDayDate.getMonth() != month) {
distribution = (distribution / 7) * i;
endDate = addedDayDate;
break;
}
}
const weekToMonthInstance = {
week: weekNumber,
year: year,
month: month,
secondMonth: endDate?.getMonth(),
distribution: distribution,
} as MonthDistributionOfWeek;

return weekToMonthInstance;
}
}
Loading