Happy New Year!
[platal.git] / modules / payment.php
index ef724db..c5477e7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -87,15 +87,15 @@ function comment_decode($comment) {
 /* check if a RIB account number is valid */
 function check_rib($rib)
 {
-       if(strlen($rib) != 23) return false;
-       
+    if(strlen($rib) != 23) return false;
+    
     // extract fields
     $rib = strtr(strtoupper($rib),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','12345678912345678923456789');
-       $bank    = substr($rib,0,5);
-       $counter = substr($rib,5,5);
-       $account = substr($rib,10,11);
-       $key     = substr($rib,21,2);
-       
+    $bank    = substr($rib,0,5);
+    $counter = substr($rib,5,5);
+    $account = substr($rib,10,11);
+    $key     = substr($rib,21,2);
+    
     // check
     return 0 == fmod(89 * $bank + 15 * $counter + 3 * $account + $key, 97);
 }
@@ -180,6 +180,7 @@ class PaymentModule extends PLModule
         $page->assign('meth', $meth);
         $page->assign('pay',  $pay);
         $page->assign('evtlink', $pay->event());
+        $page->assign('sex', S::user()->isFemale());
     }
 
     function handler_cyber_return(&$page, $uid = null)
@@ -250,7 +251,7 @@ class PaymentModule extends PLModule
 
         /* on genere le mail de confirmation */
         $conf_text = str_replace(
-            array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', 'comment>'),
+            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);
@@ -296,7 +297,7 @@ class PaymentModule extends PLModule
         //}
 
         /* on extrait les informations sur l'utilisateur */
-        $user = User::get(Env::v('vads_cust_id'));
+        $user = User::get(Env::i('vads_cust_id'));
         if (!$user) {
             cb_erreur("uid invalide");
         }
@@ -310,15 +311,17 @@ class PaymentModule extends PLModule
         $res = XDB::query("SELECT  mail, text, confirmation
                              FROM  payments
                             WHERE  id={?}", $ref);
-        if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) {
+        if ($res->numRows() != 1) {
             cb_erreur("référence de commande inconnue");
         }
+        list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow();
 
         /* on extrait le montant */
         if (Env::v('vads_currency') != "978") {
             cb_erreur("monnaie autre que l'euro");
         }
-        $montant = sprintf("%.02f", ((float)Env::v('vads_amount'))/100) . " EUR";
+        $amount = ((float)Env::i('vads_amount')) / 100;
+        $montant = sprintf("%.02f EUR", $amount);
 
         /* on extrait le code de retour */
         if (Env::v('vads_result') != "00") {
@@ -332,18 +335,19 @@ class PaymentModule extends PLModule
         echo "Paiement stored.\n";
 
         // We check if it is an Xnet payment and then update the related ML.
-        $res = XDB::query('SELECT  eid
+        $res = XDB::query('SELECT  eid, asso_id
                              FROM  group_events
                             WHERE  paiement_id = {?}', $ref);
-        if ($eid = $res->fetchOneCell()) {
+        if ($res->numRows() == 1) {
+            list($eid, $asso_id) = $res->fetchOneRow();
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
-            $evt = get_event_detail($eid);
-            subscribe_lists_event($user->id(), $evt, 1, $montant, true);
+            $evt = get_event_detail($eid, false, $asso_id);
+            subscribe_lists_event($user->id(), $evt, 1, $amount, true);
         }
 
         /* on genere le mail de confirmation */
         $conf_text = str_replace(
-            array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', 'comment>'),
+            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);
@@ -480,7 +484,7 @@ class PaymentModule extends PLModule
             }
             $res = XDB::query("SELECT  1
                                  FROM  group_events AS e
-                           INNER JOIN  group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
+                           INNER JOIN  group_event_participants AS ep ON (ep.eid = e.eid AND e.uid = {?})
                                 WHERE  e.paiement_id = {?} AND e.asso_id = {?}",
                               S::i('uid'), $pid, $globals->asso('id'));
             if ($res->numRows() == 0) {
@@ -522,7 +526,7 @@ class PaymentModule extends PLModule
             }
             $res = XDB::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
                                    FROM group_events AS e
-                              LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
+                              LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND ep.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);
@@ -605,7 +609,7 @@ class PaymentModule extends PLModule
         // check RIB key
         if ($action == "update" && Post::has("account") && !check_rib(Post::v("account"))) {
             $page->trigError("Le RIB n'est pas valide");
-               $table_editor->apply($page, "edit", $id);
+            $table_editor->apply($page, "edit", $id);
             return;
         }
         
@@ -626,68 +630,68 @@ class PaymentModule extends PLModule
         $page->setTitle('Administration - Paiements - Réconciliations');
         $page->changeTpl('payment/reconcile.tpl');
         $page->assign('step', $step);
-               $list = true;
+        $list = true;
+        
+        // actions
+        if ($step == 'delete' && $param != null) {
+            S::assert_xsrf_token();
+            XDB::execute("DELETE FROM payment_reconcilations WHERE id={?}", $param);
+            // FIXME: hardcoding !!!
+            XDB::execute("UPDATE payment_transactions SET recon_id=NULL,commission=NULL WHERE recon_id={?} AND method_id=2", $param);
+            XDB::execute("UPDATE payment_transactions SET recon_id=NULL WHERE recon_id={?} AND method_id=1", $param);
+            $page->trigSuccess("L'entrée ".$param." a été supprimée.");
         
-               // actions
-               if ($step == 'delete' && $param != null) {
-                       S::assert_xsrf_token();
-                       XDB::execute("DELETE FROM payment_reconcilations WHERE id={?}", $param);
-                       // FIXME: hardcoding !!!
-                       XDB::execute("UPDATE payment_transactions SET recon_id=NULL,commission=NULL WHERE recon_id={?} AND method_id=2", $param);
-                       XDB::execute("UPDATE payment_transactions SET recon_id=NULL WHERE recon_id={?} AND method_id=1", $param);
-                       $page->trigSuccess("L'entrée ".$param." a été supprimée.");
-               
-               } elseif ($step == 'edit') {
-                       $page->trigError("L'édition n'est pas implémentée.");
-                       
-               } elseif ($step == 'step5') {
-                       $page->trigSuccess("La réconciliation est terminée. Il est maintenant nécessaire de générer les virements.");
+        } elseif ($step == 'edit') {
+            $page->trigError("L'édition n'est pas implémentée.");
+            
+        } elseif ($step == 'step5') {
+            $page->trigSuccess("La réconciliation est terminée. Il est maintenant nécessaire de générer les virements.");
+        
+        }
         
-               }
-               
-               if($list) {
-                       // show list of reconciliations, with a "add" button
-                       $page->assign('title', "Réconciliation - Liste");
+        if($list) {
+            // show list of reconciliations, with a "add" button
+            $page->assign('title', "Réconciliation - Liste");
             $page->assign('step', 'list');
-                       
-                       $recongps = array();
-       
-                       $res = XDB::query("SELECT  r.id, short_name AS method, period_start, period_end, status,
-                                                                          payment_count, sum_amounts, sum_commissions
-                                                                FROM  payment_reconcilations AS r
-                                                       LEFT JOIN  payment_methods AS m ON r.method_id=m.id
-                                                               WHERE  recongroup_id IS NULL
-                                                        ORDER BY  period_end DESC, period_start DESC");
-                       foreach ($res->fetchAllAssoc() as $recon)
-                               $recongps[] = array('recons' => array($recon), 'transfers' => array());
-                       
-                       $res = XDB::query("SELECT  recongroup_id AS id
-                                                                FROM  payment_reconcilations
-                                                        GROUP BY  recongroup_id
-                                                        ORDER BY  MAX(period_end) DESC, MIN(period_start) DESC");
-                       foreach ($res->fetchAllAssoc() as $recongp) {
-                               $res = XDB::query("SELECT  r.id, short_name AS method, period_start, period_end, status,
-                                                                                  payment_count, sum_amounts, sum_commissions
-                                                                        FROM  payment_reconcilations AS r
-                                                               LEFT JOIN  payment_methods AS m ON r.method_id=m.id
-                                                                       WHERE  recongroup_id={?}
-                                                                ORDER BY  period_end DESC, period_start DESC",
-                                                                 $recongp['id']);
-                               $recongp['recons'] = $res->fetchAllAssoc();
-
-                               $res = XDB::query("SELECT  id, payment_id, amount, account_id, message, date
-                                                                        FROM  payment_transfers
-                                                                       WHERE  recongroup_id={?}",
-                                                                 $recongp['id']);
-                               $recongp['transfers'] = $res->fetchAllAssoc();
-                                               
-                               $recongps[] = $recongp;
-                       }
-                       $page->assign_by_ref('recongps', $recongps);
-               }
+            
+            $recongps = array();
+    
+            $res = XDB::query("SELECT  r.id, short_name AS method, period_start, period_end, status,
+                                       payment_count, sum_amounts, sum_commissions
+                                 FROM  payment_reconcilations AS r
+                            LEFT JOIN  payment_methods AS m ON r.method_id=m.id
+                                WHERE  recongroup_id IS NULL
+                             ORDER BY  period_end DESC, period_start DESC");
+            foreach ($res->fetchAllAssoc() as $recon)
+                $recongps[] = array('recons' => array($recon), 'transfers' => array());
+            
+            $res = XDB::query("SELECT  recongroup_id AS id
+                                 FROM  payment_reconcilations
+                             GROUP BY  recongroup_id
+                             ORDER BY  MAX(period_end) DESC, MIN(period_start) DESC");
+            foreach ($res->fetchAllAssoc() as $recongp) {
+                $res = XDB::query("SELECT  r.id, short_name AS method, period_start, period_end, status,
+                                           payment_count, sum_amounts, sum_commissions
+                                     FROM  payment_reconcilations AS r
+                                LEFT JOIN  payment_methods AS m ON r.method_id=m.id
+                                    WHERE  recongroup_id={?}
+                                 ORDER BY  period_end DESC, period_start DESC",
+                                  $recongp['id']);
+                $recongp['recons'] = $res->fetchAllAssoc();
+
+                $res = XDB::query("SELECT  id, payment_id, amount, account_id, message, date
+                                     FROM  payment_transfers
+                                    WHERE  recongroup_id={?}",
+                                  $recongp['id']);
+                $recongp['transfers'] = $res->fetchAllAssoc();
+                        
+                $recongps[] = $recongp;
+            }
+            $page->assign_by_ref('recongps', $recongps);
+        }
     }
     
-       function handler_adm_importlogs(&$page, $step, $param = null) {
+    function handler_adm_importlogs(&$page, $step, $param = null) {
         $page->setTitle('Administration - Paiements - Réconciliations');
         $page->changeTpl('payment/reconcile.tpl');
         $page->assign('step', $step);
@@ -813,7 +817,7 @@ class PaymentModule extends PLModule
             } elseif (Post::has('savecomments')) {
                 S::assert_xsrf_token();
                 $recon['comments'] = Post::v('comments');
-                   $page->assign('recon', $recon);
+                $page->assign('recon', $recon);
                 XDB::execute("UPDATE payment_reconcilations SET comments={?} WHERE id={?}", $recon['comments'], $_SESSION['paymentrecon_id']);
                 $page->trigSuccess('Les commentaires ont été enregistrés.');
             }
@@ -856,102 +860,109 @@ class PaymentModule extends PLModule
             $page->assign_by_ref('only_database', $only_database);
             $page->assign('onlydb_count', count($only_database));
         }
-       }
-       
+    }
+    
     function handler_adm_transfers(&$page, $action = null, $id = null) {
         // list/log all bank transfers and link them to individual transactions
         
-               if (Post::has('generate')) {
-                       $recon_ids = array_keys(Post::v('recon_id'));
-                       
-                       // generate a new reconcilation group ID
-                       $res = XDB::query("SELECT MAX(recongroup_id)+1 FROM payment_reconcilations");
-                       $recongp_id = $res->fetchOneCell();
-                       if ($recongp_id == null) $recongp_id = 1;
-                       
-                       // add reconcilations to group
-                               // FIXME: should check if reconcilations are in good status
-                       XDB::execute("UPDATE payment_reconcilations SET recongroup_id={?}, status='closed'
-                                     WHERE id IN {?}",
-                                     $recongp_id, $recon_ids);
-                       
-                       // create transfers
-                       XDB::execute("INSERT INTO  payment_transfers
-                                                          SELECT  NULL, {?}, t.ref, SUM(t.amount+t.commission), NULL, p.text, NULL
-                                                                FROM  payment_transactions AS t
-                                                       LEFT JOIN  payments AS p ON t.ref = p.id
-                                                       LEFT JOIN  groups AS g ON p.asso_id = g.id
-                                                               WHERE  t.recon_id IN {?}
-                                                        GROUP BY  t.ref",
-                                                         $recongp_id, $recon_ids);
-                       
-                       //$res = XDB::query("SELECT * FROM  payment_reconcilations WHERE id IN {?}", $recon_ids);
-                       //$recons = $res->fetchAllAssoc();
-                       
-                       $page->trigSuccess("Les virements ont été générés pour ".count($recon_ids)." réconciliations.");
-                       $this->handler_adm_reconcile($page);
-                       
-               } elseif ($action == "delgroup") {
-                       S::assert_xsrf_token();
-                       XDB::execute("UPDATE payment_reconcilations SET status='transfering', recongroup_id=NULL WHERE recongroup_id={?}", $id);
-                       XDB::execute("DELETE FROM payment_transfers WHERE recongroup_id={?} AND date IS NULL", $id);
-                       
-                       $page->trigSuccess("Les virements non réalisés ont été supprimé du groupe ".$id.".");
-                       $this->handler_adm_reconcile($page);
-                   
-               } else {
-                       pl_redirect("admin/reconcile");
-               }
+        if (Post::has('generate')) {
+            $recon_ids = array_keys(Post::v('recon_id'));
+            
+            // generate a new reconcilation group ID
+            $res = XDB::query("SELECT MAX(recongroup_id)+1 FROM payment_reconcilations");
+            $recongp_id = $res->fetchOneCell();
+            if ($recongp_id == null) $recongp_id = 1;
+            
+            // add reconcilations to group
+                // FIXME: should check if reconcilations are in good status
+            XDB::execute("UPDATE payment_reconcilations SET recongroup_id={?}, status='closed'
+                          WHERE id IN {?}",
+                          $recongp_id, $recon_ids);
+            
+            // create transfers
+            XDB::execute("INSERT INTO  payment_transfers
+                               SELECT  NULL, {?}, t.ref, SUM(t.amount+t.commission), NULL, p.text, NULL
+                                 FROM  payment_transactions AS t
+                            LEFT JOIN  payments AS p ON t.ref = p.id
+                            LEFT JOIN  groups AS g ON p.asso_id = g.id
+                                WHERE  t.recon_id IN {?}
+                             GROUP BY  t.ref",
+                              $recongp_id, $recon_ids);
+            
+            //$res = XDB::query("SELECT * FROM  payment_reconcilations WHERE id IN {?}", $recon_ids);
+            //$recons = $res->fetchAllAssoc();
+            
+            $page->trigSuccess("Les virements ont été générés pour ".count($recon_ids)." réconciliations.");
+            $this->handler_adm_reconcile($page);
+            
+        } elseif ($action == "delgroup") {
+            S::assert_xsrf_token();
+            XDB::execute("UPDATE payment_reconcilations SET status='transfering', recongroup_id=NULL WHERE recongroup_id={?}", $id);
+            XDB::execute("DELETE FROM payment_transfers WHERE recongroup_id={?} AND date IS NULL", $id);
+            
+            $page->trigSuccess("Les virements non réalisés ont été supprimé du groupe ".$id.".");
+            $this->handler_adm_reconcile($page);
+            
+        } elseif ($action == "confirm") {
+            S::assert_xsrf_token();
+            XDB::execute("UPDATE payment_transfers SET date=NOW() WHERE id={?}", $id);
+            
+            $page->trigSuccess("Virement ".$id." confirmé.");
+            $this->handler_adm_reconcile($page);
+            
+        } else {
+            pl_redirect("admin/reconcile");
+        }
     }
 }
 
 class PaymentLogsImporter extends CSVImporter {
-       protected $result;
-       
-       public function __construct() {
-           parent::__construct('');
-           $this->registerFunction('systempay_commission', 'Compute BPLC commission', array($this,"compute_systempay_commission"));
-           $this->registerFunction('payment_id', 'Autocompute payment ID', array($this,"compute_payment_id"));
+    protected $result;
+    
+    public function __construct() {
+        parent::__construct('');
+        $this->registerFunction('systempay_commission', 'Compute BPLC commission', array($this,"compute_systempay_commission"));
+        $this->registerFunction('payment_id', 'Autocompute payment ID', array($this,"compute_payment_id"));
         //$this->forceValue('payment_id','func_payment_id');
-       }
-       
-       public function run($action = null, $insert_relation = null, $update_relation = null) {
-               $this->result = array();
-               foreach ($this->data as $line) {
+    }
+    
+    public function run($action = null, $insert_relation = null, $update_relation = null) {
+        $this->result = array();
+        foreach ($this->data as $line) {
             $a = $this->makeAssoc($line, $insert_relation);
             $a['date'] = preg_replace('/([0-9]{2})\/([0-9]{2})\/([0-9]{4}).*/','\3-\2-\1', $a['date']);
             $a['amount'] = str_replace(',','.',$a['amount']);
             $a['commission'] = str_replace(',','.',$a['commission']);
-                       $this->result[] = $a;
+            $this->result[] = $a;
         }
-       }
-       
-       public function get_result() {
-               return $this->result;
-       }
-       
-       static public function compute_systempay_commission($line, $key, $relation) {
-               static $EEE_countries = array('France','Allemagne','Autriche','Belgique','Bulgarie','Chypre',
-                       'Danemark','Espagne','Estonie','Finlande','Grèce','Hongrie','Irlande','Islande','Italie',
-                       'Lettonie','Liechtenstein','Lituanie','Luxembourg','Malte','Norvège','Pays-Bas','Pologne',
-                       'Portugal','Roumanie','Royaume-Uni','Slovaquie','Slovénie','Suède','République Tchèque');
-               
-               if($key!='commission' || !array_key_exists('carte',$line)) return null;
-               $amount = self::getValue($line, 'amount', $relation['amount']);
-               if (in_array($line['pays carte'],$EEE_countries))
-                   return -0.20 - round($amount*0.005, 2);
-               else
-                   return -0.20 - round($amount*0.005, 2) - 0.76;
-       }
-
-       static public function compute_payment_id($line, $key, $relation) {
-               if ($key != 'payment_id') return null;
-           $reference = self::getValue($line, 'reference', $relation['reference']);
+    }
+    
+    public function get_result() {
+        return $this->result;
+    }
+    
+    static public function compute_systempay_commission($line, $key, $relation) {
+        static $EEE_countries = array('France','Allemagne','Autriche','Belgique','Bulgarie','Chypre',
+            'Danemark','Espagne','Estonie','Finlande','Grèce','Hongrie','Irlande','Islande','Italie',
+            'Lettonie','Liechtenstein','Lituanie','Luxembourg','Malte','Norvège','Pays-Bas','Pologne',
+            'Portugal','Roumanie','Royaume-Uni','Slovaquie','Slovénie','Suède','République Tchèque');
+        
+        if($key!='commission' || !array_key_exists('carte',$line)) return null;
+        $amount = self::getValue($line, 'amount', $relation['amount']);
+        if (in_array($line['pays carte'],$EEE_countries))
+            return -0.20 - round($amount*0.005, 2);
+        else
+            return -0.20 - round($amount*0.005, 2) - 0.76;
+    }
+
+    static public function compute_payment_id($line, $key, $relation) {
+        if ($key != 'payment_id') return null;
+        $reference = self::getValue($line, 'reference', $relation['reference']);
         if (ereg('-([0-9]+)$', $reference, $matches))
-                   return $matches[1];
+            return $matches[1];
         else
             return null;
-       }
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: