Files
steve-reporting/declaratie/send_monthly_declaration.py
2025-10-31 09:52:53 +01:00

105 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import subprocess
import smtplib
import argparse
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
import calendar
os.environ['PYTHONIOENCODING'] = 'utf-8'
# Parse command line arguments
parser = argparse.ArgumentParser(description='Genereer en verstuur maandelijkse declaratie rapport')
parser.add_argument('--month', type=int, required=True, choices=range(1, 13),
help='Maand (1-12)')
parser.add_argument('--year', type=int, required=True,
help='Jaar (bijv. 2024)')
parser.add_argument('--user', help='Filter op specifieke RFID tag/gebruiker (optioneel)')
parser.add_argument('--email', default='mark@markkors.nl',
help='Email adres (default: mark@markkors.nl)')
parser.add_argument('--summary', action='store_true',
help='Stuur alleen samenvatting zonder transactie details')
args = parser.parse_args()
# Configuratie
TO_EMAIL = args.email
FROM_EMAIL = "laadpaal@raspberry.local"
SUBJECT = f"Declaratie Laadpaal - {calendar.month_name[args.month]} {args.year}"
# SMTP configuratie
SMTP_SERVER = "192.168.178.201"
SMTP_PORT = 25
# Genereer rapport commando
cmd = [
'python', 'steve_monthly_declaration.py',
'--host', '192.168.178.201',
'--port', '3307',
'--user', 'steve',
'--password', 'kS9R*xp17ZwCD@CV&E^N',
'--price-user', 'alfen_user',
'--price-password', '5uVgr%f%s2P5GR@3q!',
'--month', str(args.month),
'--year', str(args.year)
]
# Voeg optionele parameters toe
if args.user:
cmd.extend(['--user-filter', args.user])
SUBJECT += f" - Gebruiker: {args.user}"
if args.summary:
cmd.append('--summary')
try:
# Genereer rapport
print(f"📊 Genereer declaratie rapport voor {calendar.month_name[args.month]} {args.year}...")
report = subprocess.check_output(cmd, encoding='utf-8')
except subprocess.CalledProcessError as e:
print(f"✗ Fout bij genereren rapport: {e}")
exit(1)
# Email samenstellen
msg = MIMEMultipart('alternative')
msg['From'] = FROM_EMAIL
msg['To'] = TO_EMAIL
msg['Subject'] = SUBJECT
# Plain text versie
text_part = MIMEText(report, 'plain', 'utf-8')
msg.attach(text_part)
# HTML versie voor betere leesbaarheid in email clients
html_report = f"""
<html>
<head>
<style>
body {{ font-family: 'Courier New', monospace; font-size: 12px; }}
pre {{ white-space: pre-wrap; word-wrap: break-word; }}
.header {{ font-weight: bold; color: #2c3e50; }}
.total {{ font-weight: bold; background-color: #f8f9fa; padding: 5px; }}
</style>
</head>
<body>
<pre>{report.replace('DECLARATIE RAPPORT LAADTRANSACTIES', '<span class="header">DECLARATIE RAPPORT LAADTRANSACTIES</span>')
.replace('TOTAAL OVERZICHT', '<span class="header">TOTAAL OVERZICHT</span>')
.replace('TOTALE KOSTEN:', '<span class="total">TOTALE KOSTEN:</span>')}</pre>
</body>
</html>
"""
html_part = MIMEText(html_report, 'html', 'utf-8')
msg.attach(html_part)
# Verstuur email
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.send_message(msg)
print(f"✔ Declaratie rapport verzonden naar {TO_EMAIL}")
print(f" Periode: {calendar.month_name[args.month]} {args.year}")
if args.user:
print(f" Gebruiker: {args.user}")
except Exception as e:
print(f"✗ Fout bij verzenden: {e}")
exit(1)