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