def draw_items(self, invoice, canvas): """Draw items into a nice table.""" data = [[_("Description"), _("Qty"), _("Unit price"), _("Tax"), _("Total")]] for item in invoice.items.all(): data.append( [ item.description, item.quantity, format_currency(item.unit_price), str(item.tax) + "%", format_currency(item.total), ] ) data.append([u"", u"", u"", _("Total") + u":", format_currency(invoice.total)]) table = Table(data, colWidths=[10 * cm, 1.2 * cm, 2.5 * cm, 1.4 * cm, 2.8 * cm]) table.setStyle( [ ("FONT", (0, 0), (-1, -1), self.FONT_NAME), ("FONTSIZE", (0, 0), (-1, -1), 10), ("TEXTCOLOR", (0, 0), (-1, -1), (0.2, 0.2, 0.2)), ("GRID", (0, 0), (-1, -2), 1, (0.7, 0.7, 0.7)), ("GRID", (-2, -1), (-1, -1), 1, (0.7, 0.7, 0.7)), ("ALIGN", (1, 0), (-1, -1), "RIGHT"), ("BACKGROUND", (0, 0), (-1, 0), (0.8, 0.8, 0.8)), ] ) tw, th, = table.wrapOn(canvas, 15 * cm, 19 * cm) table.drawOn(canvas, 1.5 * cm, self.baseline - th) self.baseline = -26 * cm
def draw_items(self, invoice, canvas): # Items data = [[_('Quantity'), _('Description'), _('Amount'), _('Total')], ] for item in invoice.items.all(): data.append([ item.quantity, item.description, format_currency(item.unit_price), format_currency(item.total) ]) data.append( [u'', u'', _('Total') + u":", format_currency(invoice.total)] ) table = Table(data, colWidths=[1.7*cm, 11*cm, 2.5*cm, 2.5*cm]) table.setStyle([ ('FONT', (0, 0), (-1, -1), self.FONT_NAME), ('FONTSIZE', (0, 0), (-1, -1), 10), ('TEXTCOLOR', (0, 0), (-1, -1), (0.2, 0.2, 0.2)), ('GRID', (0, 0), (-1, -2), 1, (0.7, 0.7, 0.7)), ('GRID', (-2, -1), (-1, -1), 1, (0.7, 0.7, 0.7)), ('ALIGN', (-2, 0), (-1, -1), 'RIGHT'), ('BACKGROUND', (0, 0), (-1, 0), (0.8, 0.8, 0.8)), ]) tw, th, = table.wrapOn(canvas, 15*cm, 19*cm) table.drawOn(canvas, 1.5*cm, self.baseline-th) self.baseline = -26*cm
def draw_pdf(buffer, invoice): """ Draws the invoice """ canvas = Canvas(buffer, pagesize=A4) canvas.translate(0, 29.7 * cm) canvas.setFont('Helvetica', 10) canvas.saveState() header_func(canvas) canvas.restoreState() canvas.saveState() footer_func(canvas) canvas.restoreState() canvas.saveState() address_func(canvas) canvas.restoreState() # Client address textobject = canvas.beginText(1.5 * cm, -2.5 * cm) if invoice.address.contact_name: textobject.textLine(invoice.address.contact_name) textobject.textLine(invoice.address.address_one) if invoice.address.address_two: textobject.textLine(invoice.address.address_two) textobject.textLine(invoice.address.town) if invoice.address.county: textobject.textLine(invoice.address.county) textobject.textLine(invoice.address.postcode) textobject.textLine(invoice.address.country.name) canvas.drawText(textobject) # Info textobject = canvas.beginText(1.5 * cm, -6.75 * cm) textobject.textLine(u'Invoice ID: %s' % invoice.invoice_id) textobject.textLine(u'Invoice Date: %s' % invoice.invoice_date.strftime('%d %b %Y')) textobject.textLine(u'Client: %s' % invoice.user.username) canvas.drawText(textobject) # Items data = [ [u'Quantity', u'Description', u'Amount', u'Total'], ] for item in invoice.items.all(): data.append([ item.quantity, item.description, format_currency(item.unit_price, invoice.currency), format_currency(item.total(), invoice.currency) ]) data.append([ u'', u'', u'Total:', format_currency(invoice.total(), invoice.currency) ]) table = Table(data, colWidths=[2 * cm, 11 * cm, 3 * cm, 3 * cm]) table.setStyle([ ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONTSIZE', (0, 0), (-1, -1), 10), ('TEXTCOLOR', (0, 0), (-1, -1), (0.2, 0.2, 0.2)), ('GRID', (0, 0), (-1, -2), 1, (0.7, 0.7, 0.7)), ('GRID', (-2, -1), (-1, -1), 1, (0.7, 0.7, 0.7)), ('ALIGN', (-2, 0), (-1, -1), 'RIGHT'), ('BACKGROUND', (0, 0), (-1, 0), (0.8, 0.8, 0.8)), ]) tw, th, = table.wrapOn(canvas, 15 * cm, 19 * cm) table.drawOn(canvas, 1 * cm, -8 * cm - th) canvas.showPage() canvas.save()
def total_amount(self): """Return total as formated string.""" return format_currency(self.total)
def test_total_amount(self, invoice): assert invoice.total_amount() == format_currency(1005504.49)
def draw_pdf(buffer, invoice): """ Draws the invoice """ canvas = Canvas(buffer, pagesize=A4) canvas.translate(0, 29.7 * cm) canvas.setFont('Helvetica', 10) canvas.saveState() header_func(canvas) canvas.restoreState() canvas.saveState() footer_func(canvas) canvas.restoreState() canvas.saveState() address_func(canvas) canvas.restoreState() # Client address textobject = canvas.beginText(1.5 * cm, -2.5 * cm) if invoice.address.contact_name: textobject.textLine(invoice.address.contact_name) textobject.textLine(invoice.address.address_one) if invoice.address.address_two: textobject.textLine(invoice.address.address_two) textobject.textLine(invoice.address.town) if invoice.address.county: textobject.textLine(invoice.address.county) textobject.textLine(invoice.address.postcode) textobject.textLine(invoice.address.country.name) canvas.drawText(textobject) # Info textobject = canvas.beginText(1.5 * cm, -6.75 * cm) textobject.textLine(u'Invoice ID: %s' % invoice.invoice_id) textobject.textLine(u'Invoice Date: %s' % invoice.invoice_date.strftime('%d %b %Y')) textobject.textLine(u'Client: %s' % invoice.user.username) canvas.drawText(textobject) # Items data = [[u'Quantity', u'Description', u'Amount', u'Total'], ] for item in invoice.items.all(): data.append([ item.quantity, item.description, format_currency(item.unit_price, invoice.currency), format_currency(item.total(), invoice.currency) ]) data.append([u'', u'', u'Total:', format_currency(invoice.total(), invoice.currency)]) table = Table(data, colWidths=[2 * cm, 11 * cm, 3 * cm, 3 * cm]) table.setStyle([ ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONTSIZE', (0, 0), (-1, -1), 10), ('TEXTCOLOR', (0, 0), (-1, -1), (0.2, 0.2, 0.2)), ('GRID', (0, 0), (-1, -2), 1, (0.7, 0.7, 0.7)), ('GRID', (-2, -1), (-1, -1), 1, (0.7, 0.7, 0.7)), ('ALIGN', (-2, 0), (-1, -1), 'RIGHT'), ('BACKGROUND', (0, 0), (-1, 0), (0.8, 0.8, 0.8)), ]) tw, th, = table.wrapOn(canvas, 15 * cm, 19 * cm) table.drawOn(canvas, 1 * cm, -8 * cm - th) canvas.showPage() canvas.save()
def as_currency(value): """Format decimal number as currency.""" return utils.format_currency(value)