from datetime import datetime from typing import List from io import BytesIO from xhtml2pdf import pisa from calendar_manager import CalendarManager from prediction_controller import PredictionController from date_service import DateService class PredictionReportService: """Service to export the current prediction and entries into a PDF report.""" @staticmethod def export_pdf(calendar_manager: CalendarManager, prediction_controller: PredictionController, out_path: str, dateformat: str = "dd.MM.yyyy") -> bool: try: # Gather data launch_dt = prediction_controller.get_launch_date() duration = prediction_controller.get_duration() prediction_dt = prediction_controller.get_prediction() entries = calendar_manager.list_entries() # Format helpers def fmt_date(dt): return DateService.format_date_for_display(dt, dateformat) if dt else "" launch_str = fmt_date(launch_dt) duration_str = f"{duration.years} Jahre" if duration and hasattr(duration, 'years') else "" prediction_str = fmt_date(prediction_dt) # Sum time_periods (months and days separately) total_months = 0 total_days = 0 for e in entries: tp = getattr(e, 'time_period', None) if not tp: continue try: parts = tp.split() if len(parts) >= 2: value = int(parts[0]) unit = parts[1].lower() if value > 0: if 'monat' in unit: total_months += value elif 'tag' in unit: total_days += value except Exception: # Ignore unparsable time_periods pass sum_parts = [] if total_months > 0: sum_parts.append(f"{total_months} Monate") if total_days > 0: sum_parts.append(f"{total_days} Tage") sum_str = ", ".join(sum_parts) if sum_parts else "0" # Build HTML created_stamp = datetime.now().strftime('%d.%m.%Y %H:%M:%S') html = [] html.append("") html.append("
") html.append(""" """) html.append("") html.append("| Beginn | " "Ende | " "Art | " "Kommentar | " "Beginn Anrechnung | " "Ende Anrechnung | " "Zeitraum | " "
|---|---|---|---|---|---|---|
| {esc(start_text)} | " f"{esc(end_text)} | " f"{esc(keyword_text)} | " f"{esc(commentary_text)} | " f"{esc(c_start_text)} | " f"{esc(c_end_text)} | " f"{esc(time_text)} |
| Summe Anrechnungszeitraum: {sum_str} | ||||||