email_invoice.py 7.94 KB
Newer Older
Patrick's avatar
Patrick committed
1 2
# -*- coding: utf-8
from __future__ import unicode_literals
Patrick's avatar
Patrick committed
3

4
from django.core.mail import EmailMultiAlternatives
Patrick's avatar
Patrick committed
5 6
from django.core.urlresolvers import reverse
from django.template import Template, Context as TemplateContext
7 8
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
Patrick's avatar
Patrick committed
9

10
import repanier.apps
Patrick's avatar
Patrick committed
11 12
from repanier.models import Purchase
from repanier.models import Configuration
13 14 15 16 17 18
from repanier.models import Customer
from repanier.models import Permanence
from repanier.models import Producer
from repanier.tools import *


19
def send_invoice(permanence_id):
Patrick's avatar
Patrick committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    from repanier.apps import REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER, \
        REPANIER_SETTINGS_GROUP_NAME, REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER
    cur_language = translation.get_language()
    for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
        language_code = language["code"]
        translation.activate(language_code)
        permanence = Permanence.objects.get(id=permanence_id)
        config = Configuration.objects.get(id=DECIMAL_ONE)
        sender_email, sender_function, signature, cc_email_staff = get_signature(is_reply_to_invoice_email=True)
        if REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER:
            # To the producer we speak of "payment".
            # This is the detail of the payment to the producer, i.e. received products
            for producer in Producer.objects.filter(
                    permanence=permanence.id,
                    language=language_code
            ).order_by('?'):
                long_profile_name = producer.long_profile_name \
                    if producer.long_profile_name is not None else producer.short_profile_name
                if Purchase.objects.filter(
                    permanence_id=permanence.id, producer_id=producer.id
                ).order_by('?').exists():
                    try:
                        invoice_producer_mail = config.invoice_producer_mail
                    except TranslationDoesNotExist:
                        invoice_producer_mail = EMPTY_STRING
45 46 47 48 49 50 51 52
                    invoice_producer_mail_subject = "%s - %s - %s - %s" % (
                            _('Payment'), permanence, REPANIER_SETTINGS_GROUP_NAME, long_profile_name)
                    try:
                        if config.invoice_producer_mail_subject:
                            invoice_producer_mail_subject = config.invoice_producer_mail_subject
                    except TranslationDoesNotExist:
                        pass

Patrick's avatar
Patrick committed
53
                    template = Template(invoice_producer_mail)
Patrick's avatar
Patrick committed
54 55 56 57
                    context = TemplateContext({
                        'name'             : long_profile_name,
                        'long_profile_name': long_profile_name,
                        'permanence_link'  : mark_safe(
58 59 60 61
                            '<a href="http://%s%s">%s</a>' % (settings.ALLOWED_HOSTS[0],
                                                              reverse('producer_invoice_uuid_view',
                                                                      args=(0, producer.uuid)),
                                                              permanence)),
Patrick's avatar
Patrick committed
62 63 64
                        'signature'        : mark_safe(
                            '%s<br/>%s<br/>%s' % (
                                signature, sender_function, REPANIER_SETTINGS_GROUP_NAME))
Patrick's avatar
Patrick committed
65 66
                    })
                    html_content = template.render(context)
Patrick's avatar
Patrick committed
67
                    if producer.email2:
Patrick's avatar
Patrick committed
68
                        to_email_producer = [producer.email, producer.email2]
Patrick's avatar
Patrick committed
69
                    else:
Patrick's avatar
Patrick committed
70
                        to_email_producer = [producer.email]
Patrick's avatar
Patrick committed
71
                    email = EmailMultiAlternatives(
72
                        invoice_producer_mail_subject,
Patrick's avatar
Patrick committed
73
                        strip_tags(html_content),
Patrick's avatar
Patrick committed
74 75
                        from_email=sender_email,
                        to=to_email_producer
Patrick's avatar
Patrick committed
76 77 78
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
79

Patrick's avatar
Patrick committed
80 81 82 83 84 85 86
        if REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER:
            # To the customer we speak of "invoice".
            # This is the detail of the invoice, i.e. sold products
            try:
                invoice_description = permanence.invoice_description
            except TranslationDoesNotExist:
                invoice_description = EMPTY_STRING
87

Patrick's avatar
Patrick committed
88 89 90 91 92 93 94 95 96 97
            for customer in Customer.objects.filter(
                customerinvoice__permanence=permanence.id,
                customerinvoice__customer_who_pays_id=F('customer_id'),
                represent_this_buyinggroup=False,
                language=language_code
            ).order_by('?'):
                long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
                if Purchase.objects.filter(
                    permanence_id=permanence.id, customer_who_pays_id=customer.id
                ).order_by('?').exists():
Patrick's avatar
Patrick committed
98
                    to_email_customer = [customer.user.email]
Patrick's avatar
Patrick committed
99
                    if customer.email2 is not None and len(customer.email2.strip()) > 0:
Patrick's avatar
Patrick committed
100
                        to_email_customer.append(customer.email2)
Patrick's avatar
Patrick committed
101 102 103 104
                    try:
                        invoice_customer_mail = config.invoice_customer_mail
                    except TranslationDoesNotExist:
                        invoice_customer_mail = EMPTY_STRING
105 106 107 108 109 110 111
                    invoice_customer_mail_subject = "%s - %s - %s - %s" % (_('Invoice'), permanence, REPANIER_SETTINGS_GROUP_NAME,
                                               long_basket_name)
                    try:
                        if config.invoice_customer_mail_subject:
                            invoice_customer_mail_subject = config.invoice_customer_mail_subject
                    except TranslationDoesNotExist:
                        pass
Patrick's avatar
Patrick committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                    customer_last_balance, customer_on_hold_movement, customer_payment_needed, customer_order_amount = payment_message(
                        customer, permanence)
                    template = Template(invoice_customer_mail)
                    context = TemplateContext({
                        'name'               : long_basket_name,
                        'long_basket_name'   : long_basket_name,
                        'basket_name'        : customer.short_basket_name,
                        'short_basket_name'  : customer.short_basket_name,
                        'permanence_link'    : mark_safe(
                            '<a href="http://%s%s">%s</a>' % (settings.ALLOWED_HOSTS[0],
                                                              reverse('order_view', args=(permanence.id,)),
                                                              permanence)),
                        'last_balance_link'  : mark_safe('<a href="http://%s%s">%s</a>' % (
                            settings.ALLOWED_HOSTS[0], reverse('customer_invoice_view', args=(0,)),
                            customer_last_balance)),
                        'last_balance'       : mark_safe(customer_last_balance),
                        'order_amount'       : mark_safe(customer_order_amount),
                        'payment_needed'     : mark_safe(customer_payment_needed),
                        'invoice_description': mark_safe(invoice_description),
                        'signature'          : mark_safe(
                            '%s<br/>%s<br/>%s' % (
                                signature, sender_function, REPANIER_SETTINGS_GROUP_NAME))
                    })
                    html_content = template.render(context)
                    email = EmailMultiAlternatives(
137
                        invoice_customer_mail_subject,
Patrick's avatar
Patrick committed
138
                        strip_tags(html_content),
Patrick's avatar
Patrick committed
139 140
                        from_email=sender_email,
                        to=to_email_customer
Patrick's avatar
Patrick committed
141 142 143 144
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
    translation.activate(cur_language)