Move paiement db in x4dat (tables payment*).
[platal.git] / modules / xnetevents / xnetevents.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22// {{{ function get_event_detail()
d6d580ec 23
2ac0bcee 24function get_event_detail($eid, $item_id = false, $asso_id = null)
d6d580ec 25{
0337d704 26 global $globals;
2ac0bcee
FB
27 if (is_null($asso_id)) {
28 $asso_id = $globals->asso('id');
29 }
07eb5b0e
FB
30 $res = XDB::query('SELECT SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
31 IF(e.deadline_inscription,
32 e.deadline_inscription >= LEFT(NOW(), 10),
33 1) AS inscr_open,
34 LEFT(10, e.debut) AS start_day, LEFT(10, e.fin) AS last_day,
35 LEFT(NOW(), 10) AS now,
36 ei.titre, al.vid AS absent_list, pl.vid AS participant_list
5660032a
SJ
37 FROM #groupex#.evenements AS e
38 INNER JOIN #groupex#.evenements_items AS ei ON (e.eid = ei.eid)
39 LEFT JOIN #groupex#.evenements_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
40 LEFT JOIN #x4dat#.virtual AS al ON(al.type = \'evt\' AND al.alias = CONCAT(short_name, {?}))
41 LEFT JOIN #x4dat#.virtual AS pl ON(pl.type = \'evt\' AND pl.alias = CONCAT(short_name, {?}))
07eb5b0e
FB
42 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
43 GROUP BY ei.item_id',
44 '-absents@'.$globals->xnet->evts_domain,
45 '-participants@'.$globals->xnet->evts_domain,
e46cf8c4
SJ
46 '-paye@' . $globals->xnet->evts_domain,
47 '-participants-non-paye@' . $globals->xnet->evts_domain,
07eb5b0e 48 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
0337d704 49 $evt = $res->fetchOneAssoc();
d6d580ec 50
df1cf596 51 if (!$evt) {
d6d580ec 52 return null;
53 }
df1cf596
FB
54 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
55 return false;
56 }
0337d704 57
58 // smart calculation of the total number
59 if (!$item_id) {
07eb5b0e 60 $res = XDB::query('SELECT MAX(nb)
5660032a
SJ
61 FROM #groupex#.evenements AS e
62 INNER JOIN #groupex#.evenements_items AS ei ON (e.eid = ei.eid)
63 LEFT JOIN #groupex#.evenements_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
64 WHERE e.eid = {?}
65 GROUP BY ep.uid', $evt['eid']);
0337d704 66 $evt['nb_tot'] = array_sum($res->fetchColumn());
67 $evt['titre'] = '';
68 $evt['item_id'] = 0;
69 }
d6d580ec 70
07eb5b0e
FB
71 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
72 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
5660032a
SJ
73 FROM #groupex#.evenements_items AS ei
74 LEFT JOIN #groupex#.evenements_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
07eb5b0e
FB
75 AND uid = {?})
76 WHERE ei.eid = {?}',
77 S::i('uid'), $evt['eid']);
d6d580ec 78 $evt['topay'] = 0;
98a7e9dc 79 $evt['paid'] = 0;
2ac0bcee 80 $evt['notify_payment'] = false;
d6d580ec 81 foreach ($evt['moments'] as $m) {
82 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 83 if ($m['montant']) {
54ac7230 84 $evt['money'] = true;
98a7e9dc 85 }
86 $evt['paid'] = $m['paid'];
2ac0bcee 87 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 88 }
89
07eb5b0e
FB
90 $montants = XDB::fetchColumn('SELECT montant
91 FROM ' . $globals->money->mpay_tprefix . 'transactions AS t
92 WHERE ref = {?} AND uid = {?}',
93 $evt['paiement_id'], S::v('uid'));
8bac35d8 94 $evt['telepaid'] = 0;
d6d580ec 95 foreach ($montants as $m) {
96 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
97 $evt['paid'] += trim($p);
8bac35d8 98 $evt['telepaid'] += trim($p);
0337d704 99 }
d6d580ec 100
20c5c7e6
SJ
101 make_event_date($evt);
102
0337d704 103 return $evt;
104}
d6d580ec 105
0337d704 106// }}}
107
108// {{{ function get_event_participants()
07eb5b0e
FB
109function get_event_participants(&$evt, $item_id, array $tri = array(), $count = null, $offset = null)
110{
0337d704 111 global $globals;
ed21e24a 112
ed21e24a 113 $eid = $evt['eid'];
2ac0bcee 114 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 115 $pay_id = $evt['paiement_id'];
116
07eb5b0e
FB
117 $append = $item_id ? XDB::foramt(' AND ep.item_id = {?}', $item_id) : '';
118 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
119 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
5660032a 120 FROM #groupex#.evenements_participants AS ep
07eb5b0e
FB
121 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
122 GROUP BY ep.uid', $eid);
123 $uf = new UserFilter(new UFC_True(), $tri);
124 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), $count, $offset));
125 $tab = array();
126 foreach ($users as $user) {
127 $uid = $user->id();
128 $tab[$uid] = $query[$uid];
129 $tab[$uid]['user'] = $user;
130 }
ed21e24a 131
0337d704 132 if ($item_id) {
07eb5b0e 133 return $tab;
0337d704 134 }
ed21e24a 135
e01ebe65
FB
136 $evt['adminpaid'] = 0;
137 $evt['telepaid'] = 0;
138 $evt['topay'] = 0;
139 $evt['paid'] = 0;
07eb5b0e 140 foreach ($tab as $uid=>&$u) {
58591700 141 $u['adminpaid'] = $u['paid'];
0337d704 142 $u['montant'] = 0;
2ac0bcee 143 if ($money && $pay_id) {
07eb5b0e
FB
144 $montants = XDB::fetchColumn('SELECT montant
145 FROM ' . $globals->money->mpay_tprefix . 'transactions AS t
146 WHERE ref = {?} AND uid = {?}',
147 $pay_id, $uid);
0337d704 148 foreach ($montants as $m) {
e01ebe65
FB
149 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
150 $u['paid'] += trim($p);
0337d704 151 }
2ac0bcee 152 }
58591700 153 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
07eb5b0e 154 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
5660032a
SJ
155 FROM #groupex#.evenements_participants AS ep
156 INNER JOIN #groupex#.evenements_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
157 WHERE ep.eid = {?} AND ep.uid = {?}',
158 $eid, $uid);
0337d704 159 while ($i = $res_->next()) {
160 $u[$i['item_id']] = $i['nb'];
161 $u['montant'] += $i['montant']*$i['nb'];
162 }
e01ebe65
FB
163 $evt['telepaid'] += $u['telepayment'];
164 $evt['adminpaid'] += $u['adminpaid'];
165 $evt['paid'] += $u['paid'];
166 $evt['topay'] += $u['montant'];
0337d704 167 }
168 return $tab;
169}
170// }}}
171
172// {{{ function subscribe_lists_event()
9ff5b337 173function subscribe_lists_event($participate, $uid, $evt, $paid, $payment = null)
9193e8f7 174{
d7610c35
FB
175 global $globals;
176 $page =& Platal::page();
ed21e24a 177
9ff5b337
SJ
178 $participant_list = $evt['participant_list'];
179 $absent_list = $evt['absent_list'];
180 $unpayed_list = $evt['booked_unpayed_list'];
181 $payed_list = $evt['payed_list'];
ed21e24a 182
4514bea3
VZ
183 $user = User::getSilent($uid);
184 if ($user) {
185 $email = $user->forlifeEmail();
0337d704 186 } else {
9ff5b337 187 $res = XDB::query("SELECT email
00112b2e 188 FROM #groupex#.membres
9ff5b337 189 WHERE uid = {?} AND asso_id = {?}",
da146d01 190 $uid, $globals->asso('id'));
0337d704 191 $email = $res->fetchOneCell();
192 }
193
9193e8f7 194 function subscribe($list, $email)
195 {
196 if ($list && $email) {
9ff5b337
SJ
197 XDB::execute("REPLACE INTO virtual_redirect
198 VALUES ({?},{?})",
9193e8f7 199 $list, $email);
200 }
0337d704 201 }
202
9193e8f7 203 function unsubscribe($list, $email)
204 {
205 if ($list && $email) {
9ff5b337
SJ
206 XDB::execute("DELETE FROM virtual_redirect
207 WHERE vid = {?} AND redirect = {?}",
9193e8f7 208 $list, $email);
209 }
0337d704 210 }
211
9ff5b337
SJ
212 if (is_null($payment)) {
213 if (is_null($participate)) {
214 unsubscribe($participant_list, $email);
215 subscribe($absent_list, $email);
216 } elseif ($participate) {
217 subscribe($participant_list, $email);
218 unsubscribe($absent_list, $email);
219 } else {
220 unsubscribe($participant_list, $email);
221 unsubscribe($absent_list, $email);
222 }
223 }
224 if ($paid > 0) {
225 unsubscribe($unpayed_list, $email);
226 subscribe($payed_list, $email);
9193e8f7 227 } else {
9ff5b337
SJ
228 unsubscribe($payed_list, $email);
229 subscribe($unpayed_list, $email);
9193e8f7 230 }
0337d704 231}
232// }}}
233
20c5c7e6 234// {{{ function event_change_shortname()
2847640f 235function event_change_shortname(&$page, $eid, $old, $new)
5070a22d 236{
237 global $globals;
238
f56e5e53 239 if (is_null($old)) {
240 $old = '';
241 }
a7de4ef7 242 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 243 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7d35093 244 $page->trigError("Le raccourci demandé n'est pas valide.
a7de4ef7 245 Vérifie qu'il comporte entre 3 et 20 caractères
246 et qu'il ne contient que des lettres non accentuées,
247 des chiffres ou les caractères - et .");
5070a22d 248 return $old;
940ae3a0
SJ
249 } elseif ($new && ctype_digit($new)) {
250 $page->trigError("Le raccourci demandé ne peut être accepté car il
251 ne contient que des chiffres. Rajoute-lui par exemple
252 une lettre.");
253 return $old;
5070a22d 254 }
255
a7de4ef7 256 //vérifier que l'alias n'est pas déja pris
5070a22d 257 if ($new && $old != $new) {
9193e8f7 258 $res = XDB::query('SELECT COUNT(*)
00112b2e 259 FROM #groupex#.evenements
9193e8f7 260 WHERE short_name = {?}',
261 $new);
5070a22d 262 if ($res->fetchOneCell() > 0) {
a7d35093 263 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
5070a22d 264 return $old;
265 }
266 }
267
268 if ($old == $new) {
269 return $new;
270 }
271
272 if ($old && $new) {
273 // if had a previous shortname change the old lists
9ff5b337 274 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 275 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
276 XDB::execute("UPDATE virtual
277 SET alias = {?}
278 WHERE type = 'evt' AND alias = {?}",
279 $new . $v, $old . $v);
5070a22d 280 }
5070a22d 281 return $new;
282 }
283
284 if (!$old && $new) {
285 // if we have a first new short_name create the lists
9ff5b337
SJ
286 $lastid = array();
287 $where = array(
288 '-participants@' => 'ep.nb > 0',
289 '-paye@' => 'ep.paid > 0',
290 '-participants-non-paye@' => 'ep.nb > 0 AND ep.paid = 0'
291 );
292
293 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
294 XDB::execute("INSERT INTO virtual
295 SET type = 'evt', alias = {?}",
296 $new . $v . $globals->xnet->evts_domain);
297
298 $lastid[$v] = XDB::insertId();
299 }
5070a22d 300
9ff5b337 301 foreach (array('-participants@', '-paye@', '-participants-non-paye@') as $v) {
e46cf8c4
SJ
302 XDB::execute("INSERT IGNORE INTO virtual_redirect (
303 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
5660032a
SJ
304 FROM #groupex#.evenements_participants AS ep
305 LEFT JOIN #x4dat#.accounts AS a ON (ep.uid = a.uid)
306 LEFT JOIN #x4dat#.aliases AS al ON (al.id = a.uid AND al.type = 'a_vie')
e46cf8c4
SJ
307 WHERE ep.eid = {?} AND " . $where[$v] . "
308 GROUP BY ep.uid)",
309 $lastid[$v], '@' . $globals->mail->domain, $eid);
9ff5b337 310 }
07eb5b0e
FB
311 XDB::execute("INSERT IGNORE INTO virtual_redirect (
312 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
5660032a
SJ
313 FROM #groupex#.membres AS m
314 LEFT JOIN #x4dat#.accounts AS a ON (a.uid = m.uid)
315 LEFT JOIN #x4dat#.aliases AS al ON (al.id = a.uid AND al.type = 'a_vie')
316 LEFT JOIN #groupex#.evenements_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
07eb5b0e
FB
317 WHERE m.asso_id = {?} AND ep.uid IS NULL
318 GROUP BY m.uid)",
e46cf8c4 319 $lastid['-absents@'], '@' . $globals->mail->domain, $eid, $globals->asso('id'));
5070a22d 320
321 return $new;
322 }
323
324 if ($old && !$new) {
325 // if we delete the old short name, delete the lists
9ff5b337 326 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 327 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
328 XDB::execute("DELETE virtual, virtual_redirect
329 FROM virtual
330 LEFT JOIN virtual_redirect USING(vid)
331 WHERE virtual.alias = {?}",
332 $infos['short_name'] . $v);
5070a22d 333 }
334 return $new;
335 }
336
337 // cannot happen
338 return $old;
339}
20c5c7e6
SJ
340// }}}
341
342// {{{ function make_event_date()
343function make_event_date(&$e)
344{
345 $start = strtotime($e['debut']);
346 $end = strtotime($e['fin']);
07eb5b0e 347 $first_day = @strtotime($e['first_day']);
20c5c7e6
SJ
348 $last_day = strtotime($e['last_day']);
349 unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
350
351 $date = "";
352 if ($start && $end != $start) {
353 if ($first_day == $last_day) {
354 $date .= "le " . strftime("%d %B %Y", $start) . " de "
355 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
356 } else {
357 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
358 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
359 }
360 } else {
361 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
362 }
363 $e['date'] = $date;
364}
365// }}}
5070a22d 366
a7de4ef7 367// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 368?>