email_invoice.py 7.05 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5
# -*- coding: utf-8
from __future__ import unicode_literals
from django.core.urlresolvers import reverse
from django.template import Template, Context as djangoContext
from django.utils.safestring import mark_safe
6 7 8
from django.core.mail import EmailMultiAlternatives
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
Patrick's avatar
Set  
Patrick committed
9
from parler.models import TranslationDoesNotExist
10
from openpyxl.writer.excel import save_virtual_workbook
11
import repanier.apps
12 13 14 15 16
from repanier.models import Customer
from repanier.models import Permanence
from repanier.models import Producer
from repanier.models import Staff
from repanier.tools import *
17
from repanier.xlsx import xslx_invoice
Patrick's avatar
Patrick committed
18 19
from django.conf import settings
# from repanier.const import *
20 21


22
def send_invoice(permanence_id):
23
    permanence = Permanence.objects.get(id=permanence_id)
Patrick's avatar
Patrick committed
24 25
    sender_email, sender_function, signature, cc_email_staff = get_signature(is_reply_to_invoice_email=True)

26
    if repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER:
Patrick's avatar
Patrick committed
27 28 29 30 31 32 33 34 35 36
        # To the producer we speak of "payment".
        # This is the detail of the paiment to the producer, i.e. received products
        filename = ("%s - %s.xlsx" % (_("Payment"), permanence)).encode('ascii', errors='replace').replace('?', '_')
        producer_set = Producer.objects.filter(
            permanence=permanence_id).order_by()
        for producer in producer_set:
            if producer.email.upper().find("NO-SPAM.WS") < 0:
                long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
                wb = xslx_invoice.export(permanence=permanence, producer=producer, sheet_name=long_profile_name)
                if wb is not None:
37
                    invoice_producer_mail = repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_producer_mail
Patrick's avatar
Patrick committed
38 39
                    template = Template(invoice_producer_mail)
                    context = djangoContext({
40
                        'name': long_profile_name,
Patrick's avatar
Patrick committed
41
                        'long_profile_name' : long_profile_name,
42 43 44 45 46
                        'permanence': mark_safe(
                            '<a href="http://%s%s">%s</a>' % (settings.ALLOWED_HOSTS[0],
                                                              reverse('producer_invoice_uuid_view',
                                                                      args=(0, producer.uuid)),
                                                              permanence)),
Patrick Colmant's avatar
Patrick Colmant committed
47
                        'signature': mark_safe(
48
                            '%s<br/>%s<br/>%s' % (signature, sender_function, repanier.apps.REPANIER_SETTINGS_GROUP_NAME))
Patrick's avatar
Patrick committed
49 50 51
                    })
                    html_content = template.render(context)
                    email = EmailMultiAlternatives(
Patrick Colmant's avatar
Patrick Colmant committed
52
                        "%s - %s - %s - %s" % (
53
                        _('Payment'), permanence, repanier.apps.REPANIER_SETTINGS_GROUP_NAME, long_profile_name),
Patrick's avatar
Patrick committed
54 55 56 57 58 59 60 61 62 63
                        strip_tags(html_content),
                        sender_email,
                        [producer.email],
                        bcc=[sender_email]
                    )
                    email.attach(filename,
                                 save_virtual_workbook(wb),
                                 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                    email.attach_alternative(html_content, "text/html")
                    send_email(email=email)
64

65
    if repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER:
Patrick's avatar
Patrick committed
66 67 68 69 70 71 72 73 74 75 76 77
        # To the customer we speak of "invoice".
        # This is the detail of the invoice, i.e. sold products
        filename = ("%s - %s.xlsx" % (_("Invoice"), permanence)).encode('ascii', errors='replace').replace('?', '_')
        try:
            invoice_description = permanence.invoice_description
        except TranslationDoesNotExist:
             invoice_description = ""
        customer_set = Customer.objects.filter(
            purchase__permanence=permanence_id, represent_this_buyinggroup=False).order_by().distinct()
        for customer in customer_set:
            long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
            wb = xslx_invoice.export(permanence=permanence, customer=customer, sheet_name=long_basket_name)
Patrick's avatar
Patrick committed
78
            if wb is not None:
Patrick's avatar
Patrick committed
79 80 81
                email_customer = [customer.user.email,]
                if customer.email2 is not None and len(customer.email2) > 0:
                    email_customer.append(customer.email2)
82
                invoice_customer_mail = repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER
Patrick's avatar
Patrick committed
83 84
                template = Template(invoice_customer_mail)
                context = djangoContext({
85
                    'name': long_basket_name,
Patrick's avatar
Patrick committed
86
                    'long_basket_name' : long_basket_name,
87 88 89 90 91
                    'permanence': mark_safe(
                        '<a href="http://%s%s">%s</a>' % (settings.ALLOWED_HOSTS[0],
                                                          reverse('order_view', args=(permanence_id,)),
                                                          permanence)),
                    'invoice_description': invoice_description,
Patrick Colmant's avatar
Patrick Colmant committed
92
                    'signature': mark_safe(
93
                        '%s<br/>%s<br/>%s' % (signature, sender_function, repanier.apps.REPANIER_SETTINGS_GROUP_NAME))
Patrick's avatar
Patrick committed
94 95
                })
                html_content = template.render(context)
96
                email = EmailMultiAlternatives(
97
                    "%s - %s - %s - %s" % (_('Invoice'), permanence, repanier.apps.REPANIER_SETTINGS_GROUP_NAME, long_basket_name),
98 99
                    strip_tags(html_content),
                    sender_email,
Patrick's avatar
Patrick committed
100 101
                    email_customer,
                    bcc=[sender_email]
102 103 104 105 106
                )
                email.attach(filename,
                             save_virtual_workbook(wb),
                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                email.attach_alternative(html_content, "text/html")
Patrick's avatar
Patrick committed
107
                send_email(email=email)
108 109

    # Report to the staff
Patrick's avatar
Patrick committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    # wb = xslx_invoice.export(permanence=permanence, sheet_name=repanier_settings['GROUP_NAME'])
    # if wb is not None:
    #     html_content = '%s,<br/><br/>%s %s %s.<br/>%s<br/><br/>%s<br/>%s<br/>%s' \
    #         % (_('Dear staff member'),
    #            _('The invoices of'), permanence, _("are now available in attachment"),
    #            invoice_description,
    #            signature,
    #            sender_function,
    #            repanier_settings['GROUP_NAME']
    #     )
    #     email = EmailMultiAlternatives(
    #         "%s - %s - %s" % (_('Invoice'), permanence, repanier_settings['GROUP_NAME']),
    #         strip_tags(html_content),
    #         sender_email,
    #         cc_email_staff
    #     )
    #     email.attach(filename,
    #                  save_virtual_workbook(wb),
    #                  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    #     email.attach_alternative(html_content, "text/html")
    #     send_email(email=email)