utils.py 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- 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.

22 23 24 25 26 27
import os

from django.template.loader import get_template
from django.core.mail import EmailMessage

from .tex import create_pdf
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
28
from preferences.models import AssoOption, GeneralOption, CotisationsOption
29 30 31
from re2o.settings import LOGO_PATH
from re2o import settings

32

33
def find_payment_method(payment):
34
    """Finds the payment method associated to the payment if it exists."""
35 36 37 38 39 40 41 42
    from cotisations.payment_methods import PAYMENT_METHODS
    for method in PAYMENT_METHODS:
        try:
            o = method.PaymentMethod.objects.get(payment=payment)
            return o
        except method.PaymentMethod.DoesNotExist:
            pass
    return None
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


def send_mail_invoice(invoice):
    """Creates the pdf of the invoice and sends it by email to the client"""
    purchases_info = []
    for purchase in invoice.vente_set.all():
        purchases_info.append({
            'name': purchase.name,
            'price': purchase.prix,
            'quantity': purchase.number,
            'total_price': purchase.prix_total
        })

    ctx = {
        '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'),
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        '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)
    }

    pdf = create_pdf('cotisations/factures.tex', ctx)
    template = get_template('cotisations/email_invoice')

    ctx = {
        'name': "{} {}".format(
            invoice.user.name,
            invoice.user.surname
        ),
        'contact_mail': AssoOption.get_cached_value('contact'),
        'asso_name': AssoOption.get_cached_value('name')
    }

    mail = EmailMessage(
        'Votre facture / Your invoice',
        template.render(ctx),
        GeneralOption.get_cached_value('email_from'),
        [invoice.user.get_mail],
        attachments=[('invoice.pdf', pdf, 'application/pdf')]
    )
    mail.send()


def send_mail_voucher(invoice):
    """Creates a voucher from an invoice and sends it by email to the client"""
    ctx = {
        'asso_name': AssoOption.get_cached_value('name'),
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
102 103 104 105 106 107 108
        'pres_name': AssoOption.get_cached_value('pres_name'),
        '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
109
    }
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
110 111 112
    templatename = CotisationsOption.get_cached_value('voucher_template').template.name.split('/')[-1]
    pdf = create_pdf(templatename, ctx)
    template = get_template('cotisations/email_subscription_accepted')
113 114 115 116 117 118

    ctx = {
        'name': "{} {}".format(
            invoice.user.name,
            invoice.user.surname
        ),
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
119
        'asso_email': AssoOption.get_cached_value('contact'),
120 121 122 123
        'asso_name': AssoOption.get_cached_value('name')
    }

    mail = EmailMessage(
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
124
        'Votre reçu / Your voucher',
125 126
        template.render(ctx),
        GeneralOption.get_cached_value('email_from'),
127
        [invoice.user.get_mail],
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
128
        attachments=[('voucher.pdf', pdf, 'application/pdf')]
129 130
    )
    mail.send()