Improve refilling form to handle the checks and the banks

This commit is contained in:
Skia 2016-07-17 11:38:19 +02:00
parent 14595936e2
commit c099cc489b
5 changed files with 53 additions and 7 deletions

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('counter', '0003_customer_amount'),
]
operations = [
migrations.AddField(
model_name='refilling',
name='bank',
field=models.CharField(verbose_name='bank', default='other', max_length=255, choices=[('other', 'Autre'), ('la-poste', 'La Poste'), ('credit-agricole', 'Credit Agricole'), ('credit-mutuel', 'Credit Mutuel')]),
),
migrations.AlterField(
model_name='refilling',
name='payment_method',
field=models.CharField(verbose_name='payment method', default='cash', max_length=255, choices=[('cheque', 'Chèque'), ('cash', 'Espèce')]),
),
]

View File

@ -123,8 +123,9 @@ class Refilling(models.Model):
customer = models.ForeignKey(Customer, related_name="refill_customers", blank=False) customer = models.ForeignKey(Customer, related_name="refill_customers", blank=False)
date = models.DateTimeField(_('date'), auto_now=True) date = models.DateTimeField(_('date'), auto_now=True)
payment_method = models.CharField(_('payment method'), max_length=255, payment_method = models.CharField(_('payment method'), max_length=255,
choices=settings.SITH_COUNTER_PAYMENT_METHOD) choices=settings.SITH_COUNTER_PAYMENT_METHOD, default='cash')
# TODO: add the bank if the payment is made by cheque bank = models.CharField(_('bank'), max_length=255,
choices=settings.SITH_COUNTER_BANK, default='other')
def __str__(self): def __str__(self):
return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name()) return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name())

View File

@ -29,8 +29,8 @@
<h5>Refilling</h5> <h5>Refilling</h5>
<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 %}
{{ refill_form.as_p() }}
<input type="hidden" name="action" value="refill"> <input type="hidden" name="action" value="refill">
<input type="input" name="amount" value=""/>
<input type="submit" value="Go" /> <input type="submit" value="Go" />
</form> </form>
</div> </div>

View File

@ -44,6 +44,13 @@ class GetUserForm(forms.Form):
cleaned_data['user_id'] = user.user.id cleaned_data['user_id'] = user.user.id
return cleaned_data return cleaned_data
class RefillForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = Refilling
fields = ['amount', 'payment_method', 'bank']
class CounterMain(DetailView, ProcessFormView, FormMixin): class CounterMain(DetailView, ProcessFormView, FormMixin):
""" """
The public (barman) view The public (barman) view
@ -101,6 +108,7 @@ class CounterClick(DetailView):
request.session['basket'] = {} request.session['basket'] = {}
request.session['basket_total'] = 0 request.session['basket_total'] = 0
request.session['not_enough'] = False request.session['not_enough'] = False
self.refill_form = None
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
@ -110,6 +118,7 @@ class CounterClick(DetailView):
""" Handle the many possibilities of the post request """ """ Handle the many possibilities of the post request """
self.object = self.get_object() self.object = self.get_object()
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
self.refill_form = None
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) return self.cancel(request)
if 'basket' not in request.session.keys(): if 'basket' not in request.session.keys():
@ -243,16 +252,21 @@ class CounterClick(DetailView):
operator = self.customer.user operator = self.customer.user
else: else:
operator = Counter.get_random_barman(self.object.id) operator = Counter.get_random_barman(self.object.id)
amount = float(request.POST['amount']) form = RefillForm(request.POST)
s = Refilling(counter=self.object, operator=operator, customer=self.customer, if form.is_valid():
amount=amount, payment_method="cash") form.instance.counter = self.object
s.save() form.instance.operator = operator
form.instance.customer = self.customer
form.instance.save()
else:
self.refill_form = form
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" 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) kwargs['basket_total'] = self.sum_basket(self.request)
kwargs['refill_form'] = self.refill_form or RefillForm()
return kwargs return kwargs
class CounterLogin(RedirectView): class CounterLogin(RedirectView):

View File

@ -222,6 +222,13 @@ SITH_COUNTER_PAYMENT_METHOD = [
('cash', 'Espèce'), ('cash', 'Espèce'),
] ]
SITH_COUNTER_BANK = [
('other', 'Autre'),
('la-poste', 'La Poste'),
('credit-agricole', 'Credit Agricole'),
('credit-mutuel', 'Credit Mutuel'),
]
# Subscription durations are in semestres (should be settingized) # Subscription durations are in semestres (should be settingized)
SITH_SUBSCRIPTIONS = { SITH_SUBSCRIPTIONS = {
'un-semestre': { 'un-semestre': {