Coverage for apps/payments/reports.py: 8%
57 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
1from io import BytesIO
3import xlsxwriter
4from django.core.files.base import ContentFile
6from .models import Payment, PaymentExport
9def create_payment_report(instance: PaymentExport):
11 from_date = instance.from_date
12 until_date = instance.to_date
13 practitioner = instance.practitioner
14 organization = instance.organization
16 # Create a workbook and add a worksheet.
17 output = BytesIO()
18 workbook_name = f"Reporte de Cobranza del {from_date} al {until_date}.xlsx"
19 workbook = xlsxwriter.Workbook(output)
20 worksheet_name = f"Por fecha"
21 worksheet = workbook.add_worksheet(worksheet_name)
23 worksheet.set_column(0, 0, 2)
24 worksheet.set_column(1, 1, 15)
25 worksheet.set_column(2, 2, 25)
26 worksheet.set_column(3, 3, 12)
28 # get the queryset for payments
29 queryset = Payment.objects.filter(
30 payment_method__organization=instance.organization,
31 date__gte=instance.from_date,
32 date__lte=instance.to_date,
33 ).order_by()
35 """
36 if practitioner:
37 queryset.filter(practitioner=instance.practitioner)
38 """
40 # Write image for report (row 1)
41 worksheet.set_row(0, 40)
42 worksheet.embed_image(0, 1, "./app/TualLogo.png")
44 # Write title of table (rows 2 and 3)
45 title_format = workbook.add_format(
46 {
47 "bold": True,
48 "font_color": "red",
49 "font_size": 30,
50 }
51 )
53 subtitle_format = workbook.add_format({"bold": True, "italic": True})
55 worksheet.merge_range(2, 1, 2, 3, "Reporte de Cobranza", title_format)
56 worksheet.merge_range(3, 1, 3, 3, f"Del {from_date} al {until_date}", subtitle_format)
58 # Row 4 empty
60 # Write headers of table (row 5 and 6)
61 date_format = workbook.add_format({"num_format": "[$-x-sysdate]dddd, mmmm dd, yyyy", "align": "left"})
62 date_row_format = workbook.add_format({"bg_color": "#FF6600"})
63 money_format = workbook.add_format({"num_format": "$#,##0.00"})
64 start_row_for_headers = 5
65 header_format = workbook.add_format({"bold": True, "italic": True, "bg_color": "#F2F436"})
66 worksheet.write(start_row_for_headers, 1, "Fecha", header_format)
67 worksheet.write(start_row_for_headers, 2, "Cobrado Por", header_format)
68 worksheet.write(start_row_for_headers, 3, "Paciente", header_format)
69 worksheet.write(start_row_for_headers, 4, "Monto", header_format)
70 worksheet.write(start_row_for_headers, 5, "Metodos de Pago", header_format)
71 worksheet.write(start_row_for_headers, 6, "Facturación", header_format)
73 row = start_row_for_headers + 1
74 current_date = None
75 for payment in queryset:
76 if payment.date != current_date:
77 worksheet.merge_range(row, 1, row, 3, payment.date, date_format)
78 initial_date_row = row
79 row += 1
81 # worksheet.write(row, 1, payment.date, date_format)
82 worksheet.write(row, 2, getattr(payment.created_by, "email", ""))
83 worksheet.write(row, 3, payment.patient.full_name, date_format)
84 worksheet.write(row, 4, payment.amount.amount, money_format)
85 # worksheet.write(row, 2, payment.patient)
86 worksheet.write(row, 6, payment.is_invoiceable)
87 worksheet.set_row(row, None, None, {"level": 1})
88 worksheet.write(initial_date_row, 4, f"=SUM(E{initial_date_row + 2}:E{row + 1})", money_format)
90 row += 1
91 current_date = payment.date
93 workbook.close()
94 xlsx_data = output.getvalue()
96 xlsx_file = ContentFile(xlsx_data)
98 instance.export_file.save(workbook_name, xlsx_file)
99 instance.save()