Commit | Line | Data |
---|---|---|
a2558f2b | 1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
a2558f2b | 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 | /* sort en affichant une erreur */ | |
23 | function cb_erreur($text) { | |
115c90db | 24 | global $globals; |
9d773172 | 25 | echo "Error.\n"; |
1e33266a | 26 | $mymail = new PlMailer(); |
d7dd70be | 27 | $mymail->addTo($globals->money->email); |
1d55fe45 | 28 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
a7de4ef7 | 29 | $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)"); |
a2558f2b | 30 | $mymail->setTxtBody("\n\n".var_export($_REQUEST,true)); |
31 | $mymail->send(); | |
9d773172 | 32 | echo "Notification sent.\n"; |
a2558f2b | 33 | exit; |
34 | } | |
35 | ||
36 | /* sort en affichant une erreur */ | |
88e3843c | 37 | function paypal_erreur($text, $send=true) |
38 | { | |
d7610c35 | 39 | global $erreur, $globals; |
a2558f2b | 40 | if ($erreur) return; |
41 | $erreur = $text; | |
42 | if (!$send) return; | |
43 | ||
1e33266a | 44 | $mymail = new PlMailer(); |
d7dd70be | 45 | $mymail->addTo($globals->money->email); |
1d55fe45 | 46 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
a7de4ef7 | 47 | $mymail->setSubject("erreur lors d'un télépaiement (PayPal)"); |
a2558f2b | 48 | $mymail->setTxtBody("\n\n".var_export($_REQUEST,true)); |
49 | $mymail->send(); | |
50 | ||
d7610c35 | 51 | Platal::page()->trigError($text); |
a2558f2b | 52 | } |
53 | ||
54 | /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */ | |
55 | function luhn($nombre) { | |
56 | $s = strrev($nombre); | |
57 | $sum = 0; | |
58 | for ($i = 0; $i < strlen($s); $i++) { | |
9d773172 | 59 | $dgt = $s{$i}; |
a2558f2b | 60 | $sum += ($i % 2) ? (2*$dgt) % 9 : $dgt; |
61 | } | |
62 | return $sum % 10; | |
63 | } | |
64 | ||
a7de4ef7 | 65 | /* calcule la clé d'acceptation a partir de 5 champs */ |
a2558f2b | 66 | function cle_accept($d1,$d2,$d3,$d4,$d5) |
67 | { | |
68 | $m1 = luhn($d1.$d5); | |
69 | $m2 = luhn($d2.$d5); | |
70 | $m3 = luhn($d3.$d5); | |
71 | $m4 = luhn($d4.$d5); | |
72 | $n = $m1 + $m2 + $m3 + $m4; | |
73 | $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
74 | return $alpha{$n-1}.$m1.$m2.$m3.$m4; | |
75 | } | |
76 | ||
17793ccf FB |
77 | /* decode the comment */ |
78 | function comment_decode($comment) { | |
79 | $comment = urldecode($comment); | |
80 | if (is_utf8($comment)) { | |
81 | return $comment; | |
82 | } else { | |
83 | return utf8_encode($comment); | |
84 | } | |
85 | } | |
86 | ||
f56ceafe DB |
87 | /* check if a RIB account number is valid */ |
88 | function check_rib($rib) | |
89 | { | |
f3d6e2cc DB |
90 | if(strlen($rib) != 23) return false; |
91 | ||
f56ceafe DB |
92 | // extract fields |
93 | $rib = strtr(strtoupper($rib),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','12345678912345678923456789'); | |
f3d6e2cc DB |
94 | $bank = substr($rib,0,5); |
95 | $counter = substr($rib,5,5); | |
96 | $account = substr($rib,10,11); | |
97 | $key = substr($rib,21,2); | |
98 | ||
f56ceafe DB |
99 | // check |
100 | return 0 == fmod(89 * $bank + 15 * $counter + 3 * $account + $key, 97); | |
101 | } | |
a2558f2b | 102 | |
103 | class PaymentModule extends PLModule | |
104 | { | |
105 | function handlers() | |
106 | { | |
107 | return array( | |
2914271e | 108 | 'payment' => $this->make_hook('payment', AUTH_MDP, 'payment'), |
eb5a266d | 109 | 'payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
a690a74c | 110 | 'payment/cyber2_return' => $this->make_hook('cyber2_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
eb5a266d SJ |
111 | 'payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
112 | '%grp/paiement' => $this->make_hook('xnet_payment', AUTH_MDP), | |
113 | '%grp/payment' => $this->make_hook('xnet_payment', AUTH_MDP), | |
114 | '%grp/payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC, 'user', NO_HTTPS), | |
a690a74c | 115 | '%grp/payment/cyber2_return' => $this->make_hook('cyber2_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
41cce805 | 116 | '%grp/payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
eb5a266d | 117 | 'admin/payments' => $this->make_hook('admin', AUTH_MDP, 'admin'), |
f56ceafe DB |
118 | 'admin/payments/methods' => $this->make_hook('adm_methods', AUTH_MDP, 'admin'), |
119 | 'admin/payments/transactions'=> $this->make_hook('adm_transactions', AUTH_MDP, 'admin'), | |
77b81d91 DB |
120 | 'admin/reconcile' => $this->make_hook('adm_reconcile', AUTH_MDP, 'admin'), |
121 | 'admin/reconcile/importlogs' => $this->make_hook('adm_importlogs', AUTH_MDP, 'admin'), | |
122 | 'admin/reconcile/transfers' => $this->make_hook('adm_transfers', AUTH_MDP, 'admin'), | |
123 | 'admin/reconcile/bankaccounts'=> $this->make_hook('adm_bankaccounts', AUTH_MDP, 'admin'), | |
a2558f2b | 124 | ); |
125 | } | |
126 | ||
127 | function handler_payment(&$page, $ref = -1) | |
128 | { | |
129 | global $globals; | |
130 | ||
460d8f55 | 131 | $this->load('money.inc.php'); |
a2558f2b | 132 | |
98a7e9dc | 133 | if (!empty($GLOBALS['IS_XNET_SITE'])) { |
134 | if (!$globals->asso('id')) { | |
135 | return PL_NOT_FOUND; | |
136 | } | |
137 | $res = XDB::query("SELECT asso_id | |
69fffc4b | 138 | FROM payments |
98a7e9dc | 139 | WHERE asso_id = {?} AND id = {?}", |
140 | $globals->asso('id'), $ref); | |
141 | if (!$res->numRows()) { | |
142 | return PL_FORBIDDEN; | |
143 | } | |
98a7e9dc | 144 | } |
1490093c | 145 | $page->changeTpl('payment/index.tpl'); |
46f272fe | 146 | $page->setTitle('Télépaiements'); |
a2558f2b | 147 | |
148 | // initialisation | |
5e2307dc | 149 | $op = Env::v('op', 'select'); |
150 | $meth = new PayMethod(Env::i('methode', -1)); | |
a2558f2b | 151 | $pay = new Payment($ref); |
152 | ||
153 | if($pay->flags->hasflag('old')){ | |
a7d35093 | 154 | $page->trigError("La transaction selectionnée est périmée."); |
a2558f2b | 155 | $pay = new Payment(); |
156 | } | |
a690a74c | 157 | $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->amount_def; |
a2558f2b | 158 | |
159 | if (($e = $pay->check($val)) !== true) { | |
a7d35093 | 160 | $page->trigError($e); |
a2558f2b | 161 | } |
162 | ||
163 | if ($op=='submit') { | |
164 | $pay->init($val, $meth); | |
165 | $pay->prepareform($pay); | |
166 | } else { | |
69fffc4b FB |
167 | $res = XDB::iterator("SELECT timestamp, amount |
168 | FROM payment_transactions | |
8a7fab54 | 169 | WHERE uid = {?} AND ref = {?} |
170 | ORDER BY timestamp DESC", | |
171 | S::v('uid', -1), $ref); | |
a2558f2b | 172 | |
173 | if ($res->total()) $page->assign('transactions', $res); | |
174 | } | |
175 | ||
176 | $val = floor($val).".".substr(floor(($val - floor($val))*100+100),1); | |
177 | $page->assign('montant',$val); | |
bff6d838 | 178 | $page->assign('comment',Env::v('comment')); |
a2558f2b | 179 | |
180 | $page->assign('meth', $meth); | |
181 | $page->assign('pay', $pay); | |
182 | $page->assign('evtlink', $pay->event()); | |
d6c91d50 | 183 | $page->assign('sex', S::user()->isFemale()); |
a2558f2b | 184 | } |
185 | ||
186 | function handler_cyber_return(&$page, $uid = null) | |
187 | { | |
a2558f2b | 188 | /* reference banque (numero de transaction) */ |
7280eb45 | 189 | $champ901 = Env::s('CHAMP901'); |
a2558f2b | 190 | /* cle d'acceptation */ |
7280eb45 | 191 | $champ905 = Env::s('CHAMP905'); |
a2558f2b | 192 | /* code retour */ |
7280eb45 | 193 | $champ906 = Env::s('CHAMP906'); |
a2558f2b | 194 | /* email renvoye par la banque */ |
7280eb45 | 195 | $champ104 = Env::s('CHAMP104'); |
a2558f2b | 196 | /* reference complete de la commande */ |
7280eb45 | 197 | $champ200 = Env::s('CHAMP200'); |
a2558f2b | 198 | /* montant de la transaction */ |
7280eb45 | 199 | $champ201 = Env::s('CHAMP201'); |
a2558f2b | 200 | /* devise */ |
7280eb45 | 201 | $champ202 = Env::s('CHAMP202'); |
a2558f2b | 202 | $montant = "$champ201 $champ202"; |
203 | ||
204 | /* on extrait les informations sur l'utilisateur */ | |
1eaaa62d FB |
205 | $user = User::get($uid); |
206 | if (!$user) { | |
a2558f2b | 207 | cb_erreur("uid invalide"); |
208 | } | |
209 | ||
210 | ||
211 | /* on extrait la reference de la commande */ | |
1eaaa62d | 212 | if (!ereg('-xorg-([0-9]+)$', $champ200, $matches)) { |
a7de4ef7 | 213 | cb_erreur("référence de commande invalide"); |
a2558f2b | 214 | } |
215 | ||
216 | echo ($ref = $matches[1]); | |
1eaaa62d | 217 | $res = XDB::query("SELECT mail, text, confirmation |
69fffc4b | 218 | FROM payments |
98a7e9dc | 219 | WHERE id={?}", $ref); |
1eaaa62d | 220 | if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) { |
a7de4ef7 | 221 | cb_erreur("référence de commande inconnue"); |
a2558f2b | 222 | } |
223 | ||
224 | /* on extrait le code de retour */ | |
225 | if ($champ906 != "0000") { | |
1eaaa62d | 226 | $res = XDB::query('SELECT rcb.text, c.id, c.text |
69fffc4b FB |
227 | FROM payment_codeRCB AS rcb |
228 | LEFT JOIN payment_codeC AS c ON (rcb.codeC = c.id) | |
1eaaa62d | 229 | WHERE rcb.id = {?}', $champ906); |
a2558f2b | 230 | if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) { |
231 | cb_erreur("erreur lors du paiement : $c_text ($c_id)"); | |
eaf30d86 | 232 | } else{ |
a2558f2b | 233 | cb_erreur("erreur inconnue lors du paiement"); |
234 | } | |
235 | } | |
236 | ||
237 | /* on fait l'insertion en base de donnees */ | |
69fffc4b | 238 | XDB::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment) |
1eaaa62d FB |
239 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})", |
240 | $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env::v('comment')); | |
a2558f2b | 241 | |
9ff5b337 SJ |
242 | // We check if it is an Xnet payment and then update the related ML. |
243 | $res = XDB::query('SELECT eid | |
eb41eda9 | 244 | FROM group_events |
9ff5b337 SJ |
245 | WHERE paiement_id = {?}', $ref); |
246 | if ($eid = $res->fetchOneCell()) { | |
fd03857b | 247 | require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php'; |
9ff5b337 | 248 | $evt = get_event_detail($eid); |
50208d22 | 249 | subscribe_lists_event($uid, $evt, 1, $montant, true); |
9ff5b337 SJ |
250 | } |
251 | ||
a2558f2b | 252 | /* on genere le mail de confirmation */ |
d1e61677 | 253 | $conf_text = str_replace( |
2d81f9c4 | 254 | array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', '<comment>'), |
d1e61677 SJ |
255 | array($user->firstName(), $user->lastName(), $user->promo(), $montant, |
256 | $user->isFemale() ? 'Chère' : 'Cher', $user->isFemale() ? 'Chère' : 'Cher', | |
257 | Env::v('comment')), $conf_text); | |
a2558f2b | 258 | |
7895f3c1 | 259 | global $globals; |
1e33266a | 260 | $mymail = new PlMailer(); |
a2558f2b | 261 | $mymail->setFrom($conf_mail); |
a2558f2b | 262 | $mymail->addCc($conf_mail); |
263 | $mymail->setSubject($conf_title); | |
88e3843c | 264 | $mymail->setWikiBody($conf_text); |
1eaaa62d | 265 | $mymail->sendTo($user); |
a2558f2b | 266 | |
a7de4ef7 | 267 | /* on envoie les details de la transaction à telepaiement@ */ |
1e33266a | 268 | $mymail = new PlMailer(); |
1d55fe45 | 269 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
d7dd70be | 270 | $mymail->addTo($globals->money->email); |
a2558f2b | 271 | $mymail->setSubject($conf_title); |
1eaaa62d FB |
272 | $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" . |
273 | 'mail : ' . $user->forlifeEmail() . "\n\n" . | |
a2558f2b | 274 | "paiement : $conf_title ($conf_mail)\n". |
275 | "reference : $champ200\n". | |
276 | "montant : $montant\n\n". | |
277 | "dump de REQUEST:\n". | |
278 | var_export($_REQUEST,true); | |
279 | $mymail->setTxtBody($msg); | |
280 | $mymail->send(); | |
281 | exit; | |
282 | } | |
283 | ||
a690a74c DB |
284 | function handler_cyber2_return(&$page, $uid = null) |
285 | { | |
286 | global $globals, $platal; | |
aab2ffdd | 287 | |
a690a74c DB |
288 | /* on vérifie la signature */ |
289 | $vads_params = array(); | |
290 | foreach($_REQUEST as $key => $value) | |
9d773172 DB |
291 | if(substr($key,0,5) == "vads_") |
292 | $vads_params[$key] = $value; | |
a690a74c DB |
293 | ksort($vads_params); |
294 | $signature = sha1(join('+',$vads_params).'+'.$globals->money->cyperplus_key); | |
295 | //if($signature != Env::v('signature')) { | |
296 | // cb_erreur("signature invalide"); | |
297 | //} | |
aab2ffdd | 298 | |
a690a74c | 299 | /* on extrait les informations sur l'utilisateur */ |
ba0fd7f8 | 300 | $user = User::get(Env::i('vads_cust_id')); |
a690a74c DB |
301 | if (!$user) { |
302 | cb_erreur("uid invalide"); | |
303 | } | |
304 | ||
305 | /* on extrait la reference de la commande */ | |
306 | if (!ereg('-([0-9]+)$', Env::v('vads_order_id'), $matches)) { | |
307 | cb_erreur("référence de commande invalide"); | |
308 | } | |
309 | ||
9d773172 | 310 | $ref = $matches[1]; |
a690a74c DB |
311 | $res = XDB::query("SELECT mail, text, confirmation |
312 | FROM payments | |
313 | WHERE id={?}", $ref); | |
781c92a2 | 314 | if ($res->numRows() != 1) { |
a690a74c DB |
315 | cb_erreur("référence de commande inconnue"); |
316 | } | |
781c92a2 | 317 | list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow(); |
aab2ffdd | 318 | |
a690a74c DB |
319 | /* on extrait le montant */ |
320 | if (Env::v('vads_currency') != "978") { | |
321 | cb_erreur("monnaie autre que l'euro"); | |
322 | } | |
ba0fd7f8 RB |
323 | $amount = ((float)Env::i('vads_amount')) / 100; |
324 | $montant = sprintf("%.02f EUR", $amount); | |
a690a74c DB |
325 | |
326 | /* on extrait le code de retour */ | |
327 | if (Env::v('vads_result') != "00") { | |
328 | cb_erreur("erreur lors du paiement : ?? (".Env::v('vads_result').")"); | |
329 | } | |
aab2ffdd | 330 | |
a690a74c DB |
331 | /* on fait l'insertion en base de donnees */ |
332 | XDB::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment) | |
333 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})", | |
334 | Env::v('vads_trans_date'), $user->id(), $ref, Env::v('vads_order_id'), $montant, "", Env::v('vads_order_info')); | |
9d773172 | 335 | echo "Paiement stored.\n"; |
aab2ffdd | 336 | |
a690a74c | 337 | // We check if it is an Xnet payment and then update the related ML. |
6848fb7e | 338 | $res = XDB::query('SELECT eid, asso_id |
a690a74c DB |
339 | FROM group_events |
340 | WHERE paiement_id = {?}', $ref); | |
781c92a2 | 341 | if ($res->numRows() == 1) { |
6848fb7e | 342 | list($eid, $asso_id) = $res->fetchOneRow(); |
a690a74c | 343 | require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php'; |
6848fb7e | 344 | $evt = get_event_detail($eid, false, $asso_id); |
ba0fd7f8 | 345 | subscribe_lists_event($user->id(), $evt, 1, $amount, true); |
a690a74c DB |
346 | } |
347 | ||
348 | /* on genere le mail de confirmation */ | |
349 | $conf_text = str_replace( | |
781c92a2 | 350 | array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', '<comment>'), |
a690a74c DB |
351 | array($user->firstName(), $user->lastName(), $user->promo(), $montant, |
352 | $user->isFemale() ? 'Chère' : 'Cher', $user->isFemale() ? 'Chère' : 'Cher', | |
353 | Env::v('comment')), $conf_text); | |
354 | ||
355 | global $globals; | |
356 | $mymail = new PlMailer(); | |
357 | $mymail->setFrom($conf_mail); | |
358 | $mymail->addCc($conf_mail); | |
359 | $mymail->setSubject($conf_title); | |
360 | $mymail->setWikiBody($conf_text); | |
361 | $mymail->sendTo($user); | |
362 | ||
363 | /* on envoie les details de la transaction à telepaiement@ */ | |
364 | $mymail = new PlMailer(); | |
365 | $mymail->setFrom("webmaster@" . $globals->mail->domain); | |
366 | $mymail->addTo($globals->money->email); | |
367 | $mymail->setSubject($conf_title); | |
368 | $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" . | |
369 | 'mail : ' . $user->forlifeEmail() . "\n\n" . | |
370 | "paiement : $conf_title ($conf_mail)\n". | |
9d773172 | 371 | "reference : " . Env::v('vads_order_id') . "\n". |
a690a74c DB |
372 | "montant : $montant\n\n". |
373 | "dump de REQUEST:\n". | |
374 | var_export($_REQUEST,true); | |
375 | $mymail->setTxtBody($msg); | |
376 | $mymail->send(); | |
9d773172 | 377 | echo "Notifications sent.\n"; |
a690a74c DB |
378 | exit; |
379 | } | |
380 | ||
a2558f2b | 381 | function handler_paypal_return(&$page, $uid = null) |
382 | { | |
da157660 | 383 | $page->changeTpl('payment/retour_paypal.tpl'); |
a2558f2b | 384 | |
385 | /* reference banque (numero de transaction) */ | |
7280eb45 | 386 | $no_transaction = Env::s('tx'); |
a2558f2b | 387 | /* token a renvoyer pour avoir plus d'information */ |
7280eb45 | 388 | $clef = Env::s('sig'); |
a2558f2b | 389 | /* code retour */ |
7280eb45 | 390 | $status = Env::s('st'); |
a2558f2b | 391 | /* raison */ |
7280eb45 | 392 | $reason = ($status == 'Pending')? Env::s('pending_reason'): Env::s('reason_code'); |
a2558f2b | 393 | /* reference complete de la commande */ |
7280eb45 | 394 | $fullref = Env::s('cm'); |
a2558f2b | 395 | /* montant de la transaction */ |
7280eb45 | 396 | $montant_nb = Env::s('amt'); |
a2558f2b | 397 | /* devise */ |
7280eb45 | 398 | $montant_dev = Env::s('cc'); |
a2558f2b | 399 | $montant = "$montant_nb $montant_dev"; |
400 | ||
401 | /* on extrait le code de retour */ | |
402 | if ($status != "Completed") { | |
403 | if ($status) | |
404 | paypal_erreur("erreur lors du paiement : $status - $reason"); | |
405 | else | |
a7de4ef7 | 406 | paypal_erreur("Paiement annulé", false); |
a2558f2b | 407 | } |
408 | ||
409 | /* on extrait les informations sur l'utilisateur */ | |
1eaaa62d FB |
410 | $user = User::get($uid); |
411 | if (!$user) { | |
a2558f2b | 412 | paypal_erreur("uid invalide"); |
413 | } | |
414 | ||
415 | /* on extrait la reference de la commande */ | |
1eaaa62d | 416 | if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) { |
a7de4ef7 | 417 | paypal_erreur("référence de commande invalide"); |
a2558f2b | 418 | } |
419 | ||
420 | $ref = $matches[1]; | |
1eaaa62d | 421 | $res = XDB::query("SELECT mail, text, confirmation |
69fffc4b | 422 | FROM payments |
1eaaa62d | 423 | WHERE id = {?}", $ref); |
a2558f2b | 424 | if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) { |
a7de4ef7 | 425 | paypal_erreur("référence de commande inconnue"); |
a2558f2b | 426 | } |
427 | ||
428 | /* on fait l'insertion en base de donnees */ | |
69fffc4b | 429 | XDB::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment) |
1eaaa62d FB |
430 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})", |
431 | $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env::v('comment')); | |
a2558f2b | 432 | |
9ff5b337 SJ |
433 | // We check if it is an Xnet payment and then update the related ML. |
434 | $res = XDB::query('SELECT eid | |
eb41eda9 | 435 | FROM group_events |
9ff5b337 SJ |
436 | WHERE paiement_id = {?}', $ref); |
437 | if ($eid = $res->fetchOneCell()) { | |
fd03857b | 438 | require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php'; |
9ff5b337 | 439 | $evt = get_event_detail($eid); |
50208d22 | 440 | subscribe_lists_event($user->id(), $evt, 1, $montant, true); |
9ff5b337 SJ |
441 | } |
442 | ||
a2558f2b | 443 | /* on genere le mail de confirmation */ |
1eaaa62d FB |
444 | $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'), |
445 | array($user->firstName(), $user->lastName(), $user->promo(), $montant, | |
446 | $user->isFemale() ? 'Chère' : 'Cher', | |
447 | $user->isFemale() ? 'Chère' : 'Cher'), $conf_text); | |
a2558f2b | 448 | |
7895f3c1 | 449 | global $globals; |
1e33266a | 450 | $mymail = new PlMailer(); |
a2558f2b | 451 | $mymail->setFrom($conf_mail); |
a2558f2b | 452 | $mymail->addCc($conf_mail); |
453 | $mymail->setSubject($conf_title); | |
88e3843c | 454 | $mymail->setWikiBody($conf_text); |
1eaaa62d | 455 | $mymail->sendTo($user); |
a2558f2b | 456 | |
a7de4ef7 | 457 | /* on envoie les details de la transaction à telepaiement@ */ |
1e33266a | 458 | $mymail = new PlMailer(); |
1d55fe45 | 459 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
d7dd70be | 460 | $mymail->addTo($globals->money->email); |
a2558f2b | 461 | $mymail->setSubject($conf_title); |
1eaaa62d FB |
462 | $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" . |
463 | 'mail : ' . $user->forlifeEmail() . "\n\n" . | |
a2558f2b | 464 | "paiement : $conf_title ($conf_mail)\n". |
1eaaa62d | 465 | "reference : $champ200\n". |
a2558f2b | 466 | "montant : $montant\n\n". |
467 | "dump de REQUEST:\n". | |
468 | var_export($_REQUEST,true); | |
469 | $mymail->setTxtBody($msg); | |
470 | $mymail->send(); | |
471 | ||
472 | $page->assign('texte', $conf_text); | |
473 | $page->assign('erreur', $erreur); | |
a2558f2b | 474 | } |
98a7e9dc | 475 | |
476 | function handler_xnet_payment(&$page, $pid = null) | |
477 | { | |
478 | global $globals; | |
eaf30d86 | 479 | |
45a5307b FB |
480 | $perms = S::v('perms'); |
481 | if (!$perms->hasFlag('groupmember')) { | |
482 | if (is_null($pid)) { | |
483 | return PL_FORBIDDEN; | |
484 | } | |
485 | $res = XDB::query("SELECT 1 | |
eb41eda9 | 486 | FROM group_events AS e |
60dd1466 | 487 | INNER JOIN group_event_participants AS ep ON (ep.eid = e.eid AND e.uid = {?}) |
45a5307b FB |
488 | WHERE e.paiement_id = {?} AND e.asso_id = {?}", |
489 | S::i('uid'), $pid, $globals->asso('id')); | |
490 | if ($res->numRows() == 0) { | |
491 | return PL_FORBIDDEN; | |
492 | } | |
493 | } | |
494 | ||
98a7e9dc | 495 | if (!is_null($pid)) { |
496 | return $this->handler_payment($page, $pid); | |
497 | } | |
1490093c | 498 | $page->changeTpl('payment/xnet.tpl'); |
eaf30d86 | 499 | |
98a7e9dc | 500 | $res = XDB::query( |
501 | "SELECT id, text, url | |
a690a74c | 502 | FROM payments |
010268b2 | 503 | WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags) |
98a7e9dc | 504 | ORDER BY id DESC", $globals->asso('id')); |
505 | $tit = $res->fetchAllAssoc(); | |
506 | $page->assign('titres', $tit); | |
507 | ||
98a7e9dc | 508 | |
1eaaa62d | 509 | // TODO: replug sort. |
98a7e9dc | 510 | $trans = array(); |
511 | $event = array(); | |
512 | foreach($tit as $foo) { | |
513 | $pid = $foo['id']; | |
514 | if (may_update()) { | |
b3cd1320 | 515 | $res = XDB::query('SELECT t.uid, timestamp AS `date`, t.comment, amount |
a690a74c | 516 | FROM payment_transactions AS t |
1eaaa62d FB |
517 | WHERE t.ref = {?}', $pid); |
518 | $trans[$pid] = User::getBulkUsersWithUIDs($res->fetchAllAssoc(), 'uid', 'user'); | |
519 | $sum = 0; | |
520 | foreach ($trans[$pid] as $i => $t) { | |
b3cd1320 DB |
521 | $sum += strtr(substr($t['amount'], 0, strpos($t['amount'], 'EUR')), ',', '.'); |
522 | $trans[$pid][$i]['amount'] = str_replace('EUR', '€', $t['amount']); | |
1eaaa62d FB |
523 | } |
524 | $trans[$pid][] = array('nom' => 'somme totale', | |
b3cd1320 | 525 | 'amount' => strtr($sum, '.', ',').' €'); |
98a7e9dc | 526 | } |
527 | $res = XDB::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid | |
eb41eda9 | 528 | FROM group_events AS e |
e848335b | 529 | LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND ep.uid = {?}) |
eb41eda9 | 530 | INNER JOIN group_event_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id) |
98a7e9dc | 531 | WHERE e.paiement_id = {?}", |
532 | S::v('uid'), $pid); | |
533 | $event[$pid] = array(); | |
534 | $event[$pid]['paid'] = 0; | |
535 | if ($res->total()) { | |
536 | $event[$pid]['topay'] = 0; | |
537 | while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) { | |
538 | $event[$pid]['topay'] += ($nb * $montant); | |
539 | $event[$pid]['eid'] = $eid; | |
540 | $event[$pid]['shortname'] = $shortname; | |
541 | $event[$pid]['title'] = $title; | |
542 | $event[$pid]['ins'] = !is_null($nb); | |
543 | $event[$pid]['paid'] = $paid; | |
544 | } | |
545 | } | |
b3cd1320 | 546 | $res = XDB::query("SELECT amount |
a690a74c | 547 | FROM payment_transactions AS t |
98a7e9dc | 548 | WHERE ref = {?} AND uid = {?}", $pid, S::v('uid')); |
549 | $montants = $res->fetchColumn(); | |
550 | ||
551 | foreach ($montants as $m) { | |
552 | $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.'); | |
553 | $event[$pid]['paid'] += trim($p); | |
554 | } | |
555 | } | |
17793ccf | 556 | $page->register_modifier('decode_comment', 'decode_comment'); |
98a7e9dc | 557 | $page->assign('trans', $trans); |
558 | $page->assign('event', $event); | |
559 | } | |
eaf30d86 | 560 | |
92423144 | 561 | function handler_admin(&$page, $action = 'list', $id = null) { |
46f272fe | 562 | $page->setTitle('Administration - Paiements'); |
a7de4ef7 | 563 | $page->assign('title', 'Gestion des télépaiements'); |
69fffc4b | 564 | $table_editor = new PLTableEditor('admin/payments','payments','id'); |
f56ceafe | 565 | //$table_editor->add_join_table('payment_transactions','ref',true); => on ne supprime jamais une transaction |
2e7b5921 | 566 | $table_editor->add_sort_field('flags'); |
de61dbcf | 567 | $table_editor->add_sort_field('id', true, true); |
69fffc4b | 568 | $table_editor->on_delete("UPDATE payments SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé"); |
a7de4ef7 | 569 | $table_editor->describe('text','intitulé',true); |
92423144 | 570 | $table_editor->describe('url','site web',false); |
69fffc4b FB |
571 | $table_editor->describe('amount_def','montant par défaut',false); |
572 | $table_editor->describe('amount_min','montant minimum',false); | |
573 | $table_editor->describe('amount_max','montant maximum',false); | |
92423144 | 574 | $table_editor->describe('mail','email contact',true); |
575 | $table_editor->describe('confirmation','message confirmation',false); | |
9c966750 PC |
576 | |
577 | // adds a column with the start date of the linked event if there is one | |
578 | $table_editor->add_option_table('group_events','group_events.paiement_id = t.id'); | |
579 | $table_editor->add_option_field('group_events.debut', 'related_event', 'évènement', 'timestamp'); | |
580 | ||
92423144 | 581 | $table_editor->apply($page, $action, $id); |
eaf30d86 | 582 | } |
f56ceafe DB |
583 | |
584 | function handler_adm_transactions(&$page, $payment_id = null, $action = "list", $id = null) { | |
585 | // show transactions. FIXME: should not be modifiable | |
586 | $page->setTitle('Administration - Paiements - Transactions'); | |
587 | $page->assign('title', "Liste des transactions pour le paiement {$payment_id}"); | |
588 | ||
589 | if ($payment_id == null) | |
590 | $page->trigError("Aucun ID de paiement fourni."); | |
591 | ||
592 | $table_editor = new PLTableEditor("admin/transactions/{$payment_id}",'payment_transactions','id'); | |
593 | $table_editor->set_where_clause(XDB::format('ref = {?}', $payment_id)); | |
594 | $table_editor->apply($page, 'list', $id); // only the 'list' action is allowed | |
595 | $page->assign("readonly","readonly"); // don't show modification features | |
596 | } | |
597 | ||
598 | function handler_adm_bankaccounts(&$page, $action = "list", $id = null) { | |
599 | // managment of bank account used for money transfert | |
600 | $page->setTitle('Administration - Paiements - RIBs'); | |
601 | $page->assign('title', "Liste des RIBs"); | |
602 | ||
603 | $table_editor = new PLTableEditor("admin/payments/bankaccounts",'payment_bankaccounts','id'); | |
604 | $table_editor->describe('asso_id','ID du groupe',false); | |
605 | $table_editor->describe('owner','titulaire',true); | |
606 | $table_editor->add_option_table('groups','groups.id = t.asso_id'); | |
607 | $table_editor->add_option_field('groups.diminutif', 'group_name', 'groupe', 'varchar','account'); | |
608 | ||
609 | // check RIB key | |
610 | if ($action == "update" && Post::has("account") && !check_rib(Post::v("account"))) { | |
611 | $page->trigError("Le RIB n'est pas valide"); | |
f3d6e2cc | 612 | $table_editor->apply($page, "edit", $id); |
f56ceafe DB |
613 | return; |
614 | } | |
615 | ||
616 | $table_editor->apply($page, $action, $id); | |
617 | } | |
618 | ||
619 | function handler_adm_methods(&$page, $action = "list", $id = null) { | |
620 | // show and edit payment methods | |
621 | $page->setTitle('Administration - Paiements - Méthodes'); | |
622 | $page->assign('title', "Méthodes de paiement"); | |
623 | $table_editor = new PLTableEditor("admin/payments/methods",'payment_methods','id'); | |
624 | $table_editor->apply($page, $action, $id); | |
625 | } | |
626 | ||
627 | function handler_adm_reconcile(&$page, $step = 'list', $param = null) { | |
628 | // reconciles logs with transactions | |
629 | // FIXME: the admin is considered to be fair => he doesn't hack the $step value, nor other params | |
630 | $page->setTitle('Administration - Paiements - Réconciliations'); | |
631 | $page->changeTpl('payment/reconcile.tpl'); | |
632 | $page->assign('step', $step); | |
f3d6e2cc DB |
633 | $list = true; |
634 | ||
635 | // actions | |
636 | if ($step == 'delete' && $param != null) { | |
637 | S::assert_xsrf_token(); | |
638 | XDB::execute("DELETE FROM payment_reconcilations WHERE id={?}", $param); | |
639 | // FIXME: hardcoding !!! | |
640 | XDB::execute("UPDATE payment_transactions SET recon_id=NULL,commission=NULL WHERE recon_id={?} AND method_id=2", $param); | |
641 | XDB::execute("UPDATE payment_transactions SET recon_id=NULL WHERE recon_id={?} AND method_id=1", $param); | |
642 | $page->trigSuccess("L'entrée ".$param." a été supprimée."); | |
643 | ||
644 | } elseif ($step == 'edit') { | |
645 | $page->trigError("L'édition n'est pas implémentée."); | |
646 | ||
647 | } elseif ($step == 'step5') { | |
648 | $page->trigSuccess("La réconciliation est terminée. Il est maintenant nécessaire de générer les virements."); | |
f56ceafe | 649 | |
f3d6e2cc | 650 | } |
77b81d91 | 651 | |
f3d6e2cc DB |
652 | if($list) { |
653 | // show list of reconciliations, with a "add" button | |
654 | $page->assign('title', "Réconciliation - Liste"); | |
77b81d91 | 655 | $page->assign('step', 'list'); |
f3d6e2cc DB |
656 | |
657 | $recongps = array(); | |
658 | ||
659 | $res = XDB::query("SELECT r.id, short_name AS method, period_start, period_end, status, | |
660 | payment_count, sum_amounts, sum_commissions | |
661 | FROM payment_reconcilations AS r | |
662 | LEFT JOIN payment_methods AS m ON r.method_id=m.id | |
663 | WHERE recongroup_id IS NULL | |
664 | ORDER BY period_end DESC, period_start DESC"); | |
665 | foreach ($res->fetchAllAssoc() as $recon) | |
666 | $recongps[] = array('recons' => array($recon), 'transfers' => array()); | |
667 | ||
668 | $res = XDB::query("SELECT recongroup_id AS id | |
669 | FROM payment_reconcilations | |
670 | GROUP BY recongroup_id | |
671 | ORDER BY MAX(period_end) DESC, MIN(period_start) DESC"); | |
672 | foreach ($res->fetchAllAssoc() as $recongp) { | |
673 | $res = XDB::query("SELECT r.id, short_name AS method, period_start, period_end, status, | |
674 | payment_count, sum_amounts, sum_commissions | |
675 | FROM payment_reconcilations AS r | |
676 | LEFT JOIN payment_methods AS m ON r.method_id=m.id | |
677 | WHERE recongroup_id={?} | |
678 | ORDER BY period_end DESC, period_start DESC", | |
679 | $recongp['id']); | |
680 | $recongp['recons'] = $res->fetchAllAssoc(); | |
681 | ||
682 | $res = XDB::query("SELECT id, payment_id, amount, account_id, message, date | |
683 | FROM payment_transfers | |
684 | WHERE recongroup_id={?}", | |
685 | $recongp['id']); | |
686 | $recongp['transfers'] = $res->fetchAllAssoc(); | |
687 | ||
688 | $recongps[] = $recongp; | |
689 | } | |
690 | $page->assign_by_ref('recongps', $recongps); | |
691 | } | |
77b81d91 DB |
692 | } |
693 | ||
f3d6e2cc | 694 | function handler_adm_importlogs(&$page, $step, $param = null) { |
77b81d91 DB |
695 | $page->setTitle('Administration - Paiements - Réconciliations'); |
696 | $page->changeTpl('payment/reconcile.tpl'); | |
697 | $page->assign('step', $step); | |
698 | ||
699 | if (isset($_SESSION['paymentrecon_data'])) { | |
f56ceafe DB |
700 | // create temporary table with imported data |
701 | XDB::execute("CREATE TEMPORARY TABLE payment_tmp ( | |
702 | reference VARCHAR(255) PRIMARY KEY, | |
703 | date DATE, | |
704 | amount DECIMAL(9,2), | |
705 | commission DECIMAL(9,2) | |
706 | )"); | |
707 | foreach ($_SESSION['paymentrecon_data'] as $i) | |
708 | XDB::execute("INSERT INTO payment_tmp VALUES ({?},{?},{?},{?})", | |
709 | $i['reference'], $i['date'], $i['amount'], $i['commission']); | |
710 | } | |
711 | ||
77b81d91 | 712 | if ($step == 'step1') { |
f56ceafe | 713 | $page->assign('title', "Étape 1"); |
77b81d91 DB |
714 | unset($_SESSION['paymentrecon_method']); |
715 | unset($_SESSION['paymentrecon_data']); | |
716 | unset($_SESSION['paymentrecon_id']); | |
f56ceafe DB |
717 | |
718 | // was a payment method choosen ? | |
719 | if ($param != null) { | |
720 | $_SESSION['paymentrecon_method'] = (int)$param; | |
77b81d91 | 721 | pl_redirect("admin/reconcile/importlogs/step2"); |
f56ceafe DB |
722 | |
723 | } else { | |
724 | // ask to choose a payment method | |
725 | $res = XDB::query("SELECT id, text FROM payment_methods"); | |
726 | $page->assign('methods', $res->fetchAllAssoc()); | |
727 | } | |
728 | ||
729 | } elseif ( $step == 'step2' ) { | |
730 | $page->assign('title', "Étape 2"); | |
731 | ||
732 | // import logs formated in CVS | |
733 | $fields = array('date','reference','amount','commission'); | |
734 | $importer = new PaymentLogsImporter(); | |
77b81d91 | 735 | $importer->apply($page, 'admin/reconcile/importlogs/step2', $fields); |
f56ceafe DB |
736 | |
737 | // if import is finished | |
738 | $result = $importer->get_result(); | |
739 | if($result != null) { | |
740 | $_SESSION['paymentrecon_data'] = $result; | |
77b81d91 | 741 | pl_redirect("admin/reconcile/importlogs/step3"); |
f56ceafe DB |
742 | } |
743 | ||
744 | } elseif ($step == 'step3' ) { | |
745 | $page->assign('title', "Étape 3"); | |
746 | ||
747 | // compute reconcilation summary data | |
748 | $res = XDB::query("SELECT MIN(date) AS period_start, MAX(date) AS period_end, | |
749 | count(*) AS payment_count, SUM(amount) AS sum_amounts, | |
750 | SUM(commission) AS sum_commissions | |
751 | FROM payment_tmp"); | |
752 | $recon = $res->fetchOneAssoc(); | |
753 | $recon['method_id'] = $_SESSION['paymentrecon_method']; | |
754 | ||
755 | // create reconciliation item in database | |
756 | if(Post::has('next')) { | |
757 | S::assert_xsrf_token(); | |
758 | ||
759 | // get parameters | |
760 | $recon['period_start'] = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/','\3-\2-\1',Post::v('period_start')); | |
761 | $recon['period_end'] = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/','\3-\2-\1',Post::v('period_end')); | |
762 | // FIXME: save checks to be done at next step | |
763 | ||
764 | // Create reconcilation item in database | |
765 | // FIXME: check if period doesn't overlap with others for the same method_id | |
766 | XDB::execute("INSERT INTO payment_reconcilations (method_id, period_start, period_end, | |
767 | payment_count, sum_amounts, sum_commissions) | |
768 | VALUES ({?}, {?}, {?}, {?}, {?}, {?})", | |
769 | $recon['method_id'], $recon['period_start'], $recon['period_end'], | |
770 | $recon['payment_count'], $recon['sum_amounts'], $recon['sum_commissions']); | |
771 | $_SESSION['paymentrecon_id'] = XDB::insertId(); | |
772 | ||
773 | // reconcile simple cases (trans.commission n'est modifié que s'il vaut NULL) | |
774 | XDB::execute("UPDATE payment_transactions AS trans, payment_tmp AS tmp | |
775 | SET trans.recon_id={?}, trans.commission=tmp.commission | |
776 | WHERE trans.fullref=tmp.reference | |
777 | AND trans.amount=tmp.amount AND DATE(trans.ts_confirmed)=tmp.date | |
778 | AND (trans.commission IS NULL OR trans.commission=tmp.commission) | |
779 | AND method_id={?} AND recon_id IS NULL AND status='confirmed'", | |
780 | $_SESSION['paymentrecon_id'], $recon['method_id']); | |
781 | ||
77b81d91 | 782 | pl_redirect("admin/reconcile/importlogs/step4"); |
f56ceafe DB |
783 | |
784 | // show summary of the imported data + ask form start/end of reconcilation period | |
785 | } else { | |
786 | $recon['period_start'] = preg_replace('/([0-9]{4})-([0-9]{2})-([0-9]{2})/', '\3/\2/\1', $recon['period_start']); | |
787 | $recon['period_end'] = preg_replace('/([0-9]{4})-([0-9]{2})-([0-9]{2})/', '\3/\2/\1', $recon['period_end']); | |
788 | $page->assign('recon', $recon); | |
789 | } | |
790 | ||
791 | } elseif ($step == 'step4' ) { | |
792 | $page->assign('title', "Étape 4"); | |
793 | ||
794 | // get reconcilation summary informations | |
795 | $res = XDB::query("SELECT * FROM payment_reconcilations WHERE id={?}", $_SESSION['paymentrecon_id']); | |
796 | $recon = $res->fetchOneAssoc(); | |
797 | $page->assign('recon', $recon); | |
798 | ||
799 | if (Post::has('force')) { | |
800 | S::assert_xsrf_token(); | |
801 | foreach (Post::v('force') as $id => $value) { | |
802 | XDB::execute("UPDATE payment_transactions AS trans, payment_tmp AS tmp | |
803 | SET trans.recon_id={?}, trans.commission=tmp.commission | |
804 | WHERE trans.id={?} AND trans.fullref=tmp.reference", | |
805 | $_SESSION['paymentrecon_id'], $id); | |
806 | } | |
807 | $page->trigSuccess('La réconciliation a été forcée pour '.count(Post::v('force')).' transaction(s).'); | |
808 | ||
809 | } elseif (Post::has('next')) { | |
810 | if (strlen($recon['comments'])<3) { | |
811 | $page->trigError("Le commentaire doit contenir au moins 3 caractères."); | |
812 | } else { | |
813 | XDB::execute("UPDATE payment_reconcilations SET status='transfering' WHERE id={?}", $_SESSION['paymentrecon_id']); | |
77b81d91 | 814 | pl_redirect('admin/reconcile/step5'); |
f56ceafe DB |
815 | } |
816 | ||
817 | } elseif (Post::has('savecomments')) { | |
818 | S::assert_xsrf_token(); | |
819 | $recon['comments'] = Post::v('comments'); | |
f3d6e2cc | 820 | $page->assign('recon', $recon); |
f56ceafe DB |
821 | XDB::execute("UPDATE payment_reconcilations SET comments={?} WHERE id={?}", $recon['comments'], $_SESSION['paymentrecon_id']); |
822 | $page->trigSuccess('Les commentaires ont été enregistrés.'); | |
823 | } | |
824 | ||
825 | // reconcilation results - ok | |
826 | $res = XDB::query("SELECT count(*),SUM(amount),SUM(commission) | |
827 | FROM payment_transactions WHERE recon_id={?}", | |
828 | $recon['id']); | |
829 | list($ok_count,$ok_sum_amounts,$ok_sum_coms) = $res->fetchOneRow(); | |
830 | $page->assign('ok_count', $ok_count); | |
831 | ||
832 | // reconcilation results - ref exists, but some data differs | |
833 | $res = XDB::query("SELECT id, fullref, method_id, ts_confirmed, trans.amount, trans.commission, status, recon_id, | |
834 | reference, date, tmp.amount as amount2, tmp.commission as commission2 | |
835 | FROM payment_transactions AS trans | |
836 | INNER JOIN payment_tmp AS tmp ON trans.fullref=tmp.reference | |
837 | WHERE trans.recon_id IS NULL OR trans.recon_id != {?}", | |
838 | $recon['id']); | |
839 | $differs = $res->fetchAllAssoc(); | |
840 | $page->assign_by_ref('differs', $differs); | |
841 | $page->assign('differ_count', count($differs)); | |
842 | ||
843 | // reconcilation results - ref doesn't exists in database | |
844 | $res = XDB::query("SELECT tmp.* | |
845 | FROM payment_tmp AS tmp | |
846 | LEFT JOIN payment_transactions AS trans ON trans.fullref=tmp.reference | |
847 | WHERE trans.fullref IS NULL"); | |
848 | $only_import = $res->fetchAllAssoc(); | |
849 | $page->assign_by_ref('only_import', $only_import); | |
850 | $page->assign('onlyim_count', count($only_import)); | |
851 | ||
852 | // reconcilation results - exists in database but not in import | |
853 | $res = XDB::query("SELECT trans.* | |
854 | FROM payment_transactions AS trans | |
855 | LEFT JOIN payment_tmp AS tmp ON trans.fullref=tmp.reference | |
856 | WHERE {?}<=DATE(trans.ts_confirmed) AND DATE(trans.ts_confirmed)<={?} | |
857 | AND tmp.reference IS NULL AND method_id={?}", | |
858 | $recon['period_start'], $recon['period_end'], $recon['method_id']); | |
859 | $only_database = $res->fetchAllAssoc(); | |
860 | $page->assign_by_ref('only_database', $only_database); | |
861 | $page->assign('onlydb_count', count($only_database)); | |
f56ceafe | 862 | } |
f3d6e2cc DB |
863 | } |
864 | ||
77b81d91 DB |
865 | function handler_adm_transfers(&$page, $action = null, $id = null) { |
866 | // list/log all bank transfers and link them to individual transactions | |
867 | ||
f3d6e2cc DB |
868 | if (Post::has('generate')) { |
869 | $recon_ids = array_keys(Post::v('recon_id')); | |
870 | ||
871 | // generate a new reconcilation group ID | |
872 | $res = XDB::query("SELECT MAX(recongroup_id)+1 FROM payment_reconcilations"); | |
873 | $recongp_id = $res->fetchOneCell(); | |
874 | if ($recongp_id == null) $recongp_id = 1; | |
875 | ||
876 | // add reconcilations to group | |
877 | // FIXME: should check if reconcilations are in good status | |
878 | XDB::execute("UPDATE payment_reconcilations SET recongroup_id={?}, status='closed' | |
879 | WHERE id IN {?}", | |
880 | $recongp_id, $recon_ids); | |
881 | ||
882 | // create transfers | |
883 | XDB::execute("INSERT INTO payment_transfers | |
884 | SELECT NULL, {?}, t.ref, SUM(t.amount+t.commission), NULL, p.text, NULL | |
885 | FROM payment_transactions AS t | |
886 | LEFT JOIN payments AS p ON t.ref = p.id | |
887 | LEFT JOIN groups AS g ON p.asso_id = g.id | |
888 | WHERE t.recon_id IN {?} | |
889 | GROUP BY t.ref", | |
890 | $recongp_id, $recon_ids); | |
891 | ||
892 | //$res = XDB::query("SELECT * FROM payment_reconcilations WHERE id IN {?}", $recon_ids); | |
893 | //$recons = $res->fetchAllAssoc(); | |
894 | ||
895 | $page->trigSuccess("Les virements ont été générés pour ".count($recon_ids)." réconciliations."); | |
896 | $this->handler_adm_reconcile($page); | |
897 | ||
898 | } elseif ($action == "delgroup") { | |
899 | S::assert_xsrf_token(); | |
900 | XDB::execute("UPDATE payment_reconcilations SET status='transfering', recongroup_id=NULL WHERE recongroup_id={?}", $id); | |
901 | XDB::execute("DELETE FROM payment_transfers WHERE recongroup_id={?} AND date IS NULL", $id); | |
902 | ||
903 | $page->trigSuccess("Les virements non réalisés ont été supprimé du groupe ".$id."."); | |
904 | $this->handler_adm_reconcile($page); | |
905 | ||
906 | } elseif ($action == "confirm") { | |
907 | S::assert_xsrf_token(); | |
908 | XDB::execute("UPDATE payment_transfers SET date=NOW() WHERE id={?}", $id); | |
909 | ||
910 | $page->trigSuccess("Virement ".$id." confirmé."); | |
911 | $this->handler_adm_reconcile($page); | |
912 | ||
913 | } else { | |
914 | pl_redirect("admin/reconcile"); | |
915 | } | |
f56ceafe DB |
916 | } |
917 | } | |
918 | ||
919 | class PaymentLogsImporter extends CSVImporter { | |
f3d6e2cc DB |
920 | protected $result; |
921 | ||
922 | public function __construct() { | |
923 | parent::__construct(''); | |
924 | $this->registerFunction('systempay_commission', 'Compute BPLC commission', array($this,"compute_systempay_commission")); | |
925 | $this->registerFunction('payment_id', 'Autocompute payment ID', array($this,"compute_payment_id")); | |
f56ceafe | 926 | //$this->forceValue('payment_id','func_payment_id'); |
f3d6e2cc DB |
927 | } |
928 | ||
929 | public function run($action = null, $insert_relation = null, $update_relation = null) { | |
930 | $this->result = array(); | |
931 | foreach ($this->data as $line) { | |
f56ceafe | 932 | $a = $this->makeAssoc($line, $insert_relation); |
77b81d91 DB |
933 | $a['date'] = preg_replace('/([0-9]{2})\/([0-9]{2})\/([0-9]{4}).*/','\3-\2-\1', $a['date']); |
934 | $a['amount'] = str_replace(',','.',$a['amount']); | |
935 | $a['commission'] = str_replace(',','.',$a['commission']); | |
f3d6e2cc | 936 | $this->result[] = $a; |
f56ceafe | 937 | } |
f3d6e2cc DB |
938 | } |
939 | ||
940 | public function get_result() { | |
941 | return $this->result; | |
942 | } | |
943 | ||
944 | static public function compute_systempay_commission($line, $key, $relation) { | |
945 | static $EEE_countries = array('France','Allemagne','Autriche','Belgique','Bulgarie','Chypre', | |
946 | 'Danemark','Espagne','Estonie','Finlande','Grèce','Hongrie','Irlande','Islande','Italie', | |
947 | 'Lettonie','Liechtenstein','Lituanie','Luxembourg','Malte','Norvège','Pays-Bas','Pologne', | |
948 | 'Portugal','Roumanie','Royaume-Uni','Slovaquie','Slovénie','Suède','République Tchèque'); | |
949 | ||
950 | if($key!='commission' || !array_key_exists('carte',$line)) return null; | |
951 | $amount = self::getValue($line, 'amount', $relation['amount']); | |
952 | if (in_array($line['pays carte'],$EEE_countries)) | |
953 | return -0.20 - round($amount*0.005, 2); | |
954 | else | |
955 | return -0.20 - round($amount*0.005, 2) - 0.76; | |
956 | } | |
957 | ||
958 | static public function compute_payment_id($line, $key, $relation) { | |
959 | if ($key != 'payment_id') return null; | |
960 | $reference = self::getValue($line, 'reference', $relation['reference']); | |
f56ceafe | 961 | if (ereg('-([0-9]+)$', $reference, $matches)) |
f3d6e2cc | 962 | return $matches[1]; |
f56ceafe DB |
963 | else |
964 | return null; | |
f3d6e2cc | 965 | } |
a2558f2b | 966 | } |
967 | ||
a7de4ef7 | 968 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
a2558f2b | 969 | ?> |