2018-07-07 22:01:06 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Hugo Levy-Falk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2018-07-22 22:13:25 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.template.loader import get_template
|
|
|
|
from django.core.mail import EmailMessage
|
2020-04-19 18:06:34 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.contrib import messages
|
|
|
|
from smtplib import SMTPException
|
2018-07-22 22:13:25 +00:00
|
|
|
|
|
|
|
from .tex import create_pdf
|
2019-11-04 16:55:03 +00:00
|
|
|
from preferences.models import AssoOption, GeneralOption, CotisationsOption, Mandate
|
2018-07-22 22:13:25 +00:00
|
|
|
from re2o.settings import LOGO_PATH
|
|
|
|
from re2o import settings
|
|
|
|
|
2018-07-07 22:01:06 +00:00
|
|
|
|
2018-07-02 20:14:51 +00:00
|
|
|
def find_payment_method(payment):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""Finds the payment method associated to the payment if it exists."""
|
2018-07-02 20:14:51 +00:00
|
|
|
from cotisations.payment_methods import PAYMENT_METHODS
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-07-02 20:14:51 +00:00
|
|
|
for method in PAYMENT_METHODS:
|
|
|
|
try:
|
|
|
|
o = method.PaymentMethod.objects.get(payment=payment)
|
|
|
|
return o
|
|
|
|
except method.PaymentMethod.DoesNotExist:
|
|
|
|
pass
|
|
|
|
return None
|
2018-07-22 22:13:25 +00:00
|
|
|
|
|
|
|
|
2020-04-19 18:06:34 +00:00
|
|
|
def send_mail(mail, request):
|
|
|
|
"""Wrapper for Django's EmailMessage.send which handles errors"""
|
|
|
|
try:
|
|
|
|
mail.send()
|
2020-04-19 19:13:31 +00:00
|
|
|
except (SMTPException, ConnectionError) as e:
|
2020-04-19 18:06:34 +00:00
|
|
|
if request:
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("Failed to send email: %(error)s.") % {
|
|
|
|
"error": e,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def send_mail_invoice(invoice, request=None):
|
2018-07-22 22:13:25 +00:00
|
|
|
"""Creates the pdf of the invoice and sends it by email to the client"""
|
|
|
|
purchases_info = []
|
|
|
|
for purchase in invoice.vente_set.all():
|
2019-11-04 16:55:03 +00:00
|
|
|
purchases_info.append(
|
|
|
|
{
|
|
|
|
"name": purchase.name,
|
|
|
|
"price": purchase.prix,
|
|
|
|
"quantity": purchase.number,
|
|
|
|
"total_price": purchase.prix_total,
|
|
|
|
}
|
|
|
|
)
|
2018-07-22 22:13:25 +00:00
|
|
|
|
|
|
|
ctx = {
|
2019-11-04 16:55:03 +00:00
|
|
|
"paid": True,
|
|
|
|
"fid": invoice.id,
|
|
|
|
"DATE": invoice.date,
|
|
|
|
"recipient_name": "{} {}".format(invoice.user.name, invoice.user.surname),
|
|
|
|
"address": invoice.user.room,
|
|
|
|
"article": purchases_info,
|
|
|
|
"total": invoice.prix_total(),
|
|
|
|
"asso_name": AssoOption.get_cached_value("name"),
|
|
|
|
"line1": AssoOption.get_cached_value("adresse1"),
|
|
|
|
"line2": AssoOption.get_cached_value("adresse2"),
|
|
|
|
"siret": AssoOption.get_cached_value("siret"),
|
|
|
|
"email": AssoOption.get_cached_value("contact"),
|
|
|
|
"phone": AssoOption.get_cached_value("telephone"),
|
|
|
|
"tpl_path": os.path.join(settings.BASE_DIR, LOGO_PATH),
|
2019-01-05 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 20:09:32 +00:00
|
|
|
template = CotisationsOption.get_cached_value("invoice_template").template.name.split("/")[-1]
|
|
|
|
pdf = create_pdf(template, ctx)
|
2019-11-04 16:55:03 +00:00
|
|
|
template = get_template("cotisations/email_invoice")
|
2019-01-05 18:45:21 +00:00
|
|
|
|
|
|
|
ctx = {
|
2019-11-04 16:55:03 +00:00
|
|
|
"name": "{} {}".format(invoice.user.name, invoice.user.surname),
|
|
|
|
"contact_mail": AssoOption.get_cached_value("contact"),
|
|
|
|
"asso_name": AssoOption.get_cached_value("name"),
|
2019-01-05 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mail = EmailMessage(
|
2019-11-04 16:55:03 +00:00
|
|
|
"Votre facture / Your invoice",
|
2019-01-05 18:45:21 +00:00
|
|
|
template.render(ctx),
|
2019-11-04 16:55:03 +00:00
|
|
|
GeneralOption.get_cached_value("email_from"),
|
2019-01-05 18:45:21 +00:00
|
|
|
[invoice.user.get_mail],
|
2019-11-04 16:55:03 +00:00
|
|
|
attachments=[("invoice.pdf", pdf, "application/pdf")],
|
2019-01-05 18:45:21 +00:00
|
|
|
)
|
|
|
|
|
2020-04-19 18:06:34 +00:00
|
|
|
send_mail(mail, request)
|
2019-01-05 18:45:21 +00:00
|
|
|
|
2020-04-19 18:06:34 +00:00
|
|
|
|
|
|
|
def send_mail_voucher(invoice, request=None):
|
2019-01-05 18:45:21 +00:00
|
|
|
"""Creates a voucher from an invoice and sends it by email to the client"""
|
2019-09-28 11:01:22 +00:00
|
|
|
president = Mandate.get_mandate(invoice.date).president
|
2019-01-05 18:45:21 +00:00
|
|
|
ctx = {
|
2019-11-04 16:55:03 +00:00
|
|
|
"asso_name": AssoOption.get_cached_value("name"),
|
|
|
|
"pres_name": " ".join([president.name, president.surname]),
|
|
|
|
"firstname": invoice.user.name,
|
|
|
|
"lastname": invoice.user.surname,
|
|
|
|
"email": invoice.user.email,
|
|
|
|
"phone": invoice.user.telephone,
|
|
|
|
"date_end": invoice.get_subscription().latest("date_end").date_end,
|
|
|
|
"date_begin": invoice.get_subscription().earliest("date_start").date_start,
|
2018-07-22 22:13:25 +00:00
|
|
|
}
|
2019-11-04 16:55:03 +00:00
|
|
|
templatename = CotisationsOption.get_cached_value(
|
|
|
|
"voucher_template"
|
|
|
|
).template.name.split("/")[-1]
|
2019-01-10 23:39:16 +00:00
|
|
|
pdf = create_pdf(templatename, ctx)
|
2019-11-04 16:55:03 +00:00
|
|
|
template = get_template("cotisations/email_subscription_accepted")
|
2018-07-22 22:13:25 +00:00
|
|
|
|
|
|
|
ctx = {
|
2019-11-04 16:55:03 +00:00
|
|
|
"name": "{} {}".format(invoice.user.name, invoice.user.surname),
|
|
|
|
"asso_email": AssoOption.get_cached_value("contact"),
|
|
|
|
"asso_name": AssoOption.get_cached_value("name"),
|
|
|
|
"date_end": invoice.get_subscription().latest("date_end").date_end,
|
2018-07-22 22:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mail = EmailMessage(
|
2019-11-04 16:55:03 +00:00
|
|
|
"Votre reçu / Your voucher",
|
2018-07-22 22:13:25 +00:00
|
|
|
template.render(ctx),
|
2019-11-04 16:55:03 +00:00
|
|
|
GeneralOption.get_cached_value("email_from"),
|
2018-09-08 19:09:48 +00:00
|
|
|
[invoice.user.get_mail],
|
2019-11-04 16:55:03 +00:00
|
|
|
attachments=[("voucher.pdf", pdf, "application/pdf")],
|
2018-07-22 22:13:25 +00:00
|
|
|
)
|
2020-04-19 18:06:34 +00:00
|
|
|
|
|
|
|
send_mail(mail, request)
|