diff --git a/IWRS/Drugs/create_report.py b/IWRS/Drugs/create_report.py index 43c8c59..c89651c 100644 --- a/IWRS/Drugs/create_report.py +++ b/IWRS/Drugs/create_report.py @@ -17,6 +17,7 @@ OUTPUT_DIR = BASE_DIR / "output" DATE_COLUMNS = { "Orig Exp Date", "Exp Date", "Rcv Date", "Date Asgn", "Disp Date", "Date Ret", "Destroyed", "Max Visit Date", + "Visit Date", "Scheduled Date", } N_SHIP_COLS = 9 # počet shipment sloupců před detail sloupci @@ -131,6 +132,42 @@ def load_shipments(cursor, study, import_id): return df +def load_visits(cursor, study, import_id): + cursor.execute( + "SELECT MAX(import_id) AS mid FROM iwrs_import WHERE study=%s AND report_type='patients'", + (study,), + ) + patients_import_id = cursor.fetchone()["mid"] or import_id + import_id = patients_import_id + sql = """ + SELECT + v.subject AS Subject, + COALESCE(v.actual_date, v.scheduled_date) AS `Visit Date`, + v.scheduled_date AS `Scheduled Date`, + v.irt_transaction_no AS `IRT Tx No`, + v.irt_transaction_description AS `Visit`, + v.medication_assignment AS `Medication`, + GROUP_CONCAT(v.medication_id ORDER BY v.medication_id SEPARATOR ', ') AS `Med IDs`, + SUM(v.quantity_assigned) AS `Qty` + FROM iwrs_subject_visits v + WHERE v.import_id = %s AND v.study = %s AND v.visit_type = 'Past' + AND v.irt_transaction_no IS NOT NULL + GROUP BY v.subject, v.actual_date, v.scheduled_date, + v.irt_transaction_no, v.irt_transaction_description, v.medication_assignment + ORDER BY v.subject, COALESCE(v.actual_date, v.scheduled_date) + """ + cursor.execute(sql, (import_id, study)) + rows = cursor.fetchall() + df = pd.DataFrame(rows) + for col in ("Visit Date", "Scheduled Date"): + if col in df.columns: + df[col] = pd.to_datetime(df[col], errors="coerce") + if study == "77242113UCO3001" and "Visit" in df.columns: + df["Visit"] = df["Visit"].replace("Subject Number Creation", "Screening") + print(f" Visits: {len(df)} řádků") + return df + + # ── Odvozené sheety ─────────────────────────────────────────────────────────── def build_site_summary(shipments_df): @@ -500,6 +537,7 @@ def create_study_report(study): df = load_inventory(cursor, study, import_id) shipments_df = load_shipments(cursor, study, import_id) df_patients = load_patients(cursor, study) + visits_df = load_visits(cursor, study, import_id) cursor.close() conn.close() @@ -517,6 +555,7 @@ def create_study_report(study): destruction_df.to_excel( writer, index=False, sheet_name="Kits for destruction") shipments_df.to_excel( writer, index=False, sheet_name="Shipments") site_summary_df.to_excel( writer, index=False, sheet_name="Site Summary") + visits_df.to_excel( writer, index=False, sheet_name="Patient Visits") wb = load_workbook(output_file) @@ -536,11 +575,16 @@ def create_study_report(study): format_sheet(wb["Kits for destruction"], header_color="595959") format_shipment_sheet(wb["Shipments"], "1F4E79", "375623", N_SHIP_COLS) format_sheet(wb["Site Summary"], header_color="1F4E79") + format_sheet(wb["Patient Visits"], header_color="1F4E79") # ── pacienti (Přehled + Next Visits) na začátek ────────────────────────── _write_prehled(wb, df_patients, study) _write_next_visits(wb, df_patients, study) + # ── pořadí listů: Patient Visits jako první ────────────────────────────── + names = wb.sheetnames + wb._sheets = [wb["Patient Visits"]] + [wb[s] for s in names if s != "Patient Visits"] + wb.save(output_file) print(f" Uloženo: {output_file.name} ({len(df)} řádků)") diff --git a/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v2.xlsx b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v2.xlsx new file mode 100644 index 0000000..8d65305 Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v2.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v3.xlsx b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v3.xlsx new file mode 100644 index 0000000..bf8628a Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v3.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v4.xlsx b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v4.xlsx new file mode 100644 index 0000000..16d0130 Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v4.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v5.xlsx b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v5.xlsx new file mode 100644 index 0000000..5c1a8c4 Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 42847922MDD3003 CZ IWRS overview v5.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v2.xlsx b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v2.xlsx new file mode 100644 index 0000000..8ac7c7d Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v2.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v3.xlsx b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v3.xlsx new file mode 100644 index 0000000..2d2afa4 Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v3.xlsx differ diff --git a/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v4.xlsx b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v4.xlsx new file mode 100644 index 0000000..30f62cd Binary files /dev/null and b/IWRS/Drugs/output/2026-05-15 77242113UCO3001 CZ IWRS overview v4.xlsx differ diff --git a/IWRS/Drugs/preview_visits.py b/IWRS/Drugs/preview_visits.py new file mode 100644 index 0000000..979f425 --- /dev/null +++ b/IWRS/Drugs/preview_visits.py @@ -0,0 +1,52 @@ +import mysql.connector +import pandas as pd +import db_config + +conn = mysql.connector.connect( + host=db_config.DB_HOST, port=db_config.DB_PORT, + user=db_config.DB_USER, password=db_config.DB_PASSWORD, + database=db_config.DB_NAME, +) +cursor = conn.cursor(dictionary=True) + +# Vezmi nejnovější import_id pro každou studii +for study in ["77242113UCO3001", "42847922MDD3003"]: + cursor.execute( + "SELECT MAX(import_id) AS mid FROM iwrs_import WHERE study=%s AND report_type='patients'", + (study,), + ) + row = cursor.fetchone() + mid = row["mid"] + print(f"\n=== {study} (import_id={mid}) ===") + + cursor.execute(""" + SELECT + v.subject, + v.actual_date, + v.scheduled_date, + v.irt_transaction_no, + v.irt_transaction_description, + v.medication_assignment, + GROUP_CONCAT(v.medication_id ORDER BY v.medication_id SEPARATOR ', ') AS medication_ids, + SUM(v.quantity_assigned) AS quantity_assigned + FROM iwrs_subject_visits v + WHERE v.import_id = %s AND v.study = %s AND v.visit_type = 'Past' + AND v.irt_transaction_no IS NOT NULL + GROUP BY v.subject, v.actual_date, v.scheduled_date, v.irt_transaction_no, + v.irt_transaction_description, v.medication_assignment + ORDER BY v.subject, v.actual_date + LIMIT 20 + """, (mid, study)) + + rows = cursor.fetchall() + df = pd.DataFrame(rows) + if df.empty: + print(" Žádná data.") + else: + pd.set_option("display.max_columns", None) + pd.set_option("display.width", 200) + pd.set_option("display.max_colwidth", 30) + print(df.to_string(index=False)) + +cursor.close() +conn.close()