Add string parser in counters

This commit is contained in:
Skia 2016-06-26 19:30:28 +02:00
parent 9429dbccc2
commit 9989b75b3e
2 changed files with 44 additions and 6 deletions

View File

@ -26,6 +26,12 @@
{% if request.session['not_enough'] %} {% if request.session['not_enough'] %}
<p><strong>Not enough money</strong></p> <p><strong>Not enough money</strong></p>
{% endif %} {% endif %}
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="code">
<input type="input" name="code" value="" autofocus />
<input type="submit" value="Go" />
</form>
<p>Basket: </p> <p>Basket: </p>
<ul> <ul>
{% for id,infos in request.session['basket'].items() %} {% for id,infos in request.session['basket'].items() %}

View File

@ -8,7 +8,9 @@ from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.utils import timezone from django.utils import timezone
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _
import re
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from subscription.models import Subscriber from subscription.models import Subscriber
@ -22,10 +24,14 @@ class GetUserForm(forms.Form):
The Form implements a nice JS widget allowing the user to type a customer account id, or search the database with The Form implements a nice JS widget allowing the user to type a customer account id, or search the database with
some nickname, first name, or last name (TODO) some nickname, first name, or last name (TODO)
""" """
code = forms.CharField(label="Code", max_length=64, required=False) code = forms.CharField(label="Code", max_length=10, required=False)
id = forms.IntegerField(label="ID", required=False) id = forms.IntegerField(label="ID", required=False)
# TODO: add a nice JS widget to search for users # TODO: add a nice JS widget to search for users
def as_p(self):
self.fields['code'].widget.attrs['autofocus'] = True
return super(GetUserForm, self).as_p()
def clean(self): def clean(self):
cleaned_data = super(GetUserForm, self).clean() cleaned_data = super(GetUserForm, self).clean()
user = None user = None
@ -114,6 +120,8 @@ class CounterClick(DetailView):
self.add_product(request) self.add_product(request)
elif 'del_product' in request.POST['action']: elif 'del_product' in request.POST['action']:
self.del_product(request) self.del_product(request)
elif 'code' in request.POST['action']:
return self.parse_code(request)
elif 'cancel' in request.POST['action']: elif 'cancel' in request.POST['action']:
return self.cancel(request) return self.cancel(request)
elif 'finish' in request.POST['action']: elif 'finish' in request.POST['action']:
@ -121,7 +129,7 @@ class CounterClick(DetailView):
context = self.get_context_data(object=self.object) context = self.get_context_data(object=self.object)
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.user.id in [s.id for s 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:
@ -141,20 +149,21 @@ class CounterClick(DetailView):
total += infos['price'] * infos['qty'] total += infos['price'] * infos['qty']
return total / 100 return total / 100
def add_product(self, request, q = 1): def add_product(self, request, q = 1, p=None):
""" Add a product to the basket """ """ Add a product to the basket """
pid = str(request.POST['product_id']) pid = p or str(request.POST['product_id'])
price = self.get_price(pid) price = self.get_price(pid)
total = self.sum_basket(request) total = self.sum_basket(request)
if self.customer.amount < (total + float(price)): if self.customer.amount < (total + q*float(price)):
request.session['not_enough'] = True request.session['not_enough'] = True
return return False
if pid in request.session['basket']: if pid in request.session['basket']:
request.session['basket'][pid]['qty'] += q request.session['basket'][pid]['qty'] += q
else: else:
request.session['basket'][pid] = {'qty': q, 'price': int(price*100)} request.session['basket'][pid] = {'qty': q, 'price': int(price*100)}
request.session['not_enough'] = False request.session['not_enough'] = False
request.session.modified = True request.session.modified = True
return True
def del_product(self, request): def del_product(self, request):
""" Delete a product from the basket """ """ Delete a product from the basket """
@ -167,6 +176,29 @@ class CounterClick(DetailView):
request.session['basket'][pid] = 0 request.session['basket'][pid] = 0
request.session.modified = True request.session.modified = True
def parse_code(self, request):
"""Parse the string entered by the barman"""
string = str(request.POST['code']).upper()
if string == _("FIN"):
return self.finish(request)
elif string == _("ANN"):
return self.cancel(request)
regex = re.compile(r"^((?P<nb>[0-9]+)X)?(?P<code>[A-Z0-9]+)$")
m = regex.match(string)
if m is not None:
nb = m.group('nb')
code = m.group('code')
if nb is None:
nb = 1
else:
nb = int(nb)
p = Product.objects.filter(code=code).first()
if p is not None:
while nb > 0 and not self.add_product(request, nb, p.id):
nb -= 1
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
def finish(self, request): def finish(self, request):
""" Finish the click session, and validate the basket """ """ Finish the click session, and validate the basket """
if self.is_barman_price(): if self.is_barman_price():