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
2.9 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], bold=True)
  56. self.hr()
  57. start = f(tx.subscription_details.billing_period_start_date)
  58. end = f(tx.subscription_details.billing_period_end_date)
  59. if tx.currency_iso_code == "USD":
  60. amount = "$%s" % tx.amount
  61. elif tx.currency_iso_code == "EUR":
  62. amount = "%s" % tx.amount
  63. else:
  64. amount = "%s %s" % (tx.currency_iso_code, tx.amount)
  65. self.row(["healthchecks.io paid plan", start, end, amount])
  66. self.hr()
  67. self.row(["", "", "", "Total: %s" % amount], bold=True)
  68. self.linefeed()
  69. self.text("Bill to:", bold=True)
  70. for s in bill_to.split("\n"):
  71. self.text(s.strip())
  72. self.linefeed()
  73. self.showPage()
  74. self.save()