2022-09-14 19:06:26 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
# Libre en Communs's cotisation control program
|
2024-09-16 16:30:30 +02:00
|
|
|
# Copyright (C) 2022-2024 Libre en Communs
|
2022-09-14 19:06:26 +02:00
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-09-14 23:47:02 +02:00
|
|
|
import os, requests, json, datetime, shutil, quopri, subprocess, base64
|
2022-09-14 19:06:26 +02:00
|
|
|
from requests.auth import HTTPBasicAuth
|
|
|
|
from requests.auth import HTTPDigestAuth
|
2024-09-16 16:12:07 +02:00
|
|
|
import smtplib
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.mime.base import MIMEBase
|
|
|
|
from email import encoders
|
|
|
|
from email.utils import formataddr
|
|
|
|
from pathlib import Path
|
2022-09-14 19:06:26 +02:00
|
|
|
|
2022-09-15 00:36:34 +02:00
|
|
|
VERSION="1.0.0"
|
2022-09-14 23:20:52 +02:00
|
|
|
GESTION_SECRET_FILE="/home/tresorier/.secret/gestion_api_password"
|
|
|
|
GIT_SECRET_FILE="/home/tresorier/.secret/git_api_password"
|
2022-09-14 23:18:03 +02:00
|
|
|
WORKDIR="/srv/validation_cotisation.d"
|
2022-09-14 19:06:26 +02:00
|
|
|
MODALITY_MAIL="mail_instructions.txt"
|
|
|
|
MODALITY_MAIL_HEADERS="mail_instructions_headers.txt"
|
|
|
|
RECEPT_MAIL="mail_recu.txt"
|
|
|
|
RECEPT_MAIL_HEADERS="mail_recu_headers.txt"
|
|
|
|
RECEPT_MAIL_ATTACHMENT="mail_recu_attachment.txt"
|
|
|
|
SUMMARY_MAIL="mail_recap_header.txt"
|
|
|
|
SENDMAIL_LOCATION = "/usr/sbin/sendmail" # sendmail location
|
|
|
|
BUF=[]
|
2023-11-04 15:19:21 +01:00
|
|
|
GITEA_URL = "https://forge.a-lec.org"
|
2022-09-14 19:06:26 +02:00
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
# Update these with your SMTP server details
|
|
|
|
SMTP_SERVER = 'mail.a-lec.org'
|
|
|
|
SMTP_PORT = 587
|
|
|
|
SMTP_USER = 'tresorier@a-lec.org'
|
|
|
|
SMTP_SECRET_FILE = "/home/tresorier/.secret/smtp_api_password"
|
|
|
|
|
2022-09-14 19:06:26 +02:00
|
|
|
# gestion_read("SELECT * FROM services_users su \
|
|
|
|
# INNER JOIN membres m ON m.id = su.id_user \
|
|
|
|
# INNER JOIN services_fees sf ON sf.id = su.id_fee \
|
|
|
|
# LEFT JOIN acc_transactions_users tu ON tu.id_service_user = su.id \
|
|
|
|
# LEFT JOIN acc_transactions_lines l ON l.id_transaction = tu.id_transaction \
|
|
|
|
# WHERE m.id = 3 AND l.id_account = 481;")
|
|
|
|
|
|
|
|
def gestion_get_secret():
|
|
|
|
with open(GESTION_SECRET_FILE) as sfile:
|
|
|
|
return sfile.readline().replace("\n", "")
|
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
def smtp_get_secret():
|
|
|
|
with open(SMTP_SECRET_FILE) as sfile:
|
|
|
|
return sfile.readline().replace("\n", "")
|
|
|
|
|
2022-09-14 19:06:26 +02:00
|
|
|
def git_get_secret():
|
|
|
|
with open(GIT_SECRET_FILE) as sfile:
|
|
|
|
return sfile.readline().replace("\n", "")
|
|
|
|
|
|
|
|
def get_file_content(filename):
|
|
|
|
with open(filename) as sfile:
|
|
|
|
return sfile.readlines()
|
|
|
|
|
|
|
|
def get_file_content_all(filename):
|
|
|
|
with open(filename) as sfile:
|
|
|
|
return sfile.read()
|
|
|
|
|
|
|
|
def set_file_content(filename, lines):
|
|
|
|
with open(filename, "x") as sfile:
|
|
|
|
return sfile.writelines(lines)
|
|
|
|
|
|
|
|
def gestion_read(req):
|
|
|
|
response = requests.post('https://gestion.a-lec.org/api/sql/',
|
|
|
|
auth = HTTPBasicAuth('api666', gestion_get_secret()),
|
|
|
|
data = req)
|
|
|
|
return response.json()
|
|
|
|
|
2022-09-14 23:16:10 +02:00
|
|
|
def git_mail_ticket_read(req):
|
2023-11-04 15:19:21 +01:00
|
|
|
response: requests.Response = requests.get('https://forge.a-lec.org/api/v1/repos/{}/{}/issues?access_token={}'.format("cominfra", "mail", git_get_secret()), params=req)
|
2022-09-14 19:06:26 +02:00
|
|
|
return response.json()
|
|
|
|
|
|
|
|
def setup_workdir():
|
|
|
|
if not os.path.isdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR)
|
|
|
|
if not "impayé" in os.listdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR+"/impayé")
|
|
|
|
if not "expiré" in os.listdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR+"/expiré")
|
|
|
|
if not "validé" in os.listdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR+"/validé")
|
2022-09-14 23:16:10 +02:00
|
|
|
if not "transition" in os.listdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR+"/transition")
|
2022-09-14 19:06:26 +02:00
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
# def sendmail(headers, data):
|
|
|
|
# msg = bytes(headers + "\n", 'utf-8') + quopri.encodestring(bytes(data, 'utf-8'))
|
|
|
|
# subprocess.run([SENDMAIL_LOCATION, "-t", "-oi"], input=msg)
|
|
|
|
|
|
|
|
# def sendmail_with_attachment(headers, data, attachment_header, attachment, ending):
|
|
|
|
# msg = bytes(headers + "\n", 'utf-8') \
|
|
|
|
# + quopri.encodestring(bytes(data, 'utf-8')) \
|
|
|
|
# + bytes(attachment_header + "\n", 'utf-8') \
|
|
|
|
# + base64.b64encode(attachment) \
|
|
|
|
# + bytes(ending, 'utf-8')
|
|
|
|
|
|
|
|
# subprocess.run([SENDMAIL_LOCATION, "-t", "-oi"], input=msg)
|
2022-09-14 19:06:26 +02:00
|
|
|
|
2022-09-14 23:41:43 +02:00
|
|
|
|
2024-09-16 17:15:25 +02:00
|
|
|
def sendmail_with_attachment(headers, data, attachment_path=None, filename=None):
|
2024-09-16 16:12:07 +02:00
|
|
|
# Parse headers
|
|
|
|
msg = MIMEMultipart()
|
|
|
|
for header in headers.split("\n"):
|
|
|
|
if ": " in header:
|
|
|
|
key, value = header.split(": ", 1)
|
2024-09-16 17:15:25 +02:00
|
|
|
msg[key] = value.strip()
|
2024-09-16 16:12:07 +02:00
|
|
|
|
|
|
|
# Add the email body
|
|
|
|
body = MIMEText(data, 'plain', 'utf-8')
|
|
|
|
msg.attach(body)
|
|
|
|
|
2024-09-16 17:15:25 +02:00
|
|
|
# Add the attachment only if attachment_path is provided
|
|
|
|
if attachment_path and filename:
|
|
|
|
attachment = MIMEBase('application', 'octet-stream')
|
|
|
|
with open(attachment_path, "rb") as attach_file:
|
|
|
|
attachment.set_payload(attach_file.read())
|
|
|
|
encoders.encode_base64(attachment)
|
|
|
|
attachment.add_header('Content-Disposition', f'attachment; filename={filename}')
|
|
|
|
msg.attach(attachment)
|
2024-09-16 16:12:07 +02:00
|
|
|
|
|
|
|
# Send the email
|
|
|
|
try:
|
|
|
|
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
|
|
|
server.starttls() # Start TLS encryption
|
|
|
|
server.login(SMTP_USER, smtp_get_secret())
|
|
|
|
server.sendmail(SMTP_USER, msg['To'], msg.as_string())
|
|
|
|
print("Email sent successfully")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Failed to send email: {e}")
|
2022-09-14 19:06:26 +02:00
|
|
|
|
2024-09-16 17:15:25 +02:00
|
|
|
|
2022-09-14 23:16:10 +02:00
|
|
|
def gestion_get_expired():
|
|
|
|
request_expired = "SELECT id_user FROM services_users su " +\
|
2024-09-16 16:35:41 +02:00
|
|
|
"INNER JOIN users m ON m.id = su.id_user " +\
|
2022-09-14 23:16:10 +02:00
|
|
|
"INNER JOIN services s ON s.id = su.id_service " +\
|
|
|
|
"LEFT JOIN services_fees sf ON sf.id = su.id_fee " +\
|
|
|
|
"INNER JOIN (SELECT id, MAX(date) " +\
|
|
|
|
"FROM services_users " +\
|
|
|
|
"GROUP BY id_user, id_service) " +\
|
|
|
|
"AS su2 ON su2.id = su.id " +\
|
|
|
|
"WHERE su.id_service = 1 " +\
|
|
|
|
"AND su.expiry_date < date() " +\
|
|
|
|
"AND NOT (m.id_category = 10 " +\
|
|
|
|
"OR m.id_category = 3 " +\
|
|
|
|
"OR m.id_category = 2 " +\
|
|
|
|
"OR m.id_category = 8);"
|
|
|
|
expired_members_list = \
|
|
|
|
[ str(x["id_user"]) for x in gestion_read(request_expired)["results"]]
|
|
|
|
|
|
|
|
return expired_members_list.copy()
|
|
|
|
|
|
|
|
def gestion_get_unpaid():
|
|
|
|
request_unpaid = "SELECT id_user FROM services_users su " +\
|
2024-09-16 16:35:41 +02:00
|
|
|
"INNER JOIN users m ON m.id = su.id_user " +\
|
2022-09-14 23:16:10 +02:00
|
|
|
"INNER JOIN services s ON s.id = su.id_service " +\
|
|
|
|
"LEFT JOIN services_fees sf ON sf.id = su.id_fee " +\
|
|
|
|
"INNER JOIN (SELECT id, MAX(date) " +\
|
|
|
|
"FROM services_users " +\
|
|
|
|
"GROUP BY id_user, id_service) " +\
|
|
|
|
"AS su2 ON su2.id = su.id " +\
|
|
|
|
"WHERE su.id_service = 1 " +\
|
|
|
|
"AND su.paid = 0 " +\
|
|
|
|
"AND NOT (m.id_category = 10 " +\
|
|
|
|
"OR m.id_category = 3 " +\
|
|
|
|
"OR m.id_category = 2 " +\
|
|
|
|
"OR m.id_category = 8);"
|
|
|
|
|
|
|
|
|
|
|
|
unpaid_members_list = \
|
|
|
|
[ str(x["id_user"]) for x in gestion_read(request_unpaid)["results"]]
|
|
|
|
|
|
|
|
return unpaid_members_list.copy()
|
|
|
|
|
2022-09-15 00:00:09 +02:00
|
|
|
def gestion_get_amount(member):
|
|
|
|
request_unpaid = "SELECT amount FROM services_users su " +\
|
2024-09-16 16:35:41 +02:00
|
|
|
"INNER JOIN users m ON m.id = su.id_user " +\
|
2022-09-15 00:00:09 +02:00
|
|
|
"INNER JOIN services s ON s.id = su.id_service " +\
|
|
|
|
"LEFT JOIN services_fees sf ON sf.id = su.id_fee " +\
|
|
|
|
"INNER JOIN (SELECT id, MAX(date) " +\
|
|
|
|
"FROM services_users " +\
|
|
|
|
"GROUP BY id_user, id_service) " +\
|
|
|
|
"AS su2 ON su2.id = su.id " +\
|
|
|
|
"WHERE id_user = {};".format(member)
|
|
|
|
|
|
|
|
|
2022-09-15 00:04:07 +02:00
|
|
|
return gestion_read(request_unpaid)["results"][-1]["amount"]
|
2022-09-15 00:00:09 +02:00
|
|
|
|
2022-09-15 00:18:16 +02:00
|
|
|
def gestion_get_date(member):
|
|
|
|
request_unpaid = "SELECT date FROM services_users su " +\
|
2024-09-16 16:35:41 +02:00
|
|
|
"INNER JOIN users m ON m.id = su.id_user " +\
|
2022-09-15 00:18:16 +02:00
|
|
|
"INNER JOIN services s ON s.id = su.id_service " +\
|
|
|
|
"LEFT JOIN services_fees sf ON sf.id = su.id_fee " +\
|
|
|
|
"INNER JOIN (SELECT id, MAX(date) " +\
|
|
|
|
"FROM services_users " +\
|
|
|
|
"GROUP BY id_user, id_service) " +\
|
|
|
|
"AS su2 ON su2.id = su.id " +\
|
|
|
|
"WHERE id_user = {};".format(member)
|
|
|
|
|
|
|
|
|
|
|
|
return gestion_read(request_unpaid)["results"][-1]["date"]
|
|
|
|
|
2022-09-14 23:16:10 +02:00
|
|
|
def get_member_infos(member):
|
2024-09-16 16:35:41 +02:00
|
|
|
request = "SELECT * FROM users " +\
|
2022-09-14 23:16:10 +02:00
|
|
|
"WHERE id = '{}';".format(member)
|
|
|
|
try:
|
|
|
|
name = gestion_read(request)['results'][-1]['nom']
|
|
|
|
numero = gestion_read(request)['results'][-1]['numero']
|
|
|
|
email = gestion_read(request)['results'][-1]['email']
|
|
|
|
except:
|
|
|
|
print(gestion_read(request))
|
|
|
|
raise(Exception)
|
|
|
|
return (name, numero, email)
|
|
|
|
|
|
|
|
def check_email_created(member):
|
|
|
|
|
|
|
|
# Get member infos
|
|
|
|
name, numero, email = get_member_infos(member)
|
|
|
|
|
2023-11-04 15:53:16 +01:00
|
|
|
if numero < 15:
|
|
|
|
return True
|
|
|
|
|
|
|
|
request = {
|
2023-11-04 16:06:01 +01:00
|
|
|
'state' : "open",
|
|
|
|
'labels' : [968],
|
2023-11-04 15:53:16 +01:00
|
|
|
'q' : "Création d'un compte courriel membre n°{}".format(numero)
|
|
|
|
}
|
2023-11-04 16:06:01 +01:00
|
|
|
answer1 = git_mail_ticket_read(request)
|
2022-09-14 23:16:10 +02:00
|
|
|
|
2023-11-04 16:06:01 +01:00
|
|
|
request = {
|
|
|
|
'state' : "closed",
|
|
|
|
'q' : "Création d'un compte courriel membre n°{}".format(numero)
|
|
|
|
}
|
|
|
|
answer2 = git_mail_ticket_read(request)
|
|
|
|
|
2024-09-16 16:35:41 +02:00
|
|
|
|
|
|
|
request = {
|
|
|
|
'state' : "closed",
|
|
|
|
'q' : "Inscription {}".format(numero)
|
|
|
|
}
|
|
|
|
answer3 = git_mail_ticket_read(request)
|
|
|
|
|
|
|
|
|
|
|
|
request = {
|
|
|
|
'state' : "closed",
|
|
|
|
'q' : "Adhésion {}".format(numero)
|
|
|
|
}
|
|
|
|
answer4 = git_mail_ticket_read(request)
|
|
|
|
|
|
|
|
request = {
|
|
|
|
'state' : "closed",
|
|
|
|
'q' : "Adhésion n° {}".format(numero)
|
|
|
|
}
|
|
|
|
answer4 = git_mail_ticket_read(request)
|
|
|
|
|
|
|
|
if len(answer1) + len(answer2) + len(answer3) + len(answer4) >= 1:
|
2023-11-04 15:53:16 +01:00
|
|
|
return True
|
|
|
|
return False
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
def notify_expired(member):
|
|
|
|
BUF.append("* {}".format(member))
|
2022-09-15 00:18:16 +02:00
|
|
|
BUF.append(" EXPIRATION ADHESION")
|
2022-09-14 23:16:10 +02:00
|
|
|
BUF.append("")
|
|
|
|
|
|
|
|
def notify_unpaid(member):
|
|
|
|
if not check_email_created(member):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Get member infos
|
|
|
|
name, numero, email = get_member_infos(member)
|
2022-09-15 00:06:45 +02:00
|
|
|
amount = "{},{}".format(str(gestion_get_amount(member))[:-2],
|
|
|
|
str(gestion_get_amount(member))[-2:])
|
2022-09-15 00:21:19 +02:00
|
|
|
year = gestion_get_date(member)[:4]
|
2022-09-14 23:16:10 +02:00
|
|
|
|
2022-09-15 00:06:45 +02:00
|
|
|
BUF.append("* {} (numero {}), {}, {} €".format(member, numero, name, amount))
|
2022-09-14 23:16:10 +02:00
|
|
|
BUF.append(" NOTIFICATION MEMBRE")
|
|
|
|
BUF.append("")
|
2022-09-15 00:18:16 +02:00
|
|
|
|
2022-09-15 00:30:04 +02:00
|
|
|
mailtext = get_file_content_all(MODALITY_MAIL) + "\n"
|
|
|
|
mailtext = mailtext.replace("NOM_COTISANT", name)
|
|
|
|
mailtext = mailtext.replace("NUMERO_MEMBRE", str(numero))
|
|
|
|
mailtext = mailtext.replace("MONTANT_COTISATION", amount)
|
2022-09-15 10:36:03 +02:00
|
|
|
mailtext = mailtext.replace("ANNEE_CIVILE", year)
|
2022-09-15 00:32:39 +02:00
|
|
|
mailheaders = get_file_content_all(MODALITY_MAIL_HEADERS) + "\n"
|
|
|
|
mailheaders = mailheaders.replace("ANNEE_CIVILE", year)
|
2022-09-15 00:30:04 +02:00
|
|
|
mailheaders = mailheaders.replace("COURRIEL-COTISANT", email)
|
2022-09-15 00:18:16 +02:00
|
|
|
|
2024-09-16 17:18:51 +02:00
|
|
|
sendmail_with_attachment(mailheaders, mailtext, None, None)
|
2022-09-14 23:16:10 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def renotify_unpaid(member):
|
2023-11-04 15:53:16 +01:00
|
|
|
if not check_email_created(member):
|
|
|
|
return False
|
2022-09-14 23:16:10 +02:00
|
|
|
# Get member infos
|
|
|
|
name, numero, email = get_member_infos(member)
|
2022-09-15 00:20:06 +02:00
|
|
|
amount = "{},{}".format(str(gestion_get_amount(member))[:-2],
|
|
|
|
str(gestion_get_amount(member))[-2:])
|
2022-09-15 00:21:19 +02:00
|
|
|
year = gestion_get_date(member)[:4]
|
2022-09-14 23:16:10 +02:00
|
|
|
|
2022-09-15 00:20:06 +02:00
|
|
|
BUF.append("* {} (numero {}), {}, {} €".format(member, numero, name, amount))
|
2022-09-15 10:36:03 +02:00
|
|
|
BUF.append(" RENOTIFICATION MEMBRE")
|
2022-09-14 23:16:10 +02:00
|
|
|
BUF.append("")
|
2022-09-15 00:20:06 +02:00
|
|
|
|
|
|
|
mailtext = get_file_content_all(MODALITY_MAIL) + "\n"
|
|
|
|
mailtext = mailtext.replace("NOM_COTISANT", name)
|
2022-09-15 00:21:58 +02:00
|
|
|
mailtext = mailtext.replace("NUMERO_MEMBRE", str(numero))
|
2022-09-15 00:20:06 +02:00
|
|
|
mailtext = mailtext.replace("MONTANT_COTISATION", amount)
|
2022-09-15 10:36:03 +02:00
|
|
|
mailtext = mailtext.replace("ANNEE_CIVILE", year)
|
2022-09-15 00:32:39 +02:00
|
|
|
mailheaders = get_file_content_all(MODALITY_MAIL_HEADERS) + "\n"
|
|
|
|
mailheaders = mailheaders.replace("ANNEE_CIVILE", year)
|
2022-09-15 00:20:06 +02:00
|
|
|
mailheaders = mailheaders.replace("COURRIEL-COTISANT", email)
|
2022-09-15 10:59:47 +02:00
|
|
|
mailheaders = mailheaders.replace("Modalit", "Rappel_:_modalit")
|
2022-09-15 00:20:06 +02:00
|
|
|
|
2024-09-16 17:18:51 +02:00
|
|
|
sendmail_with_attachment(mailheaders, mailtext, None, None)
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
def check_expired_unpaid():
|
|
|
|
expired_members = gestion_get_expired()
|
|
|
|
unpaid_members = gestion_get_unpaid()
|
|
|
|
|
|
|
|
# Check expired members
|
|
|
|
if set(expired_members) != set(os.listdir(WORKDIR+"/expiré")):
|
2022-09-15 00:06:45 +02:00
|
|
|
BUF.append("Membres expirés : {}\n".format(gestion_get_expired()))
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
# Check for no-more-expired members
|
|
|
|
for record in os.listdir(WORKDIR+"/expiré"):
|
|
|
|
if not record in expired_members:
|
|
|
|
os.rename(WORKDIR+"/expiré/"+record,
|
|
|
|
WORKDIR+"/transition/"+record)
|
|
|
|
|
|
|
|
# Check for new expired members
|
|
|
|
for member in expired_members:
|
|
|
|
if not str(member) in os.listdir(WORKDIR+"/expiré"):
|
|
|
|
set_file_content(WORKDIR+"/expiré/"+str(member), "")
|
|
|
|
notify_expired(member)
|
|
|
|
|
|
|
|
# Check unpaid members
|
|
|
|
if set(unpaid_members) != set(os.listdir(WORKDIR+"/impayé")):
|
2022-09-15 00:06:45 +02:00
|
|
|
BUF.append("Membres en impayé : {}\n".format(gestion_get_unpaid()))
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
# Check for no-more-unpaid members
|
|
|
|
for record in os.listdir(WORKDIR+"/impayé"):
|
|
|
|
if not record in unpaid_members:
|
|
|
|
os.rename(WORKDIR+"/impayé/"+record,
|
|
|
|
WORKDIR+"/transition/"+record)
|
|
|
|
|
|
|
|
# Check for new unpaid members and set date of first contact
|
|
|
|
for member in unpaid_members:
|
|
|
|
if not member in os.listdir(WORKDIR+"/impayé"):
|
|
|
|
# Get member infos
|
|
|
|
name, numero, email = get_member_infos(member)
|
|
|
|
|
|
|
|
if notify_unpaid(member):
|
|
|
|
set_file_content(WORKDIR+"/impayé/"+str(member),
|
|
|
|
str(datetime.datetime.now().strftime("%d/%m/%Y")))
|
|
|
|
else:
|
|
|
|
# Ancient date for the system to be triggered
|
|
|
|
set_file_content(WORKDIR+"/impayé/"+str(member),"27/09/1983")
|
|
|
|
BUF.append("* {} (numero {}), {}".format(member, numero, name))
|
|
|
|
BUF.append(" COURRIEL NON FONCTIONNEL")
|
|
|
|
|
|
|
|
# Check periodically for unpaid members
|
|
|
|
for record in os.listdir(WORKDIR+"/impayé"):
|
|
|
|
last_contact = \
|
|
|
|
datetime.datetime.strptime(
|
2022-09-15 10:59:02 +02:00
|
|
|
get_file_content(WORKDIR+"/impayé/"+record)[0].replace("\n", ""),
|
|
|
|
'%d/%m/%Y')
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
# Too old to be a relaunch
|
|
|
|
if abs(datetime.datetime.now() - last_contact).days > 300:
|
|
|
|
if notify_unpaid(record):
|
|
|
|
os.remove(WORKDIR+"/impayé/"+str(record))
|
|
|
|
set_file_content(WORKDIR+"/impayé/"+str(record),
|
|
|
|
str(datetime.datetime.now().strftime("%d/%m/%Y")))
|
|
|
|
# Relaunch
|
|
|
|
elif abs(datetime.datetime.now() - last_contact).days > 30:
|
|
|
|
renotify_unpaid(record)
|
|
|
|
os.remove(WORKDIR+"/impayé/"+str(record))
|
|
|
|
set_file_content(WORKDIR+"/impayé/"+str(record),
|
|
|
|
str(datetime.datetime.now().strftime("%d/%m/%Y")))
|
|
|
|
|
|
|
|
# Clean transited member if unpaid or expired
|
|
|
|
for record in os.listdir(WORKDIR+"/transition"):
|
|
|
|
if record in os.listdir(WORKDIR+"/impayé") or \
|
|
|
|
record in os.listdir(WORKDIR+"/expiré"):
|
|
|
|
os.remove(WORKDIR+"/transition/"+record)
|
|
|
|
|
|
|
|
def validate(member):
|
2023-11-04 15:53:16 +01:00
|
|
|
if not check_email_created(member):
|
|
|
|
return False
|
2022-09-14 23:16:10 +02:00
|
|
|
# Get infos
|
|
|
|
request = "SELECT *, l.reference true_reference " +\
|
|
|
|
"FROM services_users su " +\
|
2024-09-16 16:35:41 +02:00
|
|
|
"INNER JOIN users m ON m.id = su.id_user " +\
|
2022-09-14 23:16:10 +02:00
|
|
|
"INNER JOIN services_fees sf ON sf.id = su.id_fee " +\
|
|
|
|
"LEFT JOIN acc_transactions_users tu ON tu.id_service_user = su.id " +\
|
|
|
|
"LEFT JOIN acc_transactions_lines l ON l.id_transaction = tu.id_transaction " +\
|
|
|
|
"INNER JOIN acc_transactions tr ON tr.id = l.id_transaction " +\
|
|
|
|
"WHERE m.id = {} AND l.id_account = 481;".format(member)
|
2022-11-04 14:40:13 +01:00
|
|
|
# Note: su.id_service = 1 parceque la cotisation correspond au service 1
|
2022-09-14 23:16:10 +02:00
|
|
|
|
2022-09-14 23:28:21 +02:00
|
|
|
try:
|
|
|
|
answer = gestion_read(request)["results"][-1]
|
2022-11-04 14:40:13 +01:00
|
|
|
except IndexError:
|
|
|
|
BUF.append("* {} (numero inconnu)".format(member))
|
|
|
|
BUF.append(" SUPPRESSION MEMBRE")
|
|
|
|
BUF.append("")
|
|
|
|
os.remove(WORKDIR+"/transition/"+member)
|
|
|
|
return
|
2022-09-14 23:16:10 +02:00
|
|
|
|
|
|
|
date = datetime.datetime.strptime(
|
|
|
|
answer["date"],'%Y-%m-%d').strftime("%d/%m/%Y")
|
|
|
|
filename = "{}_reçu_{}".format(
|
2023-11-04 15:19:21 +01:00
|
|
|
answer["nom"].replace(" ", "_").replace("'", "_"),
|
2022-09-14 23:16:10 +02:00
|
|
|
date.replace("/", "."))
|
|
|
|
|
|
|
|
BUF.append("* {} (numero {}), {}".format(answer["id"],
|
|
|
|
answer["numero"],
|
|
|
|
answer["nom"]))
|
|
|
|
BUF.append(" COTISATION : {},{}€".format(str(answer["amount"])[:-2],
|
|
|
|
str(answer["amount"])[-2:]))
|
|
|
|
BUF.append(" ANNEE CIVILE : {}".format(answer["date"][:4]))
|
|
|
|
BUF.append(" VALIDATION MEMBRE")
|
|
|
|
BUF.append("")
|
|
|
|
|
|
|
|
# Generate receipt
|
|
|
|
latexfile = get_file_content_all("RECU_COTISATION.tex")
|
|
|
|
latexfile = latexfile.replace("ANNEE-CIVILE", answer["date"][:4])
|
|
|
|
latexfile = latexfile.replace("NOM-COTISANT", answer["nom"])
|
|
|
|
latexfile = latexfile.replace("STATUT-COTISANT", answer["statut_juridique"])
|
|
|
|
latexfile = latexfile.replace("ADRESSE-COTISANT", "{}, {} {}".format(
|
|
|
|
answer["adresse"],
|
|
|
|
answer["code_postal"],
|
|
|
|
answer["ville"]))
|
|
|
|
latexfile = latexfile.replace("SOMME", "{},{}".format(
|
|
|
|
str(answer["amount"])[:-2],
|
|
|
|
str(answer["amount"])[-2:]))
|
|
|
|
latexfile = latexfile.replace("DATE-VERSEMENT", date)
|
2023-11-04 15:22:54 +01:00
|
|
|
|
|
|
|
if not answer["true_reference"]:
|
|
|
|
BUF.append("Erreur : référence introuvable (déféré)")
|
|
|
|
return
|
|
|
|
|
2022-09-14 23:16:10 +02:00
|
|
|
latexfile = latexfile.replace("MODE-VERSEMENT", answer["true_reference"])
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(WORKDIR+"/validé/"+filename+".tex")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
set_file_content(WORKDIR+"/validé/"+filename+".tex", latexfile)
|
|
|
|
os.system("cd {} && pdflatex {}".format(WORKDIR+"/validé/", filename+".tex"))
|
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
# Preparing the email
|
|
|
|
mailheaders = get_file_content_all(RECEPT_MAIL_HEADERS).replace("COURRIEL-COTISANT", answer["email"]) + "\n"
|
|
|
|
mailtext = get_file_content_all(RECEPT_MAIL).replace("ANNEE-CIVILE", answer["date"][:4]) + "\n"
|
2022-09-14 23:41:43 +02:00
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
# Sending the email with the attached PDF
|
|
|
|
pdf_path = WORKDIR+"/validé/"+filename+".pdf"
|
|
|
|
sendmail_with_attachment(mailheaders, mailtext, pdf_path, filename + ".pdf")
|
2022-09-14 23:41:43 +02:00
|
|
|
|
2024-09-16 16:12:07 +02:00
|
|
|
# Cleanup
|
2022-09-14 23:44:45 +02:00
|
|
|
os.remove(WORKDIR+"/transition/"+member)
|
|
|
|
|
2022-09-14 23:16:10 +02:00
|
|
|
def validate_members():
|
|
|
|
for record in os.listdir(WORKDIR+"/transition"):
|
|
|
|
validate(record)
|
2022-09-14 19:06:26 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
setup_workdir()
|
2022-09-14 23:16:10 +02:00
|
|
|
check_expired_unpaid()
|
|
|
|
validate_members()
|
2022-09-14 19:06:26 +02:00
|
|
|
|
|
|
|
# End of work
|
|
|
|
# Launch summary mail
|
|
|
|
mailheaders = get_file_content_all(SUMMARY_MAIL) + "\n"
|
|
|
|
mailtext = ""
|
|
|
|
|
2024-09-16 17:09:11 +02:00
|
|
|
is_sendable = False
|
2022-09-14 19:06:26 +02:00
|
|
|
for line in BUF:
|
|
|
|
mailtext += line + "\n"
|
2024-09-16 17:09:11 +02:00
|
|
|
is_sendable = True
|
2022-09-14 19:06:26 +02:00
|
|
|
print(line)
|
|
|
|
|
|
|
|
if is_sendable:
|
2024-09-16 17:09:11 +02:00
|
|
|
# Assuming no attachment is needed for the summary mail
|
|
|
|
sendmail_with_attachment(mailheaders, mailtext, None, None)
|
2022-09-14 19:06:26 +02:00
|
|
|
|
|
|
|
## Bootstrap
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|