2022-09-15 11:09:29 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2022-09-15 11:10:14 +02:00
|
|
|
# Libre en Communs's mecene control program
|
2022-09-15 11:09:29 +02:00
|
|
|
# Copyright (C) 2022 Libre en Communs
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import os, requests, json, datetime, shutil, quopri, subprocess, base64
|
|
|
|
from requests.auth import HTTPBasicAuth
|
|
|
|
from requests.auth import HTTPDigestAuth
|
|
|
|
|
|
|
|
|
|
|
|
VERSION="1.0.0"
|
2022-10-08 22:39:32 +02:00
|
|
|
GESTION_SECRET_FILE="/home/tresorier/.secret/gestion_api_password"
|
2022-10-08 22:40:26 +02:00
|
|
|
WORKDIR="/srv/validation_don.d"
|
2022-09-15 11:09:29 +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=[]
|
|
|
|
|
|
|
|
# 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", "")
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def setup_workdir():
|
|
|
|
if not os.path.isdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR)
|
2022-10-08 19:04:42 +02:00
|
|
|
if not "transition" in os.listdir(WORKDIR):
|
|
|
|
os.mkdir(WORKDIR+"/transition")
|
2022-10-06 23:09:21 +02:00
|
|
|
if not "nouveau" in os.listdir(WORKDIR):
|
2022-10-08 19:04:42 +02:00
|
|
|
os.mkdir(WORKDIR+"/nouveau")
|
2022-10-06 23:09:21 +02:00
|
|
|
if not "validé" in os.listdir(WORKDIR):
|
2022-10-08 19:04:42 +02:00
|
|
|
os.mkdir(WORKDIR+"/validé")
|
2022-09-15 11:09:29 +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-10-09 00:31:06 +02:00
|
|
|
def notify_unpaid(record):
|
2022-09-15 11:09:29 +02:00
|
|
|
# Get infos
|
2022-10-09 00:30:06 +02:00
|
|
|
name, surname, address, postal_code, city, email, amount, mode, truc = \
|
2022-10-09 00:31:06 +02:00
|
|
|
get_file_content_all(WORKDIR+"/transition/"+record).split("\n")
|
2022-09-15 11:09:29 +02:00
|
|
|
pass
|
|
|
|
|
2022-10-09 00:31:06 +02:00
|
|
|
BUF.append("* {} {}, {} €".format(record, name, amount))
|
2022-09-15 11:09:29 +02:00
|
|
|
BUF.append(" NOTIFICATION DONATEUR")
|
|
|
|
BUF.append("")
|
|
|
|
|
|
|
|
mailtext = get_file_content_all(MODALITY_MAIL) + "\n"
|
|
|
|
mailtext = mailtext.replace("NOM_DONNEUR", name)
|
2022-10-09 00:31:06 +02:00
|
|
|
mailtext = mailtext.replace("NUMERO_DON", str(record))
|
2022-09-15 11:09:29 +02:00
|
|
|
mailtext = mailtext.replace("MONTANT_DON", amount)
|
|
|
|
mailtext = mailtext.replace("MODE_DON", year)
|
|
|
|
mailheaders = get_file_content_all(MODALITY_MAIL_HEADERS) + "\n"
|
|
|
|
mailheaders = mailheaders.replace("COURRIEL_DONNEUR", email)
|
|
|
|
|
2022-10-09 00:30:06 +02:00
|
|
|
print(mailtext)
|
|
|
|
#sendmail(mailheaders, mailtext)
|
2022-09-15 11:09:29 +02:00
|
|
|
|
2022-10-08 19:04:42 +02:00
|
|
|
def validate(record):
|
2022-09-15 11:09:29 +02:00
|
|
|
|
|
|
|
# Get infos
|
2022-10-08 19:04:42 +02:00
|
|
|
request = "SELECT * FROM acc_transactions tr " +\
|
|
|
|
"INNER JOIN acc_transactions_lines l " +\
|
|
|
|
" ON tr.id = l.id_transaction " +\
|
|
|
|
"WHERE tr.notes LIKE '%{}%' and id_account = 469".format(record)
|
2022-09-15 11:09:29 +02:00
|
|
|
# Note: su.id_service = 1 parceque la cotisation correspond au service 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
answer = gestion_read(request)["results"][-1]
|
|
|
|
except:
|
2022-10-08 22:37:31 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
name, surname, address, postal_code, city, email, amount, mode, truc = \
|
|
|
|
get_file_content_all(WORKDIR+"/transition/"+record).split("\n")
|
2022-09-15 11:09:29 +02:00
|
|
|
|
|
|
|
date = datetime.datetime.strptime(
|
|
|
|
answer["date"],'%Y-%m-%d').strftime("%d/%m/%Y")
|
|
|
|
|
2022-10-08 22:37:31 +02:00
|
|
|
filename = "{}_reçu_{}_{}".format(
|
|
|
|
name,
|
|
|
|
record,
|
2022-09-15 11:09:29 +02:00
|
|
|
date.replace("/", "."))
|
|
|
|
|
2022-10-08 22:37:31 +02:00
|
|
|
BUF.append("* {} {}, {} €".format(record, name, "{},{}".format(
|
|
|
|
str(answer["credit"])[:-2],
|
|
|
|
str(answer["credit"])[-2:])))
|
2022-09-15 11:09:29 +02:00
|
|
|
BUF.append(" VALIDATION DON")
|
|
|
|
BUF.append("")
|
|
|
|
|
|
|
|
# Generate receipt
|
2022-10-08 22:37:31 +02:00
|
|
|
latexfile = get_file_content_all("RECU_FISCAL.tex")
|
|
|
|
latexfile = latexfile.replace("NUMERO-DON", record)
|
2022-09-15 11:09:29 +02:00
|
|
|
latexfile = latexfile.replace("ANNEE-CIVILE", answer["date"][:4])
|
2022-10-08 22:37:31 +02:00
|
|
|
latexfile = latexfile.replace("NOM-DONATEUR", name)
|
|
|
|
latexfile = latexfile.replace("STATUT-DONATEUR", "Personne physique") # XXX
|
2022-09-15 11:09:29 +02:00
|
|
|
latexfile = latexfile.replace("ADRESSE-DONATEUR", "{}, {} {}".format(
|
2022-10-08 22:37:31 +02:00
|
|
|
address,
|
|
|
|
postal_code,
|
|
|
|
city))
|
2022-09-15 11:09:29 +02:00
|
|
|
latexfile = latexfile.replace("SOMME", "{},{}".format(
|
2022-10-08 22:37:31 +02:00
|
|
|
str(answer["credit"])[:-2],
|
|
|
|
str(answer["credit"])[-2:]))
|
2022-09-15 11:09:29 +02:00
|
|
|
latexfile = latexfile.replace("DATE-VERSEMENT", date)
|
2022-10-08 22:37:31 +02:00
|
|
|
latexfile = latexfile.replace("MODE-VERSEMENT", answer["reference"])
|
|
|
|
latexfile = latexfile.replace("FORME-DON", "Déclaration de don manuel") # XXX
|
|
|
|
latexfile = latexfile.replace("NATURE-DON", "Numéraire") # XXX
|
2022-09-15 11:09:29 +02:00
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
# Preparing mail
|
2022-10-08 22:48:33 +02:00
|
|
|
mailheaders = get_file_content_all(RECEPT_MAIL_HEADERS).replace(
|
|
|
|
"COURRIEL-DON",
|
|
|
|
email) + "\n"
|
|
|
|
mailtext = get_file_content_all(RECEPT_MAIL).replace("DATE-DON", date) + "\n"
|
|
|
|
mailtattach = get_file_content_all(RECEPT_MAIL_ATTACHMENT).replace(
|
2022-10-08 22:52:34 +02:00
|
|
|
"DATE-DON",
|
|
|
|
record + "_" + date.replace("/", ".")) + "\n"
|
2022-09-15 11:09:29 +02:00
|
|
|
|
|
|
|
# Opening PDF file as binary
|
|
|
|
attachment = open(WORKDIR+"/validé/"+filename+".pdf", "rb")
|
|
|
|
data = attachment.read()
|
|
|
|
attachment.close()
|
|
|
|
|
|
|
|
ending = "--------------3yxkFgv0AINs5nd0i6BJrWaV--"
|
|
|
|
|
|
|
|
sendmail_with_attachment(mailheaders, mailtext, mailtattach, data, ending)
|
|
|
|
|
|
|
|
# The end
|
2022-10-08 22:37:31 +02:00
|
|
|
os.remove(WORKDIR+"/transition/"+record)
|
|
|
|
|
|
|
|
return True
|
2022-09-15 11:09:29 +02:00
|
|
|
|
2022-10-08 19:04:42 +02:00
|
|
|
def check_record(intent):
|
|
|
|
numero, content = get_file_content_all(intent).split("|")
|
|
|
|
name, surname, address, postal_code, city, email, amount, mode = \
|
|
|
|
content.split(";")
|
|
|
|
|
|
|
|
BUF.append("* {} {}, {} €".format(numero, name+" "+surname, amount))
|
|
|
|
BUF.append(" NOUVEAU DON")
|
|
|
|
BUF.append("")
|
|
|
|
|
|
|
|
lines = [ "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format(
|
|
|
|
name, surname, address, postal_code, city, email, amount, mode
|
|
|
|
) ]
|
|
|
|
set_file_content(WORKDIR+"/transition/"+numero, lines)
|
|
|
|
if numero in os.listdir(WORKDIR+"/transition/"):
|
|
|
|
os.remove(intent)
|
|
|
|
|
2022-09-15 11:09:29 +02:00
|
|
|
def validate_donors():
|
2022-10-08 19:04:42 +02:00
|
|
|
|
|
|
|
# Get new
|
|
|
|
for new_intent in os.listdir(WORKDIR+"/nouveau"):
|
|
|
|
check_record(WORKDIR+"/nouveau/"+new_intent)
|
|
|
|
|
|
|
|
# Validate record
|
2022-09-15 11:09:29 +02:00
|
|
|
for record in os.listdir(WORKDIR+"/transition"):
|
2022-10-08 22:37:31 +02:00
|
|
|
if not validate(record):
|
2022-10-09 00:30:06 +02:00
|
|
|
notify_unpaid(record)
|
2022-09-15 11:09:29 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
setup_workdir()
|
|
|
|
#GET DATA()
|
|
|
|
validate_donors()
|
|
|
|
|
|
|
|
# End of work
|
|
|
|
# Launch summary mail
|
|
|
|
mailheaders = get_file_content_all(SUMMARY_MAIL) + "\n"
|
|
|
|
mailtext = ""
|
|
|
|
|
|
|
|
is_sendable = False;
|
|
|
|
for line in BUF:
|
|
|
|
mailtext += line + "\n"
|
|
|
|
is_sendable = True;
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
if is_sendable:
|
|
|
|
sendmail(mailheaders, mailtext)
|
|
|
|
|
|
|
|
## Bootstrap
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|