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.

141 lines
4.0 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from osinacore.models import *
from billing.models import *
from django.http import JsonResponse, HttpResponse
from django.template.loader import get_template
from django.conf import settings
import os
from weasyprint import HTML
def add_product (request, *args, **kwargs):
item_types = ProjectType.objects.all().order_by('name')
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
item_type_id = request.POST.get('item_type')
item_type = get_object_or_404(ProjectType, id=item_type_id)
amount = request.POST.get('amount')
recurring = request.POST.get('recurring')
Item.objects.create(
type='Product',
title=title,
description = description,
item_type = item_type,
amount = amount,
recurring = recurring,
)
return redirect('items')
context = {
'item_types' : item_types,
}
return render(request, 'add_templates/add-product.html', context)
def add_service (request, *args, **kwargs):
item_types = ProjectType.objects.all().order_by('name')
customers = CustomerProfile.objects.all().order_by('user__first_name')
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
customer_id = request.POST.get('customer')
customer = get_object_or_404(CustomerProfile, id=customer_id)
item_type_id = request.POST.get('item_type')
item_type = get_object_or_404(ProjectType, id=item_type_id)
amount = request.POST.get('amount')
recurring = request.POST.get('recurring')
Item.objects.create(
type='Service',
title=title,
description = description,
customer = customer,
item_type = item_type,
amount = amount,
recurring = recurring,
)
return redirect('items')
context = {
'item_types' : item_types,
'customers' : customers
}
return render(request, 'add_templates/add-service.html', context)
def add_order (request, *args, **kwargs):
customers = CustomerProfile.objects.all().order_by('-id')
if request.method == 'POST':
customer_id = request.POST.get('customer')
customer = get_object_or_404(CustomerProfile, id=customer_id)
status = request.POST.get('status')
customer_id = request.POST.get('customer')
customer = get_object_or_404(CustomerProfile, id=customer_id)
selected_items = request.POST.getlist('items')
order = Order.objects.create(
customer=customer,
status=status
)
for item_id in selected_items:
item = Item.objects.get(id=item_id)
OrderItem.objects.create(order=order, item=item, purchased_at=datetime.now())
return redirect('orders')
context = {
'customers': customers,
}
return render(request, 'add_templates/add-order.html', context)
def add_invoice_pdf(request, order_id):
order = get_object_or_404(Order, id=order_id)
template = get_template('details_templates/invoice-details.html')
context = {'order': order}
html_string = template.render(context)
# Generate PDF
pdf = HTML(string=html_string).write_pdf()
invoice = Invoice.objects.create(
invoice_number='111',
order=order,
date_created=datetime.now(),
)
# Save PDF to a file
pdf_file_path = os.path.join(settings.MEDIA_ROOT, f'invoice_{invoice.id}.pdf')
with open(pdf_file_path, 'wb') as pdf_file:
pdf_file.write(pdf)
# Associate PDF file path with the Invoice object
invoice.pdf = pdf_file_path
invoice.save()
# Return PDF
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="my_pdf.pdf"'
return response