Skip to content

Commit d3d946c

Browse files
committed
Added support for qr codes (http://qr-platba.cz/)
1 parent bd121dc commit d3d946c

File tree

7 files changed

+76
-10
lines changed

7 files changed

+76
-10
lines changed

InvoiceGenerator/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = (0, 4, 0)
1+
VERSION = (0, 4, 1)
22

33
__version__ = VERSION
44
__versionstr__ = '.'.join(map(str, VERSION))

InvoiceGenerator/api.py

+46
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from PIL import Image
3+
import qrcode
24

35
from conf import _
46

@@ -217,3 +219,47 @@ class Correction(Invoice):
217219

218220
def __init__(self, client, provider, creator):
219221
super(Correction, self).__init__(client, provider, creator)
222+
223+
224+
class QrCodeBuilder(object):
225+
226+
def __init__(self, invoice):
227+
"""
228+
:param invoice: Invoice
229+
"""
230+
self.invoice = invoice
231+
self.qr = self._fill(invoice)
232+
self.tmp_file = None
233+
234+
def _fill(self, invoice):
235+
from qrplatba import QRPlatbaGenerator
236+
237+
qr_kwargs = {
238+
'account': invoice.provider.bank_account,
239+
'amount': invoice.price_tax,
240+
}
241+
if invoice.variable_symbol:
242+
qr_kwargs['x_vs'] = invoice.variable_symbol
243+
if invoice.variable_symbol:
244+
qr_kwargs['x_ss'] = invoice.specific_symbol
245+
if invoice.payback:
246+
qr_kwargs['due_date'] = invoice.payback
247+
248+
return QRPlatbaGenerator(**qr_kwargs)
249+
250+
@property
251+
def filename(self):
252+
from tempfile import NamedTemporaryFile
253+
img = qrcode.make(self.qr.get_text())
254+
255+
self.tmp_file = NamedTemporaryFile(mode='w+b', suffix='.png',
256+
delete=False)
257+
img.save(self.tmp_file)
258+
self.tmp_file.close()
259+
return self.tmp_file.name
260+
261+
def destroy(self):
262+
if hasattr(self.tmp_file, 'name'):
263+
import os
264+
os.unlink(self.tmp_file.name)
265+

InvoiceGenerator/pdf.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from reportlab.platypus.tables import Table, TableStyle
1010

1111
from conf import _, FONT_PATH, FONT_BOLD_PATH
12-
from api import Invoice
12+
from api import Invoice, QrCodeBuilder
1313

1414
class BaseInvoice(object):
1515

@@ -24,10 +24,16 @@ def gen(self, filename):
2424

2525
class SimpleInvoice(BaseInvoice):
2626

27-
def gen(self, filename):
27+
def gen(self, filename, generate_qr_code=False):
2828
self.TOP = 260
2929
self.LEFT = 20
3030
self.filename = filename
31+
if generate_qr_code:
32+
qr_builder = QrCodeBuilder(self.invoice)
33+
else:
34+
qr_builder = None
35+
36+
self.qr_builder = qr_builder
3137

3238
pdfmetrics.registerFont(TTFont('DejaVu', FONT_PATH))
3339
pdfmetrics.registerFont(TTFont('DejaVu-Bold', FONT_BOLD_PATH))
@@ -51,6 +57,8 @@ def gen(self, filename):
5157

5258
self.pdf.showPage()
5359
self.pdf.save()
60+
if self.qr_builder:
61+
self.qr_builder.destroy()
5462

5563
#############################################################
5664
## Draw methods
@@ -266,6 +274,14 @@ def drawItems(self,TOP,LEFT):
266274
height = float(im.size[1]) / (float(im.size[0])/200.0)
267275
self.pdf.drawImage(self.invoice.creator.stamp_filename, (LEFT + 98) * mm, (TOP - i - 72) * mm, 200, height)
268276

277+
if self.qr_builder:
278+
qr_filename = self.qr_builder.filename
279+
im = Image.open(qr_filename)
280+
height = float(im.size[1]) / (float(im.size[0]) / 200.0)
281+
self.pdf.drawImage(qr_filename, LEFT * mm, (TOP - i - 100) * mm,
282+
200, height)
283+
284+
269285
path = self.pdf.beginPath()
270286
path.moveTo((LEFT + 110) * mm, (TOP - i - 70) * mm)
271287
path.lineTo((LEFT + 164) * mm, (TOP - i - 70) * mm)

README README.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ Usage::
2727

2828
from tempfile import NamedTemporaryFile
2929

30-
from InvoiceGenerator.generator import Generator
3130
from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator
3231
from InvoiceGenerator.pdf import SimpleInvoice
3332

3433

35-
invoice = Invoice(Client('Client company'), Provider('My company'), Creator('John Doe'))
34+
client = Client('Client company')
35+
provider = Provider('My company', bank_account='2600420569/2010')
36+
creator = Creator('John Doe')
37+
38+
invoice = Invoice(client, provider, creator)
3639
invoice.add_item(Item(32, 600))
3740
invoice.add_item(Item(60, 50, tax=10))
3841
invoice.add_item(Item(50, 60, tax=5))
3942
invoice.add_item(Item(5, 600, tax=50))
4043

4144
tmp_file = NamedTemporaryFile()
42-
Generator(invoice).gen(tmp_file.name, SimpleInvoice)
45+
pdf = SimpleInvoice(invoice)
46+
pdf.gen(tmp_file.name, generate_qr_code=True)
4347

example_with_vat.png

36.4 KB
Loading

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def read(fname):
2424
"License :: OSI Approved :: BSD License",
2525
],
2626
install_requires=[
27-
"reportlab", "PIL"
27+
"reportlab", "PIL", "qrplatba>=0.3.3"
2828
],
2929
package_data={'InvoiceGenerator': ['locale/cs/LC_MESSAGES/*']},
3030
)

tests/test_pdf.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_generate(self):
2323
provider.vat_id = 'CZ8590875682'
2424
provider.ir = '785684523'
2525
provider.email = '[email protected]'
26-
provider.bank_account = '56484984968/68'
26+
provider.bank_account = '2600420569/2010'
2727
provider.bank_name = 'RB'
2828
provider.note = u'zapsaná v obchodním rejstříku vedeném městským soudem v Praze,\noddíl C, vložka 176551'
2929

@@ -50,10 +50,10 @@ def test_generate(self):
5050
invoice.rounding_result = True
5151

5252

53-
tmp_file = NamedTemporaryFile()
53+
tmp_file = NamedTemporaryFile(delete=False)
5454

5555
pdf = SimpleInvoice(invoice)
56-
pdf.gen(tmp_file.name)
56+
pdf.gen(tmp_file.name, True)
5757

5858

5959
def test_generate_with_vat(self):

0 commit comments

Comments
 (0)