notebookVB
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import pymysql
|
||||
from knihovny.medicus_db import MedicusDB
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from weasyprint import HTML
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
# ==========================================
|
||||
# CONFIGURATION
|
||||
@@ -17,6 +22,20 @@ MYSQL_CONFIG = {
|
||||
"autocommit": True
|
||||
}
|
||||
|
||||
# ==========================================
|
||||
# PATHS
|
||||
# ==========================================
|
||||
script_location = Path(__file__).resolve().parent
|
||||
project_root = script_location.parent
|
||||
template_dir = project_root / "Templates"
|
||||
output_dir = script_location / "Output"
|
||||
output_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Font paths
|
||||
font_regular = (template_dir / "fonts" / "DejaVuSans.ttf").as_uri()
|
||||
font_bold = (template_dir / "fonts" / "DejaVuSans-Bold.ttf").as_uri()
|
||||
font_italic = (template_dir / "fonts" / "DejaVuSans-Oblique.ttf").as_uri()
|
||||
|
||||
# ==========================================
|
||||
# INIT CONNECTIONS
|
||||
# ==========================================
|
||||
@@ -30,38 +49,27 @@ mysql = pymysql.connect(
|
||||
)
|
||||
|
||||
# ==========================================
|
||||
# FETCH REGISTERED PATIENTS (MEDICUS)
|
||||
# FETCH REGISTERED PATIENTS
|
||||
# ==========================================
|
||||
print("Fetching registered patients from Medicus...")
|
||||
patients = medicus.get_active_registered_patients(as_dict=True)
|
||||
|
||||
patients_by_rc = {
|
||||
p["rodcis"]: p
|
||||
for p in patients
|
||||
}
|
||||
|
||||
patients_by_rc = {p["rodcis"]: p for p in patients}
|
||||
print(f"Loaded {len(patients_by_rc)} registered patients")
|
||||
|
||||
# ==========================================
|
||||
# FETCH LAST INSURANCE STATES (MYSQL)
|
||||
# FETCH LAST INSURANCE STATES
|
||||
# ==========================================
|
||||
print("Fetching last insurance states from MySQL...")
|
||||
|
||||
sql_last_states = """
|
||||
SELECT
|
||||
rc,
|
||||
stav
|
||||
SELECT rc, stav
|
||||
FROM (
|
||||
SELECT
|
||||
rc,
|
||||
stav,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY rc
|
||||
ORDER BY k_datu DESC
|
||||
) AS rn
|
||||
SELECT rc, stav,
|
||||
ROW_NUMBER() OVER (PARTITION BY rc ORDER BY k_datu DESC) AS rn
|
||||
FROM vzp_stav_pojisteni
|
||||
) t
|
||||
WHERE t.rn = 1
|
||||
WHERE rn = 1
|
||||
"""
|
||||
|
||||
with mysql.cursor() as cur:
|
||||
@@ -71,7 +79,7 @@ with mysql.cursor() as cur:
|
||||
print(f"Loaded {len(last_states)} last insurance states")
|
||||
|
||||
# ==========================================
|
||||
# COMPARE & FIND NON-OK STATES
|
||||
# COMPARE
|
||||
# ==========================================
|
||||
suspected = []
|
||||
|
||||
@@ -89,26 +97,42 @@ for row in last_states:
|
||||
"stav": stav
|
||||
})
|
||||
|
||||
# ==========================================
|
||||
# OUTPUT
|
||||
# ==========================================
|
||||
print("\n==========================================")
|
||||
print("PACIENTI S NEOK STAVEM POJIŠTĚNÍ")
|
||||
print("==========================================")
|
||||
|
||||
if not suspected:
|
||||
print("✔️ Všichni registrovaní pacienti mají stav 1")
|
||||
else:
|
||||
for s in suspected:
|
||||
print(
|
||||
f'{s["rc"]} | {s["prijmeni"]} {s["jmeno"]} '
|
||||
f'| pojišťovna={s["poj"]} | stav={s["stav"]}'
|
||||
)
|
||||
|
||||
print("\nCelkem nalezeno:", len(suspected))
|
||||
print(f"Nalezeno {len(suspected)} problémových záznamů")
|
||||
|
||||
# ==========================================
|
||||
# CLEANUP
|
||||
# CLEANUP DB
|
||||
# ==========================================
|
||||
medicus.close()
|
||||
mysql.close()
|
||||
|
||||
# ==========================================
|
||||
# PDF GENERATION (WEASYPRINT)
|
||||
# ==========================================
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(str(template_dir)),
|
||||
autoescape=True
|
||||
)
|
||||
|
||||
template = env.get_template("vzp_console_report.html")
|
||||
|
||||
html_content = template.render(
|
||||
patients=suspected,
|
||||
generated_at=datetime.now().strftime("%d. %m. %Y %H:%M"),
|
||||
font_regular=font_regular,
|
||||
font_bold=font_bold,
|
||||
font_italic=font_italic
|
||||
)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
pdf_file = output_dir / f"kontrola_pojisteni_{timestamp}.pdf"
|
||||
|
||||
print("Generuji PDF přes WeasyPrint...")
|
||||
HTML(string=html_content, base_url=str(template_dir)).write_pdf(pdf_file)
|
||||
|
||||
print("\n✅ PDF REPORT VYTVOŘEN:")
|
||||
print(pdf_file.resolve())
|
||||
|
||||
try:
|
||||
os.startfile(pdf_file)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user