a895ee26735172ce5783ce84f7ebccdc09d37b77
5 class Cotisations_Membres
7 const ITEMS_PER_PAGE
= 100;
10 * Vérification des champs fournis pour la modification de donnée
11 * @param array $data Tableau contenant les champs à ajouter/modifier
14 protected function _checkFields(&$data, $compta = false)
16 $db = DB
::getInstance();
18 if (empty($data['date']) ||
!utils
::checkDate($data['date']))
20 throw new UserException('Date vide ou invalide.');
23 if (empty($data['id_cotisation'])
24 ||
!$db->simpleQuerySingle('SELECT 1 FROM cotisations WHERE id = ?;', false, (int) $data['id_cotisation']))
26 throw new UserException('Cotisation inconnue.');
29 $data['id_cotisation'] = (int) $data['id_cotisation'];
31 if (empty($data['id_membre'])
32 ||
!$db->simpleQuerySingle('SELECT 1 FROM membres WHERE id = ?;', false, (int) $data['id_membre']))
34 throw new UserException('Membre inconnu ou invalide.');
37 $data['id_membre'] = (int) $data['id_membre'];
41 if (!isset($data['moyen_paiement']) ||
trim($data['moyen_paiement']) === '')
43 throw new UserException('Moyen de paiement inconnu ou invalide.');
46 if ($data['moyen_paiement'] != 'ES')
48 if (trim($data['banque']) == '')
50 throw new UserException('Le compte bancaire choisi est invalide.');
53 if (!$db->simpleQuerySingle('SELECT 1 FROM compta_comptes_bancaires WHERE id = ?;',
54 false, $data['banque']))
56 throw new UserException('Le compte bancaire choisi n\'existe pas.');
60 if (empty($data['montant']) ||
!is_numeric($data['montant']))
62 throw new UserException('Le montant indiqué n\'est pas un nombre valide.');
68 * Enregistrer un événement de cotisation
69 * @param array $data Tableau des champs à insérer
70 * @return integer ID de l'événement créé
72 public function add($data)
74 $db = DB
::getInstance();
76 $co = $db->simpleQuerySingle('SELECT * FROM cotisations WHERE id = ?;',
77 true, (int)$data['id_cotisation']);
79 $this->_checkFields($data, !empty($co['id_categorie_compta']));
81 $check = $db->simpleQuerySingle('SELECT 1 FROM cotisations_membres
82 WHERE id_cotisation = ? AND id_membre = ? AND date = ?;',
83 false, (int)$data['id_cotisation'], (int)$data['id_membre'], $data['date']);
87 throw new UserException('Cette cotisation a déjà été enregistrée pour ce jour-ci et ce membre-ci.');
92 $db->simpleInsert('cotisations_membres', [
93 'date' => $data['date'],
94 'id_cotisation' => $data['id_cotisation'],
95 'id_membre' => $data['id_membre'],
98 $id = $db->lastInsertRowId();
100 if ($co['id_categorie_compta'] && $co['montant'] > 0)
103 $id_operation = $this->addOperationCompta($id, [
104 'id_categorie' => $co['id_categorie_compta'],
105 'libelle' => 'Cotisation (automatique)',
106 'montant' => $data['montant'],
107 'date' => $data['date'],
108 'moyen_paiement'=> $data['moyen_paiement'],
109 'numero_cheque' => isset($data['numero_cheque']) ?
$data['numero_cheque'] : null,
110 'id_auteur' => $data['id_auteur'],
111 'banque' => isset($data['banque']) ?
$data['banque'] : null,
112 'id_membre' => $data['id_membre'],
115 catch (\Exception
$e)
128 * Supprimer un événement de cotisation
129 * @param integer $id ID de l'événement à supprimer
130 * @return integer true en cas de succès
132 public function delete($id)
134 $db = DB
::getInstance();
135 $db->simpleExec('DELETE FROM membres_operations WHERE id_cotisation = ?;', (int)$id);
136 return $db->simpleExec('DELETE FROM cotisations_membres WHERE id = ?;', (int) $id);
139 public function get($id)
141 $db = DB
::getInstance();
142 return $db->simpleQuerySingle('SELECT * FROM cotisations_membres WHERE id = ?;', true, (int)$id);
146 * Renvoie une liste des écritures comptables liées à une cotisation
147 * @param int $id Numéro de la cotisation membre
148 * @return array Liste des écritures
150 public function listOperationsCompta($id)
152 $db = DB
::getInstance();
153 return $db->simpleStatementFetch('SELECT * FROM compta_journal
154 WHERE id IN (SELECT id_operation FROM membres_operations
155 WHERE id_cotisation = ?);', \SQLITE3_ASSOC
, (int)$id);
159 * Ajouter une écriture comptable pour un paiemement membre
160 * @param int $id Numéro de la cotisation membre
161 * @param array $data Données
163 public function addOperationCompta($id, $data)
165 $journal = new Compta_Journal
;
166 $db = DB
::getInstance();
168 if (!isset($data['libelle']) ||
trim($data['libelle']) == '')
170 throw new UserException('Le libellé ne peut rester vide.');
173 $data['libelle'] = trim($data['libelle']);
175 if (!isset($data['montant']) ||
!is_numeric($data['montant']) ||
(float)$data['montant'] < 0)
177 throw new UserException('Le montant doit être un nombre positif et valide.');
180 $data['montant'] = (float) $data['montant'];
182 if ($data['moyen_paiement'] != 'ES')
184 $debit = $data['banque'];
188 $debit = Compta_Comptes
::CAISSE
;
191 $credit = $db->simpleQuerySingle('SELECT compte FROM compta_categories WHERE id = ?;',
192 false, $data['id_categorie']);
194 $id_operation = $journal->add([
195 'libelle' => $data['libelle'],
196 'montant' => $data['montant'],
197 'date' => $data['date'],
198 'moyen_paiement'=> $data['moyen_paiement'],
199 'numero_cheque' => isset($data['numero_cheque']) ?
$data['numero_cheque'] : null,
200 'compte_debit' => $debit,
201 'compte_credit' => $credit,
202 'id_categorie' => (int)$data['id_categorie'],
203 'id_auteur' => (int)$data['id_auteur'],
206 $db->simpleInsert('membres_operations', [
207 'id_operation' => $id_operation,
208 'id_membre' => $data['id_membre'],
209 'id_cotisation' => (int)$id,
212 return $id_operation;
216 * Nombre de membres pour une cotisation
217 * @param integer $id Numéro de la cotisation
218 * @return integer Nombre d'événements pour cette cotisation
220 public function countMembersForCotisation($id)
222 $db = DB
::getInstance();
223 return $db->simpleQuerySingle('SELECT COUNT(DISTINCT id_membre) FROM cotisations_membres
224 WHERE id_cotisation = ?;',
229 * Liste des membres qui sont inscrits à une cotisation
230 * @param integer $id Numéro de la cotisation
231 * @return array Liste des membres
233 public function listMembersForCotisation($id, $page = 1, $order = null, $desc = true)
235 $begin = ($page - 1) * self
::ITEMS_PER_PAGE
;
237 $db = DB
::getInstance();
238 $champ_id = Config
::getInstance()->get('champ_identite');
249 $order = 'transliterate_to_ascii('.$champ_id.') COLLATE NOCASE';
252 $order = 'cm.id_membre';
256 $desc = $desc ?
'DESC' : 'ASC';
258 return $db->simpleStatementFetch('SELECT cm.id_membre, cm.date, cm.id,
259 (SELECT '.$champ_id.' FROM membres WHERE id = cm.id_membre) AS nom, c.montant,
260 CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\') >= date()
261 WHEN c.fin IS NOT NULL THEN c.fin >= date() ELSE 1 END AS a_jour
262 FROM cotisations_membres AS cm
263 INNER JOIN cotisations AS c ON c.id = cm.id_cotisation
266 GROUP BY cm.id_membre ORDER BY '.$order.' '.$desc.' LIMIT ?,?;',
267 \SQLITE3_ASSOC
, (int)$id, $begin, self
::ITEMS_PER_PAGE
);
271 * Liste des événements d'un membre
272 * @param integer $id Numéro de membre
273 * @return array Liste des événements de cotisation fait par ce membre
275 public function listForMember($id)
277 $db = DB
::getInstance();
278 return $db->simpleStatementFetch('SELECT cm.*, c.intitule, c.duree, c.debut, c.fin, c.montant,
279 (SELECT COUNT(*) FROM membres_operations WHERE id_cotisation = cm.id) AS nb_operations
280 FROM cotisations_membres AS cm
281 LEFT JOIN cotisations AS c ON c.id = cm.id_cotisation
282 WHERE cm.id_membre = ? ORDER BY cm.date DESC;', \SQLITE3_ASSOC
, (int)$id);
286 * Liste des cotisations / activités en cours pour ce membre
287 * @param integer $id Numéro de membre
288 * @return array Liste des cotisations en cours de validité
290 public function listSubscriptionsForMember($id)
292 $db = DB
::getInstance();
293 return $db->simpleStatementFetch('SELECT c.*,
294 CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\') >= date()
295 WHEN c.fin IS NOT NULL THEN c.fin >= date()
296 WHEN cm.id IS NOT NULL THEN 1 ELSE 0 END AS a_jour,
297 CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\')
298 WHEN c.fin IS NOT NULL THEN c.fin ELSE 1 END AS expiration,
299 (julianday(date()) - julianday(CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\')
300 WHEN c.fin IS NOT NULL THEN c.fin END)) AS nb_jours
301 FROM cotisations_membres AS cm
302 INNER JOIN cotisations AS c ON c.id = cm.id_cotisation
303 WHERE cm.id_membre = ?
304 GROUP BY cm.id_cotisation
305 ORDER BY cm.date DESC;', \SQLITE3_ASSOC
, (int)$id);
309 * Ce membre est-il à jour sur cette cotisation ?
310 * @param integer $id Numéro de membre
311 * @param integer $id_cotisation Numéro de cotisation
312 * @return array Infos sur la cotisation, et champ expiration
313 * (si NULL = cotisation jamais enregistrée, si 1 = cotisation ponctuelle enregistrée, sinon date d'expiration)
315 public function isMemberUpToDate($id, $id_cotisation)
317 $db = DB
::getInstance();
318 return $db->simpleQuerySingle('SELECT c.*,
319 CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\') >= date()
320 WHEN c.fin IS NOT NULL THEN c.fin >= date()
321 WHEN cm.id IS NOT NULL THEN 1 ELSE 0 END AS a_jour,
322 CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\')
323 WHEN c.fin IS NOT NULL THEN c.fin ELSE 1 END AS expiration
324 FROM cotisations AS c
325 LEFT JOIN cotisations_membres AS cm ON cm.id_cotisation = c.id AND cm.id_membre = ?
326 WHERE c.id = ? ORDER BY cm.date DESC;',
327 true, (int)$id, (int)$id_cotisation);
330 public function countForMember($id)
332 $db = DB
::getInstance();
333 return $db->simpleQuerySingle('SELECT COUNT(DISTINCT id_cotisation) FROM cotisations_membres
334 WHERE id_membre = ?;', false, (int)$id);