email_invoice.py 7.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 45
# -*- coding: utf-8 -*-
from repanier.const import *
from django.conf import settings
from django.core import urlresolvers
from django.core.mail import EmailMultiAlternatives
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from openpyxl.writer.excel import save_virtual_workbook
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


def send(permanence_id, current_site_name):
    permanence = Permanence.objects.get(id=permanence_id)
    sender_email = settings.DEFAULT_FROM_EMAIL
    sender_function = ""
    signature = ""
    cc_email_staff = []
    for staff in Staff.objects.filter(is_active=True, is_external_group=False):
        cc_email_staff.append(staff.user.email)
        if staff.is_reply_to_invoice_email:
            sender_email = staff.user.username + '@repanier.be'
            sender_function = staff.long_name
            r = staff.customer_responsible
            if r:
                if r.long_basket_name:
                    signature = r.long_basket_name + " - " + r.phone1
                else:
                    signature = r.short_basket_name + " - " + r.phone1
                if r.phone2:
                    signature += " / " + r.phone2

    # To the producer we speak of "payment".
    # This is the detail of the paiment to the producer, i.e. received products
    filename = (unicode(_("Payment")) + u" - " + permanence.__unicode__() + u'.xlsx').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:
Patrick's avatar
Patrick committed
46
            long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
47
            wb = xslx_invoice.export(permanence=permanence, producer=producer, wb=None, sheet_name=long_profile_name)
Patrick's avatar
Patrick committed
48
            if wb is not None:
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                invoices_url = 'http://' + settings.ALLOWED_HOSTS[0] + urlresolvers.reverse(
                    'invoicep_uuid_view',
                    args=(0, producer.uuid )
                )
                html_content = unicode(_('Dear')) + " " + long_profile_name + ",<br/><br/>" + unicode(
                    _('In attachment, you will find the detail of our payment for the')) + \
                               ' <a href="' + invoices_url + '">' + unicode(permanence) + \
                               "</a>.<br/><br/>" + unicode(
                    _('In case of discordance, please advertise the staff team :')) + \
                               "<br/><br/>" + signature + \
                               "<br/>" + sender_function + \
                               "<br/>" + current_site_name
                email = EmailMultiAlternatives(
                    unicode(_('Payment')) + " - " + unicode(
                        permanence) + " - " + current_site_name + " - " + long_profile_name,
                    strip_tags(html_content),
                    sender_email,
                    [producer.email],
                    cc=[sender_email]
                )
                email.attach(filename,
                             save_virtual_workbook(wb),
                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                email.attach_alternative(html_content, "text/html")
                if not settings.DEBUG:
                    email.send()
                else:
                    email.to = [v for k, v in settings.ADMINS]
                    email.cc = []
                    email.bcc = []
                    email.send()

    # To the customer we speak of "invoice".
    # This is the detail of the invoice, i.e. sold products
    filename = (unicode(_("Invoice")) + u" - " + permanence.__unicode__() + u'.xlsx').encode('ascii',
                                                                                             errors='replace').replace(
        '?', '_')
    customer_set = Customer.objects.filter(
        purchase__permanence=permanence_id, represent_this_buyinggroup=False).order_by().distinct()
    for customer in customer_set:
Patrick's avatar
Patrick committed
89
        long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
90
        wb = xslx_invoice.export(permanence=permanence, customer=customer, wb=None, sheet_name=long_basket_name)
Patrick's avatar
Patrick committed
91
        if wb is not None:
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
            html_content = unicode(_('Dear')) + " " + long_basket_name + ",<br/><br/>" + unicode(_('Your invoice of')) + \
                           " " + unicode(permanence) + " " + unicode(
                _("is now available in attachment")) + ".<br/>" + permanence.invoice_description + \
                           "<br/><br/>" + signature + \
                           "<br/>" + sender_function + \
                           "<br/>" + current_site_name
            email = EmailMultiAlternatives(
                unicode(_('Invoice')) + " - " + unicode(
                    permanence) + " - " + current_site_name + " - " + long_basket_name,
                strip_tags(html_content),
                sender_email,
                [customer.user.email]
            )
            email.attach(filename,
                         save_virtual_workbook(wb),
                         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
            email.attach_alternative(html_content, "text/html")
            if not settings.DEBUG:
                email.send()
            else:
                email.to = [v for k, v in settings.ADMINS]
                email.cc = []
                email.bcc = []
                email.send()

    # Report to the staff
    wb = xslx_invoice.export(permanence=permanence, wb=None, sheet_name=current_site_name)
Patrick's avatar
Patrick committed
119
    if wb is not None:
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        html_content = unicode(_('Dear staff member')) + ",<br/><br/>" + unicode(_('The invoices of')) + \
                       " " + unicode(permanence) + " " + unicode(
            _("are now available in attachment")) + ".<br/>" + permanence.invoice_description + \
                       "<br/><br/>" + signature + \
                       "<br/>" + sender_function + \
                       "<br/>" + current_site_name
        email = EmailMultiAlternatives(
            unicode(_('Invoice')) + " - " + unicode(permanence) + " - " + current_site_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")
        if not settings.DEBUG:
            email.send()
        else:
            email.to = [v for k, v in settings.ADMINS]
            email.cc = []
            email.bcc = []
            email.send()