uplatnica_generator.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import os
  2. from fpdf import FPDF
  3. import config
  4. from fpdf import FPDF
  5. import os
  6. import config
  7. def format_amount(amount):
  8. return f"{amount:.2f}".replace(".", ",")
  9. def generate_uplatnica(order_id, payer_name, payer_address, amount):
  10. pdf = FPDF(orientation='P', unit='mm', format='A4')
  11. pdf.set_auto_page_break(False)
  12. pdf.add_page()
  13. pdf.set_font("helvetica", size=7)
  14. # === UPLATNICA AREA (top of A4) ===
  15. pdf.rect(0, 0, 210, 99)
  16. # Vertical divider
  17. pdf.line(105, 0, 105, 99)
  18. # ===== TOP OPTIONS =====
  19. labels = ["Hitnost", "Prenos", "Uplata", "Isplata"]
  20. start_x = 110
  21. step = 22
  22. for i, label in enumerate(labels):
  23. x = start_x + i * step
  24. pdf.set_xy(x, 2)
  25. pdf.cell(14, 4, label)
  26. pdf.rect(x + 14, 2, 4, 4)
  27. if label == "Uplata":
  28. pdf.line(x + 14, 2, x + 18, 6)
  29. pdf.line(x + 18, 2, x + 14, 6)
  30. # ===== LEFT =====
  31. pdf.set_font("helvetica", "B", 8)
  32. pdf.set_xy(5, 5)
  33. pdf.cell(95, 5, "NALOG PLATIOCA", align="C")
  34. pdf.rect(5, 12, 95, 14)
  35. pdf.set_font("helvetica", size=9)
  36. pdf.set_xy(7, 14)
  37. pdf.cell(90, 4, payer_name[:40])
  38. pdf.set_xy(7, 18)
  39. pdf.cell(90, 4, payer_address[:40])
  40. pdf.set_font("helvetica", size=6)
  41. pdf.set_xy(5, 26)
  42. pdf.cell(95, 4, "(Naziv platioca)", align="C")
  43. pdf.rect(5, 30, 95, 14)
  44. pdf.set_font("helvetica", size=9)
  45. pdf.set_xy(7, 32)
  46. pdf.cell(90, 4, "Usluge 3D stampe")
  47. pdf.set_xy(7, 36)
  48. pdf.cell(90, 4, f"Narudzba {order_id}")
  49. pdf.set_font("helvetica", size=6)
  50. pdf.set_xy(5, 44)
  51. pdf.cell(95, 4, "(Svrha placanja)", align="C")
  52. pdf.rect(5, 48, 95, 14)
  53. pdf.set_font("helvetica", size=9)
  54. pdf.set_xy(7, 50)
  55. pdf.cell(90, 4, config.COMPANY_NAME[:40])
  56. pdf.set_font("helvetica", size=6)
  57. pdf.set_xy(5, 62)
  58. pdf.cell(95, 4, "(Naziv primaoca)", align="C")
  59. pdf.line(5, 90, 100, 90)
  60. pdf.set_xy(5, 90)
  61. pdf.cell(95, 4, "(Potpis platioca)", align="C")
  62. # ===== RIGHT =====
  63. pdf.rect(110, 12, 95, 8)
  64. pdf.set_font("helvetica", size=7)
  65. pdf.set_xy(110, 22)
  66. pdf.cell(10, 4, "EUR")
  67. pdf.rect(120, 22, 50, 10)
  68. pdf.rect(175, 22, 30, 10)
  69. pdf.set_font("helvetica", "B", 11)
  70. pdf.set_xy(120, 24)
  71. pdf.cell(50, 6, format_amount(amount), align="C")
  72. pdf.set_xy(175, 24)
  73. pdf.cell(30, 6, "121", align="C")
  74. pdf.set_font("helvetica", size=6)
  75. pdf.set_xy(120, 32)
  76. pdf.cell(50, 4, "(Iznos)", align="C")
  77. pdf.set_xy(175, 32)
  78. pdf.cell(30, 4, "(Sifra)", align="C")
  79. pdf.rect(110, 36, 95, 10)
  80. pdf.set_font("helvetica", size=10)
  81. pdf.set_xy(110, 38)
  82. pdf.cell(95, 6, config.ZIRO_RACUN, align="C")
  83. pdf.rect(110, 50, 20, 8)
  84. pdf.rect(135, 50, 70, 8)
  85. pdf.set_xy(110, 52)
  86. pdf.cell(20, 4, "00", align="C")
  87. pdf.set_xy(135, 52)
  88. pdf.cell(70, 4, str(order_id), align="C")
  89. pdf.line(110, 90, 205, 90)
  90. pdf.set_xy(110, 90)
  91. pdf.cell(95, 4, "(Potpis primaoca)", align="C")
  92. # ===== SAVE =====
  93. pdf_dir = os.path.join(config.UPLOAD_DIR, "invoices")
  94. os.makedirs(pdf_dir, exist_ok=True)
  95. filename = f"uplatnica_order_{order_id}.pdf"
  96. filepath = os.path.join(pdf_dir, filename)
  97. pdf.output(filepath)
  98. return os.path.join("uploads", "invoices", filename).replace("\\", "/")