This commit is contained in:
2026-05-15 10:29:53 +02:00
parent 06b484c847
commit aa8819c31c
9 changed files with 96 additions and 0 deletions
+44
View File
@@ -17,6 +17,7 @@ OUTPUT_DIR = BASE_DIR / "output"
DATE_COLUMNS = { DATE_COLUMNS = {
"Orig Exp Date", "Exp Date", "Rcv Date", "Orig Exp Date", "Exp Date", "Rcv Date",
"Date Asgn", "Disp Date", "Date Ret", "Destroyed", "Max Visit 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 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 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 ─────────────────────────────────────────────────────────── # ── Odvozené sheety ───────────────────────────────────────────────────────────
def build_site_summary(shipments_df): def build_site_summary(shipments_df):
@@ -500,6 +537,7 @@ def create_study_report(study):
df = load_inventory(cursor, study, import_id) df = load_inventory(cursor, study, import_id)
shipments_df = load_shipments(cursor, study, import_id) shipments_df = load_shipments(cursor, study, import_id)
df_patients = load_patients(cursor, study) df_patients = load_patients(cursor, study)
visits_df = load_visits(cursor, study, import_id)
cursor.close() cursor.close()
conn.close() conn.close()
@@ -517,6 +555,7 @@ def create_study_report(study):
destruction_df.to_excel( writer, index=False, sheet_name="Kits for destruction") destruction_df.to_excel( writer, index=False, sheet_name="Kits for destruction")
shipments_df.to_excel( writer, index=False, sheet_name="Shipments") shipments_df.to_excel( writer, index=False, sheet_name="Shipments")
site_summary_df.to_excel( writer, index=False, sheet_name="Site Summary") 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) wb = load_workbook(output_file)
@@ -536,11 +575,16 @@ def create_study_report(study):
format_sheet(wb["Kits for destruction"], header_color="595959") format_sheet(wb["Kits for destruction"], header_color="595959")
format_shipment_sheet(wb["Shipments"], "1F4E79", "375623", N_SHIP_COLS) format_shipment_sheet(wb["Shipments"], "1F4E79", "375623", N_SHIP_COLS)
format_sheet(wb["Site Summary"], header_color="1F4E79") format_sheet(wb["Site Summary"], header_color="1F4E79")
format_sheet(wb["Patient Visits"], header_color="1F4E79")
# ── pacienti (Přehled + Next Visits) na začátek ────────────────────────── # ── pacienti (Přehled + Next Visits) na začátek ──────────────────────────
_write_prehled(wb, df_patients, study) _write_prehled(wb, df_patients, study)
_write_next_visits(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) wb.save(output_file)
print(f" Uloženo: {output_file.name} ({len(df)} řádků)") print(f" Uloženo: {output_file.name} ({len(df)} řádků)")
+52
View File
@@ -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()