You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # coding: utf-8
  2. try:
  3. from reportlab.lib.pagesizes import A4
  4. from reportlab.lib.units import inch
  5. from reportlab.pdfgen.canvas import Canvas
  6. W, H = A4
  7. except ImportError:
  8. # Don't crash if reportlab is not installed.
  9. Canvas = object
  10. def f(dt):
  11. return dt.strftime("%b. %-d, %Y")
  12. class PdfInvoice(Canvas):
  13. def __init__(self, fileobj):
  14. Canvas.__init__(self, fileobj, pagesize=A4, pageCompression=0)
  15. self.head_y = H - inch * 0.5
  16. def linefeed(self):
  17. self.head_y -= inch / 8
  18. def text(self, s, align="left", size=10, bold=False):
  19. self.head_y -= inch / 24
  20. self.linefeed()
  21. self.setFont("Helvetica-Bold" if bold else "Helvetica", size)
  22. if align == "left":
  23. self.drawString(inch * 0.5, self.head_y, s)
  24. elif align == "right":
  25. self.drawRightString(W - inch * 0.5, self.head_y, s)
  26. elif align == "center":
  27. self.drawCentredString(W / 2, self.head_y, s)
  28. self.head_y -= inch / 24
  29. def hr(self):
  30. self.setLineWidth(inch / 72 / 8)
  31. self.line(inch * 0.5, self.head_y, W - inch * 0.5, self.head_y)
  32. def row(self, items, align="left", bold=False, size=10):
  33. self.head_y -= inch / 8
  34. self.linefeed()
  35. self.setFont("Helvetica-Bold" if bold else "Helvetica", size)
  36. self.drawString(inch * 0.5, self.head_y, items[0])
  37. self.drawString(inch * 3.5, self.head_y, items[1])
  38. self.drawString(inch * 5.5, self.head_y, items[2])
  39. self.drawRightString(W - inch * 0.5, self.head_y, items[3])
  40. self.head_y -= inch / 8
  41. def render(self, tx, bill_to):
  42. invoice_id = "MS-HC-%s" % tx.id.upper()
  43. self.setTitle(invoice_id)
  44. self.text("SIA Monkey See Monkey Do", size=16)
  45. self.linefeed()
  46. self.text("Gaujas iela 4-2")
  47. self.text("Valmiera, LV-4201, Latvia")
  48. self.text("VAT: LV44103100701")
  49. self.linefeed()
  50. created = f(tx.created_at)
  51. self.text("Date Issued: %s" % created, align="right")
  52. self.text("Invoice Id: %s" % invoice_id, align="right")
  53. self.linefeed()
  54. self.hr()
  55. self.row(["Description", "Start", "End", tx.currency_iso_code],
  56. bold=True)
  57. self.hr()
  58. start = f(tx.subscription_details.billing_period_start_date)
  59. end = f(tx.subscription_details.billing_period_end_date)
  60. if tx.currency_iso_code == "USD":
  61. amount = "$%s" % tx.amount
  62. elif tx.currency_iso_code == "EUR":
  63. amount = "%s" % tx.amount
  64. else:
  65. amount = "%s %s" % (tx.currency_iso_code, tx.amount)
  66. self.row(["healthchecks.io paid plan", start, end, amount])
  67. self.hr()
  68. self.row(["", "", "", "Total: %s" % amount], bold=True)
  69. self.linefeed()
  70. self.text("Bill to:", bold=True)
  71. for s in bill_to.split("\n"):
  72. self.text(s.strip())
  73. self.linefeed()
  74. self.showPage()
  75. self.save()