diff --git a/announce/views.py b/announce/views.py index 3252367..ffa6cde 100644 --- a/announce/views.py +++ b/announce/views.py @@ -10,12 +10,11 @@ import django.forms as forms from django.db.models import Q -from members.models import get_active_members, ContactInfo, KindOfMembership +from members.models import get_active_members, members_due_for_bank_collection, KindOfMembership def _announce_filter_collection(users): - users = users.filter(paymentinfo__bank_collection_allowed=True) \ - .filter(paymentinfo__bank_collection_mode__id=4) + users = members_due_for_bank_collection(users) for u in users: debt = u.contactinfo.get_debt_for_month(date.today()) if debt == 0: diff --git a/members/admin.py b/members/admin.py index eea62c4..2d9d81f 100644 --- a/members/admin.py +++ b/members/admin.py @@ -22,6 +22,7 @@ from django.conf import settings from django.contrib import messages import django.forms as forms +from members.models import members_due_for_bank_collection from .models import BankCollectionMode, ContactInfo, KindOfMembership from .models import Locker, MailinglistMail, MembershipFee, MembershipPeriod @@ -97,8 +98,7 @@ def make_into_real_payments(modeladmin, request, queryset): @admin.action(description="Generate SEPA XML for members (is active & has debt & allows monthly bank collection)") @transaction.atomic() def make_sepa_xml_for_members(modeladmin, request, queryset): - queryset = queryset.filter(paymentinfo__bank_collection_allowed=True) - queryset = queryset.filter(paymentinfo__bank_collection_mode__id=4) + queryset = members_due_for_bank_collection(queryset) dt = datetime.now() # active members only queryset = queryset.filter(Q(membershipperiod__begin__lte=dt), Q(membershipperiod__end__isnull=True) | Q(membershipperiod__end__gte=dt)) @@ -215,7 +215,7 @@ def lookups(self, request, model_admin): def queryset(self, request, qs): if self.value(): if self.value() == "not_monthly": - qs = qs.exclude(paymentinfo__bank_collection_mode__pk=4) + qs = qs.exclude(paymentinfo__bank_collection_mode__num_month=1) else: qs = qs.filter(paymentinfo__bank_collection_mode__pk=int(self.value())) return qs diff --git a/members/models.py b/members/models.py index 74dcc9a..1d82185 100644 --- a/members/models.py +++ b/members/models.py @@ -9,6 +9,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.utils.encoding import smart_str, force_str +from django.db.models import Q, F, Value import smtplib from django.core.mail import send_mail # for iButton regex @@ -322,6 +323,22 @@ def get_active_and_future_members(): .distinct() +def members_due_for_bank_collection(users=None): + if users is None: + users = get_active_members() + + current_month = datetime.now().month + + users = users.filter(paymentinfo__bank_collection_allowed=True) + users = users.filter(paymentinfo__bank_collection_mode__num_month__gt=0) + users = users.annotate( + is_collectable=Value(current_month - 1) % F("paymentinfo__bank_collection_mode__num_month"), + ) + users = users.filter(is_collectable=0) + + return users + + def get_active_membership_months_until(date): periods = MembershipPeriod.objects.filter(Q(begin__lte=date)) res = {}