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.

88 lines
2.4 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, HttpResponse
from django.template.loader import get_template
# LISTING
def items(request, *args, **kwargs):
products = Item.objects.filter(type='Product').order_by('-id')
services = Item.objects.filter(type='Service').order_by('-id')
context = {
'products': products,
'services': services,
}
return render(request, 'listing_pages/items.html', context)
def orders(request, *args, **kwargs):
orders = Order.objects.all().order_by('-id')
context = {
'orders': orders,
}
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)
#DETAILS
def invoice_details(request, order_id):
order = get_object_or_404(Order, id=order_id)
context = {
'order' : order,
}
return render(request, 'details_templates/invoice-details.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_customer_businesses(request, customer_id):
try:
customer_profile = CustomerProfile.objects.get(id=customer_id)
except CustomerProfile.DoesNotExist:
customer_profile = None
if customer_profile:
businesses = Business.objects.filter(customer=customer_profile)
# Create a list to hold dictionary representations of each business
business_data = []
for business in businesses:
business_data.append({
'id': business.id,
'name': business.name,
})
else:
business_data = None
return JsonResponse({'businesses': business_data})