2 from django.db import models
3 from django.utils.timezone import now
4 from django.utils.translation import ugettext_lazy as _
5 from .bank import parse_export_feedback
8 class Campaign(models.Model):
9 name = models.CharField(_('name'), max_length=255, unique=True)
10 description = models.TextField(_('description'), blank=True)
13 verbose_name = _('campaign')
14 verbose_name_plural = _('campaigns')
20 class Fundraiser(models.Model):
21 name = models.CharField(_('name'), max_length=255, unique=True)
24 verbose_name = _('fundraiser')
25 verbose_name_plural = _('fundraisers')
31 class DirectDebit(models.Model):
32 first_name = models.CharField(_('first name'), max_length=255, blank=True)
33 last_name = models.CharField(_('last name'), max_length=255, blank=True)
34 sex = models.CharField(_('sex'), max_length=1, blank=True, choices=[
38 date_of_birth = models.DateField(_('date of birth'), null=True, blank=True)
39 street = models.CharField(_('street'), max_length=255, blank=True)
40 building = models.CharField(_('building'), max_length=255, blank=True)
41 flat = models.CharField(_('flat'), max_length=255, blank=True)
42 town = models.CharField(_('town'), max_length=255, blank=True)
43 postal_code = models.CharField(_('postal code'), max_length=255, blank=True)
44 phone = models.CharField(_('phone'), max_length=255, blank=True)
45 email = models.CharField(_('e-mail'), max_length=255, blank=True)
46 iban = models.CharField(_('IBAN'), max_length=255, blank=True)
47 iban_valid = models.NullBooleanField(_('IBAN valid'), default=False)
48 is_consumer = models.BooleanField(_('is a consumer'), default=True)
49 payment_id = models.CharField(_('payment identifier'), max_length=255, blank=True, unique=True)
50 agree_fundraising = models.BooleanField(_('agree fundraising'), default=False)
51 agree_newsletter = models.BooleanField(_('agree newsletter'), default=False)
53 acquisition_date = models.DateField(_('acquisition date'), help_text=_('Date from the form'), null=True, blank=True)
54 submission_date = models.DateField(_('submission date'), null=True, blank=True, help_text=_('Date the fundaiser submitted the form'))
55 bank_submission_date = models.DateField(_('bank submission date'), null=True, blank=True, help_text=_('Date when the form data is submitted to the bank'))
56 bank_acceptance_date = models.DateField(_('bank accepted date'), null=True, blank=True, help_text=_('Date when bank accepted the form'))
58 fundraiser = models.ForeignKey(Fundraiser, models.PROTECT, blank=True, null=True, verbose_name=_('fundraiser'))
59 fundraiser_commission = models.IntegerField(_('fundraiser commission'), null=True, blank=True)
60 fundraiser_bill = models.CharField(_('fundaiser bill number'), max_length=255, blank=True)
62 amount = models.IntegerField(_('amount'), null=True, blank=True)
64 notes = models.TextField(_('notes'), blank=True)
66 needs_redo = models.BooleanField(_('needs redo'), default=False)
67 cancelled_at = models.DateTimeField(_('cancelled at'), null=True, blank=True)
68 optout = models.BooleanField(_('optout'), default=False)
70 campaign = models.ForeignKey(Campaign, models.PROTECT, null=True, blank=True, verbose_name=_('campaign'))
73 verbose_name = _('direct debit')
74 verbose_name_plural = _('direct debits')
77 return self.payment_id
79 def save(self, **kwargs):
80 self.iban_valid = not self.iban_warning() if self.iban else None
81 super().save(**kwargs)
84 def get_next_payment_id(cls):
85 # Find the last object added.
86 last = cls.objects.order_by('-id').first()
89 match = re.match(r'^(.*?)(\d+)$', last.payment_id)
92 prefix = match.group(1)
93 number = int(match.group(2))
94 number_length = len(match.group(2))
97 payment_id = f'{prefix}{number:0{number_length}}'
98 if not cls.objects.filter(payment_id=payment_id).exists():
104 return ' '.join((self.first_name, self.last_name)).strip()
107 def street_address(self):
108 street_addr = self.street
110 street_addr += ' ' + self.building
112 street_addr += ' m. ' + self.flat
113 street_addr = street_addr.strip()
116 def iban_warning(self):
119 if len(self.iban) != 26:
120 return 'Bad IBAN length'
121 if int(self.iban[2:] + '2521' + self.iban[:2]) % 97 != 1:
122 return 'This IBAN number looks invalid'
124 iban_warning.short_description = ''
127 class BankExportFeedback(models.Model):
128 created_at = models.DateTimeField(auto_now_add=True)
129 csv = models.FileField(upload_to='pz/feedback/')
131 def save(self, **kwargs):
132 super().save(**kwargs)
133 for payment_id, status, comment in parse_export_feedback(self.csv):
134 debit = DirectDebit.objects.get(payment_id = payment_id)
135 b, created = self.bankexportfeedbackline_set.get_or_create(
146 if status == 1 and not debit.bank_acceptance_date:
147 debit.bank_acceptance_date = now().date()
151 class BankExportFeedbackLine(models.Model):
152 feedback = models.ForeignKey(BankExportFeedback, models.CASCADE)
153 debit = models.ForeignKey(DirectDebit, models.CASCADE)
154 status = models.SmallIntegerField()
155 comment = models.CharField(max_length=255)
159 class BankOrder(models.Model):
160 payment_date = models.DateField(null=True, blank=True)
161 sent = models.DateTimeField(null=True, blank=True)
162 debits = models.ManyToManyField(DirectDebit, blank=True)