From 5e40bc46089556aa55bb1c2976ec22c780cf7b8c Mon Sep 17 00:00:00 2001 From: "vladimir.buzalka" Date: Fri, 16 Jan 2026 16:41:06 +0100 Subject: [PATCH] z230 --- .../10 FinalSaveInsuranceStatusScript.py | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 20 Ověření proti Medicus/10 FinalSaveInsuranceStatusScript.py diff --git a/20 Ověření proti Medicus/10 FinalSaveInsuranceStatusScript.py b/20 Ověření proti Medicus/10 FinalSaveInsuranceStatusScript.py new file mode 100644 index 0000000..f9d3aac --- /dev/null +++ b/20 Ověření proti Medicus/10 FinalSaveInsuranceStatusScript.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +#this script can be run several times on the same day, in such case it works incrementaly + +import sys +from pathlib import Path + +# add project root (one level up) to PYTHONPATH +PROJECT_ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(PROJECT_ROOT)) + + +import time +import logging +from knihovny.medicus_db import MedicusDB +from vzpb2b_client import VZPB2BClient +import pymysql +from datetime import date + +# ========================================== +# LOGGING SETUP +# ========================================== +logging.basicConfig( + filename="insurance_check.log", + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(message)s", + encoding="utf-8" +) + +console = logging.getLogger("console") +console.setLevel(logging.INFO) +handler = logging.StreamHandler() +handler.setFormatter(logging.Formatter("%(message)s")) +console.addHandler(handler) + +def log_info(msg): + logging.info(msg) + console.info(msg) + +def log_error(msg): + logging.error(msg) + console.error(msg) + +# ========================================== +# MYSQL CONNECTION +# ========================================== +mysql = pymysql.connect( + host="192.168.1.76", + port=3307, + user="root", + password="Vlado9674+", + database="medevio", + charset="utf8mb4", + autocommit=True +) + +# ========================================== +# SAVE RESULT +# ========================================== +def save_insurance_status(mysql_conn, rc, k_datu, result, xml_text): + sql = """ + INSERT INTO vzp_stav_pojisteni + (rc, k_datu, stav, kod_pojistovny, nazev_pojistovny, + pojisteni_kod, stav_vyrizeni, response_xml) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s) + """ + with mysql_conn.cursor() as cur: + cur.execute(sql, ( + rc, + k_datu, + result["stav"], + result["kodPojistovny"], + result["nazevPojistovny"], + result["pojisteniKod"], + result["stavVyrizeni"], + xml_text + )) + +# ========================================== +# CONFIGURATION +# ========================================== +HOST = "192.168.1.4" +DB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB" + +PFX_PATH = r"MBcert.pfx" +PFX_PASSWORD = "Vlado7309208104++" + +ENV = "prod" +ICZ = "00000000" +DIC = "00000000" + +# ========================================== +# INIT CONNECTIONS +# ========================================== +db = MedicusDB(HOST, DB_PATH) +vzp = VZPB2BClient(ENV, PFX_PATH, PFX_PASSWORD, icz=ICZ, dic=DIC) + +# ========================================== +# FETCH REGISTERED PATIENTS +# ========================================== +patients = db.get_active_registered_patients() +log_info(f"Loaded {len(patients)} registered patients") + +today = date.today() + +# ========================================== +# FILTER: ONLY PATIENTS NOT CHECKED TODAY +# ========================================== +patients_to_check = [] + +with mysql.cursor() as cur: + for rc, prijmeni, jmeno, poj in patients: + cur.execute( + "SELECT MAX(k_datu) AS last_check FROM vzp_stav_pojisteni WHERE rc = %s", + (rc,) + ) + row = cur.fetchone() + last_check = row["last_check"] + + if last_check is None or last_check < today: + patients_to_check.append((rc, prijmeni, jmeno)) + +log_info(f"Incremental run: {len(patients_to_check)} patients to check today\n") + +# ========================================== +# MAIN LOOP (1 of N) +# ========================================== +total = len(patients_to_check) + +for idx, (rodcis, prijmeni, jmeno) in enumerate(patients_to_check, 1): + + log_info(f"[{idx}/{total}] Checking {prijmeni} {jmeno} ({rodcis})") + + try: + xml = vzp.stav_pojisteni(rc=rodcis, k_datu=today.isoformat()) + except Exception as e: + log_error(f"❌ VZP REQUEST FAILED for {rodcis}: {e}") + time.sleep(2) + continue + + if not xml.strip().startswith("<"): + log_error(f"❌ INVALID XML for RC {rodcis}") + time.sleep(2) + continue + + try: + result = vzp.parse_stav_pojisteni(xml) + except Exception as e: + log_error(f"❌ XML PARSE ERROR for RC {rodcis}: {e}") + time.sleep(2) + continue + + try: + save_insurance_status(mysql, rodcis, today, result, xml) + except Exception as e: + log_error(f"❌ MYSQL INSERT ERROR for RC {rodcis}: {e}") + time.sleep(2) + continue + + log_info(f" ✔ OK ({result['nazevPojistovny']})") + time.sleep(2) + +db.close() +mysql.close() +log_info("\nDONE – incremental insurance check finished.")