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.
128 lines
3.2 KiB
Python
128 lines
3.2 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
|
|
from django.http import HttpResponse
|
|
from django.template.loader import get_template
|
|
from weasyprint import HTML
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
|
|
|
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
|