2022-01-19 16:04:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Garradin\Plugin\RecusFiscaux;
|
|
|
|
|
|
|
|
use Garradin\DB;
|
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
|
|
|
{
|
2022-02-10 17:05:24 +01:00
|
|
|
/**
|
|
|
|
* @return tarifs demandés
|
|
|
|
* @param array $tarifs
|
|
|
|
*/
|
|
|
|
public static function getTarifs($tarifs)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT id, id_service as idActivite, label, description, amount as montant
|
|
|
|
FROM services_fees
|
|
|
|
WHERE services_fees.%s',
|
|
|
|
$db->where('id', $tarifs));
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return activités correspondant aux tarifs demandés
|
|
|
|
* @param array $tarifs
|
|
|
|
*/
|
|
|
|
public static function getActivites($tarifs)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT services.id, services.label, services.description
|
|
|
|
FROM services
|
|
|
|
LEFT JOIN services_fees ON services_fees.id_service = services.id
|
|
|
|
WHERE services_fees.%s
|
|
|
|
GROUP BY services.id',
|
|
|
|
$db->where('id', $tarifs));
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:00:39 +01: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, $champsNom)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
membres.id as idUser,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN membres on acc_transactions_users.id_user = membres.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 acc_transactions_lines on acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
WHERE
|
|
|
|
(strftime(%s, acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_transactions_lines.credit > 0)
|
|
|
|
ORDER by %s, acc_transactions.date',
|
|
|
|
'"%Y"',
|
|
|
|
$annee,
|
|
|
|
$tri
|
|
|
|
);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:05:24 +01:00
|
|
|
/**
|
|
|
|
* @return versements correspondants à l'année et aux tarifs donnés
|
|
|
|
* @param $annee
|
|
|
|
* @param array $tarifs
|
2022-03-18 20:39:02 +01:00
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
2022-02-10 17:05:24 +01:00
|
|
|
*/
|
2022-03-18 20:39:02 +01:00
|
|
|
public static function getVersementsTarifs($annee, $tarifs, $champsNom)
|
2022-02-10 17:05:24 +01:00
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
2022-03-18 20:39:02 +01:00
|
|
|
$tri = Utils::combinerTri($champsNom);
|
2022-02-10 17:05:24 +01:00
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
services_fees.id as idTarif,
|
|
|
|
membres.id as idUser,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN membres on acc_transactions_users.id_user = membres.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
|
|
|
|
WHERE
|
|
|
|
(strftime(%s, acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
services_fees.%s
|
|
|
|
AND
|
|
|
|
acc_transactions_lines.credit > 0)
|
2022-03-18 20:39:02 +01:00
|
|
|
ORDER by services_fees.id, %s, acc_transactions.date',
|
2022-02-10 17:05:24 +01:00
|
|
|
'"%Y"',
|
|
|
|
$annee,
|
2022-03-18 20:39:02 +01:00
|
|
|
$db->where('id', $tarifs),
|
|
|
|
$tri
|
|
|
|
);
|
2022-02-10 17:05:24 +01:00
|
|
|
return $db->get($sql);
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:00:39 +01:00
|
|
|
/**
|
|
|
|
* @return versements correspondants à l'année et aux comptes donnés
|
|
|
|
* @param $annee
|
|
|
|
* @param array $comptes
|
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
|
|
|
*/
|
|
|
|
public static function getVersementsComptes($annee, $comptes, $champsNom)
|
|
|
|
{
|
|
|
|
$db = DB::getInstance();
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
|
|
|
acc_accounts.code as compte,
|
|
|
|
membres.id as idUser,
|
|
|
|
acc_transactions_lines.credit as versement,
|
|
|
|
acc_transactions.date
|
|
|
|
FROM acc_transactions_users
|
|
|
|
INNER JOIN membres on acc_transactions_users.id_user = membres.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 acc_transactions_lines on acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
WHERE
|
|
|
|
(strftime(%s, acc_transactions.date) = "%d"
|
|
|
|
AND
|
|
|
|
acc_accounts.%s
|
|
|
|
AND
|
|
|
|
acc_transactions_lines.credit > 0)
|
|
|
|
ORDER by acc_accounts.code, %s, acc_transactions.date',
|
|
|
|
'"%Y"',
|
|
|
|
$annee,
|
|
|
|
$db->where('code', $comptes),
|
|
|
|
$tri
|
|
|
|
);
|
|
|
|
return $db->get($sql);
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:21:33 +01:00
|
|
|
/**
|
|
|
|
* Versements totaux par personne pour une année donnée
|
|
|
|
* @param année
|
2022-03-18 20:39:02 +01:00
|
|
|
* @param array $champsNom : liste non vide des champs de nom/prénom
|
2022-02-17 10:21:33 +01:00
|
|
|
*/
|
2022-03-18 20:39:02 +01:00
|
|
|
public static function getVersementsTotaux($annee, $champsNom)
|
2022-02-17 10:21:33 +01:00
|
|
|
{
|
2022-03-18 20:39:02 +01:00
|
|
|
$tri = Utils::combinerTri($champsNom);
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT
|
2022-02-17 10:21:33 +01:00
|
|
|
membres.id as idUser,
|
|
|
|
sum(acc_transactions_lines.credit) AS versement
|
|
|
|
FROM
|
|
|
|
acc_transactions_users,
|
|
|
|
membres,
|
|
|
|
acc_transactions
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
WHERE (
|
2022-03-18 20:39:02 +01:00
|
|
|
strftime(%s, acc_transactions.date) = "%d"
|
2022-02-17 10:21:33 +01:00
|
|
|
AND
|
|
|
|
acc_transactions_lines.credit > 0
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_user = membres.id
|
|
|
|
)
|
|
|
|
GROUP by acc_transactions_users.id_user
|
2022-03-18 20:39:02 +01:00
|
|
|
ORDER by %s COLLATE U_NOCASE',
|
|
|
|
'"%Y"',
|
|
|
|
$annee,
|
|
|
|
$tri);
|
|
|
|
return DB::getInstance()->get($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(membres.' . $champs[0] . ', "")';
|
|
|
|
for ($i = 1; $i < count($champs); ++$i)
|
|
|
|
{
|
|
|
|
$result .= $op . 'ifnull(membres.' . $champs[$i] . ', "")';
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function combinerTri($champs)
|
|
|
|
{
|
|
|
|
$tri = 'membres.' . $champs[0];
|
|
|
|
for ($i = 1; $i < count($champs); ++$i)
|
|
|
|
{
|
|
|
|
$tri .= ', membres.' . $champs[$i];
|
|
|
|
}
|
|
|
|
return $tri;
|
2022-02-17 10:21:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 08:49:04 +01:00
|
|
|
/**
|
|
|
|
* @return personnes ayant versé des dons pour une année donnée
|
|
|
|
* @param $annee
|
2022-03-18 20:39:02 +01:00
|
|
|
* @param array $champsNom : champs qui définissent le nom et le prénom d'une personne
|
2022-02-17 08:49:04 +01:00
|
|
|
*/
|
2022-03-18 20:39:02 +01:00
|
|
|
public static function getDonateurs($annee, $champsNom)
|
2022-02-17 08:49:04 +01:00
|
|
|
{
|
2022-03-18 20:39:02 +01:00
|
|
|
// concaténer les champs nom/prénoms pour la sélection
|
2022-03-18 21:23:42 +01:00
|
|
|
$nom = 'trim(' . Utils::combinerChamps($champsNom) . ') as nom,';
|
2022-03-18 20:39:02 +01:00
|
|
|
// et pour le tri
|
|
|
|
$tri = Utils::combinerTri($champsNom);
|
2022-02-17 08:49:04 +01:00
|
|
|
$sql =
|
|
|
|
"SELECT
|
|
|
|
membres.id as idUser,
|
2022-03-18 20:39:02 +01:00
|
|
|
" .
|
|
|
|
$nom . "
|
2022-02-17 08:49:04 +01:00
|
|
|
membres.adresse as adresse,
|
|
|
|
membres.code_postal as codePostal,
|
|
|
|
membres.ville as ville
|
|
|
|
FROM
|
|
|
|
acc_transactions_users,
|
|
|
|
membres,
|
|
|
|
acc_transactions
|
|
|
|
INNER JOIN acc_transactions_lines
|
|
|
|
ON acc_transactions_lines.id_transaction = acc_transactions.id
|
|
|
|
WHERE (
|
|
|
|
strftime('%Y', acc_transactions.date) = ?
|
|
|
|
AND
|
|
|
|
acc_transactions_lines.credit > 0
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_transaction = acc_transactions.id
|
|
|
|
AND
|
|
|
|
acc_transactions_users.id_user = membres.id
|
|
|
|
)
|
|
|
|
GROUP by membres.id
|
2022-03-18 20:39:02 +01:00
|
|
|
ORDER by " . $tri . " COLLATE U_NOCASE
|
2022-02-17 08:49:04 +01:00
|
|
|
";
|
|
|
|
return DB::getInstance()->get($sql, $annee);
|
|
|
|
}
|
|
|
|
|
2022-02-18 12:51:13 +01:00
|
|
|
public static function getLignesReduction($lesTaux)
|
|
|
|
{
|
|
|
|
foreach ($lesTaux as $elem)
|
|
|
|
{
|
2022-02-25 15:51:48 +01:00
|
|
|
/*
|
2022-02-18 12:51:13 +01:00
|
|
|
$ligne = "taux " . $elem->taux . ", ligne " . $elem->ligne;
|
|
|
|
if ($elem->remarque != "") {
|
|
|
|
$ligne .= ", " . $elem->remarque;
|
|
|
|
}
|
|
|
|
$lignes[$elem->taux] = $ligne;
|
2022-02-25 15:51:48 +01:00
|
|
|
*/
|
|
|
|
$lignes[$elem->taux] = $elem->remarque;
|
2022-02-18 12:51:13 +01:00
|
|
|
}
|
|
|
|
return $lignes;
|
|
|
|
}
|
|
|
|
public static function getLigneReduction($taux)
|
|
|
|
{
|
|
|
|
return $_SESSION['ligneReduction'][$taux];
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:04:42 +01:00
|
|
|
/**
|
|
|
|
* @return liste de toutes les activités, tarifs et comptes associés
|
|
|
|
*/
|
|
|
|
public static function getActivitesTarifsEtComptes()
|
|
|
|
{
|
|
|
|
return DB::getInstance()->get(
|
|
|
|
"SELECT
|
2022-01-28 15:40:21 +01:00
|
|
|
services.id as idActivite,
|
|
|
|
services.label as titreActivite,
|
|
|
|
services.description as descActivite,
|
|
|
|
services_fees.id as idTarif,
|
|
|
|
services_fees.label as titreTarif,
|
2022-01-19 16:04:42 +01:00
|
|
|
services_fees.description as descTarif,
|
2022-01-28 15:40:21 +01:00
|
|
|
acc_accounts.code as numeroCpt,
|
|
|
|
acc_accounts.label as nomCpt
|
2022-01-19 16:04:42 +01:00
|
|
|
FROM services
|
|
|
|
LEFT JOIN services_fees ON services_fees.id_service = services.id
|
|
|
|
LEFT JOIN acc_accounts ON services_fees.id_account = acc_accounts.id
|
|
|
|
ORDER BY services.label"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:10:00 +01:00
|
|
|
/**
|
2022-01-28 15:40:21 +01:00
|
|
|
* @return liste des années fiscales
|
2022-01-27 09:10:00 +01:00
|
|
|
*/
|
2022-01-28 15:40:21 +01:00
|
|
|
public static function getAnneesFiscales() {
|
|
|
|
$rows = DB::getInstance()->get(
|
2022-01-27 09:10:00 +01:00
|
|
|
"SELECT strftime('%Y', start_date) as annee
|
|
|
|
FROM acc_years
|
2022-01-28 15:40:21 +01:00
|
|
|
ORDER by start_date DESC"
|
|
|
|
);
|
|
|
|
$anneesFiscales = array();
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$anneesFiscales[] = $row->annee;
|
|
|
|
}
|
|
|
|
return $anneesFiscales;
|
2022-01-27 09:10:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 15:40:21 +01: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
|
|
|
|
*/
|
2022-01-25 17:19:31 +01:00
|
|
|
static function makeArchive(
|
|
|
|
$fileList,
|
|
|
|
$year,
|
2022-01-28 15:40:21 +01:00
|
|
|
$archiveDir = null)
|
2022-01-25 17:19:31 +01:00
|
|
|
{
|
|
|
|
$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
|
|
|
}
|