118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Export Fio transactions (from MySQL → Excel)
|
|
--------------------------------------------
|
|
- Reads only cislo_uctu = '2800046620'
|
|
- For OZP (protiucet=2070101041) includes only positive objem
|
|
- Each sheet = insurance company (filtered by protiucet)
|
|
- First sheet = summary with total amounts and transaction counts
|
|
"""
|
|
|
|
import pandas as pd
|
|
import pymysql
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# ======== CONFIG ========
|
|
MYSQL_CONFIG = {
|
|
"host": "192.168.1.76",
|
|
"port": 3307,
|
|
"user": "root",
|
|
"password": "Vlado9674+",
|
|
"database": "fio",
|
|
"charset": "utf8mb4",
|
|
}
|
|
|
|
REPORTOVAT = {
|
|
"VZP": "1114007221",
|
|
"VOZP": "2010009091",
|
|
"ČPZP": "2054108761",
|
|
"OZP": "2070101041",
|
|
"ZPŠ": "2090309181",
|
|
"ZPMV": "2112108031",
|
|
}
|
|
|
|
EXPORT_PATH = Path(r"u:\Dropbox\!!!Days\Downloads Z230") / f"Fio_report_{datetime.now():%Y-%m-%d_%H-%M-%S}.xlsx"
|
|
|
|
|
|
# ======== LOAD DATA ========
|
|
def load_data():
|
|
print("🔄 Načítám data z MySQL (účet 2800046620, pro OZP jen kladné objemy)...")
|
|
conn = pymysql.connect(**MYSQL_CONFIG)
|
|
|
|
sql = """
|
|
SELECT *
|
|
FROM transactions
|
|
WHERE cislo_uctu = '2800046620'
|
|
AND (
|
|
protiucet <> '2070101041'
|
|
OR (protiucet = '2070101041' AND objem > 0)
|
|
);
|
|
"""
|
|
df = pd.read_sql(sql, conn)
|
|
conn.close()
|
|
|
|
df.columns = df.columns.str.strip()
|
|
print(f"✅ Načteno {len(df)} řádků, {len(df.columns)} sloupců.")
|
|
return df
|
|
|
|
|
|
# ======== EXPORT TO EXCEL ========
|
|
def export_to_excel(df):
|
|
summary_rows = [] # to collect summary per insurer
|
|
|
|
with pd.ExcelWriter(EXPORT_PATH, engine="openpyxl") as writer:
|
|
# --- INDIVIDUAL SHEETS ---
|
|
for name, acc in REPORTOVAT.items():
|
|
filtered = df[df["protiucet"].astype(str) == acc]
|
|
if filtered.empty:
|
|
print(f"⚠️ {name}: žádné transakce (účet {acc})")
|
|
summary_rows.append({
|
|
"Pojišťovna": name,
|
|
"Číslo účtu": acc,
|
|
"Počet transakcí": 0,
|
|
"Součet objemu": 0.0
|
|
})
|
|
continue
|
|
|
|
# safe numeric conversion
|
|
filtered = filtered.copy()
|
|
filtered["objem_num"] = (
|
|
filtered["objem"]
|
|
.astype(str)
|
|
.str.replace("\u00A0", "", regex=False)
|
|
.str.replace(",", ".", regex=False)
|
|
.astype(float)
|
|
)
|
|
|
|
# --- summary data ---
|
|
total_sum = filtered["objem_num"].sum()
|
|
total_count = len(filtered)
|
|
|
|
summary_rows.append({
|
|
"Pojišťovna": name,
|
|
"Číslo účtu": acc,
|
|
"Počet transakcí": total_count,
|
|
"Součet objemu": round(total_sum, 2)
|
|
})
|
|
|
|
# --- write detailed sheet ---
|
|
filtered.to_excel(writer, index=False, sheet_name=name)
|
|
print(f"✅ {name}: {len(filtered)} řádků exportováno, součet {total_sum:,.2f} Kč")
|
|
|
|
# --- SUMMARY SHEET ---
|
|
summary_df = pd.DataFrame(summary_rows)
|
|
summary_df["Součet objemu"] = summary_df["Součet objemu"].map("{:,.2f} Kč".format)
|
|
summary_df.to_excel(writer, index=False, sheet_name="Přehled")
|
|
print("🧾 Přidán přehledový list s celkovými součty.")
|
|
|
|
print(f"\n📊 Hotovo! Soubor uložen jako:\n{EXPORT_PATH}")
|
|
|
|
|
|
# ======== MAIN ========
|
|
if __name__ == "__main__":
|
|
df = load_data()
|
|
export_to_excel(df)
|