gestion/controle_don/check_don.sh

174 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# Check_don
# Copyright 2022 Adrien Bourmault
# Licence AGPL v3
set -e
contains()
{
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]
}
get_nom()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 1
}
get_prenom()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 2
}
get_adresse()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 3
}
get_code_postal()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 4
}
get_ville()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 5
}
get_courriel()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 6
}
get_somme()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 7
}
get_mode()
{
find /srv/validation_don.d/*pre_don* -exec cat -v {} \; -exec echo " " \; | grep "$1" | cut -d "|" -f 2 | cut -d ";" -f 8
}
################################################################################
# CREER ENVIRONNEMENT DE TRAVAIL
PASSWORD=$(cat ~/.secret/gestion_api_password)
VALIDATION_D=$(cat /etc/gestion-tresorerie/validation_don.conf)
PRE_VALID_F="pre-valid"
VALID_F="valid"
POST_VALID_F="post-valid"
ENVOI=$((0))
SORTIE=$(mktemp /tmp/temporary-file.XXXXXXXX)
echo "From: root@gestion.a-lec.org (Cron Daemon)" >> "$SORTIE"
echo "To: tresorier@a-lec.org" >> "$SORTIE"
echo "Subject: [Tresorerie] Nouveau don" >> "$SORTIE"
echo "MIME-Version: 1.0" >> "$SORTIE"
echo "Content-Type: text/plain; charset=UTF-8" >> "$SORTIE"
echo "Content-Transfer-Encoding: 8bit" >> "$SORTIE"
echo " " >> "$SORTIE"
if [ ! -d "$VALIDATION_D" ]; then
mkdir -p "$VALIDATION_D"
fi
if [ ! -f "$VALIDATION_D/$PRE_VALID_F" ]; then
echo " " > "$VALIDATION_D/$PRE_VALID_F"
fi
if [ ! -f "$VALIDATION_D/$VALID_F" ]; then
echo " " > "$VALIDATION_D/$VALID_F"
fi
if [ ! -f "$VALIDATION_D/$POST_VALID_F" ]; then
echo " " > "$VALIDATION_D/$POST_VALID_F"
fi
POST_VALID_L=$(cat $VALIDATION_D/$POST_VALID_F) # TROISIEME TOUR
VALID_L=$(cat $VALIDATION_D/$VALID_F) # DEUXIEME TOUR
PRE_VALID_L=$(cat $VALIDATION_D/$PRE_VALID_F) # PREMIER TOUR
DONS_EN_ATTENTE=$(find $VALIDATION_D/*pre_don* -exec cat -v {} \; -exec echo " " \; | cut -d "|" -f 1)
# LANCER VALIDATION
# CONSTRUIRE L'IMAGE POST-VALIDATION (TROISIEME TOUR)
if [ ! -z "$VALID_L" ]
then
for don in $VALID_L
do
POST_VALID_L="$POST_VALID_L $don"
echo -e "VALIDATION FAITE DE $(get_nom $don) ($don)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
./valider_don.sh "$don" "$(get_nom $don) $(get_prenom $don)" "Personne physique" "$(get_adresse $don) $(get_code_postal $don) $(get_ville $don)" "$(get_somme $don)" "$(get_courriel $don)" >> "$SORTIE"
done
POST_VALID_L=$(echo $POST_VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
VALID_L=" "
echo "$POST_VALID_L" > "$VALIDATION_D/$POST_VALID_F"
fi
# CONSTRUIRE L'IMAGE DE VALIDATION (DEUXIEME TOUR)
if [ ! -z "$PRE_VALID_L" ]
then
for don in $PRE_VALID_L
do
if [ ! -z "$POST_VALID_L" ] && ! contains "${POST_VALID_L}" "${don}"
then
# RECUPERATION DONNEES
requete=$(curl https://api666:$PASSWORD@gestion.a-lec.org/api/sql/ -s -d "SELECT * FROM acc_transactions tr INNER JOIN acc_transactions_lines l ON tr.id = l.id_transaction WHERE tr.notes = $don and id_account = 469")
requete=$(echo -e $requete | tr -d "{}\"[] " | cut -c 10-1000)
if [ ! -z "$requete" ]
then
VALID_L="$VALID_L $don"
echo -e "VALIDATION DE $(get_nom $don), ($don)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
fi
done
VALID_L=$(echo $VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
echo "$VALID_L" > "$VALIDATION_D/$VALID_F"
fi
# CONSTRUIRE LIMAGE DE PRE-VALIDATION (PREMIER TOUR)
if [ ! -z "$DONS_EN_ATTENTE" ]
then
for don in $DONS_EN_ATTENTE
do
if [ ! -z "$PRE_VALID_L" ] && ! contains "${PRE_VALID_L}" "${don}" && [ ! -z "$VALID_L" ] && ! contains "${VALID_L}" "${don}" && [ ! -z "$POST_VALID_L" ] && ! contains "${POST_VALID_L}" "${don}"
then
PRE_VALID_L="$PRE_VALID_L $don"
echo -e "PRE-VALIDATION DE $(get_nom $don), $(get_somme $don) € par $(get_mode $don) ($don)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
# RECUPERATION DONNEES
requete=$(curl https://api666:$PASSWORD@gestion.a-lec.org/api/sql/ -s -d "SELECT * FROM acc_transactions tr INNER JOIN acc_transactions_lines l ON tr.id = l.id_transaction WHERE tr.notes = $don and id_account = 469")
requete=$(echo -e $requete | tr -d "{}\"[] " | cut -c 9-1000)
if [ -z "$requete" ]
then
bash -c "./envoi_instructions_don.sh $don"
echo -e " COURRIEL INSTRUCTION TRANSMIS" >> "$SORTIE"
fi
fi
done
PRE_VALID_L=$(echo $PRE_VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
DONS_EN_ATTENTE=" "
echo "$PRE_VALID_L" > "$VALIDATION_D/$PRE_VALID_F"
fi
echo " " >> "$SORTIE"
echo "------------------------------------------------------------" >> "$SORTIE"
echo -e "Récapitulatif:" >> "$SORTIE"
echo -e "Dons réglés: $POST_VALID_L" >> "$SORTIE"
echo -e "Dons validés: $VALID_L" >> "$SORTIE"
echo -e "Dons en validation (attente paiement): $PRE_VALID_L" >> "$SORTIE"
echo -e "Dons en attente: $DONS_EN_ATTENTE" >> "$SORTIE"
# ENVOI
if [ $ENVOI -ge 1 ]
then
(cat "$SORTIE") | /usr/sbin/sendmail -i -- tresorier@a-lec.org
fi
rm -rf "$SORTIE"