Permalink
Browse files

Mail is now sent to member when invoice is marked as paid

  • Loading branch information...
1 parent 8dbd448 commit dbcc34fc594e0fef5f39ce3a5516d4a363b96229 @anttihirvonen committed Sep 22, 2012
@@ -1,8 +1,11 @@
# encoding: utf-8
from django.db import models
-from django.db.models.signals import post_save
+from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
+from django.template.loader import render_to_string
+from django.core.mail import send_mail
+from django.conf import settings
import reversion
@@ -14,11 +17,11 @@
# Create your models here.
class Invoice(models.Model):
- STATUS = Choices(("created", _(u"luotu")),
- ("sent", _(u"lähetetty")),
- ("paid", _(u"maksettu")),
- ("due", _(u"erääntynyt")),
- ("missed", _(u"välistä")))
+ STATUS = Choices(("created", _(u"luotu")), # created, not shown/sent to member
+ ("sent", _(u"lähetetty")), # member has receiver invoice
+ ("paid", _(u"maksettu")), # member has paid the invoide
+ ("due", _(u"erääntynyt")), # the invoice wasn't paid before due date
+ ("missed", _(u"välistä"))) # the invoice wasn't paid during year
PAYMENT = Choices(("cash", _(u"käteinen")), ("bank", _(u"pankki")))
@@ -27,7 +30,7 @@ class Invoice(models.Model):
status = models.CharField(_(u"tila"), choices=STATUS, default=STATUS.created, max_length=15)
# Year of the membership payment invoiced here
- for_year = models.IntegerField(_(u"kohdevuosi"))
+ for_year = models.IntegerField(_(u"kohdevuosi"), editable=False)
# Dates
created = models.DateTimeField(auto_now_add=True, verbose_name=_(u"luotu"))
@@ -41,12 +44,30 @@ class Invoice(models.Model):
reference_number = models.IntegerField(_(u"viitenumero"), blank=True, null=True, editable=False)
+ def clean(self):
+ from django.core.exceptions import ValidationError
+ if self.status == self.STATUS.paid and not (self.payment_date and self.payment_method):
+ raise ValidationError(_(u"Syötä maksupäivä ja -tapa."))
+
def save(self, *args, **kwargs):
if not self.due_date:
self.due_date = date.today() + timedelta(days=14)
+ send_paid_mail = False
+ if self.status == self.STATUS.paid:
+ previous = Invoice.objects.get(pk=self.pk)
+ if previous.status != self.STATUS.paid:
+ # status has changed – send email
+ send_paid_mail = True
+
super(Invoice, self).save(*args, **kwargs)
+ if send_paid_mail:
+ subject = _(u"Jäsenmaksusi vuodelle {0} kirjattu".format(self.for_year))
+ body = render_to_string("billing/mails/invoice_paid.txt",
+ {'invoice': self})
+ send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [self.member.email])
+
def __unicode__(self):
return u"{0}, {1}".format(self.member, self.for_year)
@@ -0,0 +1,7 @@
+Hei {{ invoice.member.first_name }},
+
+jäsenmaksusi vuodelle {{ invoice.for_year }} on nyt
+kirjattu rekisteriimme. Kiitos!
+
+--
+Digitaalisen median ammattiainekerho DOT ry

0 comments on commit dbcc34f

Please sign in to comment.