gestion/controle_cotisation/check_cotisation.sh

235 lines
6.4 KiB
Bash
Raw Normal View History

2022-09-13 22:20:40 +02:00
#!/bin/bash
# Check_cotisation
# Copyright 2022 Adrien Bourmault
# Licence AGPL v3
contains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]
}
get_membre_nom()
{
requete=$(curl https://api666:$PASSWORD@gestion.a-lec.org/api/sql/ -s -d "SELECT nom FROM membres m WHERE m.id = $1;")
# Note: su.id_service = 1 parceque la cotisation correspond au service 1
echo -e $requete | cut -d ":" -f 3 | tr -d "\"}]"
}
################################################################################
# CREER ENVIRONNEMENT DE TRAVAIL
PASSWORD=$(cat ~/.secret/gestion_api_password)
VALIDATION_D=$(cat /etc/gestion-tresorerie/validation.conf)
PRE_VALID_F="pre-valid"
RELANCE_F="relance"
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] Mis-a-jour cotisations " >> "$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
touch "$VALIDATION_D/$PRE_VALID_F"
fi
if [ ! -f "$VALIDATION_D/$VALID_F" ]; then
touch "$VALIDATION_D/$VALID_F"
fi
if [ ! -f "$VALIDATION_D/$POST_VALID_F" ]; then
touch "$VALIDATION_D/$POST_VALID_F"
fi
if [ ! -f "$VALIDATION_D/$RELANCE_F" ]; then
touch "$VALIDATION_D/$RELANCE_F"
fi
# LANCER VALIDATION
# CONSTRUIRE L'IMAGE POST-VALIDATION (TROISIEME TOUR)
IMPAYE_L=$(./get_impayes.sh) # cotisations pas à jour
EXPIRE_L=$(./get_expires.sh) # cotisations expirées
RELANCE_L=$(cat $VALIDATION_D/$RELANCE_F)
VALID_L=$(cat $VALIDATION_D/$VALID_F) # DEUXIEME TOUR
POST_VALID_L=$(cat $VALIDATION_D/$POST_VALID_F) # TROISIEME TOUR
if [ ! -z "$VALID_L" ]
then
for adherent in $VALID_L
do
if ! contains "${IMPAYE_L}" "${adherent}"
then
if ! contains "${EXPIRE_L}" "${adherent}"
then
POST_VALID_L="$POST_VALID_L $adherent"
NOM_ADHERENT=$(get_membre_nom $adherent)
echo -e "VALIDATION FAITE DE $NOM_ADHERENT ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
./valider_cotisation.sh "$adherent" >> "$SORTIE"
fi
fi
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
# CONTROLER LES ADHERENTS VALIDES
if [ ! -z "$POST_VALID_L" ]
then
NEW_POST_VALID_L=" "
for adherent in $POST_VALID_L
do
if ! contains "${IMPAYE_L}" "${adherent}"
then
if ! contains "${EXPIRE_L}" "${adherent}"
then
NEW_POST_VALID_L="$NEW_POST_VALID_L $adherent"
fi
fi
done
POST_VALID_L=$(echo $NEW_POST_VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
echo "$POST_VALID_L" > "$VALIDATION_D/$POST_VALID_F"
fi
# CONSTRUIRE L'IMAGE DE VALIDATION (DEUXIEME TOUR)
PRE_VALID_L=$(cat $VALIDATION_D/$PRE_VALID_F) # PREMIER TOUR
if [ ! -z "$PRE_VALID_L" ]
then
for adherent in $PRE_VALID_L
do
if [ ! -z "$POST_VALID_L" ]
then
if ! contains "${POST_VALID_L}" "${adherent}"
then
VALID_L="$VALID_L $adherent"
echo -e "VALIDATION DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
fi
done
VALID_L=$(echo $VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
PRE_VALID_L=" "
echo "$VALID_L" > "$VALIDATION_D/$VALID_F"
fi
# CONSTRUIRE L'IMAGE DE PRE-VALIDATION (PREMIER TOUR)
A_JOUR_L=$(./get_payes.sh) # cotisations à jour
if [ ! -z "$A_JOUR_L" ]
then
for adherent in $A_JOUR_L
do
if [ ! -z "$VALID_L" ]
then
if ! contains "${VALID_L}" "${adherent}"
then
if [ ! -z "$POST_VALID_L" ]
then
if ! contains "${POST_VALID_L}" "${adherent}"
then
PRE_VALID_L="$PRE_VALID_L $adherent"
echo -e "PRE-VALIDATION DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
fi
fi
else
PRE_VALID_L="$PRE_VALID_L $adherent"
echo -e "PRE-VALIDATION DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
done
PRE_VALID_L=$(echo $PRE_VALID_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
A_JOUR_L=" "
echo "$PRE_VALID_L" > "$VALIDATION_D/$PRE_VALID_F"
fi
# ENVOYER LES RELANCES POUR IMPAYES OU EXPIRES
if [ ! -z "$IMPAYE_L" ]
then
for adherent in $IMPAYE_L
do
if [ ! -z "$RELANCE_L" ]
then
if ! contains "${RELANCE_L}" "${adherent}"
then
echo -e "LANCE OU RELANCE DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ret=$(./envoi_instruction_cotisation.sh $adherent)
if [ -z "$ret" ]
then
RELANCE_L="$RELANCE_L $adherent"
ENVOI=$((ENVOI + 1))
else
echo -e " COURRIEL NON FONCTIONNEL" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
fi
else
echo -e "LANCE OU RELANCE DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ret=$(./envoi_instruction_cotisation.sh $adherent)
if [ -z "$ret" ]
then
RELANCE_L="$RELANCE_L $adherent"
ENVOI=$((ENVOI + 1))
else
echo -e " COURRIEL NON FONCTIONNEL" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
fi
done
RELANCE_L=$(echo $RELANCE_L | tr " " "\n" | sort -u | uniq | tr "\n" " ")
echo "$RELANCE_L" > "$VALIDATION_D/$RELANCE_F"
fi
if [ ! -z "$EXPIRE_L" ]
then
for adherent in $EXPIRE_L
do
if [ ! -z "$RELANCE_L" ]
then
if ! contains "${RELANCE_L}" "${adherent}"
then
echo -e "EXPIRATION DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
else
echo -e "EXPIRATION DE $(get_membre_nom $adherent) ($adherent)" >> "$SORTIE"
ENVOI=$((ENVOI + 1))
fi
done
echo "$RELANCE_L" > "$VALIDATION_D/$RELANCE_F"
fi
echo " " >> "$SORTIE"
echo "------------------------------------------------------------" >> "$SORTIE"
echo -e "Récapitulatif:" >> "$SORTIE"
echo -e "Adhérents impayés: $IMPAYE_L" >> "$SORTIE"
echo -e "Adhérents expirés: $EXPIRE_L" >> "$SORTIE"
echo -e "Adhérents relancés ou avertis: $RELANCE_L" >> "$SORTIE"
echo -e "Adhérents validés: $POST_VALID_L" >> "$SORTIE"
echo -e "Adhérents en validation: $VALID_L" >> "$SORTIE"
echo -e "Adhérents en pré-validation: $PRE_VALID_L" >> "$SORTIE"
# ENVOI
if [ ! -z $1 ] && [ $1 == "debug" ]
then
echo DEBUG
cat "$SORTIE"
else
if [ $ENVOI -ge 1 ]
then
cat "$SORTIE" | /usr/sbin/sendmail -i -- tresorier@a-lec.org
fi
fi
rm -rf "$SORTIE"