Merge remote branch 'origin/xorg/maint' into xorg/master
[platal.git] / include / validations / paiements.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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 // {{{ class PayReq
23
24 class PayReq extends Validate
25 {
26 // {{{ properties
27
28 public $titre;
29 public $site;
30
31 public $montant;
32 public $montant_min;
33 public $montant_max;
34
35 public $msg_reponse;
36 public $asso_id;
37 public $asso;
38 public $evt;
39 public $evt_intitule;
40 public $public;
41
42 public $rules = "Vérifier que les balises &lt;salutation&gt;, &lt;prenom&gt;, &lt;nom&gt;, &lt;montant&gt; et &lt;comment&gt; n'ont pas été modifiées.
43 Vérifier que le demandeur n'a pas laissé les crochets [].
44 Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, laisser la validation à un trésorier";
45 // }}}
46 // {{{ constructor
47
48 public function __construct(User $_user, $_intitule, $_site, $_montant, $_msg,
49 $_montantmin=0, $_montantmax=999, $_asso_id = 0,
50 $_evt = 0, $_public = false, $_stamp = 0)
51 {
52 parent::__construct($_user, false, 'paiements', $_stamp);
53
54 $this->titre = $_intitule;
55 $this->site = $_site;
56 $this->msg_reponse = $_msg;
57 $this->asso_id = (string)$_asso_id;
58 $this->evt = (string)$_evt;
59 $this->montant = $_montant;
60 $this->montant_min = $_montantmin;
61 $this->montant_max = $_montantmax;
62 $this->public = $_public;
63
64 if ($_asso_id) {
65 $res = XDB::query("SELECT nom FROM groups WHERE id = {?}", $_asso_id);
66 $this->asso = $res->fetchOneCell();
67 }
68 if ($_asso_id && $_evt) {
69 $res = XDB::query("SELECT intitule FROM group_events WHERE asso_id = {?} AND eid = {?}", $_asso_id, $_evt);
70 $this->evt_intitule = $res->fetchOneCell();
71 }
72 }
73
74 // }}}
75 // {{{ function same_event()
76
77 static public function same_event($evt, $asso_id)
78 {
79 $wevt = 's:3:"evt";s:'.strlen($evt+"").':"'.$evt.'"';
80 $wassoid = 's:7:"asso_id";s:'.strlen($asso_id + "").':"'.$asso_id.'"';
81 $where = "%".$wassoid."%".$wevt."%";
82 return $where;
83 }
84
85 // }}}
86 // {{{ function accept()
87
88 // check the message
89 public function accept()
90 {
91 // no text [AI JMIAJM IJA MIJ]
92 if (preg_match('/\[[-\'"a-zA-Z ]+\]/', replace_accent($this->msg_reponse))) {
93 $this->trigError("La demande de paiement n'est pas valide. Merci de compléter le texte avant de la soumettre");
94 return false;
95 }
96 if (!preg_match('/<montant>/', $this->msg_reponse)) {
97 $this->trigError("Le demande de paiement ne contient pas la balise obligatoire &lt;montant&gt;");
98 return false;
99 }
100 return true;
101 }
102
103 // }}}
104 // {{{ function submit()
105
106 // supprime les demandes de paiments pour le meme evenement
107 public function submit()
108 {
109 if ($this->evt)
110 {
111 XDB::execute('DELETE FROM requests WHERE type={?} AND data LIKE {?}', 'paiements', PayReq::same_event($this->evt, $this->asso_id));
112 }
113 parent::submit();
114 }
115 // }}}
116 // {{{ function formu()
117
118 public function formu()
119 {
120 return 'include/form.valid.paiements.tpl';
121 }
122
123 // }}}
124 // {{{ function editor()
125
126 public function editor()
127 {
128 return 'include/form.valid.edit-paiements.tpl';
129 }
130
131 // }}}
132 // {{{ function handle_editor()
133
134 protected function handle_editor()
135 {
136 $this->titre = Env::v('pay_titre');
137 $this->site = Env::v('pay_site');
138 $this->montant = Env::i('pay_montant');
139 $this->montant_min = Env::i('pay_montant_min');
140 $this->montant_max = Env::i('pay_montant_max');
141 $this->msg_reponse = Env::v('pay_msg_reponse');
142 $this->public = (Env::v('pay_public') == 'yes');
143 return true;
144 }
145
146 // }}}
147 // {{{ function _mail_subj
148
149 protected function _mail_subj()
150 {
151 return "[Polytechnique.org/Paiments] Demande de création de paiement {$this->titre}";
152 }
153
154 // }}}
155 // {{{ function _mail_body
156
157 protected function _mail_body($isok)
158 {
159 if ($isok) {
160 return " Le paiement demandé pour {$this->titre} vient d'être créé.".($this->evt?" Il a bien été associé à la gestion de l'événement du groupe":"");
161 } else {
162 return " La demande faite pour le paiement de {$this->intitule} a été refusée.";
163 }
164 }
165
166 // }}}
167 // {{{ function commit()
168
169 public function commit()
170 {
171 $res = XDB::query("SELECT MAX(id) FROM payments");
172 $id = $res->fetchOneCell()+1;
173 $ret = XDB::execute('INSERT INTO payments (id, text, url, amount_def, amount_min, amount_max, mail, confirmation, asso_id, flags)
174 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
175 $id, $this->titre, $this->site, $this->montant, $this->montant_min,
176 $this->montant_max, $this->user->bestEmail(), $this->msg_reponse, $this->asso_id,
177 ($this->public ? 'public' : ''));
178 if ($this->asso_id && $this->evt) {
179 XDB::execute("UPDATE group_events
180 SET paiement_id = {?}
181 WHERE asso_id = {?} AND eid = {?}",
182 $id, $this->asso_id, $this->evt);
183 $res = XDB::query("SELECT a.nom, a.diminutif, e.intitule
184 FROM groups AS a
185 INNER JOIN group_events AS e ON (a.id = e.asso_id)
186 WHERE e.eid = {?}",
187 $this->evt);
188 list($nom, $diminutif, $evt) = $res->fetchOneRow();
189 require_once dirname(__FILE__) . '/../../modules/xnetevents/xnetevents.inc.php';
190 $participants = get_event_participants(get_event_detail($this->evt, false, $this->asso_id), null);
191 foreach ($participants as $u) {
192 if (!$u['notify_payment']) {
193 continue;
194 }
195 $topay = $u['montant'] - $u['paid'];
196 if ($topay > 0) {
197 $mailer = new PlMailer('xnetevents/newpayment.mail.tpl');
198 $mailer->addTo($u['user']);
199 $mailer->assign('asso', $nom);
200 $mailer->assign('diminutif', $diminutif);
201 $mailer->assign('evt', $evt);
202 $mailer->assign('payment', $id);
203 $mailer->assign('prenom', $u['user']->firstName());
204 $mailer->assign('topay', $topay);
205 $mailer->send();
206 }
207 }
208 }
209 return $ret;
210 }
211
212 // }}}
213 }
214
215 // }}}
216
217 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
218 ?>