<commit_before><?php

require_once 'spodoosync.civix.php';

/**
 * Implementation of hook_civicrm_odoo_object_definition
 * 
 */
function spodoosync_civicrm_odoo_object_definition(&$list) {
  if (spodoosync_paymentarrangement()) {
    $config = CRM_Paymentarrangement_Config::singleton();
    $table_name = $config->getPaymentArrangementGroup('table_name');
    $list[$table_name] = new CRM_PaymentArrangement_Definition();
  }
}

/** 
 * Implementation of hook_civicrm_custom
 * 
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_custom
 */
function spodoosync_civicrm_custom($op,$groupID, $entityID, &$params ) {
  if (spodoosync_paymentarrangement()) {  
    //check if the group if the Payment arrangement group
    $config = CRM_Paymentarrangement_Config::singleton();
    if ($groupID == $config->getPaymentArrangementGroup('id')) {
      //add the payment arrangement for syncing
      if ($op == 'delete') {
        //when deleting the params contains the id
        $objectId = $params;
      } else {
        //first find the id for this custom value pair
        $contributionParams = array();
        $contributionParams['id'] = $entityID;
        foreach($params as $param) {
          $contributionParams['custom_'.$param['custom_field_id']] = $param['value'];
          $contributionParams['return.custom_'.$param['custom_field_id']] = 1;
        }
        $contribution = civicrm_api3('Contribution', 'getsingle', $contributionParams);
        //extract the custom value table id
        $objectId = $contribution[$config->getPaymentArrangementGroup('table_name').'_id'];
      }

      $objects = CRM_Odoosync_Objectlist::singleton();
      $objects->post($op,$config->getPaymentArrangementGroup('table_name'), $objectId);
    }
    if ($groupID == $config->getContactPaymentArrangementGroup('id')) {
      $op = 'edit'; //if this custom field is deleted it doesn't mean that the contact is deleted.
      $objects = CRM_Odoosync_Objectlist::singleton();
      $objects->post($op,'civicrm_contact', $entityID);
    }
  }
}

/**
 * Implementation of hook_civicrm_odoo_alter
 * 
 * Through this hook we will make sure that every contact created in Odoo is a company (even if it is an individual)
 * 
 * @param xmlrpcval $parameters
 * @param type $entity
 * @param type $entity_id
 * @param type $action
 */
function spodoosync_civicrm_odoo_alter_parameters(&$parameters, $resource, $entity, $entity_id, $action) {
  if ($entity == 'civicrm_contribution' && $action != 'credit') {
    $parameters['civicrm_id'] = new xmlrpcval($entity_id, 'int');
    $parameters['payment_term'] = new xmlrpcval(0, 'int');
    $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $entity_id));
    if (!empty($contribution['instrument_id'])) {
      $payment_instrument_to_payment_term = CRM_Spodoosync_PaymentInstrumentToOdooPaymentTerm::singleton();
      $payment_term = $payment_instrument_to_payment_term->getOdooPaymentTerm($contribution['instrument_id']);
      if (!empty($payment_term)) {
        $parameters['payment_term'] = new xmlrpcval($payment_term, 'int');
      }
    }
  } elseif ($entity == 'civicrm_contribution' && $action == 'credit') {
    $parameters['payment_term'] = new xmlrpcval(18, 'int'); //verrekenen
    $parameters['civicrm_id'] = new xmlrpcval($entity_id, 'int');
    if ($entity_id) {
      $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $entity_id));
      $refund_option_value = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name');
      if ($contribution['contribution_status_id'] == $refund_option_value) {
        $parameters['payment_term'] = new xmlrpcval(17, 'int'); //terugbetalen
      }
    }
  }
  if ($entity == 'civicrm_contact') {
    //sync field retour post
    CRM_Spodoosync_RetourPost::syncRetourPostToOdoo($entity_id, $parameters);

    unset($parameters['title']);
    $contact = civicrm_api3('Contact', 'getsingle', array('id' => $entity_id));
    $parameters['civicrm_id'] = new xmlrpcval($contact['id'], 'int');
    if ($contact['contact_type'] == 'Individual') {
      $parameters['is_company'] = new xmlrpcval(true, 'boolean');
      $parameters['firstname'] = new xmlrpcval($contact['first_name'], 'string');
      $parameters['prename'] = new xmlrpcval($contact['middle_name'], 'string');
      $parameters['lastname'] = new xmlrpcval($contact['last_name'], 'string');
      <commit_msg>Added sync for fields for geen_post and reden_geen_post see SP issue #1345<commit_after><?php

require_once 'spodoosync.civix.php';

/**
 * Implementation of hook_civicrm_odoo_object_definition
 * 
 */
function spodoosync_civicrm_odoo_object_definition(&$list) {
  if (spodoosync_paymentarrangement()) {
    $config = CRM_Paymentarrangement_Config::singleton();
    $table_name = $config->getPaymentArrangementGroup('table_name');
    $list[$table_name] = new CRM_PaymentArrangement_Definition();
  }
}

/** 
 * Implementation of hook_civicrm_custom
 * 
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_custom
 */
function spodoosync_civicrm_custom($op,$groupID, $entityID, &$params ) {
  if (spodoosync_paymentarrangement()) {  
    //check if the group if the Payment arrangement group
    $config = CRM_Paymentarrangement_Config::singleton();
    if ($groupID == $config->getPaymentArrangementGroup('id')) {
      //add the payment arrangement for syncing
      if ($op == 'delete') {
        //when deleting the params contains the id
        $objectId = $params;
      } else {
        //first find the id for this custom value pair
        $contributionParams = array();
        $contributionParams['id'] = $entityID;
        foreach($params as $param) {
          $contributionParams['custom_'.$param['custom_field_id']] = $param['value'];
          $contributionParams['return.custom_'.$param['custom_field_id']] = 1;
        }
        $contribution = civicrm_api3('Contribution', 'getsingle', $contributionParams);
        //extract the custom value table id
        $objectId = $contribution[$config->getPaymentArrangementGroup('table_name').'_id'];
      }

      $objects = CRM_Odoosync_Objectlist::singleton();
      $objects->post($op,$config->getPaymentArrangementGroup('table_name'), $objectId);
    }
    if ($groupID == $config->getContactPaymentArrangementGroup('id')) {
      $op = 'edit'; //if this custom field is deleted it doesn't mean that the contact is deleted.
      $objects = CRM_Odoosync_Objectlist::singleton();
      $objects->post($op,'civicrm_contact', $entityID);
    }
  }
}

/**
 * Implementation of hook_civicrm_odoo_alter
 * 
 * Through this hook we will make sure that every contact created in Odoo is a company (even if it is an individual)
 * 
 * @param xmlrpcval $parameters
 * @param type $entity
 * @param type $entity_id
 * @param type $action
 */
function spodoosync_civicrm_odoo_alter_parameters(&$parameters, $resource, $entity, $entity_id, $action) {
  if ($entity == 'civicrm_contribution' && $action != 'credit') {
    $parameters['civicrm_id'] = new xmlrpcval($entity_id, 'int');
    $parameters['payment_term'] = new xmlrpcval(0, 'int');
    $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $entity_id));
    if (!empty($contribution['instrument_id'])) {
      $payment_instrument_to_payment_term = CRM_Spodoosync_PaymentInstrumentToOdooPaymentTerm::singleton();
      $payment_term = $payment_instrument_to_payment_term->getOdooPaymentTerm($contribution['instrument_id']);
      if (!empty($payment_term)) {
        $parameters['payment_term'] = new xmlrpcval($payment_term, 'int');
      }
    }
  } elseif ($entity == 'civicrm_contribution' && $action == 'credit') {
    $parameters['payment_term'] = new xmlrpcval(18, 'int'); //verrekenen
    $parameters['civicrm_id'] = new xmlrpcval($entity_id, 'int');
    if ($entity_id) {
      $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $entity_id));
      $refund_option_value = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name');
      if ($contribution['contribution_status_id'] == $refund_option_value) {
        $parameters['payment_term'] = new xmlrpcval(17, 'int'); //terugbetalen
      }
    }
  }
  if ($entity == 'civicrm_contact') {
    //sync field retour post
    CRM_Spodoosync_RetourPost::syncRetourPostToOdoo($entity_id, $parameters);
    // Sync field geen_post and reden_geen_post
    $geenpost = false;
    $geenpost_reden = '';
    $geenpost_dao = CRM_Core_DAO::executeQuery("SELECT geen_post, reden_geen_post FROM civicrm_value_communicatie WHERE entity_id", array(1=>$entity_id, 'Integer'));
    if ($geenpost_dao->fetch()) {
      if ($geenpost_dao->geen_post) {
        $geenpost = true;
      }
      if ($geenpost_dao->reden_geen_post) {
        $geenpost_reden = CRM_Core_DAO::singleValueQuery("
          SELECT label FROM civicrm_option_value 
          INNER JOIN civicrm_option_group ON civicrm_option_value.option_group_id = civicrm_option_group.id 
          WHERE civicrm_option_group.name = 'reden_geen_post' AND civicrm_option_value.value =%1",
          array(1=>array($geenpost_dao->reden_geen_post, 'String'))
        );
      }
    }
    $parameters['geenpost'] = new xmlrpcval($geenpost, 'boolean');
    $parameters['geenpost_reden'] = new xmlrpcval($geenpost_reden, 'string');

    unset($parameters['title']);
    $contact = civicrm_api3('Contact', 'getsingle', array('id' => $entity_id));
    $parameters['civicrm_id'] = new xmlrpcval($contact['id'], 'int');
    if ($contact['contact_type'] == 'Individual') {
      $parameters['is_company'] = new xmlrpcval(true, 'boolean');
      $parameters['firstname'] = new xmlrpcval($contact['first_name'], 'string');
      $parameters['prename'] = new xmlrpcval($contact['middle_name'], 'string');
      $parameters['lastname'] = new xmlrpcval($contact['last_name'], 'string');
      
{'fraction_non_alphanumeric': 0.1472281126931233, 'fraction_numerical': 0.002726446531354135, 'mean_word_length': 4.459757442116869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, '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': '1594917', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3169, 'n_words': 593}