notebookVB
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#this script can be run several times on the same day, in such case it works incrementaly
|
||||
# this script can be run several times on the same day, in such case it works incrementally
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# add project root (one level up) to PYTHONPATH
|
||||
# ==========================================
|
||||
# PROJECT ROOT (import fix)
|
||||
# ==========================================
|
||||
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
|
||||
from knihovny.vzpb2b_client import VZPB2BClient
|
||||
import pymysql
|
||||
from datetime import date
|
||||
|
||||
@@ -57,43 +58,64 @@ mysql = pymysql.connect(
|
||||
# ==========================================
|
||||
# SAVE RESULT
|
||||
# ==========================================
|
||||
def save_insurance_status(mysql_conn, rc, k_datu, result, xml_text):
|
||||
def save_insurance_status(mysql_conn, rc, prijmeni, jmeno, k_datu, result, xml_text):
|
||||
"""
|
||||
Uloží čistou odpověď VZP + identifikační údaje pacienta.
|
||||
Pojišťovna je VÝHRADNĚ z odpovědi VZP.
|
||||
"""
|
||||
sql = """
|
||||
INSERT INTO vzp_stav_pojisteni
|
||||
(rc, k_datu, stav, kod_pojistovny, nazev_pojistovny,
|
||||
(rc, prijmeni, jmeno, k_datu,
|
||||
stav, kod_pojistovny, nazev_pojistovny,
|
||||
pojisteni_kod, stav_vyrizeni, response_xml)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
VALUES (%s, %s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s)
|
||||
"""
|
||||
|
||||
with mysql_conn.cursor() as cur:
|
||||
cur.execute(sql, (
|
||||
rc,
|
||||
prijmeni,
|
||||
jmeno,
|
||||
k_datu,
|
||||
result["stav"],
|
||||
result["kodPojistovny"],
|
||||
result["nazevPojistovny"],
|
||||
result["pojisteniKod"],
|
||||
result["stavVyrizeni"],
|
||||
result["kodPojistovny"], # ← VZP
|
||||
result["nazevPojistovny"], # ← VZP
|
||||
result["pojisteniKod"], # ← VZP
|
||||
result["stavVyrizeni"], # ← VZP
|
||||
xml_text
|
||||
))
|
||||
|
||||
|
||||
# ==========================================
|
||||
# CONFIGURATION
|
||||
# ==========================================
|
||||
HOST = "192.168.1.4"
|
||||
DB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB"
|
||||
|
||||
PFX_PATH = r"MBcert.pfx"
|
||||
PFX_PATH = PROJECT_ROOT / "certificates" / "MBcert.pfx"
|
||||
PFX_PASSWORD = "Vlado7309208104++"
|
||||
|
||||
ENV = "prod"
|
||||
ICZ = "00000000"
|
||||
DIC = "00000000"
|
||||
|
||||
# sanity check
|
||||
if not PFX_PATH.exists():
|
||||
raise FileNotFoundError(f"PFX certificate not found: {PFX_PATH}")
|
||||
|
||||
# ==========================================
|
||||
# INIT CONNECTIONS
|
||||
# ==========================================
|
||||
db = MedicusDB(HOST, DB_PATH)
|
||||
vzp = VZPB2BClient(ENV, PFX_PATH, PFX_PASSWORD, icz=ICZ, dic=DIC)
|
||||
vzp = VZPB2BClient(
|
||||
ENV,
|
||||
str(PFX_PATH), # <-- important: pass as string
|
||||
PFX_PASSWORD,
|
||||
icz=ICZ,
|
||||
dic=DIC
|
||||
)
|
||||
|
||||
# ==========================================
|
||||
# FETCH REGISTERED PATIENTS
|
||||
@@ -108,7 +130,7 @@ today = date.today()
|
||||
# ==========================================
|
||||
patients_to_check = []
|
||||
|
||||
with mysql.cursor() as cur:
|
||||
with mysql.cursor(pymysql.cursors.DictCursor) 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",
|
||||
@@ -151,7 +173,15 @@ for idx, (rodcis, prijmeni, jmeno) in enumerate(patients_to_check, 1):
|
||||
continue
|
||||
|
||||
try:
|
||||
save_insurance_status(mysql, rodcis, today, result, xml)
|
||||
save_insurance_status(
|
||||
mysql,
|
||||
rodcis,
|
||||
prijmeni,
|
||||
jmeno,
|
||||
today,
|
||||
result,
|
||||
xml
|
||||
)
|
||||
except Exception as e:
|
||||
log_error(f"❌ MYSQL INSERT ERROR for RC {rodcis}: {e}")
|
||||
time.sleep(2)
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from datetime import datetime, date, timedelta
|
||||
import os
|
||||
import pymysql
|
||||
|
||||
from knihovny.medicus_db import MedicusDB
|
||||
from knihovny.vzpb2b_client import VZPB2BClient
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from weasyprint import HTML
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
# ==========================================
|
||||
# PROJECT ROOT (import + paths)
|
||||
# ==========================================
|
||||
script_location = Path(__file__).resolve().parent
|
||||
project_root = script_location.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
# ==========================================
|
||||
# CONFIGURATION
|
||||
@@ -22,16 +32,23 @@ MYSQL_CONFIG = {
|
||||
"autocommit": True
|
||||
}
|
||||
|
||||
PFX_PATH = project_root / "certificates" / "MBcert.pfx"
|
||||
PFX_PASSWORD = "Vlado7309208104++"
|
||||
|
||||
ENV = "prod"
|
||||
ICZ = "00000000"
|
||||
DIC = "00000000"
|
||||
|
||||
if not PFX_PATH.exists():
|
||||
raise FileNotFoundError(f"PFX certificate not found: {PFX_PATH}")
|
||||
|
||||
# ==========================================
|
||||
# PATHS
|
||||
# PATHS (templates, output)
|
||||
# ==========================================
|
||||
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()
|
||||
@@ -48,20 +65,61 @@ mysql = pymysql.connect(
|
||||
**MYSQL_CONFIG
|
||||
)
|
||||
|
||||
print("Initializing VZP B2B client...")
|
||||
vzp = VZPB2BClient(
|
||||
ENV,
|
||||
str(PFX_PATH),
|
||||
PFX_PASSWORD,
|
||||
icz=ICZ,
|
||||
dic=DIC
|
||||
)
|
||||
|
||||
# ==========================================
|
||||
# BINÁRNÍ HLEDÁNÍ DATA ZLOMU
|
||||
# ==========================================
|
||||
def find_insurance_break_date(vzp_client, rc, start_date, end_date):
|
||||
try:
|
||||
low = start_date
|
||||
high = end_date
|
||||
|
||||
stav_low = vzp_client.parse_stav_pojisteni(
|
||||
vzp_client.stav_pojisteni(rc=rc, k_datu=low.isoformat())
|
||||
)["stav"]
|
||||
|
||||
stav_high = vzp_client.parse_stav_pojisteni(
|
||||
vzp_client.stav_pojisteni(rc=rc, k_datu=high.isoformat())
|
||||
)["stav"]
|
||||
|
||||
if stav_low != "1" or stav_high == "1":
|
||||
return None, None
|
||||
|
||||
while (high - low).days > 1:
|
||||
mid = low + timedelta(days=(high - low).days // 2)
|
||||
|
||||
xml = vzp_client.stav_pojisteni(rc=rc, k_datu=mid.isoformat())
|
||||
stav_mid = vzp_client.parse_stav_pojisteni(xml)["stav"]
|
||||
|
||||
if stav_mid == "1":
|
||||
low = mid
|
||||
else:
|
||||
high = mid
|
||||
|
||||
return low, high
|
||||
|
||||
except Exception:
|
||||
return None, None
|
||||
|
||||
# ==========================================
|
||||
# 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}
|
||||
print(f"Loaded {len(patients_by_rc)} registered patients")
|
||||
|
||||
# ==========================================
|
||||
# FETCH LAST INSURANCE STATES
|
||||
# ==========================================
|
||||
print("Fetching last insurance states from MySQL...")
|
||||
|
||||
sql_last_states = """
|
||||
SELECT rc, stav
|
||||
FROM (
|
||||
@@ -76,12 +134,11 @@ with mysql.cursor() as cur:
|
||||
cur.execute(sql_last_states)
|
||||
last_states = cur.fetchall()
|
||||
|
||||
print(f"Loaded {len(last_states)} last insurance states")
|
||||
|
||||
# ==========================================
|
||||
# COMPARE
|
||||
# COMPARE + BREAK DATE
|
||||
# ==========================================
|
||||
suspected = []
|
||||
today = date.today()
|
||||
|
||||
for row in last_states:
|
||||
rc = row["rc"]
|
||||
@@ -89,12 +146,30 @@ for row in last_states:
|
||||
|
||||
if rc in patients_by_rc and stav != "1":
|
||||
p = patients_by_rc[rc]
|
||||
|
||||
with mysql.cursor() as c2:
|
||||
c2.execute("""
|
||||
SELECT MAX(k_datu) AS last_insured
|
||||
FROM vzp_stav_pojisteni
|
||||
WHERE rc = %s AND stav = '1'
|
||||
""", (rc,))
|
||||
r2 = c2.fetchone()
|
||||
last_known_insured = r2["last_insured"]
|
||||
|
||||
insured_to = uninsured_from = None
|
||||
if last_known_insured:
|
||||
insured_to, uninsured_from = find_insurance_break_date(
|
||||
vzp, rc, last_known_insured, today
|
||||
)
|
||||
|
||||
suspected.append({
|
||||
"rc": rc,
|
||||
"prijmeni": p["prijmeni"],
|
||||
"jmeno": p["jmeno"],
|
||||
"poj": p["poj"],
|
||||
"stav": stav
|
||||
"stav": stav,
|
||||
"insured_to": insured_to,
|
||||
"uninsured_from": uninsured_from
|
||||
})
|
||||
|
||||
print(f"Nalezeno {len(suspected)} problémových záznamů")
|
||||
@@ -106,13 +181,9 @@ medicus.close()
|
||||
mysql.close()
|
||||
|
||||
# ==========================================
|
||||
# PDF GENERATION (WEASYPRINT)
|
||||
# PDF GENERATION
|
||||
# ==========================================
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(str(template_dir)),
|
||||
autoescape=True
|
||||
)
|
||||
|
||||
env = Environment(loader=FileSystemLoader(str(template_dir)), autoescape=True)
|
||||
template = env.get_template("vzp_console_report.html")
|
||||
|
||||
html_content = template.render(
|
||||
|
||||
Reference in New Issue
Block a user