diff --git a/osinaweb/billing/add/__pycache__/views.cpython-312.pyc b/osinaweb/billing/add/__pycache__/views.cpython-312.pyc index 76c4c18b..7f983ad1 100644 Binary files a/osinaweb/billing/add/__pycache__/views.cpython-312.pyc and b/osinaweb/billing/add/__pycache__/views.cpython-312.pyc differ diff --git a/osinaweb/billing/add/urls.py b/osinaweb/billing/add/urls.py index 84f0f892..4b383d0a 100644 --- a/osinaweb/billing/add/urls.py +++ b/osinaweb/billing/add/urls.py @@ -6,10 +6,10 @@ urlpatterns = [ path('product', views.add_product, name='addproduct'), 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'), - + path('add_payment_comment', views.add_payment_comment_modal, name='add_payment_comment_modal'), path('add_payment', views.add_payment_modal, name='add_payment_modal'), ] diff --git a/osinaweb/billing/add/views.py b/osinaweb/billing/add/views.py index 28ccc22b..32ec723f 100644 --- a/osinaweb/billing/add/views.py +++ b/osinaweb/billing/add/views.py @@ -8,6 +8,7 @@ import os from osinacore.decorators import * from django.core.files.base import ContentFile from django.db.models import Q +from weasyprint import HTML, CSS @@ -143,6 +144,62 @@ def add_payment_modal(request): +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