2 from django.db import models
3 from django.utils.timezone import now
4 from .bank import parse_export_feedback, parse_payment_feedback
7 class Campaign(models.Model):
8 name = models.CharField('nazwa', max_length=255, unique=True)
9 description = models.TextField('opis', blank=True)
12 verbose_name = 'kampania'
13 verbose_name_plural = 'kampanie'
19 class Fundraiser(models.Model):
20 name = models.CharField('imię i nazwisko', max_length=255, unique=True)
23 verbose_name = 'fundraiser'
24 verbose_name_plural = 'fundraiserki i fundraiserzy'
30 class DirectDebit(models.Model):
31 first_name = models.CharField('imię', max_length=255, blank=True)
32 last_name = models.CharField('nazwisko', max_length=255, blank=True)
33 sex = models.CharField('płeć', max_length=1, blank=True, choices=[
37 date_of_birth = models.DateField('data urodzenia', null=True, blank=True)
38 street = models.CharField('ulica', max_length=255, blank=True)
39 building = models.CharField('nr domu', max_length=255, blank=True)
40 flat = models.CharField('nr mieszkania', max_length=255, blank=True)
41 town = models.CharField('miejscowość', max_length=255, blank=True)
42 postal_code = models.CharField('kod pocztowy', max_length=255, blank=True)
43 phone = models.CharField('telefon', max_length=255, blank=True)
44 email = models.CharField('e-mail', max_length=255, blank=True)
45 iban = models.CharField('nr rachunku', max_length=255, blank=True)
46 iban_valid = models.BooleanField('prawidłowy IBAN', default=False, null=True)
47 is_consumer = models.BooleanField('konsument', default=True)
48 payment_id = models.CharField('identyfikator płatności', max_length=255, blank=True, unique=True)
49 agree_fundraising = models.BooleanField('zgoda na kontakt fundraisingowy', default=False)
50 agree_newsletter = models.BooleanField('zgoda na newsletter', default=False)
52 acquisition_date = models.DateField(
53 'data pozyskania', help_text='Data z formularza',
54 null=True, blank=True)
55 submission_date = models.DateField(
56 'data dostarczenia', null=True, blank=True,
57 help_text='Data złożenia formularza przez fundraisera')
58 bank_submission_date = models.DateField(
59 'data złożenia do banku', null=True, blank=True,
60 help_text='Data przesłania danych z formularza do banku')
61 bank_acceptance_date = models.DateField(
62 'data akceptacji przez bank', null=True, blank=True,
63 help_text='Data kiedy bank przekazał informację o akceptacji danych z formularza')
65 fundraiser = models.ForeignKey(Fundraiser, models.PROTECT, blank=True, null=True, verbose_name='fundraiser')
66 fundraiser_commission = models.IntegerField('prowizja fundraisera', null=True, blank=True)
67 fundraiser_bonus = models.IntegerField('bonus fundraisera', null=True, blank=True)
68 fundraiser_bill = models.CharField('nr rachunku wystawionego przez fundraisera', max_length=255, blank=True)
70 amount = models.IntegerField('kwota', null=True, blank=True)
72 notes = models.TextField('uwagi', blank=True)
74 needs_redo = models.BooleanField('do powtórki', default=False)
75 cancelled_at = models.DateTimeField('anulowane', null=True, blank=True)
76 optout = models.BooleanField('optout', default=False)
78 campaign = models.ForeignKey(Campaign, models.PROTECT, null=True, blank=True, verbose_name='kampania')
80 latest_status = models.CharField(max_length=255, blank=True)
83 verbose_name = 'polecenie zapłaty'
84 verbose_name_plural = 'polecenia zapłaty'
87 return "{} {}".format(self.payment_id, self.latest_status)
89 def get_latest_status(self):
90 line = self.bankexportfeedbackline_set.order_by('-feedback__created_at').first()
91 if line is None: return ""
94 def save(self, **kwargs):
95 self.iban_valid = not self.iban_warning() if self.iban else None
96 self.latest_status = self.get_latest_status()
97 super().save(**kwargs)
100 def get_next_payment_id(cls):
101 # Find the last object added.
102 last = cls.objects.order_by('-id').first()
105 match = re.match(r'^(.*?)(\d+)$', last.payment_id)
108 prefix = match.group(1)
109 number = int(match.group(2))
110 number_length = len(match.group(2))
113 payment_id = f'{prefix}{number:0{number_length}}'
114 if not cls.objects.filter(payment_id=payment_id).exists():
120 return ' '.join((self.first_name, self.last_name)).strip()
123 def street_address(self):
124 street_addr = self.street
126 street_addr += ' ' + self.building
128 street_addr += ' m. ' + self.flat
129 street_addr = street_addr.strip()
132 def iban_warning(self):
135 if len(self.iban) != 26:
136 return 'Bad IBAN length'
137 if int(self.iban[2:] + '2521' + self.iban[:2]) % 97 != 1:
138 return 'This IBAN number looks invalid'
140 iban_warning.short_description = ''
143 class BankExportFeedback(models.Model):
144 created_at = models.DateTimeField(auto_now_add=True)
145 csv = models.FileField(upload_to='pz/feedback/')
147 def save(self, **kwargs):
148 super().save(**kwargs)
150 self.save_payment_items()
151 except AssertionError:
152 self.save_export_feedback_items()
154 def save_payment_items(self):
155 for payment_id, booking_date, is_dd, realised, reject_code in parse_payment_feedback(self.csv.open()):
156 debit = DirectDebit.objects.get(payment_id = payment_id)
157 b, created = self.payment_set.get_or_create(
160 'booking_date': booking_date,
162 'realised': realised,
163 'reject_code': reject_code,
167 b.booking_date = booking_date
169 b.realised = realised
170 b.reject_code = reject_code
173 def save_export_feedback_items(self):
174 for payment_id, status, comment in parse_export_feedback(self.csv.open()):
175 debit = DirectDebit.objects.get(payment_id = payment_id)
176 b, created = self.bankexportfeedbackline_set.get_or_create(
187 if status == 1 and not debit.bank_acceptance_date:
188 debit.bank_acceptance_date = now().date()
192 class BankExportFeedbackLine(models.Model):
193 feedback = models.ForeignKey(BankExportFeedback, models.CASCADE)
194 debit = models.ForeignKey(DirectDebit, models.CASCADE)
195 status = models.SmallIntegerField()
196 comment = models.CharField(max_length=255)
199 class Payment(models.Model):
200 feedback = models.ForeignKey(BankExportFeedback, models.CASCADE)
201 debit = models.ForeignKey(DirectDebit, models.CASCADE)
202 booking_date = models.DateField()
203 is_dd = models.BooleanField()
204 realised = models.BooleanField()
205 reject_code = models.CharField(max_length=128, blank=True)
209 class BankOrder(models.Model):
210 payment_date = models.DateField(null=True, blank=True)
211 sent = models.DateTimeField(null=True, blank=True)
212 debits = models.ManyToManyField(DirectDebit, blank=True)