2022-01-19 16:04:42 +01:00
|
|
|
<?php
|
|
|
|
|
2023-09-28 11:08:46 +02:00
|
|
|
namespace Paheko\Plugin\RecusFiscaux;
|
2022-01-19 16:04:42 +01:00
|
|
|
|
2023-09-28 11:08:46 +02:00
|
|
|
use Paheko\DB;
|
2023-10-04 14:27:56 +02:00
|
|
|
use Paheko\Users\DynamicFields;
|
2022-01-25 17:19:31 +01:00
|
|
|
use KD2\ZipWriter;
|
2022-01-19 16:04:42 +01:00
|
|
|
|
2022-01-27 09:10:00 +01:00
|
|
|
class Utils
|
2022-01-19 16:04:42 +01:00
|
|
|
{
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return informations sur les tarifs
|
|
|
|
*/
|
|
|
|
public static function getTarifs()
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
id,
|
|
|
|
id_service as idActivite,
|
|
|
|
label,
|
|
|
|
description,
|
|
|
|
amount as montant
|
|
|
|
FROM services_fees');
|
2024-09-18 09:43:50 +02:00
|
|
|
return $db->getGrouped($sql);
|
2024-04-11 11:50:29 +02:00
|
|
|
}
|
2022-02-10 17:05:24 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return informations sur les activités
|
|
|
|
*/
|
|
|
|
public static function getActivites()
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
services.id,
|
|
|
|
services.label,
|
|
|
|
services.description
|
|
|
|
FROM services');
|
2024-09-18 09:43:50 +02:00
|
|
|
return $db->getGrouped($sql);
|
2024-04-11 11:50:29 +02:00
|
|
|
}
|
2022-02-10 17:05:24 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return comptes sur lesquels des versements de membres ont été faits
|
|
|
|
* @param string $annee
|
|
|
|
* @param $op : opérateur de combinaison des comptes
|
|
|
|
* @param array $comptes
|
|
|
|
*/
|
|
|
|
public static function getComptes($annee, $op, $comptes)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
acc_accounts.id,
|
|
|
|
acc_years.label,
|
|
|
|
acc_accounts.code as codeCompte,
|
|
|
|
acc_accounts.label as nomCompte
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN users
|
|
|
|
ON acc_transactions_users.id_user = users.id
|
|
|
|
INNER JOIN acc_transactions
|
|
|
|
ON acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_accounts
|
|
|
|
ON acc_transactions_lines.id_account = acc_accounts.id
|
|
|
|
INNER JOIN acc_years
|
|
|
|
ON acc_transactions.id_year = acc_years.id
|
|
|
|
WHERE
|
|
|
|
(strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_accounts.%s
|
|
|
|
)
|
|
|
|
GROUP by acc_accounts.id
|
|
|
|
ORDER by acc_accounts.code',
|
|
|
|
$annee,
|
|
|
|
$db->where('code', $op, $comptes)
|
|
|
|
);
|
2024-09-18 09:43:50 +02:00
|
|
|
return $db->getGrouped($sql);
|
2024-04-11 11:50:29 +02:00
|
|
|
}
|
2022-05-18 12:29:19 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return tarifs des activités et comptes ayant des versements de
|
|
|
|
* membres dans l'année
|
|
|
|
* @param string $annee
|
|
|
|
* @param $op : opérateur de combinaison des comptes
|
|
|
|
* @param array $comptes
|
|
|
|
*/
|
|
|
|
public static function getTarifsComptes($annee, $op, $comptes)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
services_users.id_fee as idTarif,
|
|
|
|
acc_accounts.id as idCompte,
|
|
|
|
acc_accounts.code as codeCompte
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN acc_transactions
|
|
|
|
ON acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN services_users
|
|
|
|
ON acc_transactions_users.id_service_user = services_users.id
|
|
|
|
INNER JOIN services_fees
|
|
|
|
ON services_users.id_fee = services_fees.id
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_accounts
|
|
|
|
ON acc_transactions_lines.id_account = acc_accounts.id
|
|
|
|
WHERE
|
|
|
|
(strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_accounts.%s
|
|
|
|
)
|
|
|
|
GROUP BY services_fees.id, acc_accounts.code
|
|
|
|
ORDER BY acc_accounts.code
|
|
|
|
',
|
|
|
|
$annee,
|
|
|
|
$db->where('code', $op, $comptes)
|
|
|
|
);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
2022-03-24 19:00:39 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return versements correspondants à l'année donnée
|
|
|
|
* @param $annee
|
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
|
|
|
*/
|
|
|
|
public static function getVersementsPersonnes($annee, $op, $comptes, $champsNom)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
users.id as idUser,
|
|
|
|
acc_accounts.id as idCompte,
|
|
|
|
acc_accounts.code as codeCompte,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN users
|
|
|
|
ON acc_transactions_users.id_user = users.id
|
|
|
|
INNER JOIN acc_transactions
|
|
|
|
ON acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_accounts
|
|
|
|
ON acc_transactions_lines.id_account = acc_accounts.id
|
|
|
|
WHERE
|
|
|
|
(strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_accounts.%s
|
|
|
|
)
|
|
|
|
GROUP BY acc_transactions.id, acc_accounts.id
|
|
|
|
ORDER by %s, acc_accounts.code, acc_transactions.date',
|
|
|
|
$annee,
|
|
|
|
$db->where('code', $op, $comptes),
|
|
|
|
$tri
|
|
|
|
);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
2022-02-10 17:05:24 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return versements correspondants à :
|
|
|
|
* @param $annee : année fiscale
|
|
|
|
* @param array $tarifs : tarifs sélectionnés
|
|
|
|
* @param array $comptes : comptes associés aux tarifs
|
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
|
|
|
* @remarks tri par tarif, nom, compte, date
|
|
|
|
*/
|
|
|
|
public static function getVersementsTarifsComptes($annee,
|
|
|
|
$tarifs,
|
|
|
|
$comptes,
|
|
|
|
$champsNom)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$condition = Utils::combinerTarifsComptes($tarifs, $comptes);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
services_fees.id as idTarif,
|
|
|
|
acc_accounts.id as idCompte,
|
|
|
|
acc_accounts.code as codeCompte,
|
|
|
|
users.id as idUser,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN users
|
|
|
|
ON acc_transactions_users.id_user = users.id
|
|
|
|
INNER JOIN acc_transactions
|
|
|
|
ON acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN services_users
|
|
|
|
ON acc_transactions_users.id_service_user = services_users.id
|
|
|
|
INNER JOIN services_fees
|
|
|
|
ON services_users.id_fee = services_fees.id
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_accounts
|
|
|
|
ON acc_transactions_lines.id_account = acc_accounts.id
|
|
|
|
WHERE
|
|
|
|
(strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
%s
|
|
|
|
)
|
|
|
|
GROUP BY acc_transactions.id, acc_accounts.id
|
|
|
|
ORDER by %s, acc_accounts.code, acc_transactions.date',
|
|
|
|
$annee,
|
|
|
|
$condition,
|
|
|
|
$tri
|
|
|
|
);
|
|
|
|
// error_log("\ngetVersementsTarifsComptes : sql=" . $sql);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
2022-03-24 19:00:39 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return versements correspondants à :
|
|
|
|
* @param $annee année fiscale
|
|
|
|
* @param $comptesIsoles comptes NON associés à un tarif
|
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
|
|
|
* @remarks tri par nom, compte, date
|
|
|
|
*/
|
|
|
|
public static function getVersementsComptes($annee,
|
|
|
|
$comptesIsoles,
|
|
|
|
$champsNom)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
0 as idTarif,
|
|
|
|
acc_accounts.id as idCompte,
|
|
|
|
acc_accounts.code as codeCompte,
|
|
|
|
users.id as idUser,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN users
|
|
|
|
ON acc_transactions_users.id_user = users.id
|
|
|
|
INNER JOIN acc_transactions
|
|
|
|
ON acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
INNER JOIN acc_accounts
|
|
|
|
ON acc_transactions_lines.id_account = acc_accounts.id
|
|
|
|
WHERE
|
|
|
|
(strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_accounts.%s
|
|
|
|
)
|
|
|
|
GROUP BY acc_transactions.id, acc_accounts.id
|
|
|
|
ORDER by %s, acc_accounts.code, acc_transactions.date
|
|
|
|
',
|
|
|
|
$annee,
|
|
|
|
$db->where('id', 'in', $comptesIsoles),
|
|
|
|
$tri
|
|
|
|
);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
2022-02-17 10:21:33 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return personnes ayant versé des dons pour une année donnée
|
|
|
|
* @param $annee
|
|
|
|
* @param array $champsNom : champs qui définissent le nom et le prénom d'une personne
|
|
|
|
*/
|
|
|
|
public static function getDonateurs($annee, $champsNom) : array
|
|
|
|
{
|
|
|
|
// concaténer les champs nom/prénoms pour la sélection
|
|
|
|
$nom = Utils::combinerChamps($champsNom);
|
|
|
|
// et pour le tri
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
users.id as idUser,
|
|
|
|
users.numero,
|
|
|
|
users.email,
|
|
|
|
row_number() over(order by %s) as rang,
|
|
|
|
%s as nom,
|
|
|
|
users.adresse as adresse,
|
|
|
|
users.code_postal as codePostal,
|
|
|
|
users.ville as ville
|
|
|
|
FROM
|
|
|
|
acc_transactions_users,
|
|
|
|
users,
|
|
|
|
acc_transactions
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
WHERE (
|
|
|
|
strftime("%%Y", acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_user = users.id
|
|
|
|
)
|
|
|
|
GROUP by users.id
|
|
|
|
ORDER by %1$s COLLATE U_NOCASE
|
|
|
|
',
|
|
|
|
$tri,
|
|
|
|
$nom,
|
|
|
|
$annee
|
|
|
|
);
|
|
|
|
$donateurs = array();
|
|
|
|
foreach (DB::getInstance()->iterate($sql) as $personne)
|
|
|
|
{
|
|
|
|
$donateurs[$personne->idUser] = new Personne($personne->idUser,
|
|
|
|
$personne->numero,
|
|
|
|
$personne->email,
|
|
|
|
$personne->rang,
|
|
|
|
$personne->nom,
|
|
|
|
$personne->adresse,
|
|
|
|
$personne->codePostal,
|
|
|
|
$personne->ville);
|
|
|
|
}
|
|
|
|
return $donateurs;
|
|
|
|
}
|
2022-02-17 08:49:04 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* combiner les champs avec un opérateur
|
|
|
|
* @param array $champs : liste (non vide) de champs
|
|
|
|
* @return chaîne combinée
|
|
|
|
*/
|
|
|
|
private static function combinerChamps($champs)
|
|
|
|
{
|
|
|
|
$op = ' || " " || ';
|
|
|
|
$result = 'ifnull(users.' . $champs[0] . ', "")';
|
|
|
|
for ($i = 1; $i < count($champs); ++$i)
|
|
|
|
{
|
|
|
|
$result .= $op . 'ifnull(users.' . $champs[$i] . ', "")';
|
|
|
|
}
|
|
|
|
return 'trim(' . $result . ')';
|
|
|
|
}
|
2022-02-18 12:51:13 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* combiner les clés de tri
|
|
|
|
* @param clés de tri
|
|
|
|
* @return chaîne combinée
|
|
|
|
*/
|
|
|
|
private static function combinerTri(array $champs) : string
|
|
|
|
{
|
|
|
|
$tri = 'users.' . $champs[0];
|
|
|
|
for ($i = 1; $i < count($champs); ++$i)
|
|
|
|
{
|
|
|
|
$tri .= ', users.' . $champs[$i];
|
|
|
|
}
|
|
|
|
return $tri;
|
|
|
|
}
|
2022-01-19 16:04:42 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* combiner chaque tarif avec le numéro de compte associé
|
|
|
|
*/
|
|
|
|
private static function combinerTarifsComptes($tarifs, $comptes)
|
|
|
|
{
|
|
|
|
$condition = '(';
|
|
|
|
$lesCond = array_map(fn($e1, $e2) : string =>
|
|
|
|
"(services_fees.id = '$e1' AND acc_accounts.id = '$e2')",
|
|
|
|
$tarifs, $comptes);
|
|
|
|
$nb = 0;
|
|
|
|
foreach ($lesCond as $cond)
|
|
|
|
{
|
|
|
|
if ($nb > 0) { $condition .= ' OR '; }
|
|
|
|
$condition .= $cond;
|
|
|
|
++$nb;
|
|
|
|
}
|
|
|
|
$condition .= ')';
|
|
|
|
return $condition;
|
|
|
|
}
|
2023-04-06 18:16:46 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* @return liste des années fiscales
|
|
|
|
*/
|
|
|
|
public static function getAnneesFiscales() : array
|
|
|
|
{
|
|
|
|
$rows = DB::getInstance()->get(
|
|
|
|
"SELECT strftime('%Y', start_date) as annee
|
|
|
|
FROM acc_years
|
|
|
|
UNION
|
|
|
|
SELECT strftime('%Y', end_date) as annee
|
|
|
|
FROM acc_years
|
|
|
|
ORDER by annee DESC"
|
|
|
|
);
|
|
|
|
$anneesFiscales = array();
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$anneesFiscales[] = $row->annee;
|
|
|
|
}
|
|
|
|
return $anneesFiscales;
|
|
|
|
}
|
2022-01-27 09:10:00 +01:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
public static function getLignesReduction($lesTaux)
|
|
|
|
{
|
|
|
|
foreach ($lesTaux as $elem)
|
|
|
|
{
|
|
|
|
$lignes[$elem->taux] = $elem->remarque;
|
|
|
|
}
|
|
|
|
return $lignes;
|
|
|
|
}
|
2022-04-24 14:07:36 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* récupérer dans la config du plugin les champs des membres
|
|
|
|
* utilisés pour le nom et le prénom ; ajouter/supprimer les
|
|
|
|
* modifications par rapport à la config paheko
|
|
|
|
* @return array tableau des champs : clé = nom, valeur = { titre, position }
|
|
|
|
*/
|
|
|
|
public static function getChampsNom($config, $plugin) : array
|
|
|
|
{
|
|
|
|
// récupérer dans la config du plugin les champs mémorisés
|
|
|
|
// pour le nom et le prénom (le tableau est vide si pas mémorisé)
|
|
|
|
$champsNom = (array) $plugin->getConfig('champsNom');
|
2022-03-29 14:49:03 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
// récupérer dans la config Paheko les champs des membres
|
|
|
|
// utilisés pour le nom et le prénom
|
|
|
|
$champsPaheko = DynamicFields::getInstance()->listAssocNames();
|
2022-03-29 14:49:03 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
foreach ($champsPaheko as $name => $title)
|
|
|
|
{
|
|
|
|
if (stristr($title, 'nom'))
|
|
|
|
{
|
|
|
|
// retenir les champs dont le titre contient le terme 'nom'
|
|
|
|
// est-il présent dans la config du plugin ?
|
|
|
|
if (! array_key_exists($name, $champsNom))
|
|
|
|
{
|
|
|
|
// absent => l'ajouter
|
|
|
|
$champ = new \stdClass();
|
|
|
|
$champ->titre = $title;
|
|
|
|
$champ->position = 0;
|
|
|
|
$champsNom[$name] = $champ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// opération symétrique : un champ mémorisé dans la config du
|
|
|
|
// plugin a-t-il disparu de la config paheko ?
|
|
|
|
foreach ($champsNom as $nom => $champ)
|
|
|
|
{
|
|
|
|
if (! array_key_exists($nom, $champsPaheko))
|
|
|
|
{
|
|
|
|
// absent => le supprimer
|
|
|
|
unset($champsNom[$nom]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// mettre à jour la config du plugin
|
|
|
|
$plugin->setConfigProperty('champsNom', $champsNom);
|
|
|
|
return $champsNom;
|
|
|
|
}
|
2022-03-29 14:49:03 +02:00
|
|
|
|
2024-04-11 11:50:29 +02:00
|
|
|
/**
|
|
|
|
* enregistrer les fichiers dans une archive zip
|
|
|
|
* @param $fileList : liste des fichiers à archiver
|
|
|
|
* @param $year : pour générer le nom de l'archive
|
|
|
|
* @param $archiveDir : ne sert plus
|
|
|
|
*/
|
|
|
|
static function makeArchive(
|
|
|
|
$fileList,
|
|
|
|
$year,
|
|
|
|
$archiveDir = null)
|
|
|
|
{
|
|
|
|
$zipFilename = "recus_dons" . $year . ".zip";
|
|
|
|
header('Content-type: application/zip');
|
|
|
|
header(sprintf('Content-Disposition: attachment; filename="%s"', $zipFilename));
|
|
|
|
$zip = new ZipWriter('php://output');
|
|
|
|
$zip->setCompression(0);
|
|
|
|
foreach ($fileList as $fileName)
|
|
|
|
{
|
|
|
|
$zip->add(basename($fileName), null, $fileName);
|
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
} // makeArchive
|
2022-01-19 16:04:42 +01:00
|
|
|
}
|