Almost finish the counter views, still need the plateaux

This commit is contained in:
Skia 2016-06-02 14:55:12 +02:00
parent b6904ad88c
commit aafcc9c174
2 changed files with 27 additions and 19 deletions

View File

@ -23,13 +23,18 @@
<div>
<p>Customer: {{ customer.user.get_display_name() }}, {{ customer.amount }} €</p>
{% if request.session['not_enough'] %}
<p><strong>Not enough money</strong></p>
{% endif %}
<p>Basket: </p>
<ul>
{% for id,qte in request.session['basket'].items() %}
{% for id,infos in request.session['basket'].items() %}
{% set product = counter.products.filter(id=id).first() %}
<li>{{ del_product(id, '-') }} {{ qte }} {{ add_product(id, '+') }} {{ product.name }}</li>
{% set s = infos['qty'] * infos['price'] / 100 %}
<li>{{ del_product(id, '-') }} {{ infos['qty'] }} {{ add_product(id, '+') }} {{ product.name }}: {{ "%0.2f"|format(s) }} €</li>
{% endfor %}
</ul>
<p><strong>Total: {{ "%0.2f"|format(basket_total) }} €</strong></p>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="finish">

View File

@ -90,11 +90,13 @@ class CounterClick(DetailView):
def get(self, request, *args, **kwargs):
"""Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
if 'basket' not in request.session.keys(): # Init the basket session entry
request.session['basket'] = {}
request.session['basket_total'] = 0
request.session['not_enough'] = False
ret = super(CounterClick, self).get(request, *args, **kwargs)
if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in
return self.cancel(request) # Otherwise, go to main view
if 'basket' not in request.session.keys(): # Init the basket session entry
request.session['basket'] = {}
return ret
def post(self, request, *args, **kwargs):
@ -105,6 +107,8 @@ class CounterClick(DetailView):
return self.cancel(request)
if 'basket' not in request.session.keys():
request.session['basket'] = {}
request.session['basket_total'] = 0
request.session['not_enough'] = False
if 'add_product' in request.POST['action']:
self.add_product(request)
@ -118,7 +122,7 @@ class CounterClick(DetailView):
return self.render_to_response(context)
def is_barman_price(self, ):
if self.customer in Counter.get_barmen_list(self.object.id):
if self.customer.user.id in [s.id for s in Counter.get_barmen_list(self.object.id)]:
return True
else:
return False
@ -129,27 +133,27 @@ class CounterClick(DetailView):
price = p.special_selling_price
else:
price = p.selling_price
print("Price: %s" % price)
return price
def sum_basket(self, request):
total = 0
for pid,infos in request.session['basket'].items():
total += infos['price'] * infos['qty']
print("Total: %s" % total)
return total
return total / 100
def add_product(self, request):
def add_product(self, request, q = 1):
""" Add a product to the basket """
pid = str(request.POST['product_id'])
price = self.get_price(pid)
if self.customer.amount - (self.sum_basket(request)+price) < 0:
total = self.sum_basket(request)
if self.customer.amount < (total + float(price)):
request.session['not_enough'] = True
return
if pid in request.session['basket']:
request.session['basket'][pid]['qty'] += 1
request.session['basket'][pid]['qty'] += q
else:
request.session['basket'][pid] = {'qty': 1, 'price': price}
request.session['basket'][pid] = {'qty': q, 'price': int(price*100)}
request.session['not_enough'] = False
request.session.modified = True
def del_product(self, request):
@ -169,22 +173,20 @@ class CounterClick(DetailView):
seller = self.customer.user
else:
seller = Counter.get_random_barman(self.object.id)
total = 0
request.session['last_basket'] = []
for pid,qty in request.session['basket'].items():
for pid,infos in request.session['basket'].items():
# This duplicates code for DB optimization (prevent to load many times the same object)
p = Product.objects.filter(pk=pid).first()
if self.is_barman_price():
uprice = p.special_selling_price
else:
uprice = p.selling_price
total += uprice * qty
request.session['last_basket'].append("%d x %s" % (qty, p.name))
request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
s = Selling(product=p, counter=self.object, unit_price=uprice,
quantity=qty, seller=seller, customer=self.customer)
quantity=infos['qty'], seller=seller, customer=self.customer)
s.save()
request.session['last_customer'] = self.customer.user.get_display_name()
request.session['last_total'] = str(total)
request.session['last_total'] = "%0.2f" % self.sum_basket(request)
request.session['new_customer_amount'] = str(self.customer.amount)
del request.session['basket']
request.session.modified = True
@ -203,6 +205,7 @@ class CounterClick(DetailView):
""" Add customer to the context """
kwargs = super(CounterClick, self).get_context_data(**kwargs)
kwargs['customer'] = self.customer
kwargs['basket_total'] = self.sum_basket(self.request)
return kwargs
class CounterLogin(RedirectView):