Skip to content

Instantly share code, notes, and snippets.

@skolo-online
Created August 22, 2021 09:20
Show Gist options
  • Save skolo-online/763b4c0890edac510ec81df6d657234d to your computer and use it in GitHub Desktop.
Save skolo-online/763b4c0890edac510ec81df6d657234d to your computer and use it in GitHub Desktop.
create invoice part 2 view
@login_required
def createBuildInvoice(request, slug):
#fetch that invoice
try:
invoice = Invoice.objects.get(slug=slug)
pass
except:
messages.error(request, 'Something went wrong')
return redirect('invoices')
#fetch all the products - related to this invoice
products = Product.objects.filter(invoice=invoice)
context = {}
context['invoice'] = invoice
context['products'] = products
if request.method == 'GET':
prod_form = ProductForm()
inv_form = InvoiceForm(instance=invoice)
client_form = ClientSelectForm(initial_client=invoice.client)
context['prod_form'] = prod_form
context['inv_form'] = inv_form
context['client_form'] = client_form
return render(request, 'invoice/create-invoice.html', context)
if request.method == 'POST':
prod_form = ProductForm(request.POST)
inv_form = InvoiceForm(request.POST, instance=invoice)
client_form = ClientSelectForm(request.POST, initial_client=invoice.client, instance=invoice)
if prod_form.is_valid():
obj = prod_form.save(commit=False)
obj.invoice = invoice
obj.save()
messages.success(request, "Invoice product added succesfully")
return redirect('create-build-invoice', slug=slug)
elif inv_form.is_valid and 'paymentTerms' in request.POST:
inv_form.save()
messages.success(request, "Invoice updated succesfully")
return redirect('create-build-invoice', slug=slug)
elif client_form.is_valid() and 'client' in request.POST:
client_form.save()
messages.success(request, "Client added to invoice succesfully")
return redirect('create-build-invoice', slug=slug)
else:
context['prod_form'] = prod_form
context['inv_form'] = inv_form
context['client_form'] = client_form
messages.error(request,"Problem processing your request")
return render(request, 'invoice/create-invoice.html', context)
return render(request, 'invoice/create-invoice.html', context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment