summaryrefslogtreecommitdiff
path: root/date_calculator.py
blob: befccfc1496fbc34e5deafb0349d6a96fddc672d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# date_calculator.py
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import math

class DateCalculator:
    @staticmethod
    def truncate_periods(periods, launch, prediction):
        considered_periods = []
        for start, end, id in periods:
            # print(start)
            # print(launch)
            truncated_start = max(start, launch)
            if truncated_start <= end:
                considered_periods.append((truncated_start, end, id))
        return considered_periods

    @staticmethod
    def round_periods(periods):
        rounded_periods = []
        total_months = 0
        
        for start, end, id in periods:
            year_diff = end.year - start.year
            month_diff = end.month - start.month
            months = year_diff * 12 + month_diff
            if end.day >= start.day:
                months += 1
            rounded_end = start + relativedelta(months=months) - timedelta(days=1)
            
            rounded_periods.append((start, rounded_end, id))
            total_months += months
        
        return rounded_periods, total_months
    
    @staticmethod
    def adjust_periods(periods):
        """Adjust overlapping periods without merging.
        - Later periods overlapping with a previous one have their start moved to the previous end + 1 day.
        - Periods fully contained in a previous one are discarded.
        """
        if not periods:
            return []

        # Sort by start date, then end date for stability
        periods = sorted(periods, key=lambda p: (p[0], p[1]))

        adjusted = []
        for start, end, pid in periods:
            if not adjusted:
                adjusted.append((start, end, pid))
                continue

            last_start, last_end, last_pid = adjusted[-1]

            if start <= last_end:
                # Fully contained in previous period → discard
                if end <= last_end:
                    continue
                # Overlaps head; push start to the day after last_end
                new_start = last_end + timedelta(days=1)
                if new_start <= end:
                    adjusted.append((new_start, end, pid))
                # else new_start > end → discard
            else:
                adjusted.append((start, end, pid))

        return adjusted
    
    @staticmethod
    def find_non_overlapping_periods(existing_periods, test_period):
        existing_periods.sort(key=lambda x: x[0])
        
        test_start, test_end, id = test_period
        non_overlapping_periods = []
        
        for start, end, _ in existing_periods:
            if test_end < start:
                non_overlapping_periods.append((test_start, test_end, id))
                return non_overlapping_periods
            
            elif test_start > end:
                continue
            
            else:
                if test_start < start:
                    non_overlapping_periods.append((test_start, start - timedelta(days=1), id))
                
                test_start = end + timedelta(days=1)
        
        if test_start <= test_end:
            non_overlapping_periods.append((test_start, test_end, id))
        
        return non_overlapping_periods
        
    def calculate_prediction(self, launch_date, duration, **kwargs):
        total_days = 0
        total_months = 0
        updated = True
        prediction_start = launch_date + duration - timedelta(days = 1)
        prediction = prediction_start
        
        events = []
        half_projects = []
        full_projects = []
        other_kwargs = {}
        
        for k, v in kwargs.items():
            if k == "Sonstige":
                events.extend(v)
            elif k == "EZ 50%":
                half_projects.extend(v)
            elif k == "EZ 100%":
                full_projects.extend(v)
            elif k == "EZ pauschal":
                full_projects.extend(v)
            else:
                other_kwargs[k] = v
        
        while updated:
            updated = False
            considered_events = self.truncate_periods(events, launch_date, prediction)
            considered_full_projects = self.truncate_periods(full_projects, launch_date, prediction)
            considered_half_projects = self.truncate_periods(half_projects, launch_date, prediction)
            
            considered_full_projects_merged = self.adjust_periods(considered_full_projects)
            considered_full_projects_rounded, months = self.round_periods(considered_full_projects_merged)
            considered_full_projects_merged2 = self.adjust_periods(considered_full_projects_rounded)
            considered_full_projects_rounded2, months = self.round_periods(considered_full_projects_merged2)
            
            non_overlapping_half_projects = []
            for test_interval in considered_half_projects:
                non_overlapping_half_projects.extend(
                    self.find_non_overlapping_periods(considered_full_projects_rounded2, test_interval)
                )
            
            considered_half_projects_merged = self.adjust_periods(non_overlapping_half_projects)
            considered_half_projects_rounded, months2 = self.round_periods(considered_half_projects_merged)
            considered_half_projects_merged2 = self.adjust_periods(considered_half_projects_rounded)
            considered_half_projects_rounded2, months2 = self.round_periods(considered_half_projects_merged2)
            
            all_projects_merged = self.adjust_periods(
                considered_full_projects_rounded2 + considered_half_projects_rounded2
            )
            merged_event_periods = self.adjust_periods(considered_events)
            
            non_overlapping_event_periods = []
            for test_interval in merged_event_periods:
                non_overlapping_event_periods.extend(
                    self.find_non_overlapping_periods(all_projects_merged, test_interval)
                )
                
            new_total_months = months + math.ceil(months2 / 2)
            new_total_days = sum((end - start).days + 1 for start, end, _ in non_overlapping_event_periods)

            if new_total_days != total_days or new_total_months != total_months:
                total_days = new_total_days
                total_months = new_total_months
                updated = True
                prediction = launch_date + duration + relativedelta(months=total_months) + timedelta(days=total_days-1)
        
        # print(prediction, prediction_start + relativedelta(years = 6))
        if prediction > prediction_start + relativedelta(years = 6):
            prediction = prediction_start + relativedelta(years = 6)
            
        return prediction, considered_full_projects_rounded2 + considered_half_projects_rounded2 + non_overlapping_event_periods