Adds checkout view, which generates an invoice, and then redirects to the invoice itself.
This commit is contained in:
parent
1b7d8a60c1
commit
a4de15830c
4 changed files with 72 additions and 4 deletions
|
@ -59,20 +59,23 @@ class InvoiceController(object):
|
|||
|
||||
# TODO: calculate line items.
|
||||
product_items = rego.ProductItem.objects.filter(cart=cart)
|
||||
product_items = product_items.order_by(
|
||||
"product__category__order", "product__order"
|
||||
)
|
||||
discount_items = rego.DiscountItem.objects.filter(cart=cart)
|
||||
invoice_value = Decimal()
|
||||
for item in product_items:
|
||||
product = item.product
|
||||
line_item = rego.LineItem.objects.create(
|
||||
invoice=invoice,
|
||||
description=item.product.name,
|
||||
description="%s - %s" % (product.category.name, product.name),
|
||||
quantity=item.quantity,
|
||||
price=item.product.price,
|
||||
price=product.price,
|
||||
)
|
||||
line_item.save()
|
||||
invoice_value += line_item.quantity * line_item.price
|
||||
|
||||
for item in discount_items:
|
||||
|
||||
line_item = rego.LineItem.objects.create(
|
||||
invoice=invoice,
|
||||
description=item.discount.description,
|
||||
|
|
37
registrasion/templates/invoice.html
Normal file
37
registrasion/templates/invoice.html
Normal file
|
@ -0,0 +1,37 @@
|
|||
<!--- Sample template. Move elsewhere once it's ready to go. -->
|
||||
|
||||
{% extends "site_base.html" %}
|
||||
{% block body %}
|
||||
|
||||
<h1>Invoice {{ invoice.id }}</h1>
|
||||
|
||||
<ul>
|
||||
<li>Void: {{ invoice.void }}</li>
|
||||
<li>Paid: {{ invoice.paid }}</li>
|
||||
</ul>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Description</th>
|
||||
<th>Quantity</th>
|
||||
<th>Price/Unit</th>
|
||||
<th>Total</th>
|
||||
</tr>
|
||||
{% for line_item in invoice.lineitem_set.all %}
|
||||
<tr>
|
||||
<td>{{line_item.description}}</td>
|
||||
<td>{{line_item.quantity}}</td>
|
||||
<td>{{line_item.price}}</td>
|
||||
<td><!-- multiply --> FIXME</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<th>TOTAL</th>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{{ invoice.value }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
{% endblock %}
|
|
@ -3,5 +3,6 @@ from django.conf.urls import url, patterns
|
|||
urlpatterns = patterns(
|
||||
"registrasion.views",
|
||||
url(r"^category/([0-9]+)$", "product_category", name="product_category"),
|
||||
# url(r"^category$", "product_category", name="product_category"),
|
||||
url(r"^checkout$", "checkout", name="checkout"),
|
||||
url(r"^invoice/([0-9]+)$", "invoice", name="invoice"),
|
||||
)
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
from registrasion import forms
|
||||
from registrasion import models as rego
|
||||
from registrasion.controllers.cart import CartController
|
||||
from registrasion.controllers.invoice import InvoiceController
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import transaction
|
||||
from django.forms import formset_factory
|
||||
from django.shortcuts import redirect
|
||||
from django.shortcuts import render
|
||||
from functools import partial, wraps
|
||||
|
||||
|
@ -55,3 +57,28 @@ def product_category(request, category_id):
|
|||
}
|
||||
|
||||
return render(request, "product_category.html", data)
|
||||
|
||||
@login_required
|
||||
def checkout(request):
|
||||
''' Runs checkout for the current cart of items, ideally generating an
|
||||
invoice. '''
|
||||
|
||||
current_cart = CartController.for_user(request.user)
|
||||
current_invoice = InvoiceController.for_cart(current_cart.cart)
|
||||
|
||||
return redirect("invoice", current_invoice.invoice.id)
|
||||
|
||||
|
||||
@login_required
|
||||
def invoice(request, invoice_id):
|
||||
''' Displays an invoice for a given invoice id. '''
|
||||
|
||||
invoice_id = int(invoice_id)
|
||||
inv = rego.Invoice.objects.get(pk=invoice_id)
|
||||
current_invoice = InvoiceController(inv)
|
||||
|
||||
data = {
|
||||
"invoice": current_invoice.invoice,
|
||||
}
|
||||
|
||||
return render(request, "invoice.html", data)
|
||||
|
|
Loading…
Reference in a new issue