From 62968d358d8ae1641630c7005c33bddc3c0bec22 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Tue, 14 Nov 2017 18:48:44 +0100 Subject: [PATCH] lingo: send new remote invoices by email (#13122) --- ...0031_regie_invoice_email_notifications_delay.py | 20 +++++++ combo/apps/lingo/models.py | 63 +++++++++++++++++++++- .../combo/invoice_email_notification_body.html | 20 +++++++ .../combo/invoice_email_notification_body.txt | 12 +++++ .../combo/invoice_email_notification_subject.txt | 4 ++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 combo/apps/lingo/migrations/0031_regie_invoice_email_notifications_delay.py create mode 100644 combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.html create mode 100644 combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.txt create mode 100644 combo/apps/lingo/templates/lingo/combo/invoice_email_notification_subject.txt diff --git a/combo/apps/lingo/migrations/0031_regie_invoice_email_notifications_delay.py b/combo/apps/lingo/migrations/0031_regie_invoice_email_notifications_delay.py new file mode 100644 index 0000000..d3e7354 --- /dev/null +++ b/combo/apps/lingo/migrations/0031_regie_invoice_email_notifications_delay.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.9 on 2018-01-09 10:36 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lingo', '0030_transaction_to_be_paid_remote_items'), + ] + + operations = [ + migrations.AddField( + model_name='regie', + name='invoice_email_notifications_delay', + field=models.IntegerField(default=10, help_text='in days', verbose_name='Delay after which email notifications will be resent'), + ), + ] diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py index 14159c5..732f142 100644 --- a/combo/apps/lingo/models.py +++ b/combo/apps/lingo/models.py @@ -34,12 +34,18 @@ from django.forms import models as model_forms, Select from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.core.exceptions import ObjectDoesNotExist, PermissionDenied +from django.core.mail import EmailMultiAlternatives +from django.core.urlresolvers import reverse from django.utils.http import urlencode +from django.contrib.auth.models import User +from django.template.loader import render_to_string + from combo.data.fields import RichTextField from combo.data.models import CellBase from combo.data.library import register_cell_class from combo.utils import NothingInCacheException, aes_hex_encrypt, requests +from combo.apps.notifications.models import Notification EXPIRED = 9999 @@ -91,6 +97,8 @@ class Regie(models.Model): text_on_success = models.TextField( verbose_name=_('Custom text displayed on success'), blank=True, null=True) + invoice_email_notifications_delay = models.IntegerField(verbose_name=_('Delay after which email notifications will be resent'), + help_text=_('in days'), default=10) def is_remote(self): return self.webservice_url != '' @@ -203,6 +211,59 @@ class Regie(models.Model): extra_fee=True, user_cancellable=False).save() + def notify_new_remote_invoices(self): + if not self.is_remote(): + return + + logger = logging.getLogger(__name__) + url = self.webservice_url + '/users/with-pending-invoices/' + response = requests.get(url, remote_service=None, cache_duration=0) + data = response.json()['data'] + if not data: + return + now = timezone.now() + for uuid, items in data.iteritems(): + try: + user = User.objects.get(username=uuid) + except User.DoesNotExist: + logger.warning('Invoices available for unknown user: %s', uuid) + continue + for invoice in items['invoices']: + invoice_id = '%s-%s' % (self.slug, invoice['id']) + notification = Notification.objects.filter(models.Q(end_timestamp__lt=now) | models.Q(acked=False), + external_id=invoice, user=user, summary=invoice['label']).last() + if notification: + continue + duration = timezone.timedelta(days=self.invoice_email_notifications_delay) + Notification.notify(user, summary=invoice['label'], id=invoice_id, start_timestamp=now, + duration=duration) + self.notify_remote_invoice_by_email(user, invoice) + + def notify_remote_invoice_by_email(self, user, invoice): + + subject_template = 'lingo/combo/invoice_email_notification_subject.txt' + text_body_template = 'lingo/combo/invoice_email_notification_body.txt' + html_body_template = 'lingo/combo/invoice_email_notification_body.html' + + remote_item = build_remote_item(invoice, self) + payment_url = reverse('view-item', kwargs={'regie_id': self.id, + 'item_crypto_id': remote_item.crypto_id}) + ctx = {'item': remote_item} + # update context with template vars in order to get things such + # portal title and url + ctx.update(settings.TEMPLATE_VARS) + ctx.update({'payment_url': urlparse.urljoin(ctx['portal_url'], payment_url)}) + subject = render_to_string([subject_template], ctx).strip() + text_body = render_to_string([text_body_template], ctx) + html_body = render_to_string([html_body_template], ctx) + message = EmailMultiAlternatives(subject, text_body, + settings.DEFAULT_FROM_EMAIL, [user.email]) + message.attach_alternative(html_body, 'text/html') + if invoice['has_pdf']: + invoice_pdf = self.get_invoice_pdf(user, invoice['id']) + message.attach('%s.pdf' % invoice['id'], invoice_pdf.content) + message.send() + class BasketItem(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True) @@ -280,7 +341,7 @@ class RemoteItem(object): self.display_id = display_id or self.id self.subject = subject self.has_pdf = has_pdf - self.online_payment = online_payment + self.online_payment = online_payment and self.amount >= regie.payment_min_amount self.paid = paid self.no_online_payment_reason = no_online_payment_reason if payment_date: diff --git a/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.html b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.html new file mode 100644 index 0000000..16bb059 --- /dev/null +++ b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.html @@ -0,0 +1,20 @@ +{% load i18n %} + + + +

{% blocktrans with id=item.id creation_date=item.creation_date|date:"DATE_FORMAT" amount=item.amount %} + We inform you that your invoice nr. {{ id }} issued on {{ creation_date }} of amount of {{ amount }}€ is available on {{ site_title }}. + {% endblocktrans %}

+ {% if item.online_payment %} +

{% blocktrans %}You can view and pay it online.{% endblocktrans %}

+ {% else %} +

{% blocktrans %}You can view it by going on your {{ site_title }}.{% endblocktrans %}

+ {% if item.no_online_payment_reason == 'autobilling' %} +

{% blocktrans with debit_date=item.payment_limit_date|date:"DATE_FORMAT" %} + The amount of this invoice will be debited from your account at {{ debit_date }}. + {% endblocktrans %} +

+ {% endif %} + {% endif %} + + diff --git a/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.txt b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.txt new file mode 100644 index 0000000..e40f191 --- /dev/null +++ b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_body.txt @@ -0,0 +1,12 @@ +{% load i18n %} +{% blocktrans with id=item.id creation_date=item.creation_date|date:"DATE_FORMAT" amount=item.amount %}We inform you that your invoice nr. {{ id }} issued on {{ creation_date }} of amount of {{ amount }}€ is available on {{ site_title }}.{% endblocktrans %} + +{% if item.online_payment %} +{% blocktrans %}You can view and pay it online on {{ payment_url }}.{% endblocktrans %} +{% else %}{% blocktrans %}You can view it by going on {{ portal_url }}.{% endblocktrans %} + +{% if item.no_online_payment_reason == 'autobilling' %} +{% blocktrans with debit_date=item.payment_limit_date|date:"DATE_FORMAT" %}The amount of this invoice will be debited from your account at {{ debit_date }}.{% endblocktrans %} +{% endif %} +{% endif %} + diff --git a/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_subject.txt b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_subject.txt new file mode 100644 index 0000000..1434cd2 --- /dev/null +++ b/combo/apps/lingo/templates/lingo/combo/invoice_email_notification_subject.txt @@ -0,0 +1,4 @@ +{% load i18n %} +{% blocktrans with invoice_id=item.id %} +{{ site_title }}: new invoice nr. {{ invoice_id }} is available +{% endblocktrans %} -- 2.15.1