Commit aafcc9c1 authored by Skia's avatar Skia
Browse files

Almost finish the counter views, still need the plateaux

parent b6904ad8
Pipeline #33 passed with stage
in 1 minute and 16 seconds
...@@ -23,13 +23,18 @@ ...@@ -23,13 +23,18 @@
<div> <div>
<p>Customer: {{ customer.user.get_display_name() }}, {{ customer.amount }}</p> <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> <p>Basket: </p>
<ul> <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() %} {% 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 %} {% endfor %}
</ul> </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) }}"> <form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="action" value="finish"> <input type="hidden" name="action" value="finish">
......
...@@ -90,11 +90,13 @@ class CounterClick(DetailView): ...@@ -90,11 +90,13 @@ class CounterClick(DetailView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
"""Simple get view""" """Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() 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) 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 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 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 return ret
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
...@@ -105,6 +107,8 @@ class CounterClick(DetailView): ...@@ -105,6 +107,8 @@ class CounterClick(DetailView):
return self.cancel(request) return self.cancel(request)
if 'basket' not in request.session.keys(): if 'basket' not in request.session.keys():
request.session['basket'] = {} request.session['basket'] = {}
request.session['basket_total'] = 0
request.session['not_enough'] = False
if 'add_product' in request.POST['action']: if 'add_product' in request.POST['action']:
self.add_product(request) self.add_product(request)
...@@ -118,7 +122,7 @@ class CounterClick(DetailView): ...@@ -118,7 +122,7 @@ class CounterClick(DetailView):
return self.render_to_response(context) return self.render_to_response(context)
def is_barman_price(self, ): 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 return True
else: else:
return False return False
...@@ -129,27 +133,27 @@ class CounterClick(DetailView): ...@@ -129,27 +133,27 @@ class CounterClick(DetailView):
price = p.special_selling_price price = p.special_selling_price
else: else:
price = p.selling_price price = p.selling_price
print("Price: %s" % price)
return price return price
def sum_basket(self, request): def sum_basket(self, request):
total = 0 total = 0
for pid,infos in request.session['basket'].items(): for pid,infos in request.session['basket'].items():
total += infos['price'] * infos['qty'] total += infos['price'] * infos['qty']
print("Total: %s" % total) return total / 100
return total
def add_product(self, request): def add_product(self, request, q = 1):
""" Add a product to the basket """ """ Add a product to the basket """
pid = str(request.POST['product_id']) pid = str(request.POST['product_id'])
price = self.get_price(pid) 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 return
if pid in request.session['basket']: if pid in request.session['basket']:
request.session['basket'][pid]['qty'] += 1 request.session['basket'][pid]['qty'] += q
else: 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 request.session.modified = True
def del_product(self, request): def del_product(self, request):
...@@ -169,22 +173,20 @@ class CounterClick(DetailView): ...@@ -169,22 +173,20 @@ class CounterClick(DetailView):
seller = self.customer.user seller = self.customer.user
else: else:
seller = Counter.get_random_barman(self.object.id) seller = Counter.get_random_barman(self.object.id)
total = 0
request.session['last_basket'] = [] 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) # This duplicates code for DB optimization (prevent to load many times the same object)
p = Product.objects.filter(pk=pid).first() p = Product.objects.filter(pk=pid).first()
if self.is_barman_price(): if self.is_barman_price():
uprice = p.special_selling_price uprice = p.special_selling_price
else: else:
uprice = p.selling_price uprice = p.selling_price
total += uprice * qty request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
request.session['last_basket'].append("%d x %s" % (qty, p.name))
s = Selling(product=p, counter=self.object, unit_price=uprice, 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() s.save()
request.session['last_customer'] = self.customer.user.get_display_name() 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) request.session['new_customer_amount'] = str(self.customer.amount)
del request.session['basket'] del request.session['basket']
request.session.modified = True request.session.modified = True
...@@ -203,6 +205,7 @@ class CounterClick(DetailView): ...@@ -203,6 +205,7 @@ class CounterClick(DetailView):
""" Add customer to the context """ """ Add customer to the context """
kwargs = super(CounterClick, self).get_context_data(**kwargs) kwargs = super(CounterClick, self).get_context_data(**kwargs)
kwargs['customer'] = self.customer kwargs['customer'] = self.customer
kwargs['basket_total'] = self.sum_basket(self.request)
return kwargs return kwargs
class CounterLogin(RedirectView): class CounterLogin(RedirectView):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment