diff --git a/cotisations/utils.py b/cotisations/utils.py index 8b7ee2b0..b1e5f613 100644 --- a/cotisations/utils.py +++ b/cotisations/utils.py @@ -25,7 +25,9 @@ from django.template.loader import get_template from django.core.mail import EmailMessage from .tex import create_pdf -from preferences.models import AssoOption, GeneralOption, CotisationsOption +from preferences.models import ( + AssoOption, GeneralOption, CotisationsOption, Mandate +) from re2o.settings import LOGO_PATH from re2o import settings @@ -97,9 +99,10 @@ def send_mail_invoice(invoice): def send_mail_voucher(invoice): """Creates a voucher from an invoice and sends it by email to the client""" + president = Mandate.get_mandate().president ctx = { 'asso_name': AssoOption.get_cached_value('name'), - 'pres_name': AssoOption.get_cached_value('pres_name'), + 'pres_name': ' '.join([president.name, president.surname]), 'firstname': invoice.user.name, 'lastname': invoice.user.surname, 'email': invoice.user.email, diff --git a/cotisations/views.py b/cotisations/views.py index e53b8553..a32c320f 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -60,7 +60,7 @@ from re2o.acl import ( can_delete_set, can_change, ) -from preferences.models import AssoOption, GeneralOption +from preferences.models import AssoOption, GeneralOption, Mandate from .models import ( Facture, Article, @@ -1068,9 +1068,10 @@ def voucher_pdf(request, invoice, **_kwargs): _("Could not find a voucher for that invoice.") ) return redirect(reverse('cotisations:index')) + president = Mandate.get_mandate().president return render_voucher(request, { 'asso_name': AssoOption.get_cached_value('name'), - 'pres_name': AssoOption.get_cached_value('pres_name'), + 'pres_name': ' '.join([president.name, president.surname]), 'firstname': invoice.user.name, 'lastname': invoice.user.surname, 'email': invoice.user.email, diff --git a/preferences/migrations/0063_mandate.py b/preferences/migrations/0063_mandate.py index f28acee4..7f5dcf8b 100644 --- a/preferences/migrations/0063_mandate.py +++ b/preferences/migrations/0063_mandate.py @@ -51,4 +51,9 @@ class Migration(migrations.Migration): bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), ), migrations.RunPython(create_current_mandate), + migrations.AlterField( + model_name='cotisationsoption', + name='send_voucher_mail', + field=models.BooleanField(default=False, help_text='Be carefull, if no mandate is defined on the preferences page, errors will be triggered when generating vouchers.', verbose_name='Send voucher by email when the invoice is controlled.'), + ), ] diff --git a/preferences/models.py b/preferences/models.py index 90655d48..eb904148 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -528,11 +528,24 @@ class Mandate(RevMixin, AclMixin, models.Model): @classmethod def get_mandate(cls, date=timezone.now): + """"Find the mandate taking place at the given date. If none is found + look for the nearest in the future. If none is found (again), look + for the nearest in the past.""" if callable(date): date = date() - return cls.objects.get( - start_date__gte=date, end_date__lte=date - ) + try: + return cls.objects.get( + start_date__gte=date, end_date__lte=date + ) + except cls.DoesNotExist: + try: + return cls.objects.filter(start_date__gte=date).earliest('start_date') + except cls.DoesNotExist: + try: + return cls.objects.filter(start_date__lte=date).latest('start_date') + except cls.DoesNotExist: + raise cls.DoesNotExist("No mandate have been created. Please go to the preferences page to create one.") + def is_over(self): return self.end_date is None @@ -847,6 +860,7 @@ class CotisationsOption(AclMixin, PreferencesModel): ) send_voucher_mail = models.BooleanField( verbose_name=_("Send voucher by email when the invoice is controlled."), + help_text=_("Be carefull, if no mandate is defined on the preferences page, errors will be triggered when generating vouchers."), default=False, )