email_invoice.py 7.78 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
                    # invoice_producer_mail_subject = "%s - %s - %s - %s" % (
                    #         _('Payment'), permanence, REPANIER_SETTINGS_GROUP_NAME, long_profile_name)
                    invoice_producer_mail_subject = "%s - %s" % (REPANIER_SETTINGS_GROUP_NAME, permanence)
48

Patrick's avatar
Patrick committed
49
                    template = Template(invoice_producer_mail)
Patrick's avatar
Patrick committed
50 51 52 53
                    context = TemplateContext({
                        'name'             : long_profile_name,
                        'long_profile_name': long_profile_name,
                        'permanence_link'  : mark_safe(
54 55 56 57
                            '<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
58 59 60
                        'signature'        : mark_safe(
                            '%s<br/>%s<br/>%s' % (
                                signature, sender_function, REPANIER_SETTINGS_GROUP_NAME))
Patrick's avatar
Patrick committed
61 62
                    })
                    html_content = template.render(context)
63 64 65
                    to_email_producer = []
                    if producer.email:
                        to_email_producer.append(producer.email)
Patrick's avatar
Patrick committed
66
                    if producer.email2:
67 68 69
                        to_email_producer.append(producer.email2)
                    if producer.email3:
                        to_email_producer.append(producer.email3)
Patrick's avatar
Patrick committed
70
                    email = EmailMultiAlternatives(
71
                        invoice_producer_mail_subject,
Patrick's avatar
Patrick committed
72
                        strip_tags(html_content),
Patrick's avatar
Patrick committed
73 74
                        from_email=sender_email,
                        to=to_email_producer
Patrick's avatar
Patrick committed
75 76 77
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
78

Patrick's avatar
Patrick committed
79 80 81 82 83 84 85
        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
86

Patrick's avatar
Patrick committed
87 88 89 90 91 92 93 94 95 96
            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
97
                    to_email_customer = [customer.user.email]
Patrick's avatar
Patrick committed
98
                    if customer.email2 is not None and len(customer.email2.strip()) > 0:
Patrick's avatar
Patrick committed
99
                        to_email_customer.append(customer.email2)
Patrick's avatar
Patrick committed
100 101 102 103
                    try:
                        invoice_customer_mail = config.invoice_customer_mail
                    except TranslationDoesNotExist:
                        invoice_customer_mail = EMPTY_STRING
104 105 106
                    # invoice_customer_mail_subject = "%s - %s - %s - %s" % (_('Invoice'), permanence, REPANIER_SETTINGS_GROUP_NAME,
                    #                            long_basket_name)
                    invoice_customer_mail_subject = "%s - %s" % (REPANIER_SETTINGS_GROUP_NAME, permanence)
Patrick's avatar
Patrick committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                    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(
132
                        invoice_customer_mail_subject,
Patrick's avatar
Patrick committed
133
                        strip_tags(html_content),
Patrick's avatar
Patrick committed
134 135
                        from_email=sender_email,
                        to=to_email_customer
Patrick's avatar
Patrick committed
136 137 138 139
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
    translation.activate(cur_language)