Gestion négatif d'un compte

- 3 paramètres ajustables : date jusqu'à, montant max, décalage de
  balance
This commit is contained in:
Aurélien Delobelle 2016-08-22 20:07:01 +02:00
parent 4e6f12bb6d
commit e89f8fd6a5
3 changed files with 65 additions and 3 deletions

View file

@ -7,7 +7,7 @@ from django.contrib.admin.widgets import FilteredSelectMultiple
from django.forms import modelformset_factory from django.forms import modelformset_factory
from django.utils import timezone from django.utils import timezone
from kfet.models import (Account, Checkout, Article, OperationGroup, Operation, from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
CheckoutStatement, ArticleCategory, Settings) CheckoutStatement, ArticleCategory, Settings, AccountNegative)
from gestioncof.models import CofProfile from gestioncof.models import CofProfile
# ----- # -----
@ -104,6 +104,15 @@ class GroupForm(forms.ModelForm):
model = Group model = Group
fields = ['name', 'permissions'] fields = ['name', 'permissions']
class AccountNegativeForm(forms.ModelForm):
class Meta:
model = AccountNegative
fields = ['authz_overdraft_amount', 'authz_overdraft_until',
'balance_offset', 'comment']
widgets = {
'authz_overdraft_until': DateTimeWidget(),
}
# ----- # -----
# Checkout forms # Checkout forms
# ----- # -----

View file

@ -1,4 +1,8 @@
{% extends "kfet/base.html" %} {% extends "kfet/base.html" %}
{% block extra_head %}
{{ negative_form.media }}
{% endblock %}
{% block title %} {% block title %}
{% if account.user == request.user %} {% if account.user == request.user %}
@ -34,7 +38,16 @@
{{ cof_form.as_p }} {{ cof_form.as_p }}
{{ account_form.as_p }} {{ account_form.as_p }}
{{ group_form.as_p }} {{ group_form.as_p }}
{% if perms.kfet.is_team and not perms.kfet.change_account %} {{ negative_form.non_field_errors }}
{% for field in negative_form %}
{{ field.errors }}
{{ field.label_tag }}
<div style="position:relative">{{ field }}</div>
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
{% endfor %}
{% if perms.kfet.is_team %}
<input type="password" name="KFETPASSWORD"> <input type="password" name="KFETPASSWORD">
{% endif %} {% endif %}
<input type="submit" value="Mettre à jour"> <input type="submit" value="Mettre à jour">
@ -44,4 +57,16 @@
</div> </div>
</div> </div>
<script type="text/javascript">
$(document).ready(function() {
$('#id_authz_overdraft_until').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
stepping: 5,
locale: 'fr',
});
});
</script>
{% endblock %} {% endblock %}

View file

@ -230,11 +230,16 @@ def account_update(request, trigramme):
group_form = UserGroupForm(instance=account.user) group_form = UserGroupForm(instance=account.user)
account_form = AccountForm(instance=account) account_form = AccountForm(instance=account)
cof_form = CofRestrictForm(instance=account.cofprofile) cof_form = CofRestrictForm(instance=account.cofprofile)
if hasattr(account, 'negative'):
negative_form = AccountNegativeForm(instance=account.negative)
else:
negative_form = None
else: else:
user_form = UserRestrictForm(instance=account.user) user_form = UserRestrictForm(instance=account.user)
account_form = None account_form = None
cof_form = None cof_form = None
group_form = None group_form = None
negative_form = None
if request.method == "POST": if request.method == "POST":
# Update attempt # Update attempt
@ -246,6 +251,8 @@ def account_update(request, trigramme):
cof_form = CofRestrictForm(request.POST, instance=account.cofprofile) cof_form = CofRestrictForm(request.POST, instance=account.cofprofile)
user_form = UserRestrictTeamForm(request.POST, instance=account.user) user_form = UserRestrictTeamForm(request.POST, instance=account.user)
group_form = UserGroupForm(request.POST, instance=account.user) group_form = UserGroupForm(request.POST, instance=account.user)
if hasattr(account, 'negative'):
negative_form = AccountNegativeForm(request.POST, instance=account.negative)
if (request.user.has_perm('kfet.change_account') if (request.user.has_perm('kfet.change_account')
and account_form.is_valid() and cof_form.is_valid() and account_form.is_valid() and cof_form.is_valid()
@ -254,6 +261,7 @@ def account_update(request, trigramme):
# Fill data for Account.save() # Fill data for Account.save()
put_cleaned_data_in_dict(data, user_form) put_cleaned_data_in_dict(data, user_form)
put_cleaned_data_in_dict(data, cof_form) put_cleaned_data_in_dict(data, cof_form)
print(vars(account.negative))
# Updating # Updating
account_form.save(data = data) account_form.save(data = data)
@ -262,6 +270,25 @@ def account_update(request, trigramme):
if (request.user.has_perm('kfet.manage_perms') if (request.user.has_perm('kfet.manage_perms')
and group_form.is_valid()): and group_form.is_valid()):
group_form.save() group_form.save()
print(vars(account.negative))
# Checking perm to manage negative
if hasattr(account, 'negative'):
balance_offset_old = 0
if account.negative.balance_offset:
balance_offset_old = account.negative.balance_offset
if (hasattr(account, 'negative')
and request.user.has_perm('kfet.change_accountnegative')
and negative_form.is_valid()):
balance_offset_new = negative_form.cleaned_data['balance_offset']
if not balance_offset_new:
balance_offset_new = 0
balance_offset_diff = balance_offset_old - balance_offset_new
Account.objects.filter(pk=account.pk).update(
balance = F('balance') + balance_offset_diff)
negative_form.save()
if not balance_offset_new and Account.objects.get(pk=account.pk).balance >= 0:
AccountNegative.objects.get(account=account).delete()
success = True success = True
messages.success(request, messages.success(request,
@ -291,6 +318,7 @@ def account_update(request, trigramme):
'cof_form' : cof_form, 'cof_form' : cof_form,
'user_form' : user_form, 'user_form' : user_form,
'group_form' : group_form, 'group_form' : group_form,
'negative_form': negative_form,
}) })
@permission_required('kfet.manage_perms') @permission_required('kfet.manage_perms')