mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-17 18:23:42 +00:00
fix: filter report data by max KW when generating reports
Previously, generate_full_report() ignored the kw parameter for data filtering — it was only stored as metadata. This caused all reports to contain data up to the latest available KW, making historical reports (e.g., for KW 8) identical to the current one. Now all 5 sheet calculation functions accept an optional max_kw parameter. When generating a report for a specific KW, only cases with kw <= max_kw are included. Dashboard and vorjahr callers are unaffected (max_kw=None). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4cd52dd0b2
commit
95e84a6978
1 changed files with 55 additions and 23 deletions
|
|
@ -94,7 +94,7 @@ def _empty_ta_weekly_row(kw: int) -> dict:
|
||||||
# Sheet 1: Auswertung KW gesamt
|
# Sheet 1: Auswertung KW gesamt
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def calculate_sheet1_data(db: Session, jahr: int) -> dict:
|
def calculate_sheet1_data(db: Session, jahr: int, max_kw: int | None = None) -> dict:
|
||||||
"""Calculate *Auswertung KW gesamt*.
|
"""Calculate *Auswertung KW gesamt*.
|
||||||
|
|
||||||
Returns::
|
Returns::
|
||||||
|
|
@ -121,8 +121,13 @@ def calculate_sheet1_data(db: Session, jahr: int) -> dict:
|
||||||
* Ablehnungen = cases where ablehnung == True
|
* Ablehnungen = cases where ablehnung == True
|
||||||
* Gutachten = cases where gutachten == True
|
* Gutachten = cases where gutachten == True
|
||||||
* Keine RM = Unterlagen - Gutachten (derived, per KW row)
|
* Keine RM = Unterlagen - Gutachten (derived, per KW row)
|
||||||
|
|
||||||
|
If *max_kw* is given, only data up to and including that KW is included.
|
||||||
"""
|
"""
|
||||||
# One query: group by kw, count the four flags
|
# One query: group by kw, count the four flags
|
||||||
|
filters = [Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr]
|
||||||
|
if max_kw is not None:
|
||||||
|
filters.append(Case.kw <= max_kw)
|
||||||
rows = (
|
rows = (
|
||||||
db.query(
|
db.query(
|
||||||
Case.kw,
|
Case.kw,
|
||||||
|
|
@ -131,7 +136,7 @@ def calculate_sheet1_data(db: Session, jahr: int) -> dict:
|
||||||
func.sum(Case.ablehnung.cast(Integer)).label("ablehnungen"),
|
func.sum(Case.ablehnung.cast(Integer)).label("ablehnungen"),
|
||||||
func.sum(Case.gutachten.cast(Integer)).label("gutachten"),
|
func.sum(Case.gutachten.cast(Integer)).label("gutachten"),
|
||||||
)
|
)
|
||||||
.filter(Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr)
|
.filter(*filters)
|
||||||
.group_by(Case.kw)
|
.group_by(Case.kw)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
@ -178,7 +183,7 @@ def calculate_sheet1_data(db: Session, jahr: int) -> dict:
|
||||||
# Sheet 2: Auswertung nach Fachgebieten
|
# Sheet 2: Auswertung nach Fachgebieten
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def calculate_sheet2_data(db: Session, jahr: int) -> dict:
|
def calculate_sheet2_data(db: Session, jahr: int, max_kw: int | None = None) -> dict:
|
||||||
"""Calculate *Auswertung nach Fachgebieten*.
|
"""Calculate *Auswertung nach Fachgebieten*.
|
||||||
|
|
||||||
Per KW, per Fallgruppe: Anzahl, Gutachten, Keine RM/Ablehnung.
|
Per KW, per Fallgruppe: Anzahl, Gutachten, Keine RM/Ablehnung.
|
||||||
|
|
@ -200,7 +205,12 @@ def calculate_sheet2_data(db: Session, jahr: int) -> dict:
|
||||||
}
|
}
|
||||||
|
|
||||||
Keine RM/Ablehnung = Anzahl - Gutachten (per the Excel formula).
|
Keine RM/Ablehnung = Anzahl - Gutachten (per the Excel formula).
|
||||||
|
|
||||||
|
If *max_kw* is given, only data up to and including that KW is included.
|
||||||
"""
|
"""
|
||||||
|
filters = [Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr]
|
||||||
|
if max_kw is not None:
|
||||||
|
filters.append(Case.kw <= max_kw)
|
||||||
rows = (
|
rows = (
|
||||||
db.query(
|
db.query(
|
||||||
Case.kw,
|
Case.kw,
|
||||||
|
|
@ -208,7 +218,7 @@ def calculate_sheet2_data(db: Session, jahr: int) -> dict:
|
||||||
func.count(Case.id).label("anzahl"),
|
func.count(Case.id).label("anzahl"),
|
||||||
func.sum(Case.gutachten.cast(Integer)).label("gutachten"),
|
func.sum(Case.gutachten.cast(Integer)).label("gutachten"),
|
||||||
)
|
)
|
||||||
.filter(Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr)
|
.filter(*filters)
|
||||||
.group_by(Case.kw, Case.fallgruppe)
|
.group_by(Case.kw, Case.fallgruppe)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
@ -242,7 +252,7 @@ def calculate_sheet2_data(db: Session, jahr: int) -> dict:
|
||||||
# Sheet 3: Auswertung Gutachten
|
# Sheet 3: Auswertung Gutachten
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def calculate_sheet3_data(db: Session, jahr: int) -> dict:
|
def calculate_sheet3_data(db: Session, jahr: int, max_kw: int | None = None) -> dict:
|
||||||
"""Calculate *Auswertung Gutachten*.
|
"""Calculate *Auswertung Gutachten*.
|
||||||
|
|
||||||
Per KW, per group (gesamt + 5 Fallgruppen):
|
Per KW, per group (gesamt + 5 Fallgruppen):
|
||||||
|
|
@ -269,7 +279,16 @@ def calculate_sheet3_data(db: Session, jahr: int) -> dict:
|
||||||
- Per Fallgruppe: Gutachten = count, Alternative = count where typ='Alternative',
|
- Per Fallgruppe: Gutachten = count, Alternative = count where typ='Alternative',
|
||||||
Bestaetigung = Gutachten - Alternative
|
Bestaetigung = Gutachten - Alternative
|
||||||
- Gesamt = sum across all Fallgruppen
|
- Gesamt = sum across all Fallgruppen
|
||||||
|
|
||||||
|
If *max_kw* is given, only data up to and including that KW is included.
|
||||||
"""
|
"""
|
||||||
|
filters = [
|
||||||
|
Case.versicherung == settings.VERSICHERUNG_FILTER,
|
||||||
|
Case.jahr == jahr,
|
||||||
|
Case.gutachten == True, # noqa: E712
|
||||||
|
]
|
||||||
|
if max_kw is not None:
|
||||||
|
filters.append(Case.kw <= max_kw)
|
||||||
rows = (
|
rows = (
|
||||||
db.query(
|
db.query(
|
||||||
Case.kw,
|
Case.kw,
|
||||||
|
|
@ -279,7 +298,7 @@ def calculate_sheet3_data(db: Session, jahr: int) -> dict:
|
||||||
(Case.gutachten_typ == "Alternative").cast(Integer)
|
(Case.gutachten_typ == "Alternative").cast(Integer)
|
||||||
).label("alternative"),
|
).label("alternative"),
|
||||||
)
|
)
|
||||||
.filter(Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr, Case.gutachten == True) # noqa: E712
|
.filter(*filters)
|
||||||
.group_by(Case.kw, Case.fallgruppe)
|
.group_by(Case.kw, Case.fallgruppe)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
@ -322,7 +341,7 @@ def calculate_sheet3_data(db: Session, jahr: int) -> dict:
|
||||||
# Sheet 4: Auswertung Therapieaenderungen
|
# Sheet 4: Auswertung Therapieaenderungen
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def calculate_sheet4_data(db: Session, jahr: int) -> dict:
|
def calculate_sheet4_data(db: Session, jahr: int, max_kw: int | None = None) -> dict:
|
||||||
"""Calculate *Auswertung Therapieaenderungen*.
|
"""Calculate *Auswertung Therapieaenderungen*.
|
||||||
|
|
||||||
Per KW: Gutachten count, TA Ja, TA Nein, Diagnosekorrektur,
|
Per KW: Gutachten count, TA Ja, TA Nein, Diagnosekorrektur,
|
||||||
|
|
@ -344,7 +363,16 @@ def calculate_sheet4_data(db: Session, jahr: int) -> dict:
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
If *max_kw* is given, only data up to and including that KW is included.
|
||||||
"""
|
"""
|
||||||
|
filters = [
|
||||||
|
Case.versicherung == settings.VERSICHERUNG_FILTER,
|
||||||
|
Case.jahr == jahr,
|
||||||
|
Case.gutachten == True, # noqa: E712
|
||||||
|
]
|
||||||
|
if max_kw is not None:
|
||||||
|
filters.append(Case.kw <= max_kw)
|
||||||
rows = (
|
rows = (
|
||||||
db.query(
|
db.query(
|
||||||
Case.kw,
|
Case.kw,
|
||||||
|
|
@ -359,7 +387,7 @@ def calculate_sheet4_data(db: Session, jahr: int) -> dict:
|
||||||
func.sum(Case.ta_unterversorgung.cast(Integer)).label("unterversorgung"),
|
func.sum(Case.ta_unterversorgung.cast(Integer)).label("unterversorgung"),
|
||||||
func.sum(Case.ta_uebertherapie.cast(Integer)).label("uebertherapie"),
|
func.sum(Case.ta_uebertherapie.cast(Integer)).label("uebertherapie"),
|
||||||
)
|
)
|
||||||
.filter(Case.versicherung == settings.VERSICHERUNG_FILTER, Case.jahr == jahr, Case.gutachten == True) # noqa: E712
|
.filter(*filters)
|
||||||
.group_by(Case.kw)
|
.group_by(Case.kw)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
@ -388,7 +416,7 @@ def calculate_sheet4_data(db: Session, jahr: int) -> dict:
|
||||||
# Sheet 5: Auswertung ICD onko
|
# Sheet 5: Auswertung ICD onko
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def calculate_sheet5_data(db: Session, jahr: int) -> dict:
|
def calculate_sheet5_data(db: Session, jahr: int, max_kw: int | None = None) -> dict:
|
||||||
"""Calculate *Auswertung ICD onko*.
|
"""Calculate *Auswertung ICD onko*.
|
||||||
|
|
||||||
Returns sorted list of ICD codes from onko cases with counts.
|
Returns sorted list of ICD codes from onko cases with counts.
|
||||||
|
|
@ -407,20 +435,23 @@ def calculate_sheet5_data(db: Session, jahr: int) -> dict:
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
If *max_kw* is given, only data up to and including that KW is included.
|
||||||
"""
|
"""
|
||||||
|
filter_conditions = [
|
||||||
|
Case.versicherung == settings.VERSICHERUNG_FILTER,
|
||||||
|
Case.fallgruppe == "onko",
|
||||||
|
Case.jahr == jahr,
|
||||||
|
]
|
||||||
|
if max_kw is not None:
|
||||||
|
filter_conditions.append(Case.kw <= max_kw)
|
||||||
rows = (
|
rows = (
|
||||||
db.query(
|
db.query(
|
||||||
func.upper(CaseICDCode.icd_code).label("icd"),
|
func.upper(CaseICDCode.icd_code).label("icd"),
|
||||||
func.count(CaseICDCode.id).label("cnt"),
|
func.count(CaseICDCode.id).label("cnt"),
|
||||||
)
|
)
|
||||||
.join(Case, CaseICDCode.case_id == Case.id)
|
.join(Case, CaseICDCode.case_id == Case.id)
|
||||||
.filter(
|
.filter(and_(*filter_conditions))
|
||||||
and_(
|
|
||||||
Case.versicherung == settings.VERSICHERUNG_FILTER,
|
|
||||||
Case.fallgruppe == "onko",
|
|
||||||
Case.jahr == jahr,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.group_by(func.upper(CaseICDCode.icd_code))
|
.group_by(func.upper(CaseICDCode.icd_code))
|
||||||
.order_by(func.count(CaseICDCode.id).desc(), func.upper(CaseICDCode.icd_code))
|
.order_by(func.count(CaseICDCode.id).desc(), func.upper(CaseICDCode.icd_code))
|
||||||
.all()
|
.all()
|
||||||
|
|
@ -550,8 +581,9 @@ def calculate_dashboard_kpis(db: Session, jahr: int) -> dict:
|
||||||
def generate_full_report(db: Session, jahr: int, kw: int | None = None) -> dict:
|
def generate_full_report(db: Session, jahr: int, kw: int | None = None) -> dict:
|
||||||
"""Generate complete report data for all 5 sheets.
|
"""Generate complete report data for all 5 sheets.
|
||||||
|
|
||||||
The *kw* parameter is recorded for metadata but does not filter the data
|
If *kw* is given, only data up to and including that calendar week is
|
||||||
-- all sheets always contain the full year up to the latest available KW.
|
included in the report. This allows generating historical reports
|
||||||
|
that reflect the state at a specific point in the year.
|
||||||
|
|
||||||
Returns::
|
Returns::
|
||||||
|
|
||||||
|
|
@ -570,9 +602,9 @@ def generate_full_report(db: Session, jahr: int, kw: int | None = None) -> dict:
|
||||||
return {
|
return {
|
||||||
"jahr": jahr,
|
"jahr": jahr,
|
||||||
"kw": kw,
|
"kw": kw,
|
||||||
"sheet1": calculate_sheet1_data(db, jahr),
|
"sheet1": calculate_sheet1_data(db, jahr, max_kw=kw),
|
||||||
"sheet2": calculate_sheet2_data(db, jahr),
|
"sheet2": calculate_sheet2_data(db, jahr, max_kw=kw),
|
||||||
"sheet3": calculate_sheet3_data(db, jahr),
|
"sheet3": calculate_sheet3_data(db, jahr, max_kw=kw),
|
||||||
"sheet4": calculate_sheet4_data(db, jahr),
|
"sheet4": calculate_sheet4_data(db, jahr, max_kw=kw),
|
||||||
"sheet5": calculate_sheet5_data(db, jahr),
|
"sheet5": calculate_sheet5_data(db, jahr, max_kw=kw),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue