Fix queries of payment, fix typo in xnetgrp, fix make check return value.
[platal.git] / modules / payment.php
index 69ba4c2..9324e08 100644 (file)
@@ -180,35 +180,31 @@ class PaymentModule extends PLModule
         $montant = "$champ201 $champ202";
 
         /* on extrait les informations sur l'utilisateur */
-        $res = XDB::query("
-            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
-              FROM  auth_user_md5 AS a
-        INNER JOIN  aliases       AS l ON (a.user_id=l.id AND type!='homonyme')
-             WHERE  a.user_id={?}", $uid);
-        if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
+        $user = User::get($uid);
+        if (!$user) {
             cb_erreur("uid invalide");
         }
 
 
         /* on extrait la reference de la commande */
-        if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
+        if (!ereg('-xorg-([0-9]+)$', $champ200, $matches)) {
             cb_erreur("référence de commande invalide");
         }
 
         echo ($ref = $matches[1]);
-        $res = XDB::query("SELECT  mail,text,confirmation
+        $res = XDB::query("SELECT  mail, text, confirmation
                              FROM  paiement.paiements
                             WHERE  id={?}", $ref);
-        if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
+        if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) {
             cb_erreur("référence de commande inconnue");
         }
 
         /* on extrait le code de retour */
         if ($champ906 != "0000") {
-            $res = XDB::query("SELECT  rcb.text,c.id,c.text
+            $res = XDB::query('SELECT  rcb.text, c.id, c.text
                                  FROM  paiement.codeRCB AS rcb
-                            LEFT JOIN  paiement.codeC   AS c ON rcb.codeC=c.id
-                                WHERE  rcb.id='$champ906'");
+                            LEFT JOIN  paiement.codeC   AS c ON (rcb.codeC = c.id)
+                                WHERE  rcb.id = {?}', $champ906);
             if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
                 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
             } else{
@@ -217,34 +213,31 @@ class PaymentModule extends PLModule
         }
 
         /* on fait l'insertion en base de donnees */
-        XDB::execute("INSERT INTO  paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
-                           VALUES  ({?},{?},{?},{?},{?},{?},{?})",
-                     $champ901, $uid, $ref, $champ200, $montant, $champ905,Env::v('comment'));
+        XDB::execute("INSERT INTO  paiement.transactions (id, uid, ref, fullref, montant, cle, comment)
+                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
+                     $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env::v('comment'));
 
         /* on genere le mail de confirmation */
-        $conf_text = str_replace("<prenom>",$prenom,$conf_text);
-        $conf_text = str_replace("<nom>",$nom,$conf_text);
-        $conf_text = str_replace("<promo>",$promo,$conf_text);
-        $conf_text = str_replace("<montant>",$montant,$conf_text);
-        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
-        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'),
+                                 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
+                                       $user->isFemale() ? 'Chère' : 'Cher',
+                                       $user->isFemale() ? 'Chère' : 'Cher'), $conf_text);
 
         global $globals;
         $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
-        $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail->domain . '>');
         $mymail->addCc($conf_mail);
         $mymail->setSubject($conf_title);
         $mymail->setWikiBody($conf_text);
-        $mymail->send(S::v('mail_fmt') == 'html');
+        $mymail->sendTo($user);
 
         /* on envoie les details de la transaction à telepaiement@ */
         $mymail = new PlMailer();
         $mymail->setFrom("webmaster@" . $globals->mail->domain);
         $mymail->addTo($globals->money->email);
         $mymail->setSubject($conf_title);
-        $msg = "utilisateur : $prenom $nom ($uid)\n".
-               "mail : $forlife@polytechnique.org\n\n".
+        $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
+               'mail : ' . $user->forlifeEmail() . "\n\n" .
                "paiement : $conf_title ($conf_mail)\n".
                "reference : $champ200\n".
                "montant : $montant\n\n".
@@ -284,59 +277,52 @@ class PaymentModule extends PLModule
         }
 
         /* on extrait les informations sur l'utilisateur */
-        $res = XDB::query("
-            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
-              FROM  auth_user_md5 AS a
-        INNER JOIN  aliases       AS l ON (a.user_id=l.id AND type!='homonyme')
-             WHERE  a.user_id={?}", $uid);
-        if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
+        $user = User::get($uid);
+        if (!$user) {
             paypal_erreur("uid invalide");
         }
 
         /* on extrait la reference de la commande */
-        if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
+        if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) {
             paypal_erreur("référence de commande invalide");
         }
 
         $ref = $matches[1];
-        $res = XDB::query("SELECT  mail,text,confirmation
+        $res = XDB::query("SELECT  mail, text, confirmation
                              FROM  paiement.paiements
-                            WHERE  id={?}", $ref);
+                            WHERE  id = {?}", $ref);
         if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
             paypal_erreur("référence de commande inconnue");
         }
 
         /* on fait l'insertion en base de donnees */
-        XDB::execute("INSERT INTO  paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
-                           VALUES  ({?},{?},{?},{?},{?},{?},{?})",
-                    $no_transaction, $uid, $ref, $fullref, $montant, $clef, Env::v('comment'));
+        XDB::execute("INSERT INTO  paiement.transactions (id, uid, ref, fullref, montant, cle, comment)
+                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
+                    $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env::v('comment'));
 
         /* on genere le mail de confirmation */
-        $conf_text = str_replace("<prenom>",$prenom,$conf_text);
-        $conf_text = str_replace("<nom>",$nom,$conf_text);
-        $conf_text = str_replace("<promo>",$promo,$conf_text);
-        $conf_text = str_replace("<montant>",$montant,$conf_text);
-        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
-        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'),
+                                 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
+                                       $user->isFemale() ? 'Chère' : 'Cher',
+                                       $user->isFemale() ? 'Chère' : 'Cher'), $conf_text);
 
         global $globals;
         $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
-        $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail->domain . '>');
         $mymail->addCc($conf_mail);
         $mymail->setSubject($conf_title);
         $mymail->setWikiBody($conf_text);
-        $mymail->send(S::v('mail_fmt') == 'html');
+        $mymail->sendTo($user);
 
         /* on envoie les details de la transaction à telepaiement@ */
         $mymail = new PlMailer();
         $mymail->setFrom("webmaster@" . $globals->mail->domain);
         $mymail->addTo($globals->money->email);
         $mymail->setSubject($conf_title);
-        $msg = "utilisateur : $prenom $nom ($uid)\n".
-               "mail : $forlife@polytechnique.org\n\n".
+        $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
+               'mail : ' . $user->forlifeEmail() . "\n\n" .
                "paiement : $conf_title ($conf_mail)\n".
-               "reference : $no_transaction\n".
+               "reference : $champ200\n".
                "montant : $montant\n\n".
                "dump de REQUEST:\n".
                var_export($_REQUEST,true);
@@ -379,58 +365,24 @@ class PaymentModule extends PLModule
         $tit = $res->fetchAllAssoc();
         $page->assign('titres', $tit);
 
-        $order = Env::v('order', 'timestamp');
-        $orders = array('timestamp', 'nom', 'promo', 'montant', 'comment');
-        if (!in_array($order, $orders)) {
-            $order = 'timestamp';
-        } elseif ($order == 'comment') {
-            $order = 't.comment';
-        }
-        $inv_order = Env::v('order_inv', 0);
-        $page->assign('order', $order);
-        $page->assign('order_inv', !$inv_order);
-
-        if ($order == 'timestamp') {
-            $inv_order = !$inv_order;
-        }
-
-        if ($inv_order) {
-            $inv_order = ' DESC';
-        } else {
-            $inv_order = '';
-        }
-        if ($order == 'montant') {
-            $order = 'LENGTH(montant) '.$inv_order.', montant';
-        }
-
-        $orderby = 'ORDER BY '.$order.$inv_order;
-        if ($order != 'nom') {
-            $orderby .= ', nom'; $inv_order = '';
-        }
-        $orderby .= ', prenom'.$inv_order;
-        if ($order != 'timestamp') {
-            $orderby .= ', timestamp DESC';
-        }
 
+        // TODO: replug sort.
         $trans = array();
         $event = array();
         foreach($tit as $foo) {
             $pid = $foo['id'];
             if (may_update()) {
-                $res = XDB::query("SELECT  IF(u.nom_usage<>'', u.nom_usage, u.nom) AS nom,
-                                           u.prenom, u.promo, a.alias, timestamp AS `date`, t.comment, montant
-                                     FROM  {$globals->money->mpay_tprefix}transactions AS t
-                               INNER JOIN  auth_user_md5  AS u ON ( t.uid = u.user_id )
-                               INNER JOIN  aliases        AS a ON ( t.uid = a.id AND a.type='a_vie' )
-                                    WHERE  ref = {?} ".$orderby, $pid);
-                    $trans[$pid] = $res->fetchAllAssoc();
-                    $sum = 0;
-                    foreach ($trans[$pid] as $i => $t) {
-                        $sum += strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.');
-                        $trans[$pid][$i]['montant'] = str_replace('EUR', '€', $t['montant']);
-                    }
-                    $trans[$pid][] = array('nom' => 'somme totale',
-                                           'montant' => strtr($sum, '.', ',').' €');
+                $res = XDB::query('SELECT  t.uid, timestamp AS `date`, t.comment, montant
+                                     FROM  ' . $globals->money->mpay_tprefix . 'transactions AS t
+                                    WHERE  t.ref = {?}', $pid);
+                $trans[$pid] = User::getBulkUsersWithUIDs($res->fetchAllAssoc(), 'uid', 'user');
+                $sum = 0;
+                foreach ($trans[$pid] as $i => $t) {
+                    $sum += strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.');
+                    $trans[$pid][$i]['montant'] = str_replace('EUR', '€', $t['montant']);
+                }
+                $trans[$pid][] = array('nom' => 'somme totale',
+                                       'montant' => strtr($sum, '.', ',').' €');
             }
             $res = XDB::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
                                    FROM groupex.evenements AS e