From 6f87ef14f0b8871abb174d09bccd0421fa7bdb8e Mon Sep 17 00:00:00 2001 From: "vladimir.buzalka" Date: Wed, 19 Nov 2025 14:02:01 +0100 Subject: [PATCH] z230 --- Medicus report/10 MedicusReport.py | 109 ++++++++++++++++++++++ Medicus report/20 Debugscript.py | 30 ++++++ Medicus report/30 MedicusReport.py | 144 +++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 Medicus report/10 MedicusReport.py create mode 100644 Medicus report/20 Debugscript.py create mode 100644 Medicus report/30 MedicusReport.py diff --git a/Medicus report/10 MedicusReport.py b/Medicus report/10 MedicusReport.py new file mode 100644 index 0000000..f4bfe6f --- /dev/null +++ b/Medicus report/10 MedicusReport.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import pymysql +import pandas as pd +from datetime import datetime, timedelta +from openpyxl import load_workbook +from openpyxl.styles import Font, PatternFill, Alignment +from pathlib import Path +import os + +# ============================== +# โš™๏ธ CONFIG +# ============================== +DB_CONFIG = { + "host": "192.168.1.76", + "port": 3307, + "user": "root", + "password": "Vlado9674+", + "database": "medevio", + "charset": "utf8mb4", +} + +# Output location +timestamp = datetime.now().strftime("%Y-%m-%d %H-%M-%S") +OUTPUT_DIR = Path(r"U:\Dropbox\!!!Days\Downloads Z230") +OUTPUT_DIR.mkdir(parents=True, exist_ok=True) + +OUTPUT_FILE = OUTPUT_DIR / f"{timestamp} Medevio report.xlsx" + +# ============================== +# ๐Ÿงน >>> NEW: Remove old reports +# ============================== +for file in OUTPUT_DIR.glob("* Medevio report.xlsx"): + try: + file.unlink() + print("Removed old report:", file) + except Exception as e: + print("Could not remove:", file, "Error:", e) + +# ============================== +# ๐Ÿ“ฅ FETCH DATA +# ============================== +conn = pymysql.connect(**DB_CONFIG) + +two_months_ago = (datetime.now() - timedelta(days=1000)).strftime("%Y-%m-%d %H:%M:%S") + +sql = """ +SELECT + id, + pacient_prijmeni, + pacient_jmeno, + pacient_rodnecislo, + displayTitle, + createdAt, + updatedAt, + doneAt, + removedAt, + attachmentsProcessed, + messagesProcessed, + communicationprocessed, + questionnaireprocessed, + lastSync +FROM pozadavky +WHERE createdAt >= %s +ORDER BY updatedAt DESC +""" + +df = pd.read_sql(sql, conn, params=(two_months_ago,)) +conn.close() + +# ============================== +# ๐Ÿ’พ SAVE TO EXCEL +# ============================== +df.to_excel(OUTPUT_FILE, index=False) + +# ============================== +# ๐ŸŽจ FORMAT EXCEL +# ============================== +wb = load_workbook(OUTPUT_FILE) +ws = wb.active + +# Yellow header +header_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") +header_font = Font(bold=True) + +for cell in ws[1]: + cell.fill = header_fill + cell.font = header_font + cell.alignment = Alignment(horizontal="center") + +# >>> NEW: AutoFilter +ws.auto_filter.ref = ws.dimensions + +# Auto column width +for column in ws.columns: + max_length = 0 + column_letter = column[0].column_letter + for cell in column: + try: + if cell.value is not None: + max_length = max(max_length, len(str(cell.value))) + except: + pass + ws.column_dimensions[column_letter].width = max_length + 2 + +wb.save(OUTPUT_FILE) + +print("Report saved:", OUTPUT_FILE) diff --git a/Medicus report/20 Debugscript.py b/Medicus report/20 Debugscript.py new file mode 100644 index 0000000..f7672d6 --- /dev/null +++ b/Medicus report/20 Debugscript.py @@ -0,0 +1,30 @@ +import pymysql +import pandas as pd +from datetime import datetime, timedelta + +DB_CONFIG = { + "host": "192.168.1.76", + "port": 3307, + "user": "root", + "password": "Vlado9674+", + "database": "medevio", + "charset": "utf8mb4", + "cursorclass": pymysql.cursors.DictCursor, +} + +conn = pymysql.connect(**DB_CONFIG) + +two_months_ago = (datetime.now() - timedelta(days=60)).strftime("%Y-%m-%d %H:%M:%S") + +sql = """ +SELECT * +FROM pozadavky +WHERE createdAt >= %s +ORDER BY createdAt DESC +""" + +df = pd.read_sql(sql, conn, params=(two_months_ago,)) +conn.close() + +print("Rows returned:", len(df)) +print(df.head(10)) diff --git a/Medicus report/30 MedicusReport.py b/Medicus report/30 MedicusReport.py new file mode 100644 index 0000000..ef3a3c0 --- /dev/null +++ b/Medicus report/30 MedicusReport.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import pymysql +import pandas as pd +from datetime import datetime, timedelta +from openpyxl import load_workbook +from openpyxl.styles import Font, PatternFill, Alignment +from pathlib import Path +import os + +# ============================== +# โš™๏ธ CONFIG +# ============================== +DB_CONFIG = { + "host": "192.168.1.76", + "port": 3307, + "user": "root", + "password": "Vlado9674+", + "database": "medevio", + "charset": "utf8mb4", +} + +# Output location +timestamp = datetime.now().strftime("%Y-%m-%d %H-%M-%S") +OUTPUT_DIR = Path(r"U:\Dropbox\!!!Days\Downloads Z230") +OUTPUT_DIR.mkdir(parents=True, exist_ok=True) + +OUTPUT_FILE = OUTPUT_DIR / f"{timestamp} Medevio report.xlsx" + +# ============================== +# ๐Ÿงน Remove old reports +# ============================== +for file in OUTPUT_DIR.glob("* Medevio report.xlsx"): + try: + file.unlink() + print("Removed old report:", file) + except Exception as e: + print("Could not remove:", file, "Error:", e) + +# ============================== +# ๐Ÿ“ฅ FETCH DATA โ€” POZADAVKY +# ============================== +conn = pymysql.connect(**DB_CONFIG) + +two_months_ago = (datetime.now() - timedelta(days=1000)).strftime("%Y-%m-%d %H:%M:%S") + +sql_pozadavky = """ +SELECT + id, + pacient_prijmeni, + pacient_jmeno, + pacient_rodnecislo, + displayTitle, + createdAt, + updatedAt, + doneAt, + removedAt, + attachmentsProcessed, + messagesProcessed, + communicationprocessed, + questionnaireprocessed, + lastSync +FROM pozadavky +WHERE createdAt >= %s +ORDER BY updatedAt DESC +""" + +df_poz = pd.read_sql(sql_pozadavky, conn, params=(two_months_ago,)) + +# ============================== +# ๐Ÿ“ฅ FETCH DATA โ€” MESSAGES (WITH JOIN) +# ============================== +ids = tuple(df_poz["id"].tolist()) +if len(ids) == 1: + ids_sql = f"('{ids[0]}')" +else: + ids_sql = ids + +sql_messages = f""" +SELECT + p.pacient_jmeno, + p.pacient_prijmeni, + p.pacient_rodnecislo, + p.displayTitle AS pozadavek_title, + + m.id, + m.text, + m.sender_name, + m.created_at, + m.read_at, + m.updated_at, + m.attachment_url, + m.attachment_description, + m.attachment_content_type, + m.inserted_at + +FROM medevio_messages m +LEFT JOIN pozadavky p + ON m.request_id COLLATE utf8mb4_unicode_ci + = p.id COLLATE utf8mb4_unicode_ci +WHERE m.request_id IN {ids_sql} +ORDER BY m.created_at DESC +""" + + +df_msg = pd.read_sql(sql_messages, conn) +conn.close() + +# ============================== +# ๐Ÿ’พ SAVE BOTH SHEETS +# ============================== +with pd.ExcelWriter(OUTPUT_FILE, engine="openpyxl") as writer: + df_poz.to_excel(writer, sheet_name="pozadavky", index=False) + df_msg.to_excel(writer, sheet_name="messages", index=False) + +# ============================== +# ๐ŸŽจ FORMAT EXCEL +# ============================== +wb = load_workbook(OUTPUT_FILE) + +yellow = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") +header_font = Font(bold=True) + +def format_sheet(ws): + for cell in ws[1]: + cell.fill = yellow + cell.font = header_font + cell.alignment = Alignment(horizontal="center") + ws.auto_filter.ref = ws.dimensions + for column in ws.columns: + max_length = 0 + col_letter = column[0].column_letter + for cell in column: + if cell.value: + max_length = max(max_length, len(str(cell.value))) + ws.column_dimensions[col_letter].width = max_length + 2 + +format_sheet(wb["pozadavky"]) +format_sheet(wb["messages"]) + +wb.save(OUTPUT_FILE) + +print("Report saved:", OUTPUT_FILE)