email_invoice.py 7.37 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
    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():
Patrick's avatar
Patrick committed
41 42 43
                    invoice_producer_mail = config.safe_translation_getter(
                        'invoice_producer_mail', any_language=True, default=EMPTY_STRING
                    )
44
                    invoice_producer_mail_subject = "%s - %s" % (REPANIER_SETTINGS_GROUP_NAME, permanence)
45

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

Patrick's avatar
Patrick committed
76 77 78
        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
Patrick's avatar
Patrick committed
79 80 81
            invoice_description = permanence.safe_translation_getter(
                'invoice_description', any_language=True, default=EMPTY_STRING
            )
Patrick's avatar
Patrick committed
82 83
            for customer in Customer.objects.filter(
                customerinvoice__permanence=permanence.id,
Patrick's avatar
Patrick committed
84
                customerinvoice__customer_charged_id=F('customer_id'),
Patrick's avatar
Patrick committed
85 86 87 88 89
                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(
Patrick's avatar
WIP  
Patrick committed
90 91
                    permanence_id=permanence.id,
                    customer_invoice__customer_charged_id=customer.id
Patrick's avatar
Patrick committed
92
                ).order_by('?').exists():
Patrick's avatar
Patrick committed
93
                    to_email_customer = [customer.user.email]
Patrick's avatar
Patrick committed
94
                    if customer.email2 is not None and len(customer.email2.strip()) > 0:
Patrick's avatar
Patrick committed
95
                        to_email_customer.append(customer.email2)
Patrick's avatar
Patrick committed
96 97 98 99

                    invoice_customer_mail = config.safe_translation_getter(
                        'invoice_customer_mail', any_language=True, default=EMPTY_STRING
                    )
100
                    invoice_customer_mail_subject = "%s - %s" % (REPANIER_SETTINGS_GROUP_NAME, permanence)
Patrick's avatar
Patrick committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    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(
126
                        invoice_customer_mail_subject,
Patrick's avatar
Patrick committed
127
                        strip_tags(html_content),
Patrick's avatar
Patrick committed
128 129
                        from_email=sender_email,
                        to=to_email_customer
Patrick's avatar
Patrick committed
130 131 132 133
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
    translation.activate(cur_language)