add bottom tip

This commit is contained in:
zhangshine 2015-06-10 00:53:08 +08:00
parent 3e58223f17
commit 983e083537
4 changed files with 43 additions and 7 deletions

2
.gitignore vendored
View file

@ -10,7 +10,7 @@ __pycache__/
env/
build/
develop-eggs/
dist/
#dist/
downloads/
eggs/
.eggs/

View file

@ -1,12 +1,13 @@
from __future__ import unicode_literals
from datetime import datetime, date
from decimal import Decimal
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_RIGHT
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, Spacer
from pyinvoice.components import SimpleTable, TableWithHeader, PaidStamp
from pyinvoice.models import PDFInfo, Item, Transaction, InvoiceInfo, ServiceProviderInfo, ClientInfo
@ -46,6 +47,8 @@ class SimpleInvoice(SimpleDocTemplate):
self._item_tax_rate = None
self._transactions = []
self._story = []
self._bottom_tip = None
self._bottom_tip_align = None
@property
def items(self):
@ -66,6 +69,10 @@ class SimpleInvoice(SimpleDocTemplate):
if isinstance(t, Transaction):
self._transactions.append(t)
def set_bottom_tip(self, text, align=TA_CENTER):
self._bottom_tip = text
self._bottom_tip_align = align
@staticmethod
def __format_value(value):
if isinstance(value, datetime):
@ -244,6 +251,20 @@ class SimpleInvoice(SimpleDocTemplate):
transaction_table_data.insert(0, ('Transaction id', 'Gateway', 'Transaction date', 'Amount'))
self._story.append(TableWithHeader(transaction_table_data, horizontal_align='LEFT'))
def __build_bottom_tip(self):
if self._bottom_tip:
self._story.append(Spacer(5, 5))
self._story.append(
Paragraph(
self._bottom_tip,
ParagraphStyle(
'BottomTip',
parent=self._defined_styles.get('Normal'),
alignment=self._bottom_tip_align
)
)
)
def finish(self):
self._story = []
@ -251,5 +272,6 @@ class SimpleInvoice(SimpleDocTemplate):
self.__build_service_provider_and_client_info()
self.__build_items()
self.__build_transactions()
self.__build_bottom_tip()
self.build(self._story, onFirstPage=PaidStamp(7 * inch, 5.8 * inch) if self.is_paid else None)

View file

@ -1,7 +1,9 @@
#!/usr/bin/env python
from setuptools import setup
import os
from setuptools import setup
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()

View file

@ -1,6 +1,6 @@
import os
import unittest
from datetime import datetime
from datetime import datetime, date
from pyinvoice.models import InvoiceInfo, ServiceProviderInfo, ClientInfo, Item, Transaction
from pyinvoice.templates import SimpleInvoice
@ -11,7 +11,8 @@ class TestSimpleInvoice(unittest.TestCase):
self.file_base_dir = os.path.dirname(os.path.realpath(__file__))
def test_simple(self):
doc = SimpleInvoice(os.path.join(self.file_base_dir, 'fixtures/dist/simple.pdf'))
invoice_path = os.path.join(self.file_base_dir, 'fixtures/dist/simple.pdf')
doc = SimpleInvoice(invoice_path)
doc.is_paid = True
@ -41,9 +42,20 @@ class TestSimpleInvoice(unittest.TestCase):
doc.add_item(Item('Item', 'Item desc', 1, '1.1'))
doc.add_item(Item('Item', 'Item desc', 2, '2.2'))
doc.add_item(Item('Item', 'Item desc', 3, '3.3'))
items = doc.items
self.assertEqual(len(items), 3)
doc.set_item_tax_rate(20) # 20%
doc.add_transaction(Transaction('Paypal', 111, datetime.now(), 1))
doc.add_transaction(Transaction('Strip', 222, datetime.now(), 2))
doc.add_transaction(Transaction('Strip', 222, date.today(), 2))
doc.finish()
transactions = doc.transactions
self.assertEqual(len(transactions), 2)
doc.set_bottom_tip("Email: example@example.com<br />Don't hesitate to contact us for any questions.")
doc.finish()
self.assertTrue(os.path.exists(invoice_path))