add order

This commit is contained in:
Ivan Hörler 2018-02-26 19:56:45 +01:00
parent b15745a3bc
commit 6c46d0a6ce
6 changed files with 111 additions and 104 deletions

View File

@ -86,6 +86,9 @@ class Order(models.Model):
date = models.DateTimeField(default=timezone.now)
exchange_rate = models.ForeignKey(ExchangeRate)
def __str__(self):
return str(self.id)
class OrderPosition(models.Model):
""" Items in Submitted Orders"""

View File

@ -2,6 +2,7 @@
{% block section_title %}<h1>CHECKOUT</h1>{% endblock %}
{% block content %}
<h3>Preview your Purchase:</h3>
</br>
<h4>Shipping Address:</h4>
{% if person %}
<p><b>Salutation: </b>{{ person.salutation }}</p>
@ -17,48 +18,58 @@
<strong>
</p>
{% endif %}
{% if articles_list %}
<h4>Articles:</h4>
<table class="table">
{% if cart_position_list %}
</br>
<h4>Your Items:</h4>
<table class="table price-table">
<tr class="table_header">
<th scope="col">POS.</th>
<th scope="col">ART#</th>
<th scope="col">NAME</th>
<th scope="col">STOCK</th>
<th scope="col">AMOUNT</th>
<th scope="col">PRICE p.pce.</th>
<th scope="col">POSITION PRICE</th>
<th scope="col" class="price-label">PRICE p.pce.</th>
<th scope="col" class="price-label">POSITION PRICE</th>
</tr>
{% for article in articles_list %}
{% for cart_position in cart_position_list %}
<tr class="table_content">
<td scope="col">{{ article.id }}</td>
<td scope="col">{{ article.article.id }}</td>
<td scope="col">{{ cart_position.id }}</td>
<td scope="col">{{ cart_position.article.id }}</td>
<td scope="col">
<a href="{% url 'details' article.article.id %}">
{{ article.article.name }}
<a href="{% url 'details' cart_position.article.id %}">
{{ cart_position.article.name }}
</a>
</td>
<td scope="col">{{ article.article.stock }}</td>
<td scope="col">
{{ article.amount }}
</td>
<td scope="col">
{{ article.article.price_in_chf }}
<td scope="col">{{ cart_position.article.stock }}</td>
<td scope="col">{{ cart_position.article.amount }}</td>
<td scope="col" class="price-value">
{{ cart_position.article.price_in_chf }}
{{ currency_name }}
</td>
<td scope="col">{{ article.position_price }} {{ currency_name }}</td>
<td scope="col" class="price-value">
{{ cart_position.position_price }} {{ currency_name }}
</td>
</tr>
{% endfor %}
<tr>
<td scope="col" colspan="7" class="text-right">
Total: {{ total }} {{ currency_name }}
<td scope="col" colspan="5"class="text-right">
<td scope="col" class="price-value">
<dl><dt>Total:</dl></dt></td>
<td scope="col" class="price-value">
<dl><dt>{{ total }} {{ currency_name }}</dl></dt>
</td>
</tr>
</table>
<form id="checkout_form" action="orders" method="post">
{% csrf_token %}
{{ checkout_form.as_p }}
<input type="submit" value="Order" class="btn btn-success" role="button"/>
</form>
{% else %}
<p class="alert alert-danger">
<strong>
Something whent wrong. Your cart is empty.
Your cart seamed to lack Items.
Go get some in the store!
<strong>
</p>
{% endif %}

View File

@ -13,6 +13,9 @@
{% else %}
<li><a href="{% url 'login' %}">LOGIN</a></li>
{% endif %}
{% if checkout %}
<!-- bewusst leer um dropdown in checkout auszublenden -->
{% else %}
<li class="dropdown">
{% if article_view %}
<form id="currency" action="" method="POST" novalidate>
@ -20,7 +23,8 @@
<li><input type="submit" value="Select"></li>
{% csrf_token %}
</form>
{% endif %}
{% endif %}
{% endif %}
</li>
</ul>
</div>

View File

@ -0,0 +1,8 @@
{% extends "webshop/base.html" %}
{% load customfilters %}
{% block section_title %}Order{% endblock %}
{% block content %}
<h1> Your order was submitted. </h1>
<h3> Thank you for Purchase. </h3>
{% endblock %}

View File

@ -23,4 +23,7 @@ urlpatterns = [
url(r'^cart/checkout/$',
views.checkout,
name='checkout'),
url(r'^orders$',
views.orders,
name='orders'),
]

View File

@ -10,7 +10,8 @@ from webshop.models import (Article,
City,
Picture,
CartPosition,
ShoppingCart)
ShoppingCart,
Order)
from webshop.forms import (RegistrationForm,
AddToCartForm,
CartForm,
@ -83,7 +84,7 @@ def restrict_cart_to_one_article(user_id, article_id, amount, operation):
if operation == 'add':
new_amount = cart_position.amount + amount
if operation == 'replace':
new_amount = amount # ref two times check later !!
new_amount = amount
# if article is in cart already update amount:
cart_position = CartPosition.objects.filter(
article=article_id).update(
@ -280,11 +281,6 @@ def cart(request):
currency,
cart_position.article.price_in_chf
)
cart_position.position_price = rate.exchange(
currency,
cart_position.position_price
)
amount_form = CartForm(
initial={'amount_form': cart_position.amount}
)
@ -315,106 +311,88 @@ def checkout(request):
amount_form = CartForm
rate = ExchangeRate
article_view = True
currency_name = "CHF"
message = ""
cart_position_list = []
amount_form_list = []
totalprice_list = []
total = 0
cart_position_list_zip = []
# here we configure the users Currency:
checkout_form = CheckoutForm()
if 'currency' not in request.session:
request.session['currency'] = None
else:
currency = request.session['currency']
exchange_rate = ExchangeRate.objects.filter(name=currency).latest('date')
# Here we handle all POST Operations:
if request.method == 'POST':
# here we react to a currency dropdown change:
if 'currencies' in request.POST:
print('currencies')
currencies_form = CurrenciesForm(request.POST)
if currencies_form.is_valid():
cf = currencies_form.cleaned_data
if cf['currencies']:
print('currencies cf:', cf)
selection = cf['currencies']
request.session['currency'] = selection.id
currency_name = ExchangeRate_name.objects.get(
id=selection.id)
print('currencies currency_name:', currency_name)
else:
request.session['currency'] = None
print('checkout post')
# here we react to a change of amount per item in the Cart:
if 'amount_form' in request.POST:
amount_form = CartForm(request.POST)
if amount_form.is_valid():
amount = amount_form.cleaned_data['amount_form']
article_id = request.POST.get('article_id')
operation = 'replace'
restrict_cart_to_one_article(
request.user.id,
article_id,
amount,
operation
if 'checkout_form' in request.POST:
print('checkout post request.POST = checkout_form')
checkout_form = CartForm(request.POST)
if checkout_form.is_valid():
print('checkout post valid')
order, created_order = Order.objects.get_or_create(
user=request.user,
defaults={'status': 1,
'exchange_rate': exchange_rate.id,
}
)
# here we react to a change of amount per item in the Cart:
if 'delete' in request.POST:
delete = CartForm(request.POST)
if delete.is_valid():
amount = delete.cleaned_data['amount_form']
article_id = request.POST.get('article_id')
amount = 1
operation = 'delete'
restrict_cart_to_one_article(
request.user.id,
article_id,
amount,
operation
)
# here we handle the normal cart view:
# if cart_id is not existent create a cart:
cart_id, created_cart = ShoppingCart.objects.get_or_create(user=request.user)
# get all items in the cart of this customer:
cart_positions = CartPosition.objects.filter(cart=cart_id)
if (cart_positions.count()) > 0:
# make a list out of all articles:
cart_position_list = list(cart_positions)
# enumerate the list of articles and loop over items:
for idx, cart_position in enumerate(cart_position_list):
# scrap out the details to calculate Total of item and Summ of All:
if currency:
# get currencyname to display:
currency_name = ExchangeRate_name.objects.get(id=currency)
# get exchange_rate multiplyed:
cart_position.price_in_chf = rate.exchange(
currency,
cart_position.article.price_in_chf
)
totalprice_list.append(cart_position.price_in_chf)
amount_form = CartForm(
initial={'amount_form': cart_position.amount}
)
amount_form_list.append(amount_form)
cart_position_list[idx] = cart_position
cart_position_list_zip = zip(cart_position_list, amount_form_list)
print('order', order, 'created:', created_order)
if created_order is False:
message = """something whent wrong.
Seams like this cart was already submitted. How come? """
# order status variables:
# • ordered -> vom Kunden bestellt
#  • delivered -> Bestellung wurde versandt
# • cancelled -> Bestellung storniert
# • on hold -> Bestellung pausiert
cart_id, created_cart = ShoppingCart.objects.get_or_create(
user=request.user)
if created_cart is False:
# get all items in the cart of this customer:
cart_positions = CartPosition.objects.filter(
cart=cart_id)
if (cart_positions.count()) > 0:
# make a list out of all articles:
cart_position_list = list(cart_positions)
# enumerate the list of articles and loop over items:
for idx, cart_position in enumerate(cart_position_list):
if currency:
# get currencyname to display:
currency_name = ExchangeRate_name.objects.get(id=currency)
# get exchange_rate multiplyed:
cart_position.article.price_in_chf = rate.exchange(
currency,
cart_position.article.price_in_chf
)
cart_position.calculate_position_price()
totalprice_list.append(cart_position.position_price)
cart_position_list[idx] = cart_position
else:
message = """something whent wrong.
Seams like your cart was
not existent before. How come? """
total = sum(totalprice_list)
checkout_form = CheckoutForm()
registration_form = RegistrationForm()
person = Person.objects.get(user=request.user.id)
return render(request, 'webshop/checkout.html',
{'cart_position_list_zip': cart_position_list_zip,
{'cart_position_list': cart_position_list,
'totalprice_list': totalprice_list,
'total': total,
'currencies_form': currencies_form,
'amount_form': amount_form,
'checkout_form': checkout_form,
'registration_form': registration_form,
'article_view': article_view,
'currency_name': currency_name,
'article_view': article_view,
'category_list': category_list,
'message': message,
'person': person
})
def orders(request):
return render(request, 'webshop/orders.html',
{
})