| 1 | import operator |
|---|
| 2 | from cStringIO import StringIO |
|---|
| 3 | import relatorio |
|---|
| 4 | |
|---|
| 5 | class Invoice(dict): |
|---|
| 6 | |
|---|
| 7 | @property |
|---|
| 8 | def total(self): |
|---|
| 9 | return sum(l['amount'] for l in self['lines']) |
|---|
| 10 | |
|---|
| 11 | @property |
|---|
| 12 | def vat(self): |
|---|
| 13 | return self.total * 0.21 |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | repos = relatorio.ReportRepository() |
|---|
| 17 | repos.add_report(Invoice, 'application/vnd.oasis.opendocument.text', |
|---|
| 18 | 'basic.odt', report_name='basic') |
|---|
| 19 | repos.add_report(Invoice, 'application/vnd.oasis.opendocument.text', |
|---|
| 20 | 'invoice.odt', report_name='complicated') |
|---|
| 21 | repos.add_report(Invoice, 'application/vnd.oasis.opendocument.spreadsheet', |
|---|
| 22 | 'pivot.ods', report_name='pivot') |
|---|
| 23 | repos.add_report(Invoice, 'application/vnd.oasis.opendocument.presentation', |
|---|
| 24 | 'presentation.odp', report_name='presentation') |
|---|
| 25 | repos.add_report(Invoice, 'application/pdf', 'basic.tex', |
|---|
| 26 | report_name='ConTeXt') |
|---|
| 27 | repos.add_report(Invoice, 'image/png', 'pie_chart', report_name='pie') |
|---|
| 28 | repos.add_report(Invoice, 'image/png', 'vbar_chart', report_name='vbar') |
|---|
| 29 | repos.add_report(Invoice, 'image/png', 'hbar_chart', report_name='hbar') |
|---|
| 30 | |
|---|
| 31 | inv = Invoice(customer={'name': 'John Bonham', |
|---|
| 32 | 'address': {'street': 'Smirnov street', |
|---|
| 33 | 'zip': 1000, |
|---|
| 34 | 'city': 'Montreux'}}, |
|---|
| 35 | lines=[{'item': {'name': 'Vodka 70cl', |
|---|
| 36 | 'reference': 'VDKA-001', |
|---|
| 37 | 'price': 10.34}, |
|---|
| 38 | 'quantity': 7, |
|---|
| 39 | 'amount': 7*10.34}, |
|---|
| 40 | {'item': {'name': 'Cognac 70cl', |
|---|
| 41 | 'reference': 'CGNC-067', |
|---|
| 42 | 'price': 13.46}, |
|---|
| 43 | 'quantity': 12, |
|---|
| 44 | 'amount': 12*13.46}, |
|---|
| 45 | {'item': {'name': 'Sparkling water 25cl', |
|---|
| 46 | 'reference': 'WATR-007', |
|---|
| 47 | 'price': 4}, |
|---|
| 48 | 'quantity': 1, |
|---|
| 49 | 'amount': 4}, |
|---|
| 50 | {'item': {'name': 'Good customer', |
|---|
| 51 | 'reference': 'BONM-001', |
|---|
| 52 | 'price': -20}, |
|---|
| 53 | 'quantity': 1, |
|---|
| 54 | 'amount': -20}, |
|---|
| 55 | ], |
|---|
| 56 | id='MZY-20080703', |
|---|
| 57 | status='late', |
|---|
| 58 | trombine=(file('bouteille.png', 'r'), 'image/png')) |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | # ODT |
|---|
| 62 | basic_report, _ = repos.reports[Invoice]['basic'] |
|---|
| 63 | file('bonham_basic.odt', 'w').write(basic_report(inv).render().getvalue()) |
|---|
| 64 | report, _ = repos.reports[Invoice]['complicated'] |
|---|
| 65 | file('bonham_complicated.odt', 'w').write(report(inv).render().getvalue()) |
|---|
| 66 | |
|---|
| 67 | # ODS |
|---|
| 68 | ods_report, _ = repos.reports[Invoice]['pivot'] |
|---|
| 69 | file('bonham_pivot.ods', 'w').write(ods_report(inv).render().getvalue()) |
|---|
| 70 | |
|---|
| 71 | # ODP |
|---|
| 72 | odp_report, _ = repos.reports[Invoice]['presentation'] |
|---|
| 73 | file('bonham_presentation.odp', 'w').write(odp_report(inv).render().getvalue()) |
|---|
| 74 | |
|---|
| 75 | #PDF |
|---|
| 76 | #pdf_report, _ = repos.reports[Invoice]['ConTeXt'] |
|---|
| 77 | #file('bonham_basic.pdf', 'w').write(pdf_report(inv).render().getvalue()) |
|---|
| 78 | |
|---|
| 79 | #Image |
|---|
| 80 | pie_report, _ = repos.reports[Invoice]['pie'] |
|---|
| 81 | file('pie.png', 'w').write(pie_report(inv).render().getvalue()) |
|---|
| 82 | hbar_report, _ = repos.reports[Invoice]['hbar'] |
|---|
| 83 | file('hbar.png', 'w').write(hbar_report(inv).render().getvalue()) |
|---|
| 84 | vbar_report, _ = repos.reports[Invoice]['vbar'] |
|---|
| 85 | file('vbar.png', 'w').write(vbar_report(inv).render().getvalue()) |
|---|