diff --git a/pyinvoice/components.py b/pyinvoice/components.py index 8e59dd0..0ddff3a 100644 --- a/pyinvoice/components.py +++ b/pyinvoice/components.py @@ -22,17 +22,21 @@ class SimpleTable(Table): class TableWithHeader(Table): - def __init__(self, data, horizontal_align=None): + def __init__(self, data, horizontal_align=None, style=None): Table.__init__(self, data, hAlign=horizontal_align) - style = TableStyle([ + default_style = [ ('INNERGRID', (0, 0), (-1, -1), .25, colors.black), ('BOX', (0, 0), (-1, -1), .25, colors.black), ('BACKGROUND', (0, 0), (-1, -len(data)), colors.lightgrey), ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE') - ]) - self.setStyle(style) + ] + + if style and isinstance(style, list): + default_style.extend(style) + + self.setStyle(TableStyle(default_style)) class PaidStamp(object): diff --git a/pyinvoice/models.py b/pyinvoice/models.py index 8733005..e41da78 100644 --- a/pyinvoice/models.py +++ b/pyinvoice/models.py @@ -64,7 +64,6 @@ class Item(object): :param unit_price: Unit price :return: """ - # TODO: add vat/tax, subtotal self.item_id = item_id self.name = name self.description = description diff --git a/pyinvoice/templates.py b/pyinvoice/templates.py index 1524ef2..adf2a47 100644 --- a/pyinvoice/templates.py +++ b/pyinvoice/templates.py @@ -42,6 +42,8 @@ class SimpleInvoice(SimpleDocTemplate): self.client_info = None self.is_paid = False self._items = [] + self.item_tax_total = None + self.item_total = None self._transactions = [] self._story = [] @@ -176,8 +178,22 @@ class SimpleInvoice(SimpleDocTemplate): self._story.append( Paragraph('Detail', self._defined_styles.get('Heading1')) ) - item_data.insert(0, ('Item id', 'Name', 'Description', 'Units', 'Unit Price', 'Vat/Tax', 'Subtotal')) - self._story.append(TableWithHeader(item_data, horizontal_align='LEFT')) + item_data.insert(0, ('#', 'Name', 'Description', 'Units', 'Unit Price', 'Vat/Tax', 'Subtotal')) + if self.item_tax_total or self.item_total: + item_data.append( + ( + 'Total', '', '', '', '', + self.item_tax_total if self.item_tax_total else '-', + self.item_total if self.item_total else '-' + ) + ) + style = [ + ('SPAN', (0, len(item_data) - 1), (4, len(item_data) - 1)), + ('ALIGN', (0, len(item_data) - 1), (-3, -1), 'RIGHT'), + ] + else: + style = None + self._story.append(TableWithHeader(item_data, horizontal_align='LEFT', style=style)) def __build_transactions(self): # Transaction diff --git a/simple.py b/simple.py index 0caf4a4..733f1e3 100644 --- a/simple.py +++ b/simple.py @@ -32,9 +32,11 @@ doc.client_info = ClientInfo( post_code='222222' ) -doc.add_item(Item('0000', 'Item', 'Item', 1, '1.1', '1.1')) +doc.add_item(Item('0000', 'Item', 'Item', 1, '1.1', '1.32', '0.22')) doc.add_item(Item('1111', 'Item', 'Item', 2, '2.2', '4.4')) doc.add_item(Item('2222', 'Item', 'Item', 3, '3.3', '9.9')) +doc.item_tax_total = Decimal('.22') +doc.item_total = Decimal('16.2') doc.add_transaction(Transaction('Paypal', 111, datetime.now(), 1)) doc.add_transaction(Transaction('Strip', 222, datetime.now(), 2))