You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.5 KiB
Python
131 lines
3.5 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
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'})
|
|
|
|
# LISTING
|
|
def items (request, *args, **kwargs):
|
|
context = {
|
|
|
|
}
|
|
return render(request, 'listing_pages/items.html', context)
|
|
|
|
|
|
def orders (request, *args, **kwargs):
|
|
context = {
|
|
|
|
}
|
|
return render(request, 'listing_pages/orders.html', context)
|
|
|
|
|
|
def invoices (request, *args, **kwargs):
|
|
invoices = Invoice.objects.all().order_by('-id')
|
|
|
|
context = {
|
|
'invoices': invoices,
|
|
}
|
|
|
|
return render(request, 'listing_pages/invoices.html', context)
|
|
|
|
|
|
|
|
|
|
# ADD
|
|
def add_product (request, *args, **kwargs):
|
|
context = {
|
|
|
|
}
|
|
return render(request, 'add_templates/add-product.html', context)
|
|
|
|
|
|
def add_service (request, *args, **kwargs):
|
|
context = {
|
|
|
|
}
|
|
return render(request, 'add_templates/add-service.html', context)
|
|
|
|
|
|
def add_order (request, *args, **kwargs):
|
|
customers = CustomerProfile.objects.all().order_by('-id')
|
|
|
|
context = {
|
|
'customers': customers,
|
|
}
|
|
|
|
return render(request, 'add_templates/add-order.html', context)
|
|
|
|
|
|
|
|
# TO FETCH THE ITEMS RELATED TO TH SELECTED CUSTOMER AND THE ITEMS THAT ARE NOT RELATED TO A CUSTOMER
|
|
def fetch_customer_items(request, customer_id):
|
|
customer = get_object_or_404(CustomerProfile, id=customer_id)
|
|
|
|
items_related_to_customer = Item.objects.filter(customer=customer)
|
|
|
|
items_without_customer = Item.objects.filter(customer__isnull=True)
|
|
|
|
data = {
|
|
'items_related_to_customer': list(items_related_to_customer.values('id', 'title')),
|
|
'items_without_customer': list(items_without_customer.values('id', 'title')),
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
|
|
def fetch_invoice_details(request, invoice_id):
|
|
invoice = get_object_or_404(Invoice, id=invoice_id)
|
|
order = invoice.order
|
|
order_items = OrderItem.objects.filter(order=order)
|
|
|
|
order_total = order.get_cart_total
|
|
|
|
data = {
|
|
'invoice_number': invoice.invoice_number,
|
|
'order_id': order.order_id,
|
|
'order_total' : order_total,
|
|
'date_created': invoice.date_created,
|
|
'customer_first_name': order.customer.user.first_name,
|
|
'customer_last_name': order.customer.user.last_name,
|
|
'customer_email': order.customer.user.email,
|
|
'order_items': list(order_items.values()), # Convert QuerySet to list of dictionaries
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
def invoice_details (request, invoice_id):
|
|
invoice = get_object_or_404(Invoice, id=invoice_id)
|
|
|
|
context = {
|
|
'invoice' : invoice,
|
|
}
|
|
|
|
return render(request, 'invoice-details.html', context)
|
|
|
|
|