<commit_before><?php
/*
 * This file is part of the OpenCmcicAction package.
 * (c) 2011  <NAME> <<EMAIL>>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Cmcic action class
 *
 * @package    OpenCmcicAction
 * @author     <NAME> <<EMAIL>>
 * @version    0.2
 */
class Cmcic
{
  const URL_BASE = 'https://paiement.creditmutuel.fr/bo'; // url de base de l'application
  const SLEEP_CALL = 5; // Nombre de seconde entre chaque appel
  
  static private $browser = null;
  static private $options = array();
  static private $class_name = null;
  static private $adapter = null;
  
  static private $tpe = null;
  
  static private $errors = array();
  
  static private $last_call = 0;
  
  /**
   * Constructeur : login à l'application
   *
   * @param   string  $login      Le login de l'administration du TPE
   * @param   string  $password   Le <PASSWORD> de passe de l'administration du TPE
   * @param   string  $tpe        L'identifiant du tpe virtuel
   * @access  public
   */
  public function __construct($login, $password, $tpe)
  {
    self::$browser = null;
    self::$errors = array();
    
    if ($this->login($login, $password) === false) {
      throw new Exception('unable to login');
    }
    
    if (is_string($tpe) === false) {
      throw new Exception('tpe must be a string');
    }
    
    self::$tpe = (string)$tpe;
  }
  
  
  /**
   * Annule un paiement
   *
   * @param   string  $reference  La référence du paiement à annuler
   * @return  bool                True en cas de succes, False sinon
   * @access  public
   */
  public function cancelPayment($reference)
  {
    if (is_string($reference) === false) {
      throw new Exception('reference must be a string');
    }
    
    self::cleanCookies();
    self::checkSleepCall();
    
    self::getBrowser()->get(self::URL_BASE.'/liste_paiements.cgi?TPE='.self::$tpe.'&type=PR&ref='.$reference);
    if (self::checkValid() === false) {
      return false;
    }
    
    $dom = self::getBrowser()->getResponseDom();
    $xpath = new DomXpath($dom);
    
    if ($radio = $xpath->query('//input[@type="radio" and @value="S"]')->item(0)) {
      $name = $radio->getAttribute('name');
    } else {
      return false;
    }
    
    self::cleanCookies();
    self::checkSleepCall();
    
    self::getBrowser()->setField($name, 'S')->click('Exécuter', array('' => 'Ex&eacute;cuter'));
    if (self::checkValid() === false) {
      return false;
    }
    
    return true;
  }
  
  
  /**
   * Récupère la liste des paiements récurrent durant une période
   *
   * @param   DateTime  $begin  La date de début de la période
   * @param   DateTime  $end    La date de fin de la période<commit_msg>Meilleure prise en compte des annulations<commit_after><?php
/*
 * This file is part of the OpenCmcicAction package.
 * (c) 2011  <NAME> <<EMAIL>>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Cmcic action class
 *
 * @package    OpenCmcicAction
 * @author     <NAME> <<EMAIL>>
 * @version    0.2
 */
class Cmcic
{
  const URL_BASE = 'https://paiement.creditmutuel.fr/bo'; // url de base de l'application
  const SLEEP_CALL = 5; // Nombre de seconde entre chaque appel
  
  static private $browser = null;
  static private $options = array();
  static private $class_name = null;
  static private $adapter = null;
  
  static private $tpe = null;
  
  static private $errors = array();
  
  static private $last_call = 0;
  
  /**
   * Constructeur : login à l'application
   *
   * @param   string  $login      Le login de l'administration du TPE
   * @param   string  $password   Le mot de passe de l'administration du TPE
   * @param   string  $tpe        L'identifiant du tpe virtuel
   * @access  public
   */
  public function __construct($login, $password, $tpe)
  {
    self::$browser = null;
    self::$errors = array();
    
    if ($this->login($login, $password) === false) {
      throw new Exception('unable to login');
    }
    
    if (is_string($tpe) === false) {
      throw new Exception('tpe must be a string');
    }
    
    self::$tpe = (string)$tpe;
  }
  
  
  /**
   * Annule un paiement
   *
   * @param   string  $reference  La référence du paiement à annuler
   * @return  bool                True en cas de succes, False sinon
   * @access  public
   */
  public function cancelPayment($reference)
  {
    if (is_string($reference) === false) {
      throw new Exception('reference must be a string');
    }
    
    self::cleanCookies();
    self::checkSleepCall();
    
    self::getBrowser()->get(self::URL_BASE.'/liste_paiements.cgi?TPE='.self::$tpe.'&type=PR&ref='.$reference);
    if (self::checkValid() === false) {
      return false;
    }
    
    $dom = self::getBrowser()->getResponseDom();
    $xpath = new DomXpath($dom);
    
    if ($radio = $xpath->query('//input[@type="radio" and @value="S"]')->item(0)) {
      $field_value = 'S';
      $name = $radio->getAttribute('name');
    } elseif ($radio = $xpath->query('//input[@type="radio" and @value="A"]')->item(0)) {
      $field_value = 'A';
      $name = $radio->getAttribute('name');
    } else {
      return false;
    }
    
    self::cleanCookies();
    self::checkSleepCall();
    
    self::getBrowser()->setField($name, $field_value)->click('Exécuter', array('' => 'Ex&eacute;cuter'));
    if (self::checkValid() === false) {
      return false;
    }
    
    return true;
  }
  
  
  /**
   * Récupère la liste des paiements récurrent durant une période
   *
   * @param   DateTime  $begin  La date de début de la période
   * @param   DateTime  $end    La date de fin de la période
{'fraction_non_alphanumeric': 0.14260929909784872, 'fraction_numerical': 0.003296321998612075, 'mean_word_length': 2.705012853470437, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1832522', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1897, 'n_words': 521}