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.

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