2006 => 2007 Happy New Year\!
[platal.git] / modules / payment.php
index dc8791e..e2f22eb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -21,7 +21,7 @@
 
 /* sort en affichant une erreur */
 function cb_erreur($text) {
-    $mymail = new HermesMailer();
+    $mymail = new PlMailer();
     $mymail->addTo("telepaiement@polytechnique.org");
     $mymail->setFrom("webmaster@polytechnique.org");
     $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)");
@@ -37,7 +37,7 @@ function paypal_erreur($text, $send=true) {
     $erreur = $text;
     if (!$send) return;
 
-    $mymail = new HermesMailer();
+    $mymail = new PlMailer();
     $mymail->addTo("telepaiement@polytechnique.org");
     $mymail->setFrom("webmaster@polytechnique.org");
     $mymail->setSubject("erreur lors d'un télépaiement (PayPal)");
@@ -77,8 +77,14 @@ class PaymentModule extends PLModule
     {
         return array(
             'payment'               => $this->make_hook('payment', AUTH_MDP),
-            'payment/cyber_return'  => $this->make_hook('cyber_return',  AUTH_PUB),
-            'payment/paypal_return' => $this->make_hook('paypal_return',  AUTH_PUB),
+            'payment/cyber_return'  => $this->make_hook('cyber_return',  AUTH_PUBLIC),
+            'payment/paypal_return' => $this->make_hook('paypal_return',  AUTH_PUBLIC),
+            '%grp/paiement'              => $this->make_hook('xnet_payment', AUTH_MDP),
+            '%grp/payment'               => $this->make_hook('xnet_payment', AUTH_MDP),
+            '%grp/payment/cyber_return'  => $this->make_hook('cyber_return', AUTH_PUBLIC),
+            '%grp/payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC),
+            'admin/payments'        => $this->make_hook('admin', AUTH_MDP, 'admin'),
+        
         );
     }
 
@@ -87,21 +93,35 @@ class PaymentModule extends PLModule
         global $globals;
 
         require_once 'profil.func.inc.php' ;
-        require_once 'money.inc.php' ;
+        require_once dirname(__FILE__).'/payment/money.inc.php' ;
 
-        $page->changeTpl('payment/index.tpl');
-        $page->assign('xorg_title','Polytechnique.org - Télépaiements');
+        if (!empty($GLOBALS['IS_XNET_SITE'])) {
+            if (!$globals->asso('id')) {
+                return PL_NOT_FOUND;
+            }
+            $res = XDB::query("SELECT asso_id
+                                 FROM paiement.paiements
+                                WHERE asso_id = {?} AND id = {?}",
+                              $globals->asso('id'), $ref);
+            if (!$res->numRows()) {
+                return PL_FORBIDDEN;
+            }
+            new_group_page('payment/index.tpl');
+        } else {
+            $page->changeTpl('payment/index.tpl');
+            $page->assign('xorg_title','Polytechnique.org - Télépaiements');
+        }
 
         // initialisation
-        $op   = Env::get('op', 'select');
-        $meth = new PayMethod(Env::getInt('methode', -1));
+        $op   = Env::v('op', 'select');
+        $meth = new PayMethod(Env::i('methode', -1));
         $pay  = new Payment($ref);
 
         if($pay->flags->hasflag('old')){
             $page->trig("La transaction selectionnée est périmée.");
             $pay = new Payment();
         }
-        $val = Env::get('montant') != 0 ? Env::get('montant') : $pay->montant_def;
+        $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->montant_def;
 
         if (($e = $pay->check($val)) !== true) {
             $page->trig($e);
@@ -112,28 +132,27 @@ class PaymentModule extends PLModule
             $pay->prepareform($pay);
         } else {
             $res = XDB::iterator("SELECT  timestamp, montant
-                                              FROM  paiement.transactions
-                                             WHERE  uid = {?} AND ref = {?}
-                                          ORDER BY  timestamp DESC",
-                                            Session::getInt('uid', -1), $ref);
+                                    FROM  paiement.transactions
+                                   WHERE  uid = {?} AND ref = {?}
+                                ORDER BY  timestamp DESC",
+                                 S::v('uid', -1), $ref);
 
             if ($res->total()) $page->assign('transactions', $res);
         }
 
         $val = floor($val).".".substr(floor(($val - floor($val))*100+100),1);
         $page->assign('montant',$val);
+        $page->assign('comment',Env::v('comment'));
 
         $page->assign('meth', $meth);
         $page->assign('pay',  $pay);
         $page->assign('evtlink', $pay->event());
 
-        $page->assign('prefix',$globals->money->mpay_tprefix);
+        $page->assign('prefix', $globals->money->mpay_tprefix);
     }
 
     function handler_cyber_return(&$page, $uid = null)
     {
-        require_once 'diogenes/diogenes.hermes.inc.php';
-
         /* reference banque (numero de transaction) */
         $champ901 = clean_request('CHAMP901');
         /* cle d'acceptation */
@@ -167,8 +186,9 @@ class PaymentModule extends PLModule
         }
 
         echo ($ref = $matches[1]);
-        $res = XDB::query("SELECT mail,text,confirmation
-                                       FROM paiement.paiements WHERE id={?}", $ref);
+        $res = XDB::query("SELECT  mail,text,confirmation
+                             FROM  paiement.paiements
+                            WHERE  id={?}", $ref);
         if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
             cb_erreur("référence de commande inconnue");
         }
@@ -176,9 +196,9 @@ class PaymentModule extends PLModule
         /* 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'");
+                                 FROM  paiement.codeRCB AS rcb
+                            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{ 
@@ -187,9 +207,9 @@ class PaymentModule extends PLModule
         }
 
         /* on fait l'insertion en base de donnees */
-        XDB::execute("INSERT INTO  paiement.transactions (id,uid,ref,fullref,montant,cle)
-                                     VALUES  ({?},{?},{?},{?},{?},{?})",
-                                $champ901, $uid, $ref, $champ200, $montant, $champ905);
+        XDB::execute("INSERT INTO  paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
+                           VALUES  ({?},{?},{?},{?},{?},{?},{?})",
+                     $champ901, $uid, $ref, $champ200, $montant, $champ905,Env::v('comment'));
 
         /* on genere le mail de confirmation */
         $conf_text = str_replace("<prenom>",$prenom,$conf_text);
@@ -199,7 +219,7 @@ class PaymentModule extends PLModule
         $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
         $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
 
-        $mymail = new HermesMailer();
+        $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
         $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
         $mymail->addCc($conf_mail);
@@ -208,9 +228,9 @@ class PaymentModule extends PLModule
         $mymail->send();
 
         /* on envoie les details de la transaction à telepaiement@ */
-        $mymail = new HermesMailer();
+        $mymail = new PlMailer();
         $mymail->setFrom("webmaster@polytechnique.org");
-        $mymail->addTo("telepaiement@polytechnique.org");
+        $mymail->addTo("telepaiement@staff.polytechnique.org");
         $mymail->setSubject($conf_title);
         $msg = "utilisateur : $prenom $nom ($uid)\n".
                "mail : $forlife@polytechnique.org\n\n".
@@ -226,10 +246,11 @@ class PaymentModule extends PLModule
 
     function handler_paypal_return(&$page, $uid = null)
     {
-        global $globals;
-
-        $page->changeTpl('payment/retour_paypal.tpl');
-        require_once 'diogenes/diogenes.hermes.inc.php';
+        if (!empty($GLOBALS['IS_XNET_SITE'])) {
+            new_group_open_page('payment/retour_paypal.tpl');
+        } else {
+            $page->changeTpl('payment/retour_paypal.tpl');
+        }
 
         /* reference banque (numero de transaction) */
         $no_transaction = clean_request('tx');
@@ -272,15 +293,16 @@ class PaymentModule extends PLModule
 
         $ref = $matches[1];
         $res = XDB::query("SELECT  mail,text,confirmation
-                                       FROM  paiement.paiements WHERE id={?}", $ref);
+                             FROM  paiement.paiements
+                            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)
-                                     VALUES  ({?},{?},{?},{?},{?},{?})",
-                                $no_transaction, $uid, $ref, $fullref, $montant, $clef);
+        XDB::execute("INSERT INTO  paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
+                           VALUES  ({?},{?},{?},{?},{?},{?},{?})",
+                    $no_transaction, $uid, $ref, $fullref, $montant, $clef, Env::v('comment'));
 
         /* on genere le mail de confirmation */
         $conf_text = str_replace("<prenom>",$prenom,$conf_text);
@@ -290,7 +312,7 @@ class PaymentModule extends PLModule
         $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
         $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
 
-        $mymail = new HermesMailer();
+        $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
         $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
         $mymail->addCc($conf_mail);
@@ -299,7 +321,7 @@ class PaymentModule extends PLModule
         $mymail->send();
 
         /* on envoie les details de la transaction à telepaiement@ */
-        $mymail = new HermesMailer();
+        $mymail = new PlMailer();
         $mymail->setFrom("webmaster@polytechnique.org");
         $mymail->addTo("telepaiement@polytechnique.org");
         $mymail->setSubject($conf_title);
@@ -316,6 +338,127 @@ class PaymentModule extends PLModule
         $page->assign('texte', $conf_text);
         $page->assign('erreur', $erreur);
     }
+
+    function handler_xnet_payment(&$page, $pid = null)
+    {
+        global $globals;
+        
+        if (!is_null($pid)) {
+            return  $this->handler_payment($page, $pid);
+        }
+        new_group_page('payment/xnet.tpl');
+        
+        $res = XDB::query(
+                "SELECT  id, text, url
+                   FROM  {$globals->money->mpay_tprefix}paiements
+                  WHERE  asso_id = {?} AND NOT FIND_IN_SET(flags, 'old')
+               ORDER BY  id DESC", $globals->asso('id'));
+        $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';
+        }
+
+        $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::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)
+                                  WHERE e.paiement_id = {?}",
+                                 S::v('uid'), $pid);
+            $event[$pid] = array();
+            $event[$pid]['paid'] = 0;
+            if ($res->total()) {
+                $event[$pid]['topay'] = 0;
+                while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) {
+                    $event[$pid]['topay']     += ($nb * $montant);
+                    $event[$pid]['eid']       = $eid;
+                    $event[$pid]['shortname'] = $shortname;
+                    $event[$pid]['title']     = $title;
+                    $event[$pid]['ins']       = !is_null($nb);
+                    $event[$pid]['paid']      = $paid;
+                }
+            }
+            $res = XDB::query("SELECT montant
+                                 FROM {$globals->money->mpay_tprefix}transactions AS t
+                                WHERE ref = {?} AND uid = {?}", $pid, S::v('uid'));
+            $montants = $res->fetchColumn();
+
+            foreach ($montants as $m) {
+                $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
+                $event[$pid]['paid'] += trim($p);
+            }
+        }
+        $page->assign('trans', $trans);
+        $page->assign('event', $event);
+    }
+    
+    function handler_admin(&$page, $action = 'list', $id = null) {
+        $page->assign('xorg_title','Polytechnique.org - 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->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->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('mail','email contact',true);
+        $table_editor->describe('confirmation','message confirmation',false);
+        $table_editor->apply($page, $action, $id);
+    }  
 }
 
 ?>