Repository: svthalia/concrexit

Issue:
BankAccount last used should be updated to withdrawal date not processing date.
### Describe the bug
We keep the last date a mandate was used so mandates that have not been used for more than 
a year
 three years can be revoked (because that are the sepa rules). However the ‘last used’ date is actually the indicated withdrawal date and not the processing date of the batch

### Expected behaviour
Update with the withdrawal date 


Discussion:
The revocation logic says 36 months https://github.com/svthalia/concrexit/blob/75764145d33181d8669575eb3c0c7df3bf8f4113/website/payments/services.py#L96
> The revocation logic says 36 months
> 
> https://github.com/svthalia/concrexit/blob/75764145d33181d8669575eb3c0c7df3bf8f4113/website/payments/services.py#L96

You are correct, it is 36 months indeed 

Resolving patch:
diff --git a/website/payments/admin_views.py b/website/payments/admin_views.py
--- a/website/payments/admin_views.py
+++ b/website/payments/admin_views.py
@@ -85,17 +85,7 @@ def post(self, request, *args, **kwargs):
                 request, _("{} already processed.").format(model_ngettext(batch, 1))
             )
         else:
-            batch.processed = True
-            payments = batch.payments_set.select_related("paid_by")
-            for payment in payments:
-                bank_account = payment.paid_by.bank_accounts.last()
-                bank_account.last_used = timezone.now()
-                bank_account.save()
-
-            batch.save()
-
-            services.send_tpay_batch_processing_emails(batch)
-
+            services.process_batch(batch)
             messages.success(
                 request,
                 _("Successfully processed {}.").format(model_ngettext(batch, 1)),
diff --git a/website/payments/services.py b/website/payments/services.py
--- a/website/payments/services.py
+++ b/website/payments/services.py
@@ -103,6 +103,26 @@ def revoke_old_mandates() -> int:
     ).update(valid_until=timezone.now().date())
 
 
+def process_batch(batch):
+    """
+    Processes a Thalia Pay batch
+
+    :param batch: the batch to be processed
+    :return:
+    """
+    batch.processed = True
+
+    payments = batch.payments_set.select_related("paid_by")
+    for payment in payments:
+        bank_account = payment.paid_by.bank_accounts.last()
+        bank_account.last_used = batch.withdrawal_date
+        bank_account.save()
+
+    batch.save()
+
+    send_tpay_batch_processing_emails(batch)
+
+
 def send_tpay_batch_processing_emails(batch):
     """Sends withdrawal notice emails to all members in a batch"""
     member_payments = batch.payments_set.values("paid_by").annotate(total=Sum("amount"))


Test patch:
diff --git a/website/payments/tests/test_services.py b/website/payments/tests/test_services.py
--- a/website/payments/tests/test_services.py
+++ b/website/payments/tests/test_services.py
@@ -154,3 +154,27 @@ def test_revoke_old_mandates(self):
         services.revoke_old_mandates()
 
         self.assertEqual(BankAccount.objects.filter(valid_until=None).count(), 1)
+
+    def test_process_batch(self):
+        with patch("payments.services.send_tpay_batch_processing_emails") as mock_mails:
+            ba = BankAccount.objects.create(
+                owner=self.member,
+                initials="J",
+                last_name="Test1",
+                iban="NL91ABNA0417164300",
+                mandate_no="11-1",
+                valid_from=timezone.now().date() - timezone.timedelta(days=2000),
+                signature="base64,png",
+            )
+            p = services.create_payment(
+                MockPayable(self.member), self.member, Payment.TPAY
+            )
+            b = Batch.objects.create()
+            p.batch = b
+            p.save()
+
+            services.process_batch(b)
+
+            mock_mails.assert_called_once()
+            ba.refresh_from_db()
+            self.assertEqual(b.withdrawal_date.date(), ba.last_used)