From 529dcb86d3ca3b1609901c77631ca8ae56c89769 Mon Sep 17 00:00:00 2001 From: zhangshine Date: Sat, 30 May 2015 23:41:06 +0800 Subject: [PATCH] Invoice info, Service Provider info, Client info --- pyinvoice/components.py | 2 +- pyinvoice/models.py | 7 ++-- pyinvoice/templates.py | 80 +++++++++++++++++++++++++++++++++++++++-- test.py | 45 ++++++++++++----------- 4 files changed, 107 insertions(+), 27 deletions(-) diff --git a/pyinvoice/components.py b/pyinvoice/components.py index e2f81b1..987c7d4 100644 --- a/pyinvoice/components.py +++ b/pyinvoice/components.py @@ -25,7 +25,7 @@ class SimpleTable(Table): ]) def __init__(self, data, horizontal_align=None): - Table.__init__(self, data, style=self.style, hAlign=horizontal_align) + Table.__init__(self, data, hAlign=horizontal_align) class PaidStamp(object): diff --git a/pyinvoice/models.py b/pyinvoice/models.py index a54f926..fe40747 100644 --- a/pyinvoice/models.py +++ b/pyinvoice/models.py @@ -1,9 +1,12 @@ +from __future__ import unicode_literals + + class PDFInfo(object): - def __init__(self, title, author, subject): + def __init__(self, title=None, author=None, subject=None): self.title = title self.author = author self.subject = subject - self.creator = 'pyinvlice (https://ciciapp.com/pyinvoice)' + self.creator = 'PyInvoice (https://ciciapp.com/pyinvoice)' class InvoiceInfo(object): diff --git a/pyinvoice/templates.py b/pyinvoice/templates.py index 5d299d7..5d9de36 100644 --- a/pyinvoice/templates.py +++ b/pyinvoice/templates.py @@ -1,10 +1,19 @@ +from __future__ import unicode_literals +from datetime import datetime, date from reportlab.lib.pagesizes import letter from reportlab.lib.units import inch from reportlab.platypus import SimpleDocTemplate +from pyinvoice.components import SimpleTable +from pyinvoice.models import PDFInfo, Item, Transaction, InvoiceInfo, ServiceProviderInfo, ClientInfo class SimpleInvoice(SimpleDocTemplate): - def __init__(self, invoice_path): + default_pdf_info = PDFInfo(title='Invoice', author='CiCiApp.com', subject='Invoice') + + def __init__(self, invoice_path, pdf_info=None): + if not pdf_info: + pdf_info = self.default_pdf_info + SimpleDocTemplate.__init__( self, invoice_path, @@ -12,5 +21,70 @@ class SimpleInvoice(SimpleDocTemplate): rightMargin=inch, leftMargin=inch, topMargin=inch, - bottomMargin=inch - ) \ No newline at end of file + bottomMargin=inch, + **pdf_info.__dict__ + ) + + self.invoice_info = None + self.service_provider_info = None + self.client_info = None + self._items = [] + self._transactions = [] + + @property + def items(self): + return self._items[:] + + def add_item(self, item): + if isinstance(item, Item): + self._items.append(item) + + @property + def transactions(self): + return self._transactions[:] + + def add_transaction(self, t): + if isinstance(t, Transaction): + self._transactions.append(t) + + @staticmethod + def __attribute_to_table_data(instance, attribute_verbose_name_list): + data = [] + + for p, v in attribute_verbose_name_list: + attr = getattr(instance, p) + if attr is not None: + # TODO: datetime format + if isinstance(attr, datetime): + attr = attr.strftime('%Y-%m-%d %H:%M') + elif isinstance(attr, date): + attr = attr.strftime('%Y-%m-%d') + data.append(['{0}:'.format(v), attr]) + + return data + + def finish(self): + story = [] + + if isinstance(self.invoice_info, InvoiceInfo): + props = [('invoice_id', 'Invoice id'), ('invoice_datetime', 'Invoice date'), + ('due_datetime', 'Invoice due date')] + + story.append( + SimpleTable(self.__attribute_to_table_data(self.invoice_info, props), horizontal_align='LEFT') + ) + + if isinstance(self.service_provider_info, ServiceProviderInfo): + props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'), + ('country', 'Country'), ('post_code', 'Post code')] + + story.append( + SimpleTable(self.__attribute_to_table_data(self.service_provider_info, props), horizontal_align='RIGHT') + ) + + if isinstance(self.client_info, ClientInfo): + props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'), + ('country', 'Country'), ('post_code', 'Post code'), ('email', 'Email'), ('client_id', 'Client id')] + story.append(SimpleTable(self.__attribute_to_table_data(self.client_info, props), horizontal_align='LEFT')) + + self.build(story) \ No newline at end of file diff --git a/test.py b/test.py index d17257d..69d3304 100644 --- a/test.py +++ b/test.py @@ -1,25 +1,28 @@ -from reportlab.lib.units import inch -from pyinvoice.components import SimpleTable, PaidStamp +from datetime import datetime +from pyinvoice.models import InvoiceInfo, ServiceProviderInfo, ClientInfo from pyinvoice.templates import SimpleInvoice -story = [] - -address_table = SimpleTable([ - ['Name', 'zhangshine'], - ['City', 'Jining'], - ['City', 'Jining'], - ['City', 'Jining'], - ['City', 'Jining'], - ['City', 'Jining'] -], horizontal_align='RIGHT') -story.append(address_table) - -merchant_table = SimpleTable([ - ['Name', 'CiCiApp'], - ['xxxx', 'yyyy'] -], horizontal_align='LEFT') -story.append(merchant_table) - doc = SimpleInvoice('test.pdf') +doc.invoice_info = InvoiceInfo('1024', datetime.now(), datetime.now()) +doc.service_provider_info = ServiceProviderInfo( + name='PyInvoice', + street='My Street', + city='My City', + state='My State', + country='My Country', + post_code='My Post code' +) -doc.build(story, onFirstPage=PaidStamp(7*inch, 5.8*inch)) +doc.client_info = ClientInfo( + email='My Email', + client_id='My Client Id', + name='Client Name', + street='Client Street', + city='Client City', + state='Client State', + country='Client country', + post_code='Client Post code' +) + +# doc.build(story, onFirstPage=PaidStamp(7*inch, 5.8*inch)) +doc.finish() \ No newline at end of file