reify script für sepa-mandate

This commit is contained in:
Fritz Grimpen 2024-09-07 17:43:35 +02:00
commit 8ef50fc67e
2 changed files with 101 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
output/
build/

99
sepa-mandate/reify.py Normal file
View file

@ -0,0 +1,99 @@
#!/bin/env python3
import csv
import subprocess
import email.message
import email.utils
import datetime
import argparse
import pathlib
import tempfile
import shutil
DEFAULT_CSV_FIELDLIST = ("creditor_name", "creditor_iban", "mandate", "recurrence", "date", "email")
def create_tex(mandate):
return f'''
\csdef{{sepa-m001}}{{{mandate['mandate']}}}
\csdef{{sepa-m006}}{{{mandate['recurrence']}}}
\csdef{{sepa-p001}}{{{mandate['creditor_name']}}}
\csdef{{sepa-d001}}{{{mandate['creditor_iban']}}}
\csdef{{sepa-m008}}{{{mandate['date']}}}
\csdef{{sepa-m009}}{{\emph{{entfällt}}}}
\csdef{{sepa-d002a}}{{}}
\csdef{{sepa-d002b}}{{}}
\csdef{{sepa-p005}}{{}}
\csdef{{sepa-remarks}}{{Dieses Mandat ist eine Abschrift des dem Zahlungsempfänger vorliegenden, originalen SEPA-Lastschriftmandats.\\\\
Erstellt am {datetime.date.today()}}}
'''
EMAIL_MESSAGE = """Liebes Mitglied,
anbei erhälst du eine Abschrift des SEPA-Lastschriftmandats mit der Mandatsreferenz "{mandate[mandate]}", das zum Einzug deiner Mitgliedsbeiträge bei uns gespeichert ist.
Sollten Angaben in diesem Mandat fehlerhaft oder veraltet sein, so teil uns dies bitte per E-Mail mit an vorstand@ccchb.de.
Viele Grüße,
Vorstand des Chaos Computer Club Bremen e.V.
"""
def create_email(mandate, pdfmandate):
msg = email.message.EmailMessage()
msg["To"] = f"{mandate['creditor_name']} <{mandate['email']}>"
msg["From"] = "Chaos Computer Club Bremen e.V. <vorstand@ccchb.de>"
msg["Subject"] = f"Dein SEPA-Lastschriftmandat (MREF: {mandate['mandate']})"
msg["Message-Id"] = email.utils.make_msgid(domain="verein.ccchb.de")
msg["Date"] = datetime.datetime.now()
msg.set_content(EMAIL_MESSAGE.format(mandate=mandate))
msg.add_attachment(pdfmandate, 'application', 'pdf', filename='Lastschriftmandat.pdf')
return msg
def main_reify_mandate(mandate, output_dir_path, tex_template_path, tempdir_path, debug):
tex_jobname = str(tempdir_path.joinpath(mandate['mandate']))
settings_file_path = tempdir_path.joinpath(f'{mandate['mandate']}-settings.tex')
with open(settings_file_path, "w") as texfile:
texfile.write(create_tex(mandate))
latexmk_args = ["latexmk", "-lualatex", str(tex_template_path), f"-jobname={tex_jobname}"]
if not debug:
latexmk_args.append("-silent")
subprocess.run(latexmk_args)
pdf_file_path = tempdir_path.joinpath(f'{mandate['mandate']}.pdf')
pdf_output_path = output_dir_path.joinpath(f'{mandate['mandate']}.pdf')
shutil.copy(pdf_file_path, pdf_output_path)
if mandate['email']:
mail_output_path = output_dir_path.joinpath(f'{mandate['mandate']}.eml')
with open(pdf_file_path, "rb") as pdffile:
msg = create_email(mandate, pdffile.read())
with open(mail_output_path, "wb") as mailfile:
mailfile.write(bytes(msg))
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument("--csv-skip", default=1)
argparser.add_argument("--csv-fields", type=lambda s: s.split(","), default=DEFAULT_CSV_FIELDLIST)
argparser.add_argument("-O", "--output-dir", default="output/")
argparser.add_argument("--build-dir", default="build")
argparser.add_argument("--debug", default=False, action="store_true")
argparser.add_argument("tex_template")
argparser.add_argument("mandate_file")
args = argparser.parse_args()
output_dir_path = pathlib.Path(args.output_dir)
# Ensure that the output directory exists
output_dir_path.mkdir(parents=True, exist_ok=True)
tex_template_path = pathlib.Path(args.tex_template)
build_dir_path = pathlib.Path(args.build_dir)
# Ensure that the build directory exists
build_dir_path.mkdir(parents=True, exist_ok=True)
with open(args.mandate_file) as csvfile:
reader = csv.DictReader(csvfile, fieldnames=args.csv_fields)
for mandate in reader:
if not mandate.get('mandate'):
continue
main_reify_mandate(mandate, output_dir_path, tex_template_path, build_dir_path, args.debug)
if __name__ == '__main__':
main()