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

Patrick's avatar
Patrick committed
73 74 75 76 77 78 79
        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
80

Patrick's avatar
Patrick committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 126 127 128 129 130 131
            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():
                    email_customer = [customer.user.email, ]
                    if customer.email2 is not None and len(customer.email2.strip()) > 0:
                        email_customer.append(customer.email2)
                    try:
                        invoice_customer_mail = config.invoice_customer_mail
                    except TranslationDoesNotExist:
                        invoice_customer_mail = EMPTY_STRING
                    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(
                        "%s - %s - %s - %s" % (_('Invoice'), permanence, REPANIER_SETTINGS_GROUP_NAME,
                                               long_basket_name),
                        strip_tags(html_content),
                        sender_email,
                        email_customer
                    )
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
    translation.activate(cur_language)