first commit
This commit is contained in:
47
app/services/export.py
Normal file
47
app/services/export.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from io import BytesIO, StringIO
|
||||
|
||||
from reportlab.lib.pagesizes import A4
|
||||
from reportlab.pdfgen import canvas
|
||||
|
||||
|
||||
def export_expenses_csv(expenses):
|
||||
output = StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(['Date', 'Title', 'Vendor', 'Category', 'Amount', 'Currency', 'Payment method', 'Tags'])
|
||||
for expense in expenses:
|
||||
writer.writerow([
|
||||
expense.purchase_date.isoformat(),
|
||||
expense.title,
|
||||
expense.vendor,
|
||||
expense.category.name_en if expense.category else '',
|
||||
str(expense.amount),
|
||||
expense.currency,
|
||||
expense.payment_method,
|
||||
expense.tags,
|
||||
])
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def export_expenses_pdf(expenses, title: str = 'Expenses'):
|
||||
buffer = BytesIO()
|
||||
pdf = canvas.Canvas(buffer, pagesize=A4)
|
||||
width, height = A4
|
||||
y = height - 40
|
||||
pdf.setFont('Helvetica-Bold', 14)
|
||||
pdf.drawString(40, y, title)
|
||||
y -= 30
|
||||
pdf.setFont('Helvetica', 10)
|
||||
for expense in expenses:
|
||||
line = f'{expense.purchase_date.isoformat()} | {expense.title} | {expense.amount} {expense.currency}'
|
||||
pdf.drawString(40, y, line[:100])
|
||||
y -= 16
|
||||
if y < 60:
|
||||
pdf.showPage()
|
||||
y = height - 40
|
||||
pdf.setFont('Helvetica', 10)
|
||||
pdf.save()
|
||||
buffer.seek(0)
|
||||
return buffer.read()
|
||||
Reference in New Issue
Block a user