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