mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 06:03:20 +00:00
At last we support trays!
This commit is contained in:
parent
62745e89fa
commit
d54f0f4c65
@ -60,7 +60,12 @@
|
|||||||
{% for id,infos in request.session['basket']|dictsort %}
|
{% for id,infos in request.session['basket']|dictsort %}
|
||||||
{% set product = counter.products.filter(id=id).first() %}
|
{% set product = counter.products.filter(id=id).first() %}
|
||||||
{% set s = infos['qty'] * infos['price'] / 100 %}
|
{% set s = infos['qty'] * infos['price'] / 100 %}
|
||||||
<li>{{ del_product(id, '-') }} {{ infos['qty'] }} {{ add_product(id, '+') }} {{ product.name }}: {{ "%0.2f"|format(s) }} €</li>
|
<li>{{ del_product(id, '-') }} {{ infos['qty'] + infos['bonus_qty'] }} {{ add_product(id, '+') }}
|
||||||
|
{{ product.name }}: {{ "%0.2f"|format(s) }} €
|
||||||
|
{% if infos['bonus_qty'] %}
|
||||||
|
P
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
<p><strong>{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket_total) }} €</strong></p>
|
<p><strong>{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket_total) }} €</strong></p>
|
||||||
|
@ -78,6 +78,11 @@ class CounterMain(DetailView, ProcessFormView, FormMixin):
|
|||||||
kwargs = super(CounterMain, self).get_context_data(**kwargs)
|
kwargs = super(CounterMain, self).get_context_data(**kwargs)
|
||||||
kwargs['login_form'] = AuthenticationForm()
|
kwargs['login_form'] = AuthenticationForm()
|
||||||
kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True
|
kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True
|
||||||
|
kwargs['login_form'].cleaned_data = {} # add_error fails if there are no cleaned_data
|
||||||
|
if "credentials" in self.request.GET:
|
||||||
|
kwargs['login_form'].add_error(None, _("Bad credentials"))
|
||||||
|
if "subscription" in self.request.GET:
|
||||||
|
kwargs['login_form'].add_error(None, _("User is not subscriber"))
|
||||||
kwargs['form'] = self.get_form()
|
kwargs['form'] = self.get_form()
|
||||||
if self.object.type == 'BAR':
|
if self.object.type == 'BAR':
|
||||||
kwargs['barmen'] = self.object.get_barmen_list()
|
kwargs['barmen'] = self.object.get_barmen_list()
|
||||||
@ -185,6 +190,13 @@ class CounterClick(DetailView):
|
|||||||
total += infos['price'] * infos['qty']
|
total += infos['price'] * infos['qty']
|
||||||
return total / 100
|
return total / 100
|
||||||
|
|
||||||
|
def get_total_quantity_for_pid(self, request, pid):
|
||||||
|
pid = str(pid)
|
||||||
|
try:
|
||||||
|
return request.session['basket'][pid]['qty'] + request.session['basket'][pid]['bonus_qty']
|
||||||
|
except:
|
||||||
|
return 0
|
||||||
|
|
||||||
def add_product(self, request, q = 1, p=None):
|
def add_product(self, request, q = 1, p=None):
|
||||||
"""
|
"""
|
||||||
Add a product to the basket
|
Add a product to the basket
|
||||||
@ -195,16 +207,23 @@ class CounterClick(DetailView):
|
|||||||
pid = str(pid)
|
pid = str(pid)
|
||||||
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 + q*float(price)):
|
product = self.get_product(pid)
|
||||||
|
bq = 0 # Bonus quantity, for trays
|
||||||
|
if product.tray: # Handle the tray to adjust the quantity q to add and the bonus quantity bq
|
||||||
|
total_qty_mod_6 = self.get_total_quantity_for_pid(request, pid) % 6
|
||||||
|
bq = int((total_qty_mod_6 + q) / 6) # Integer division
|
||||||
|
q -= bq
|
||||||
|
if self.customer.amount < (total + q*float(price)): # Check for enough money
|
||||||
request.session['not_enough'] = True
|
request.session['not_enough'] = True
|
||||||
return False
|
return False
|
||||||
if self.customer.user.get_age() < self.get_product(pid).limit_age:
|
if self.customer.user.get_age() < product.limit_age: # Check if affordable
|
||||||
request.session['too_young'] = True
|
request.session['too_young'] = True
|
||||||
return False
|
return False
|
||||||
if pid in request.session['basket']:
|
if pid in request.session['basket']: # Add if already in basket
|
||||||
request.session['basket'][pid]['qty'] += q
|
request.session['basket'][pid]['qty'] += q
|
||||||
else:
|
request.session['basket'][pid]['bonus_qty'] += bq
|
||||||
request.session['basket'][pid] = {'qty': q, 'price': int(price*100)}
|
else: # or create if not
|
||||||
|
request.session['basket'][pid] = {'qty': q, 'price': int(price*100), 'bonus_qty': bq}
|
||||||
request.session['not_enough'] = False # Reset not_enough to save the session
|
request.session['not_enough'] = False # Reset not_enough to save the session
|
||||||
request.session['too_young'] = False
|
request.session['too_young'] = False
|
||||||
request.session.modified = True
|
request.session.modified = True
|
||||||
@ -213,12 +232,16 @@ class CounterClick(DetailView):
|
|||||||
def del_product(self, request):
|
def del_product(self, request):
|
||||||
""" Delete a product from the basket """
|
""" Delete a product from the basket """
|
||||||
pid = str(request.POST['product_id'])
|
pid = str(request.POST['product_id'])
|
||||||
|
product = self.get_product(pid)
|
||||||
if pid in request.session['basket']:
|
if pid in request.session['basket']:
|
||||||
request.session['basket'][pid]['qty'] -= 1
|
if product.tray and (self.get_total_quantity_for_pid(request, pid) % 6 == 0) and request.session['basket'][pid]['bonus_qty']:
|
||||||
|
request.session['basket'][pid]['bonus_qty'] -= 1
|
||||||
|
else:
|
||||||
|
request.session['basket'][pid]['qty'] -= 1
|
||||||
if request.session['basket'][pid]['qty'] <= 0:
|
if request.session['basket'][pid]['qty'] <= 0:
|
||||||
del request.session['basket'][pid]
|
del request.session['basket'][pid]
|
||||||
else:
|
else:
|
||||||
request.session['basket'][pid] = 0
|
request.session['basket'][pid] = None
|
||||||
request.session.modified = True
|
request.session.modified = True
|
||||||
|
|
||||||
def parse_code(self, request):
|
def parse_code(self, request):
|
||||||
@ -257,10 +280,14 @@ class CounterClick(DetailView):
|
|||||||
uprice = p.selling_price
|
uprice = p.selling_price
|
||||||
if uprice * infos['qty'] > self.customer.amount:
|
if uprice * infos['qty'] > self.customer.amount:
|
||||||
raise DataError(_("You have not enough money to buy all the basket"))
|
raise DataError(_("You have not enough money to buy all the basket"))
|
||||||
request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
|
request.session['last_basket'].append("%d x %s" % (infos['qty']+infos['bonus_qty'], p.name))
|
||||||
s = Selling(label=p.name, product=p, club=p.club, counter=self.object, unit_price=uprice,
|
s = Selling(label=p.name, product=p, club=p.club, counter=self.object, unit_price=uprice,
|
||||||
quantity=infos['qty'], seller=self.operator, customer=self.customer)
|
quantity=infos['qty'], seller=self.operator, customer=self.customer)
|
||||||
s.save()
|
s.save()
|
||||||
|
if infos['bonus_qty']:
|
||||||
|
s = Selling(label=p.name + " (Plateau)", product=p, club=p.club, counter=self.object, unit_price=0,
|
||||||
|
quantity=infos['bonus_qty'], seller=self.operator, customer=self.customer)
|
||||||
|
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'] = "%0.2f" % self.sum_basket(request)
|
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)
|
||||||
@ -309,16 +336,19 @@ class CounterLogin(RedirectView):
|
|||||||
"""
|
"""
|
||||||
self.counter_id = kwargs['counter_id']
|
self.counter_id = kwargs['counter_id']
|
||||||
form = AuthenticationForm(request, data=request.POST)
|
form = AuthenticationForm(request, data=request.POST)
|
||||||
|
self.errors = []
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
user = Subscriber.objects.filter(username=form.cleaned_data['username']).first()
|
user = Subscriber.objects.filter(username=form.cleaned_data['username']).first()
|
||||||
if user.is_subscribed():
|
if user.is_subscribed():
|
||||||
Counter.add_barman(self.counter_id, user.id)
|
Counter.add_barman(self.counter_id, user.id)
|
||||||
|
else:
|
||||||
|
self.errors += ["subscription"]
|
||||||
else:
|
else:
|
||||||
print("Error logging the barman") # TODO handle that nicely
|
self.errors += ["credentials"]
|
||||||
return super(CounterLogin, self).post(request, *args, **kwargs)
|
return super(CounterLogin, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
def get_redirect_url(self, *args, **kwargs):
|
def get_redirect_url(self, *args, **kwargs):
|
||||||
return reverse_lazy('counter:details', args=args, kwargs=kwargs)
|
return reverse_lazy('counter:details', args=args, kwargs=kwargs)+"?"+'&'.join(self.errors)
|
||||||
|
|
||||||
class CounterLogout(RedirectView):
|
class CounterLogout(RedirectView):
|
||||||
permanent = False
|
permanent = False
|
||||||
|
Loading…
Reference in New Issue
Block a user