diff --git a/osinaweb/billing/add/urls.py b/osinaweb/billing/add/urls.py index 464cf76e..b5c7f44b 100644 --- a/osinaweb/billing/add/urls.py +++ b/osinaweb/billing/add/urls.py @@ -7,6 +7,6 @@ urlpatterns = [ path('service', views.add_service, name='addservice'), path('order//', views.add_order, name='addorder'), - + path('invoice//', views.add_invoice_pdf, name='addinvoice'), path('service///', views.add_service_in_order, name='addserviceinorder'), ] diff --git a/osinaweb/billing/add/views.py b/osinaweb/billing/add/views.py index e7e03699..53e288c7 100644 --- a/osinaweb/billing/add/views.py +++ b/osinaweb/billing/add/views.py @@ -7,6 +7,7 @@ from django.conf import settings import os from osinacore.decorators import * from django.core.files.base import ContentFile +from weasyprint import HTML, CSS @@ -119,6 +120,66 @@ def add_service_in_order(request, service_id, order_id): +def add_invoice_pdf(request, order_id): + order = get_object_or_404(Order, id=order_id) + + current_year = str(timezone.now().year)[-2:] + last_invoice = Invoice.objects.all().last() + if last_invoice: + last_invoice_number = int(last_invoice.invoice_number.split('-')[1].split('+')[0]) + new_invoice_number = f"$0{current_year}-{last_invoice_number + 1}" + else: + new_invoice_number = f"$0{current_year}-1425" + + + + invoice = Invoice.objects.create( + invoice_number = new_invoice_number, + order=order, + date_created=datetime.now(), + ) + + template = get_template('details_templates/invoice-details.html') + context = {'order': order} + html_string = template.render(context) + + # Define the CSS string with Poppins font + css_string = ''' + @font-face { + font-family: 'Poppins'; + src: url('path_to_poppins_font_file.ttf') format('truetype'); /* Update the path to the font file */ + } + + body { + font-family: 'Poppins', sans-serif; /* Use Poppins font for the entire document */ + } + + /* Your existing CSS styles */ + /* Add or modify styles as needed */ + ''' + + # Generate PDF + pdf = HTML(string=html_string).write_pdf( + stylesheets=[ + CSS(string=css_string), + CSS(string='@page { margin: 30px; }') + ], + presentational_hints=True + ) + + filename = f'invoice_{invoice.invoice_number}.pdf' + pdf_content = ContentFile(pdf) + invoice.pdf.save(filename, pdf_content, save=True) + + + # Return PDF + response = HttpResponse(pdf, content_type='application/pdf') + response['Content-Disposition'] = 'attachment; filename="my_pdf.pdf"' + return response + + + +