<commit_before><?php
/*-------------------------------------------------------------+
| GP StreetImporter Record Handlers                            |
| Copyright (C) 2017 SYSTOPIA                                  |
| Author: <NAME> (endres -at- systopia.de)                  |
| http://www.systopia.de/                                      |
+--------------------------------------------------------------*/

define('REPETITION_FRAME_DECEASED',   "2 years");

/**
 * Processes PostRetour barcode lists (GP-331)
 *
 * @author <NAME> (SYSTOPIA) <<EMAIL>>
 * @license AGPL-3.0
 */
class CRM_Streetimport_GP_Handler_PostRetourRecordHandler extends CRM_Streetimport_GP_Handler_GPRecordHandler {

  /** file name / reference patterns as defined in GP-331 */
  protected static $FILENAME_PATTERN       = '#^RTS_(?P<category>[a-zA-Z\-]+)(_[0-9]*)?[.][a-zA-Z]+$#';
  protected static $REFERENCE_PATTERN_NEW  = '#^0?(?P<campaign_id>[0-9]{4})C(?P<contact_id>[0-9]{9})$#';
  protected static $REFERENCE_PATTERN_OLD  = '#^(?P<campaign_id>[0-9]{4})(?P<contact_id>[0-9]{6,9})$#';
  protected static $REFERENCE_PATTERN_1296 = '#^1(?P<campaign_id>[0-9]{5})(?P<contact_id>[0-9]{9})$#';

  /** stores the parsed file name */
  protected $file_name_data = 'not parsed';

  /**
   * Check if the given handler implementation can process the record
   *
   * @param $record  an array of key=>value pairs
   * @return true or false
   */
  public function canProcessRecord($record, $sourceURI) {
    if ($this->file_name_data === 'not parsed') {
      $this->file_name_data = $this->parseRetourFile($sourceURI);
    }
    return $this->file_name_data != NULL;
  }


  /**
   * process the given record
   *
   * @param $record  an array of key=>value pairs
   * @return true
   * @throws exception if failed
   */
  public function processRecord($record, $sourceURI) {
    $config          = CRM_Streetimport_Config::singleton();
    $category        = $this->getCategory();
    $reference       = $this->getReference($record);
    $campaign_id     = $this->getCampaignID($record);
    $contact_id      = $this->getContactID($record);

    if (!$campaign_id) {
      return $this->logger->logImport($record, FALSE, 'RTS', "Couldn't identify campaign for reference '{$reference}'");
    }

    if (!$contact_id) {
      return $this->logger->logImport($record, FALSE, 'RTS', "Couldn't identify contact for reference '{$reference}'");
    }

    $primary_address = $this->getPrimaryAddress($contact_id, $record);

    switch (strtolower($category)) {
      case 'unused':
      case 'incomplete':
      case 'badcode':
      case 'rejected':
      case 'notretrieved':
      case 'other':
      case 'unknown':
      case 'moved':
        $lastRTS = $this->findLastRTS($contact_id, $record);
        if ($lastRTS) {
          if (!$this->addressChangeRecordedSince($contact_id, $lastRTS['activity_date_time'], $record)) {
            $this->increaseRTSCounter($primary_address, $record);
          } else {
            // address had been changed
            // TODO: reset counter?
          }
        } else {
          $this->increaseRTSCounter($primary_address, $record);
        }
        $this->addRTSActvity($contact_id, $category, $record);
        break;

      case 'deceased':
        $lastDeceased = $this->findLastRTS($contact_id, $record, REPETITION_FRAME_DECEASED, 'deceased');
        if ($lastDeceased) {
          // there is another 'deceased' event in the last two years
          $this->disableContact($contact_id, 'deceased', $record);
        } else {
          $this->increaseRTSCounter($primary_address, $record);
        }
        $this->addRTSActvity($contact_id, $category, $record);
        break;
<commit_msg>implemented GP-1593 (increase RTS counter when dead)<commit_after><?php
/*-------------------------------------------------------------+
| GP StreetImporter Record Handlers                            |
| Copyright (C) 2017 SYSTOPIA                                  |
| Author: <NAME> (endres -at- systopia.de)                  |
| http://www.systopia.de/                                      |
+--------------------------------------------------------------*/

define('REPETITION_FRAME_DECEASED',   "2 years");

/**
 * Processes PostRetour barcode lists (GP-331)
 *
 * @author <NAME> (SYSTOPIA) <<EMAIL>>
 * @license AGPL-3.0
 */
class CRM_Streetimport_GP_Handler_PostRetourRecordHandler extends CRM_Streetimport_GP_Handler_GPRecordHandler {

  /** file name / reference patterns as defined in GP-331 */
  protected static $FILENAME_PATTERN       = '#^RTS_(?P<category>[a-zA-Z\-]+)(_[0-9]*)?[.][a-zA-Z]+$#';
  protected static $REFERENCE_PATTERN_NEW  = '#^0?(?P<campaign_id>[0-9]{4})C(?P<contact_id>[0-9]{9})$#';
  protected static $REFERENCE_PATTERN_OLD  = '#^(?P<campaign_id>[0-9]{4})(?P<contact_id>[0-9]{6,9})$#';
  protected static $REFERENCE_PATTERN_1296 = '#^1(?P<campaign_id>[0-9]{5})(?P<contact_id>[0-9]{9})$#';

  /** stores the parsed file name */
  protected $file_name_data = 'not parsed';

  /**
   * Check if the given handler implementation can process the record
   *
   * @param $record  an array of key=>value pairs
   * @return true or false
   */
  public function canProcessRecord($record, $sourceURI) {
    if ($this->file_name_data === 'not parsed') {
      $this->file_name_data = $this->parseRetourFile($sourceURI);
    }
    return $this->file_name_data != NULL;
  }


  /**
   * process the given record
   *
   * @param $record  an array of key=>value pairs
   * @return true
   * @throws exception if failed
   */
  public function processRecord($record, $sourceURI) {
    $config          = CRM_Streetimport_Config::singleton();
    $category        = $this->getCategory();
    $reference       = $this->getReference($record);
    $campaign_id     = $this->getCampaignID($record);
    $contact_id      = $this->getContactID($record);

    if (!$campaign_id) {
      return $this->logger->logImport($record, FALSE, 'RTS', "Couldn't identify campaign for reference '{$reference}'");
    }

    if (!$contact_id) {
      return $this->logger->logImport($record, FALSE, 'RTS', "Couldn't identify contact for reference '{$reference}'");
    }

    $primary_address = $this->getPrimaryAddress($contact_id, $record);

    switch (strtolower($category)) {
      case 'unused':
      case 'incomplete':
      case 'badcode':
      case 'rejected':
      case 'notretrieved':
      case 'other':
      case 'unknown':
      case 'moved':
        $lastRTS = $this->findLastRTS($contact_id, $record);
        if ($lastRTS) {
          if (!$this->addressChangeRecordedSince($contact_id, $lastRTS['activity_date_time'], $record)) {
            $this->increaseRTSCounter($primary_address, $record);
          } else {
            // address had been changed
            // TODO: reset counter?
          }
        } else {
          $this->increaseRTSCounter($primary_address, $record);
        }
        $this->addRTSActvity($contact_id, $category, $record);
        break;

      case 'deceased':
        $lastDeceased = $this->findLastRTS($contact_id, $record, REPETITION_FRAME_DECEASED, 'deceased');
        if ($lastDeceased) {
          // there is another 'deceased' event in the last two years
          $this->disableContact($contact_id, 'deceased', $record);

          // should still increase RTS counter (see GP-1593)
          $this->increaseRTSCounter($primary_address, $record);
        } else {
          $this->increaseRTSCounter($primary_address, $record);
        }
        $this->addRTSActvity($contact_id, $category, $record);
        break;

{'fraction_non_alphanumeric': 0.1815668202764977, 'fraction_numerical': 0.011586570111915735, 'mean_word_length': 2.5182955071792494, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3310915', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2325, 'n_words': 477}