ctdo-trac/TracBooking/tracbooking/report.py

180 lines
6.0 KiB
Python
Raw Permalink Normal View History

2012-03-31 15:55:42 +00:00
# -*- coding: utf-8 -*-
import os
from datetime import datetime
from trac.util.datefmt import utc
from tracbooking.model import Attendee, Event, EventAccount
from tracbooking.utils import get_option_count
def get_file():
import codecs
return codecs.open(u"/tmp/printout.txt", "wr", "utf-8")
def add_doc(global_event, selected_tz, f):
#f.write(u".. header::\n")
#f.write(u" ###Page###\n")
#f.write(u" ###Title###\n")
2012-04-24 10:25:53 +00:00
dt = utc.localize(datetime.utcnow()).strftime("%d.%m.%Y %H:%M")
2012-03-31 15:55:42 +00:00
d = u"Erstellt am: %s\n\n" % dt
f.write(d)
f.write(u"%s\n" % global_event.name)
f.write(u"%s\n\n" % ("=" * len(global_event.name)))
def create_table(items, f):
l = list()
l.append((u"Artikel", u"Einzelpreis", u"Anzahl", u"Gesamtpreis"))
for i in items:
l.append((u"%s" % i[1], u"%.2f" % float(i[2]), u"%s" % i[3], u"%.2f" % float(i[4])))
widths = list()
for cell in xrange(4):
tmp = list()
for row in l:
tmp.append(len(row[cell]))
widths.append(max(tmp))
head = list()
for ix, j in enumerate(l[0]):
head.append(u" %s " % j.ljust(widths[ix]))
trenner = [u"=%s=" % (u"=" * i)
for i in widths]
trenner = u" ".join(trenner) + "\n"
f.write(trenner)
f.write(u" ".join(head) + "\n")
f.write(trenner)
for i in l[1:]:
t = list()
for ix, j in enumerate(i):
t.append(u" " + j.ljust(widths[ix]) + " ")
f.write(u" ".join(t) + u"\n")
indent = sum(widths[:3]) + 10
f.write(u"%s %s\n" % (
" Gesamt ".center(indent),
str("%.2f" % float(sum([item[4] for item in items]))).ljust(widths[3])))
f.write(u"%s %s\n\n" % (u"=" * indent, u"=" * (widths[3] + 2)))
f.write(u"\n")
def add_summary(env, cursor, global_event, selected_tz, f):
query_old = "SELECT booking_option.ao_id,booking_available_option.name,booking_available_option.price," \
"SUM(booking_option.count),booking_available_option.price * SUM(booking_option.count)" \
"FROM booking_available_option,booking_option,option_to_event " \
"WHERE booking_available_option.ao_id = booking_option.ao_id AND " \
"booking_available_option.ao_id = option_to_event.ao_id AND " \
"option_to_event.e_id=%s GROUP BY booking_option.ao_id;"
query = "select " \
"booking_option.ao_id, " \
"booking_available_option.name, " \
"booking_available_option.price, " \
"SUM(booking_option.count), " \
"SUM(booking_option.count) * booking_available_option.price " \
"from " \
"booking_option,booking_available_option " \
"where " \
"booking_option.a_id IN (select a_id from attendee where e_id=%s) AND " \
"booking_option.ao_id = booking_available_option.ao_id " \
"group by " \
"booking_option.ao_id;"
cursor.execute(query, (global_event.e_id,))
items = cursor.fetchall()
create_table(items, f)
def add_payment_info(attendee, account, f):
txt = "Unser Konto"
f.write(u"%s\n%s\n\n" % (txt, u"-" * len(txt)))
f.write(u" :Kontoinhaber: %s\n" % account.account_owner)
f.write(u" :Kontonummer: %s\n" % account.account_no)
f.write(u" :Blz: %s\n" % account.bank_no)
f.write(u" :Bank: %s\n" % account.bank_name)
f.write(u" :Betrag: %s\n" % attendee.calculate_fee())
f.write(u" :1. Überweisungszweck: %s\n" % account.first_reason)
f.write(u" :2. Überweisungszweck: %s\n" % ("%X" % attendee.a_id).rjust(4, "0"))
f.write(u"\n")
def add_internal_attendee_data(attendee, f):
actual_amount = attendee.actual_amount and attendee.actual_amount or 0.0
f.write(u" :ID: %s\n" % ("%X" % attendee.a_id).rjust(4, "0"))
f.write(u" :Email: %s\n" % attendee.email)
f.write(u" :Soll-Betrag: %s\n" % attendee.calculate_fee())
f.write(u" :Ist-Betrag: %f\n" % actual_amount)
f.write(u" :Bezahlt: %s\n" % (u"Ja" if bool(attendee.has_paid) else u"Nein"))
f.write(u"\n")
def add_attendee(global_event, attendee, f, internal=False):
global_event.add_options(attendee.a_id)
f.write(u"%s\n" % attendee.nick.replace("_", "\_"))
f.write(u"%s\n" % ("-" * len(attendee.nick)))
if internal:
add_internal_attendee_data(attendee, f)
items = []
for option in global_event.options:
get_option_count(attendee, option)
if not option.count:
continue
items.append([option.ao_id, option.name, option.price, option.count, option.price * option.count])
if items:
create_table(items, f)
def add_attendees(env, cursor, global_event, selected_tz, f, internal=False):
attendees = Attendee.fetch_all(env, e_id=global_event.e_id, fetch_options=True)
attendees = sorted(attendees, key=lambda x: x.nick)
for attendee in attendees:
add_attendee(global_event, attendee, f, internal)
def generate_pdf(f):
import rst2pdf.createpdf
f.close()
rst2pdf.createpdf.main(["/tmp/printout.txt", "-o" , "/tmp/printout.pdf"])
os.remove("/tmp/printout.txt")
tmp = open("/tmp/printout.pdf").read()
os.remove("/tmp/printout.pdf")
return tmp
def create_report(env, e_id, selected_tz):
db = env.get_db_cnx()
cursor = db.cursor()
event = Event.fetch_one(env, e_id=e_id)
f = get_file()
add_doc(event, selected_tz, f)
add_summary(env, cursor, event, selected_tz, f)
add_attendees(env, cursor, event, selected_tz, f, True)
return generate_pdf(f)
def create_attendee_report(env, event, attendee, selected_tz):
db = env.get_db_cnx()
cursor = db.cursor()
f = get_file()
add_doc(event, selected_tz, f)
my_attendee = Attendee.fetch_one(env, a_id=attendee.a_id, e_id=event.e_id, fetch_options=True)
account = EventAccount.fetch_by_event(env, event.e_id)
add_payment_info(my_attendee, account, f)
add_attendee(event, my_attendee, f)
return generate_pdf(f)