Merge commit 'origin/master' into account
[platal.git] / modules / payment.php
index f6aab57..edee6fe 100644 (file)
@@ -112,7 +112,7 @@ class PaymentModule extends PLModule
                 return PL_NOT_FOUND;
             }
             $res = XDB::query("SELECT asso_id
-                                 FROM #paiement#.paiements
+                                 FROM payments
                                 WHERE asso_id = {?} AND id = {?}",
                               $globals->asso('id'), $ref);
             if (!$res->numRows()) {
@@ -141,8 +141,8 @@ class PaymentModule extends PLModule
             $pay->init($val, $meth);
             $pay->prepareform($pay);
         } else {
-            $res = XDB::iterator("SELECT  timestamp, montant
-                                    FROM  #paiement#.transactions
+            $res = XDB::iterator("SELECT  timestamp, amount
+                                    FROM  payment_transactions
                                    WHERE  uid = {?} AND ref = {?}
                                 ORDER BY  timestamp DESC",
                                  S::v('uid', -1), $ref);
@@ -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
-                             FROM  #paiement#.paiements
+        $res = XDB::query("SELECT  mail, text, confirmation
+                             FROM  payments
                             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
-                                 FROM  #paiement#.codeRCB AS rcb
-                            LEFT JOIN  #paiement#.codeC   AS c ON rcb.codeC=c.id
-                                WHERE  rcb.id={?}", $champ906);
+            $res = XDB::query('SELECT  rcb.text, c.id, c.text
+                                 FROM  payment_codeRCB AS rcb
+                            LEFT JOIN  payment_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,13 +213,13 @@ 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  payment_transactions (id, uid, ref, fullref, amount, pkey, comment)
+                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
+                     $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env::v('comment'));
 
         // We check if it is an Xnet payment and then update the related ML.
         $res = XDB::query('SELECT  eid
-                             FROM  #groupex#.evenements
+                             FROM  group_events
                             WHERE  paiement_id = {?}', $ref);
         if ($eid = $res->fetchOneCell()) {
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
@@ -232,30 +228,27 @@ class PaymentModule extends PLModule
         }
 
         /* 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("<comment>", Env::v('comment'), $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>', 'comment>'),
+            array($user->firstName(), $user->lastName(), $user->promo(), $montant,
+                  $user->isFemale() ? 'Chère' : 'Cher', $user->isFemale() ? 'Chère' : 'Cher',
+                  Env::v('comment')), $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".
@@ -295,36 +288,32 @@ 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
-                             FROM  #paiement#.paiements
-                            WHERE  id={?}", $ref);
+        $res = XDB::query("SELECT  mail, text, confirmation
+                             FROM  payments
+                            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  payment_transactions (id, uid, ref, fullref, amount, pkey, comment)
+                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
+                    $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env::v('comment'));
 
         // We check if it is an Xnet payment and then update the related ML.
         $res = XDB::query('SELECT  eid
-                             FROM  #groupex#.evenements
+                             FROM  group_events
                             WHERE  paiement_id = {?}', $ref);
         if ($eid = $res->fetchOneCell()) {
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
@@ -333,31 +322,28 @@ class PaymentModule extends PLModule
         }
 
         /* 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);
@@ -378,8 +364,8 @@ class PaymentModule extends PLModule
                 return PL_FORBIDDEN;
             }
             $res = XDB::query("SELECT  1
-                                 FROM  #groupex#.evenements AS e
-                           INNER JOIN  #groupex#.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
+                                 FROM  group_events AS e
+                           INNER JOIN  group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
                                 WHERE  e.paiement_id = {?} AND e.asso_id = {?}",
                               S::i('uid'), $pid, $globals->asso('id'));
             if ($res->numRows() == 0) {
@@ -400,63 +386,29 @@ 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
-                              LEFT JOIN #groupex#.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
-                             INNER JOIN #groupex#.evenements_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id)
+                                   FROM group_events AS e
+                              LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
+                             INNER JOIN group_event_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id)
                                   WHERE e.paiement_id = {?}",
                                  S::v('uid'), $pid);
             $event[$pid] = array();
@@ -490,16 +442,16 @@ class PaymentModule extends PLModule
     function handler_admin(&$page, $action = 'list', $id = null) {
         $page->setTitle('Administration - Paiements');
         $page->assign('title', 'Gestion des télépaiements');
-        $table_editor = new PLTableEditor('admin/payments','#paiement#.paiements','id');
-        $table_editor->add_join_table('#paiement#.transactions','ref',true);
+        $table_editor = new PLTableEditor('admin/payments','payments','id');
+        $table_editor->add_join_table('payment_transactions','ref',true);
         $table_editor->add_sort_field('flags');
         $table_editor->add_sort_field('id', true, true);
-        $table_editor->on_delete("UPDATE #paiement#.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
+        $table_editor->on_delete("UPDATE payments SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
         $table_editor->describe('text','intitulé',true);
         $table_editor->describe('url','site web',false);
-        $table_editor->describe('montant_def','montant par défaut',false);
-        $table_editor->describe('montant_min','montant minimum',false);
-        $table_editor->describe('montant_max','montant maximum',false);
+        $table_editor->describe('amount_def','montant par défaut',false);
+        $table_editor->describe('amount_min','montant minimum',false);
+        $table_editor->describe('amount_max','montant maximum',false);
         $table_editor->describe('mail','email contact',true);
         $table_editor->describe('confirmation','message confirmation',false);
         $table_editor->apply($page, $action, $id);