init
[garradin.git] / include / class.cotisations.php
1 <?php
2
3 namespace Garradin;
4
5 class Cotisations
6 {
7 /**
8 * Vérification des champs fournis pour la modification de donnée
9 * @param array $data Tableau contenant les champs à ajouter/modifier
10 * @return void
11 */
12 protected function _checkFields(&$data)
13 {
14 $db = DB::getInstance();
15
16 if (!isset($data['intitule']) || trim($data['intitule']) == '')
17 {
18 throw new UserException('L\'intitulé ne peut rester vide.');
19 }
20
21 $data['intitule'] = trim($data['intitule']);
22
23 if (isset($data['description']))
24 {
25 $data['description'] = trim($data['description']);
26 }
27
28 if (!isset($data['montant']) || !is_numeric($data['montant']) || (float)$data['montant'] < 0)
29 {
30 throw new UserException('Le montant doit être un nombre positif et valide.');
31 }
32
33 $data['montant'] = (float) $data['montant'];
34
35 if (isset($data['duree']))
36 {
37 $data['duree'] = (int) $data['duree'];
38
39 if ($data['duree'] < 0)
40 {
41 $data['duree'] = 0;
42 }
43 }
44
45 if (isset($data['debut']) && trim($data['debut']) != '')
46 {
47 if (!empty($data['duree']))
48 {
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.');
50 }
51
52 if (!isset($data['fin']) || trim($data['fin']) == '')
53 {
54 throw new UserException('Une date de fin est obligatoire avec la date de début de validité.');
55 }
56
57 if (!utils::checkDate($data['debut']))
58 {
59 throw new UserException('La date de début est invalide.');
60 }
61
62 if (!utils::checkDate($data['fin']))
63 {
64 throw new UserException('La date de fin est invalide.');
65 }
66 }
67
68 if (isset($data['id_categorie_compta']))
69 {
70 if ($data['id_categorie_compta'] != 0 && !$db->simpleQuerySingle('SELECT 1 FROM compta_categories WHERE id = ?;', false, (int) $data['id_categorie_compta']))
71 {
72 throw new UserException('Catégorie comptable inconnue');
73 }
74
75 $data['id_categorie_compta'] = (int) $data['id_categorie_compta'];
76 }
77 }
78
79 /**
80 * Ajouter une cotisation
81 * @param array $data Tableau des champs à insérer
82 * @return integer ID de la cotisation créée
83 */
84 public function add($data)
85 {
86 $db = DB::getInstance();
87
88 $this->_checkFields($data);
89
90 $db->simpleInsert('cotisations', $data);
91 $id = $db->lastInsertRowId();
92
93 return $id;
94 }
95
96 /**
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
101 */
102 public function edit($id, $data)
103 {
104 $db = DB::getInstance();
105
106 $this->_checkFields($data);
107
108 return $db->simpleUpdate('cotisations', $data, 'id = \''.(int) $id.'\'');
109 }
110
111 /**
112 * Supprimer une cotisation
113 * @param integer $id ID de la cotisation à supprimer
114 * @return integer true en cas de succès
115 */
116 public function delete($id)
117 {
118 $db = DB::getInstance();
119
120 $db->exec('BEGIN;');
121 $db->simpleExec('DELETE FROM cotisations_membres WHERE id_cotisation = ?;', (int) $id);
122 $db->simpleExec('DELETE FROM cotisations WHERE id = ?;', (int) $id);
123 $db->exec('END;');
124
125 return true;
126 }
127
128 /**
129 * Renvoie les infos sur une cotisation
130 * @param integer $id Numéro de la cotisation
131 * @return array Infos de la cotisation
132 */
133 public function get($id)
134 {
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]);
142 }
143
144 public function listByName()
145 {
146 $db = DB::getInstance();
147 return $db->simpleStatementFetch('SELECT * FROM cotisations ORDER BY intitule;');
148 }
149
150 public function listCurrent()
151 {
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;');
155 }
156
157 public function listCurrentWithStats()
158 {
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;');
167 }
168 }
169
170 ?>