email_invoice.py 6.51 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
Patrick Colmant's avatar
Patrick Colmant committed
11
from repanier.apps import RepanierSettings
12 13 14 15 16 17
from repanier.models import Customer
from repanier.models import Permanence
from repanier.models import Producer
from repanier.models import Staff
from repanier.tools import *
from repanier.xslx import xslx_invoice
Patrick's avatar
Patrick committed
18 19
from django.conf import settings
# from repanier.const import *
20 21


Patrick's avatar
Patrick committed
22
def send(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)

Patrick Colmant's avatar
Patrick Colmant committed
26
    if RepanierSettings.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:
Patrick Colmant's avatar
Patrick Colmant committed
37
                    invoice_producer_mail = RepanierSettings.config.invoice_producer_mail
Patrick's avatar
Patrick committed
38 39 40 41
                    template = Template(invoice_producer_mail)
                    context = djangoContext({
                        'long_profile_name' : long_profile_name,
                        '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
42 43
                        'signature': mark_safe(
                            '%s<br/>%s<br/>%s' % (signature, sender_function, RepanierSettings.group_name))
Patrick's avatar
Patrick committed
44 45 46
                    })
                    html_content = template.render(context)
                    email = EmailMultiAlternatives(
Patrick Colmant's avatar
Patrick Colmant committed
47 48
                        "%s - %s - %s - %s" % (
                        _('Payment'), permanence, RepanierSettings.group_name, long_profile_name),
Patrick's avatar
Patrick committed
49 50 51 52 53 54 55 56 57 58
                        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)
59

Patrick Colmant's avatar
Patrick Colmant committed
60
    if RepanierSettings.send_invoice_mail_to_customer:
Patrick's avatar
Patrick committed
61 62 63 64 65 66 67 68 69 70 71 72
        # 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
73
            if wb is not None:
Patrick's avatar
Patrick committed
74 75 76
                email_customer = [customer.user.email,]
                if customer.email2 is not None and len(customer.email2) > 0:
                    email_customer.append(customer.email2)
Patrick Colmant's avatar
Patrick Colmant committed
77
                invoice_customer_mail = RepanierSettings.config.invoice_customer_mail
Patrick's avatar
Patrick committed
78 79 80 81 82
                template = Template(invoice_customer_mail)
                context = djangoContext({
                    'long_basket_name' : long_basket_name,
                    'permanence': mark_safe('<a href="http://%s%s">%s</a>' % (settings.ALLOWED_HOSTS[0], reverse('producer_invoice_uuid_view', args=(0, producer.uuid)), permanence)),
                    'invoice_description' : invoice_description,
Patrick Colmant's avatar
Patrick Colmant committed
83 84
                    'signature': mark_safe(
                        '%s<br/>%s<br/>%s' % (signature, sender_function, RepanierSettings.group_name))
Patrick's avatar
Patrick committed
85 86
                })
                html_content = template.render(context)
87
                email = EmailMultiAlternatives(
Patrick Colmant's avatar
Patrick Colmant committed
88
                    "%s - %s - %s - %s" % (_('Invoice'), permanence, RepanierSettings.group_name, long_basket_name),
89 90
                    strip_tags(html_content),
                    sender_email,
Patrick's avatar
Patrick committed
91 92
                    email_customer,
                    bcc=[sender_email]
93 94 95 96 97
                )
                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
98
                send_email(email=email)
99 100

    # Report to the staff
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
    # 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)