X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/5fb53c16b8de6efee032dea734dd9047f33d3d54..243ac5a586da9e5abb87698306794b6cc173ded1:/src/pz/models.py?ds=sidebyside diff --git a/src/pz/models.py b/src/pz/models.py index 1a77217b9..4bd3673d6 100644 --- a/src/pz/models.py +++ b/src/pz/models.py @@ -1,7 +1,8 @@ +import re from django.db import models from django.utils.timezone import now -from django.utils.translation import ugettext_lazy as _ -from .bank import parse_export_feedback +from django.utils.translation import gettext_lazy as _ +from .bank import parse_export_feedback, parse_payment_feedback class Campaign(models.Model): @@ -43,7 +44,7 @@ class DirectDebit(models.Model): phone = models.CharField(_('phone'), max_length=255, blank=True) email = models.CharField(_('e-mail'), max_length=255, blank=True) iban = models.CharField(_('IBAN'), max_length=255, blank=True) - iban_valid = models.NullBooleanField(_('IBAN valid'), default=False) + iban_valid = models.BooleanField(_('IBAN valid'), default=False, null=True) is_consumer = models.BooleanField(_('is a consumer'), default=True) payment_id = models.CharField(_('payment identifier'), max_length=255, blank=True, unique=True) agree_fundraising = models.BooleanField(_('agree fundraising'), default=False) @@ -56,6 +57,7 @@ class DirectDebit(models.Model): fundraiser = models.ForeignKey(Fundraiser, models.PROTECT, blank=True, null=True, verbose_name=_('fundraiser')) fundraiser_commission = models.IntegerField(_('fundraiser commission'), null=True, blank=True) + fundraiser_bonus = models.IntegerField(_('fundraiser bonus'), null=True, blank=True) fundraiser_bill = models.CharField(_('fundaiser bill number'), max_length=255, blank=True) amount = models.IntegerField(_('amount'), null=True, blank=True) @@ -63,20 +65,28 @@ class DirectDebit(models.Model): notes = models.TextField(_('notes'), blank=True) needs_redo = models.BooleanField(_('needs redo'), default=False) - is_cancelled = models.BooleanField(_('is cancelled'), default=False) + cancelled_at = models.DateTimeField(_('cancelled at'), null=True, blank=True) optout = models.BooleanField(_('optout'), default=False) campaign = models.ForeignKey(Campaign, models.PROTECT, null=True, blank=True, verbose_name=_('campaign')) + latest_status = models.CharField(max_length=255, blank=True) + class Meta: verbose_name = _('direct debit') verbose_name_plural = _('direct debits') def __str__(self): - return self.payment_id + return "{} {}".format(self.payment_id, self.latest_status) + + def get_latest_status(self): + line = self.bankexportfeedbackline_set.order_by('-feedback__created_at').first() + if line is None: return "" + return line.comment def save(self, **kwargs): self.iban_valid = not self.iban_warning() if self.iban else None + self.latest_status = self.get_latest_status() super().save(**kwargs) @classmethod @@ -129,7 +139,32 @@ class BankExportFeedback(models.Model): def save(self, **kwargs): super().save(**kwargs) - for payment_id, status, comment in parse_export_feedback(self.csv): + try: + self.save_payment_items() + except AssertionError: + self.save_export_feedback_items() + + def save_payment_items(self): + for payment_id, booking_date, is_dd, realised, reject_code in parse_payment_feedback(self.csv.open()): + debit = DirectDebit.objects.get(payment_id = payment_id) + b, created = self.payment_set.get_or_create( + debit=debit, + defaults={ + 'booking_date': booking_date, + 'is_dd': is_dd, + 'realised': realised, + 'reject_code': reject_code, + } + ) + if not created: + b.booking_date = booking_date + b.is_dd = is_dd + b.realised = realised + b.reject_code = reject_code + b.save() + + def save_export_feedback_items(self): + for payment_id, status, comment in parse_export_feedback(self.csv.open()): debit = DirectDebit.objects.get(payment_id = payment_id) b, created = self.bankexportfeedbackline_set.get_or_create( debit=debit, @@ -144,7 +179,7 @@ class BankExportFeedback(models.Model): b.save() if status == 1 and not debit.bank_acceptance_date: debit.bank_acceptance_date = now().date() - debit.save() + debit.save() class BankExportFeedbackLine(models.Model): @@ -154,6 +189,15 @@ class BankExportFeedbackLine(models.Model): comment = models.CharField(max_length=255) +class Payment(models.Model): + feedback = models.ForeignKey(BankExportFeedback, models.CASCADE) + debit = models.ForeignKey(DirectDebit, models.CASCADE) + booking_date = models.DateField() + is_dd = models.BooleanField() + realised = models.BooleanField() + reject_code = models.CharField(max_length=128, blank=True) + + class BankOrder(models.Model): payment_date = models.DateField(null=True, blank=True)