emile 1 year ago
parent ea84da3a92
commit d34c14bf66

@ -8,7 +8,7 @@ from billing import views
urlpatterns = [
path('check', views.create_charges_for_recurring_services, name='check'),
# LISTING
path('items', views.items, name='items'),
@ -28,4 +28,9 @@ urlpatterns = [
path('invoice-details/<int:invoice_id>/', views.invoice_details, name='invoicedetails'),
path('generate-pdf/', views.generate_pdf, name='generate-pdf'),
]

@ -3,27 +3,9 @@ from django.utils import timezone
from datetime import timedelta
from .models import *
from django.http import JsonResponse
def create_charges_for_recurring_services(request):
today = timezone.now().date()
active_recurring_services = Service.objects.filter(active=True, recurring=True)
for service in active_recurring_services:
# Get the last charge for the service
last_charge = service.charge_set.order_by('-due_date').first()
if last_charge and last_charge.due_date <= today:
# Calculate the new due date for the charge based on the recurring cycle
new_due_date = today + timedelta(days=service.recurring_cycle.number_of_months * 30)
# Create a new charge for the service
Charge.objects.create(
service=service,
amount=service.amount,
due_date=new_due_date
)
return JsonResponse({'status': 'success'})
from django.http import HttpResponse
from django.template.loader import get_template
from weasyprint import HTML
# LISTING
def items (request, *args, **kwargs):
@ -128,3 +110,18 @@ def invoice_details (request, invoice_id):
return render(request, 'invoice-details.html', context)
def generate_pdf(request):
# Render the template
template = get_template('invoice.html')
html = template.render()
# Create a PDF file
pdf_file = HTML(string=html).write_pdf()
# Prepare HTTP response
response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = 'filename="invoice.pdf"'
return response

Binary file not shown.
Loading…
Cancel
Save