Item total

This commit is contained in:
zhangshine 2015-06-09 21:25:42 +08:00
parent 03b18cec2f
commit b13a80160d
4 changed files with 29 additions and 8 deletions

View file

@ -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):

View file

@ -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

View file

@ -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

View file

@ -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))