summaryrefslogtreecommitdiffstats
path: root/invoice/generate-invoice.py
diff options
context:
space:
mode:
Diffstat (limited to 'invoice/generate-invoice.py')
-rwxr-xr-xinvoice/generate-invoice.py112
1 files changed, 86 insertions, 26 deletions
diff --git a/invoice/generate-invoice.py b/invoice/generate-invoice.py
index 6688d83..e414b5a 100755
--- a/invoice/generate-invoice.py
+++ b/invoice/generate-invoice.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
-import datetime, sqlite3, os, sys, smtplib, subprocess, time, tempfile
+import datetime, sqlite3, os, sys, smtplib, subprocess, time, tempfile, email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
@@ -73,7 +73,7 @@ def get_invoice_data(user, start=0, stop=0):
return result
-def generate_invoice_tex(user, title, subject, start=0, stop=0):
+def generate_invoice_tex(user, title, subject, start=0, stop=0, temporary=False):
userinfo = get_user_info(user)
result = "\\documentclass[ktt-template,12pt,pagesize=auto,enlargefirstpage=on,paper=a4]{scrlttr2}\n\n"
@@ -98,10 +98,13 @@ def generate_invoice_tex(user, title, subject, start=0, stop=0):
result+= "\t\twir erlauben uns, Ihnen für den Verzehr von Speisen und Getränken wie folgt zu berechnen:\n\n"
result += "\t\t\\begin{footnotesize}\n"
- result += "\t\t\t\\begin{longtable}{|l|l|l|l|}\n"
+ result += "\t\t\t\\begin{longtable}{|p{2cm}|p{1.8cm}|p{5cm}|p{2cm}|}\n"
result += "\t\t\t\t\\hline\n"
- result += "\t\t\t\tDatum & Uhrzeit & Artikel & Preis\\\\\n"
+ result += "\t\t\t\t\\textbf{Datum} & \\textbf{Uhrzeit} & \\textbf{Artikel} & \\textbf{Preis}\\\\\n"
result += "\t\t\t\t\\hline\n"
+ result += "\\endhead\n"
+ result += "\t\t\t\t\\hline\n"
+ result += "\\endfoot\n"
lastdate = ""
total = 0
@@ -109,20 +112,27 @@ def generate_invoice_tex(user, title, subject, start=0, stop=0):
total += row["price"]
if lastdate != row["date"]:
- result += "\t\t\t\t%s\t& %s\t& %s\t& %d,%02d Euro\\\\\n" % (row["date"], row["time"], row["product"], row["price"] / 100, row["price"] % 100)
+ result += "\t\t\t\t%s\t& %s\t& %s\t& \\EUR{%d,%02d}\\\\\n" % (row["date"], row["time"], row["product"], row["price"] / 100, row["price"] % 100)
lastdate = row["date"]
else:
- result += "\t\t\t\t%s\t& %s\t& %s\t& %d,%02d Euro\\\\\n" % (" ", row["time"], row["product"], row["price"] / 100, row["price"] % 100)
+ result += "\t\t\t\t%s\t& %s\t& %s\t& \\EUR{%d,%02d}\\\\\n" % (" ", row["time"], row["product"], row["price"] / 100, row["price"] % 100)
result += "\t\t\t\t\\hline\n"
- result += "\t\t\t\t\\multicolumn{3}{|l|}{Summe:} & %d,%02d Euro\\\\\n" % (total / 100, total % 100)
- result += "\t\t\t\t\\hline\n"
+ result += "\t\t\t\t\\multicolumn{3}{|l|}{Summe:} & \\EUR{%d,%02d}\\\\\n" % (total / 100, total % 100)
result += "\t\t\t\\end{longtable}\n"
result += "\t\t\\end{footnotesize}\n\n"
result += "\t\tUmsatzsteuer wird nicht erhoben, da Kreativität trifft Technik e.V. als Kleinunternehmen\n"
result += "\t\tder Regelung des § 19 Abs. 1 UStG unterfällt.\n\n"
+ if temporary is True:
+ result += "\t\tBei dieser Abrechnung handelt es sich lediglich um einen Zwischenstand. Die\n"
+ result += "\t\tHauptrechnung wird einmal monatlich getrennt zugestellt und der Gesamtbetrag\n"
+ result += "\t\twird dann vom angegebenen Bankkonto eingezogen.\n\n"
+ else:
+ result += "\t\tDer Gesamtbetrag wird in den nächsten Tagen von dem angegebenen Bankkonto\n"
+ result += "\t\teingezogen.\n\n"
+
result += "\t\t\\closing{Mit freundlichen Grüßen}\n\n"
result += "\t\\end{letter}\n"
@@ -130,7 +140,7 @@ def generate_invoice_tex(user, title, subject, start=0, stop=0):
return result
-def generate_invoice_text(user, title, subject, start=0, stop=0):
+def generate_invoice_text(user, title, subject, start=0, stop=0, temporary=False):
userinfo = get_user_info(user)
result = ""
@@ -168,11 +178,20 @@ def generate_invoice_text(user, title, subject, start=0, stop=0):
result += "Umsatzsteuer wird nicht erhoben, da Kreativität trifft Technik e.V. als Kleinunternehmen\n"
result += "der Regelung des § 19 Abs. 1 UStG unterfällt.\n\n"
+ if temporary is True:
+ result += "Bei dieser Abrechnung handelt es sich lediglich um einen Zwischenstand. Die\n"
+ result += "Hauptrechnung wird einmal monatlich getrennt zugestellt und der Gesamtbetrag\n"
+ result += "wird dann vom angegebenen Bankkonto eingezogen.\n\n"
+ else:
+ result += "Der Gesamtbetrag wird in den nächsten Tagen von dem angegebenen Bankkonto\n"
+ result += "eingezogen.\n\n"
+
return result
-def generate_mail(receiver, subject, message, pdfdata, cc = None):
+def generate_mail(receiver, subject, message, pdfdata, timestamp=time.time(), cc = None):
msg = MIMEMultipart()
- msg["From"] = "KtT Shop System <shop@kreativitaet-trifft-technik.de>"
+ msg["From"] = "KtT-Shopsystem <shop@kreativitaet-trifft-technik.de>"
+ msg["Date"] = email.utils.formatdate(timestamp, True)
try:
if receiver.encode("ascii"):
@@ -207,7 +226,7 @@ def send_mail(mail, receiver):
server.sendmail(mail["From"], receiver, maildata)
server.quit()
-def get_users_with_purches(start, stop):
+def get_users_with_purchases(start, stop):
result = []
connection = sqlite3.connect('shop.db')
@@ -234,27 +253,70 @@ def daily(timestamp = time.time()):
stop = int(dstop.strftime("%s"))
start = int(dstart.strftime("%s"))
- title = "Getränke Rechnung %04d-%02d-%02d" % (dstart.year, dstart.month, dstart.day)
- subject = "Getränke Zwischenstand %02d.%02d.%04d %02d:%02d Uhr bis %02d.%02d.%04d %02d:%02d Uhr" % (dstart.day, dstart.month, dstart.year, dstart.hour, dstart.minute, dstop.day, dstop.month, dstop.year, dstop.hour, dstop.minute)
+ title = "Getränkerechnung %04d-%02d-%02d" % (dstart.year, dstart.month, dstart.day)
+ subject = "Getränke-Zwischenstand %02d.%02d.%04d %02d:%02d Uhr bis %02d.%02d.%04d %02d:%02d Uhr" % (dstart.day, dstart.month, dstart.year, dstart.hour, dstart.minute, dstop.day, dstop.month, dstop.year, dstop.hour, dstop.minute)
- for user in get_users_with_purches(start, stop):
+ for user in get_users_with_purchases(start, stop):
userinfo = get_user_info(user)
if userinfo is not None:
receiver = "%s %s <%s>" % (userinfo["firstname"], userinfo["lastname"], userinfo["email"])
- tex = generate_invoice_tex(user, title, subject, start, stop)
- msg = generate_invoice_text(user, title, subject, start, stop)
+ tex = generate_invoice_tex(user, title, subject, start, stop, True)
+ msg = generate_invoice_text(user, title, subject, start, stop, True)
pdf = generate_pdf(tex)
- mail = generate_mail(receiver, title, msg, pdf)
+ mail = generate_mail(receiver, title, msg, pdf, timestamp)
send_mail(mail, userinfo["email"])
print("Sent invoice to", userinfo["firstname"], userinfo["lastname"])
else:
print("Can't send invoice for missing user with the following id:", user)
def monthly(timestamp = time.time()):
- print("monthly invoice()")
+ requested = datetime.datetime.fromtimestamp(timestamp)
+ # timestamps for previous month
+ dstop = requested.replace(hour = 8, minute = 0, second = 0, day = 16) - datetime.timedelta(seconds = 1)
+ if dstop > requested:
+ dstop = dstop.replace(month = dstop.month -1)
+ dstart = dstop.replace(month = dstop.month -1)
+ stop = int(dstop.strftime("%s"))
+ start = int(dstart.strftime("%s"))
+
+ title = "Getränkerechnung %04d/%02d" % (dstart.year, dstart.month)
+ number = 0
+
+ for user in get_users_with_purchases(start, stop):
+ number += 1
+ subject = "Rechnung Nr.%04d%02d5%03d" % (dstart.year, dstart.month, number)
+ userinfo = get_user_info(user)
+ if userinfo is not None:
+ receiver = "%s %s <%s>" % (userinfo["firstname"], userinfo["lastname"], userinfo["email"])
+ tex = generate_invoice_tex(user, title, subject, start, stop, False)
+ msg = generate_invoice_text(user, title, subject, start, stop, False)
+ pdf = generate_pdf(tex)
+ mail = generate_mail(receiver, title, msg, pdf, timestamp, cc = "schatzmeister@kreativitaet-trifft-technik.de")
+ send_mail(mail, [userinfo["email"], "schatzmeister@kreativitaet-trifft-oldenburg.de"])
+ print("Sent invoice to", userinfo["firstname"], userinfo["lastname"])
+ else:
+ print("Can't send invoice for missing user with the following id:", user)
+
+def backup():
+ timestamp = time.time()
+ dt = datetime.datetime.fromtimestamp(timestamp)
-def backup(timestamp = time.time()):
- print("backup()")
+ msg = MIMEMultipart()
+ msg["From"] = "KtT-Shopsystem <shop@kreativitaet-trifft-technik.de>"
+ msg["Date"] = email.utils.formatdate(timestamp, True)
+ msg["To"] = "KtT-Shopsystem Backups <shop-backup@kreativitaet-trifft-technik.de>"
+ msg["Subject"] = "Backup KtT-Shopsystem %04d-%02d-%02d %02d:%02d" % (dt.year, dt.month, dt.day, dt.hour, dt.minute)
+ msg.preamble = "Please use a MIME aware email client!"
+
+ msg.attach(MIMEText("You can find a backup of 'shop.db' attached to this mail.", 'plain', 'utf-8'))
+
+ dbfile = open('shop.db', 'rb')
+ attachment = MIMEApplication(dbfile.read())
+ attachment.add_header('Content-Disposition', 'attachment', filename = 'shop.db')
+ msg.attach(attachment)
+ dbfile.close()
+
+ send_mail(msg, "shop-backup@kreativitaet-trifft-technik.de")
def get_stock_data():
connection = sqlite3.connect('shop.db')
@@ -292,7 +354,7 @@ def gen_stock_asciitable():
def gen_stock_mail():
msg = MIMEMultipart()
- msg["From"] = "KtT Shop System <shop@kreativitaet-trifft-technik.de>"
+ msg["From"] = "KtT-Shopsystem <shop@kreativitaet-trifft-technik.de>"
msg["To"] = "KtT Einkaufsteam <einkauf@kreativitaet-trifft-technik.de>"
msg["Subject"] = Header("Aktueller Warenbestand", 'utf-8')
msg.preamble = "Please use a MIME aware email client!"
@@ -302,14 +364,12 @@ def gen_stock_mail():
def weekly():
send_mail(gen_stock_mail(), "einkauf@kreativitaet-trifft-technik.de")
-def backup():
- pass # TODO
-
if sys.argv[1] == "daily":
daily()
+ backup()
elif sys.argv[1] == "weekly":
weekly()
elif sys.argv[1] == "monthly":
- print("TODO: not yet implemented")
+ monthly()
else:
print("not supported!")