|
|
@ -3,27 +3,9 @@ from django.utils import timezone
|
|
|
|
from datetime import timedelta
|
|
|
|
from datetime import timedelta
|
|
|
|
from .models import *
|
|
|
|
from .models import *
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
|
|
from django.template.loader import get_template
|
|
|
|
def create_charges_for_recurring_services(request):
|
|
|
|
from weasyprint import HTML
|
|
|
|
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'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# LISTING
|
|
|
|
# LISTING
|
|
|
|
def items (request, *args, **kwargs):
|
|
|
|
def items (request, *args, **kwargs):
|
|
|
@ -128,3 +110,18 @@ def invoice_details (request, invoice_id):
|
|
|
|
return render(request, 'invoice-details.html', context)
|
|
|
|
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
|
|
|
|