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