From 515edc445938036fae23aa45beeb04f1c360ace9 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 5 Apr 2020 18:37:04 +0200 Subject: [PATCH] Credit note for new memberships --- apps/member/forms.py | 33 ++++++++++++++++++++++++++++ apps/member/views.py | 36 +++++++++++++++++++++++++++++-- static/js/autocomplete_model.js | 3 +++ templates/member/add_members.html | 8 +++++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/apps/member/forms.py b/apps/member/forms.py index 5c060f58..71335dce 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -4,6 +4,9 @@ from django import forms from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.models import User +from django.utils.translation import gettext_lazy as _ + +from note.models import NoteSpecial from note_kfet.inputs import Autocomplete, AmountInput, DatePickerInput from permission.models import PermissionMask @@ -48,6 +51,36 @@ class ClubForm(forms.ModelForm): class MembershipForm(forms.ModelForm): + credit_type = forms.ModelChoiceField( + queryset=NoteSpecial.objects, + label=_("Credit type"), + empty_label=_("No credit"), + required=False, + help_text=_("You can credit the note of the user."), + ) + + credit_amount = forms.IntegerField( + label=_("Credit amount"), + required=False, + initial=0, + widget=AmountInput(), + ) + + last_name = forms.CharField( + label=_("Last name"), + required=False, + ) + + first_name = forms.CharField( + label=_("First name"), + required=False, + ) + + bank = forms.CharField( + label=_("Bank"), + required=False, + ) + class Meta: model = Membership fields = ('user', 'roles', 'date_start') diff --git a/apps/member/views.py b/apps/member/views.py index 5391413b..b2befe37 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -22,7 +22,7 @@ from django_tables2.views import SingleTableView from rest_framework.authtoken.models import Token from note.forms import ImageForm from note.models import Alias, NoteUser -from note.models.transactions import Transaction +from note.models.transactions import Transaction, SpecialTransaction from note.tables import HistoryTable, AliasTable from permission.backends import PermissionBackend from permission.views import ProtectQuerysetMixin @@ -355,6 +355,7 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): .get(pk=self.kwargs["pk"]) context = super().get_context_data(**kwargs) context['club'] = club + context['form'].fields['credit_amount'].initial = club.membership_fee_paid return context @@ -364,11 +365,20 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): user = form.instance.user form.instance.club = club + credit_type = form.cleaned_data["credit_type"] + credit_amount = form.cleaned_data["credit_amount"] + last_name = form.cleaned_data["last_name"] + first_name = form.cleaned_data["first_name"] + bank = form.cleaned_data["bank"] + + if credit_type is None: + credit_amount = 0 + if user.profile.paid: fee = club.membership_fee_paid else: fee = club.membership_fee_unpaid - if user.note.balance < fee and not Membership.objects.filter( + if user.note.balance + credit_amount < fee and not Membership.objects.filter( club__name="Kfet", user=user, date_start__lte=datetime.now().date(), @@ -405,6 +415,28 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): .format(form.instance.club.membership_start)) return super().form_invalid(form) + if credit_amount > 0: + if not last_name or not first_name or not bank: + if not last_name: + form.add_error('last_name', _("This field is required.")) + if not first_name: + form.add_error('first_name', _("This field is required.")) + if not bank: + form.add_error('bank', _("This field is required.")) + return self.form_invalid(form) + + SpecialTransaction.objects.create( + source=credit_type, + destination=user.note, + quantity=1, + amount=credit_amount, + reason="Crédit " + credit_type.special_type + " (Adhésion " + club.name + ")", + last_name=last_name, + first_name=first_name, + bank=bank, + valid=True, + ) + return super().form_valid(form) def get_success_url(self): diff --git a/static/js/autocomplete_model.js b/static/js/autocomplete_model.js index e2a3f0cb..8c3f6f09 100644 --- a/static/js/autocomplete_model.js +++ b/static/js/autocomplete_model.js @@ -24,6 +24,9 @@ $(document).ready(function () { $("#" + prefix + "_" + obj.id).click(function() { target.val(obj[name_field]); $("#" + prefix + "_pk").val(obj.id); + + if (typeof autocompleted != 'undefined') + autocompleted(obj, prefix) }); if (input === obj[name_field]) diff --git a/templates/member/add_members.html b/templates/member/add_members.html index c44440bf..f9b2276d 100644 --- a/templates/member/add_members.html +++ b/templates/member/add_members.html @@ -17,5 +17,13 @@ {% block extrajavascript %} {% endblock %}