8 * Vérification des champs fournis pour la modification de donnée
9 * @param array $data Tableau contenant les champs à ajouter/modifier
12 protected function _checkFields(&$data)
14 $db = DB
::getInstance();
16 if (!isset($data['intitule']) ||
trim($data['intitule']) == '')
18 throw new UserException('L\'intitulé ne peut rester vide.');
21 $data['intitule'] = trim($data['intitule']);
23 if (isset($data['description']))
25 $data['description'] = trim($data['description']);
28 if (!isset($data['montant']) ||
!is_numeric($data['montant']) ||
(float)$data['montant'] < 0)
30 throw new UserException('Le montant doit être un nombre positif et valide.');
33 $data['montant'] = (float) $data['montant'];
35 if (isset($data['duree']))
37 $data['duree'] = (int) $data['duree'];
39 if ($data['duree'] < 0)
45 if (isset($data['debut']) && trim($data['debut']) != '')
47 if (!empty($data['duree']))
49 throw new UserException('Il n\'est pas possible de spécifier une durée ET une date fixe, merci de choisir l\'une des deux options.');
52 if (!isset($data['fin']) ||
trim($data['fin']) == '')
54 throw new UserException('Une date de fin est obligatoire avec la date de début de validité.');
57 if (!utils
::checkDate($data['debut']))
59 throw new UserException('La date de début est invalide.');
62 if (!utils
::checkDate($data['fin']))
64 throw new UserException('La date de fin est invalide.');
68 if (isset($data['id_categorie_compta']))
70 if ($data['id_categorie_compta'] != 0 && !$db->simpleQuerySingle('SELECT 1 FROM compta_categories WHERE id = ?;', false, (int) $data['id_categorie_compta']))
72 throw new UserException('Catégorie comptable inconnue');
75 $data['id_categorie_compta'] = (int) $data['id_categorie_compta'];
80 * Ajouter une cotisation
81 * @param array $data Tableau des champs à insérer
82 * @return integer ID de la cotisation créée
84 public function add($data)
86 $db = DB
::getInstance();
88 $this->_checkFields($data);
90 $db->simpleInsert('cotisations', $data);
91 $id = $db->lastInsertRowId();
97 * Modifier une cotisation
98 * @param integer $id ID de la cotisation à modifier
99 * @param array $data Tableau des champs à modifier
100 * @return bool true si succès
102 public function edit($id, $data)
104 $db = DB
::getInstance();
106 $this->_checkFields($data);
108 return $db->simpleUpdate('cotisations', $data, 'id = \''.(int) $id.'\'');
112 * Supprimer une cotisation
113 * @param integer $id ID de la cotisation à supprimer
114 * @return integer true en cas de succès
116 public function delete($id)
118 $db = DB
::getInstance();
121 $db->simpleExec('DELETE FROM cotisations_membres WHERE id_cotisation = ?;', (int) $id);
122 $db->simpleExec('DELETE FROM cotisations WHERE id = ?;', (int) $id);
129 * Renvoie les infos sur une cotisation
130 * @param integer $id Numéro de la cotisation
131 * @return array Infos de la cotisation
133 public function get($id)
135 $db = DB
::getInstance();
136 return $db->simpleQuerySingle('SELECT co.*,
137 (SELECT COUNT(DISTINCT id_membre) FROM cotisations_membres WHERE id_cotisation = co.id) AS nb_membres,
138 (SELECT COUNT(DISTINCT id_membre) FROM cotisations_membres AS cm WHERE id_cotisation = co.id
139 AND ((co.duree IS NOT NULL AND date(cm.date, \'+\'||co.duree||\' days\') >= date())
140 OR (co.fin IS NOT NULL AND co.debut <= cm.date AND co.fin >= cm.date))) AS nb_a_jour
141 FROM cotisations AS co WHERE id = :id;', true, ['id' => (int) $id]);
144 public function listByName()
146 $db = DB
::getInstance();
147 return $db->simpleStatementFetch('SELECT * FROM cotisations ORDER BY intitule;');
150 public function listCurrent()
152 $db = DB
::getInstance();
153 return $db->simpleStatementFetch('SELECT * FROM cotisations WHERE fin >= date(\'now\') OR fin IS NULL
154 ORDER BY transliterate_to_ascii(intitule) COLLATE NOCASE;');
157 public function listCurrentWithStats()
159 $db = DB
::getInstance();
160 return $db->simpleStatementFetch('SELECT co.*,
161 (SELECT COUNT(DISTINCT id_membre) FROM cotisations_membres WHERE id_cotisation = co.id) AS nb_membres,
162 (SELECT COUNT(DISTINCT id_membre) FROM cotisations_membres AS cm WHERE id_cotisation = co.id
163 AND ((co.duree IS NOT NULL AND date(cm.date, \'+\'||co.duree||\' days\') >= date())
164 OR (co.fin IS NOT NULL AND co.debut <= cm.date AND co.fin >= cm.date))) AS nb_a_jour
165 FROM cotisations AS co WHERE fin >= date(\'now\') OR fin IS NULL
166 ORDER BY transliterate_to_ascii(intitule) COLLATE NOCASE;');