diff --git a/counter/templates/counter/counter_click.jinja b/counter/templates/counter/counter_click.jinja index ad72640b..4c9d949b 100644 --- a/counter/templates/counter/counter_click.jinja +++ b/counter/templates/counter/counter_click.jinja @@ -23,13 +23,18 @@

Customer: {{ customer.user.get_display_name() }}, {{ customer.amount }} €

+ {% if request.session['not_enough'] %} +

Not enough money

+ {% endif %}

Basket:

+

Total: {{ "%0.2f"|format(basket_total) }} €

{% csrf_token %} diff --git a/counter/views.py b/counter/views.py index 1a4bc42f..a47ab222 100644 --- a/counter/views.py +++ b/counter/views.py @@ -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):