Skip to content

Commit

Permalink
Issue #5 : Convert NBAN to IBAN in importer
Browse files Browse the repository at this point in the history
  • Loading branch information
psampont committed Feb 12, 2019
1 parent 298eee6 commit 00418e3
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 22 deletions.
14 changes: 14 additions & 0 deletions CRM/Banking/PluginImpl/Importer/CODA.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ function import_file( $file_path, $params )
// look up the bank accounts
// TODO: move to abstract class
foreach ($transaction_data as $key => $value) {
// Convert NBAN to IBAN
if (preg_match('/^_party_NBAN_(..)$/', $key, $matches)) {
$country = $matches[1];
$result = civicrm_api('BankingAccountReference', 'convertnban', array('version' => 3, 'nban' => $value), $country);
if (!$result['is_error']) {
$party_iban = $result['values'][0];
$transaction_data['_party_IBAN'] = $party_iban;

$result = civicrm_api3('Bic', 'findbyiban', array(
'iban' => $party_iban,
));
$transaction_data['_party_BIC'] = $result['bic'];
}
}
// check for NBAN_?? or IBAN endings
if (preg_match('/^_.*NBAN_..$/', $key) || preg_match('/^_.*IBAN$/', $key)) {
// this is a *BAN entry -> look it up
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# CiviBanking CODA Importer

CiviBanking CODA importer is an [CiviBanking](https://github.com/Project60/org.project60.banking) extension for importing CODA files.
CiviBanking CODA importer is a [CiviBanking](https://github.com/Project60/org.project60.banking) extension for importing CODA files.

Development Installation
========================

Clone repository into you CiviCRM extensions folder, e.g. like this:
```
> cd /var/www/drupal/sites/default/files/extensions/
> git clone https://github.com/Project60/org.project60.banking.git
> cd org.project60.coda
> composer install
1. Install [Little BIC extension](https://github.com/Project60/org.project60.bic)

2. Clone repository into you CiviCRM extensions folder
> cd /var/www/drupal/sites/default/files/extensions/
> git clone https://github.com/Project60/org.project60.banking.git
> cd org.project60.coda

3. Install 3rd party libraries
> composer install
53 changes: 53 additions & 0 deletions api/v3/BankingAccountReference/Convertnban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
include_once 'vendor/autoload.php';
use globalcitizen\php\iban;

use CRM_Coda_ExtensionUtil as E;

/**
* BankingAccountReference.Convertnban API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
*/
function _civicrm_api3_banking_account_reference_Convertnban_spec(&$spec) {
$spec['nban']['api.required'] = 1;
$spec['country']['api.required'] = 0;
}

/**
* BankingAccountReference.Convertnban API
*
* @param array $params
* @return array API result descriptor
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
* @throws API_Exception
*/
function civicrm_api3_banking_account_reference_Convertnban($params) {
if (array_key_exists('nban', $params)) {

if (array_key_exists('country', $params)) {
$country = $params['country'];
}
else {
$country = 'BE';
}
$iban = $country.'00'.$params['nban'];
$checksum = iban_find_checksum($iban);

$iban = $country.$checksum.$params['nban'];

$returnValues = array(
$iban
);

// Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
return civicrm_api3_create_success($returnValues, $params, 'NewEntity', 'NewAction');
}
else {
throw new API_Exception(/*errorMessage*/ 'Everyone knows that the magicword is "sesame"', /*errorCode*/ 1234);
}
}
142 changes: 127 additions & 15 deletions coda.civix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,83 @@

// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file

/**
* The ExtensionUtil class provides small stubs for accessing resources of this
* extension.
*/
class CRM_Coda_ExtensionUtil {
const SHORT_NAME = "coda";
const LONG_NAME = "org.project60.coda";
const CLASS_PREFIX = "CRM_Coda";

/**
* Translate a string using the extension's domain.
*
* If the extension doesn't have a specific translation
* for the string, fallback to the default translations.
*
* @param string $text
* Canonical message text (generally en_US).
* @param array $params
* @return string
* Translated text.
* @see ts
*/
public static function ts($text, $params = array()) {
if (!array_key_exists('domain', $params)) {
$params['domain'] = array(self::LONG_NAME, NULL);
}
return ts($text, $params);
}

/**
* Get the URL of a resource file (in this extension).
*
* @param string|NULL $file
* Ex: NULL.
* Ex: 'css/foo.css'.
* @return string
* Ex: 'http://example.org/sites/default/ext/org.example.foo'.
* Ex: 'http://example.org/sites/default/ext/org.example.foo/css/foo.css'.
*/
public static function url($file = NULL) {
if ($file === NULL) {
return rtrim(CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME), '/');
}
return CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME, $file);
}

/**
* Get the path of a resource file (in this extension).
*
* @param string|NULL $file
* Ex: NULL.
* Ex: 'css/foo.css'.
* @return string
* Ex: '/var/www/example.org/sites/default/ext/org.example.foo'.
* Ex: '/var/www/example.org/sites/default/ext/org.example.foo/css/foo.css'.
*/
public static function path($file = NULL) {
// return CRM_Core_Resources::singleton()->getPath(self::LONG_NAME, $file);
return __DIR__ . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file));
}

/**
* Get the name of a class within this extension.
*
* @param string $suffix
* Ex: 'Page_HelloWorld' or 'Page\\HelloWorld'.
* @return string
* Ex: 'CRM_Foo_Page_HelloWorld'.
*/
public static function findClass($suffix) {
return self::CLASS_PREFIX . '_' . str_replace('\\', '_', $suffix);
}

}

use CRM_Coda_ExtensionUtil as E;

/**
* (Delegated) Implements hook_civicrm_config().
*
Expand All @@ -19,14 +96,14 @@ function _coda_civix_civicrm_config(&$config = NULL) {
$extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$extDir = $extRoot . 'templates';

if ( is_array( $template->template_dir ) ) {
array_unshift( $template->template_dir, $extDir );
if (is_array($template->template_dir)) {
array_unshift($template->template_dir, $extDir);
}
else {
$template->template_dir = array( $extDir, $template->template_dir );
$template->template_dir = array($extDir, $template->template_dir);
}

$include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
$include_path = $extRoot . PATH_SEPARATOR . get_include_path();
set_include_path($include_path);
}

Expand Down Expand Up @@ -55,6 +132,20 @@ function _coda_civix_civicrm_install() {
}
}

/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function _coda_civix_civicrm_postInstall() {
_coda_civix_civicrm_config();
if ($upgrader = _coda_civix_upgrader()) {
if (is_callable(array($upgrader, 'onPostInstall'))) {
$upgrader->onPostInstall();
}
}
}

/**
* Implements hook_civicrm_uninstall().
*
Expand Down Expand Up @@ -117,7 +208,7 @@ function _coda_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
* @return CRM_Coda_Upgrader
*/
function _coda_civix_upgrader() {
if (!file_exists(__DIR__.'/CRM/Coda/Upgrader.php')) {
if (!file_exists(__DIR__ . '/CRM/Coda/Upgrader.php')) {
return NULL;
}
else {
Expand Down Expand Up @@ -153,7 +244,8 @@ function _coda_civix_find_files($dir, $pattern) {
while (FALSE !== ($entry = readdir($dh))) {
$path = $subdir . DIRECTORY_SEPARATOR . $entry;
if ($entry{0} == '.') {
} elseif (is_dir($path)) {
}
elseif (is_dir($path)) {
$todos[] = $path;
}
}
Expand All @@ -175,7 +267,10 @@ function _coda_civix_civicrm_managed(&$entities) {
$es = include $file;
foreach ($es as $e) {
if (empty($e['module'])) {
$e['module'] = 'org.project60.coda';
$e['module'] = E::LONG_NAME;
}
if (empty($e['params']['version'])) {
$e['params']['version'] = '3';
}
$entities[] = $e;
}
Expand Down Expand Up @@ -204,7 +299,7 @@ function _coda_civix_civicrm_caseTypes(&$caseTypes) {
// throw new CRM_Core_Exception($errorMessage);
}
$caseTypes[$name] = array(
'module' => 'org.project60.coda',
'module' => E::LONG_NAME,
'name' => $name,
'file' => $file,
);
Expand All @@ -230,7 +325,7 @@ function _coda_civix_civicrm_angularModules(&$angularModules) {
$name = preg_replace(':\.ang\.php$:', '', basename($file));
$module = include $file;
if (empty($module['ext'])) {
$module['ext'] = 'org.project60.coda';
$module['ext'] = E::LONG_NAME;
}
$angularModules[$name] = $module;
}
Expand All @@ -257,8 +352,10 @@ function _coda_civix_glob($pattern) {
* Inserts a navigation menu item at a given place in the hierarchy.
*
* @param array $menu - menu hierarchy
* @param string $path - path where insertion should happen (ie. Administer/System Settings)
* @param array $item - menu you need to insert (parent/child attributes will be filled for you)
* @param string $path - path to parent of this item, e.g. 'my_extension/submenu'
* 'Mailing', or 'Administer/System Settings'
* @param array $item - the item to insert (parent/child attributes will be
* filled for you)
*/
function _coda_civix_insert_navigation_menu(&$menu, $path, $item) {
// If we are done going down the path, insert menu
Expand All @@ -273,12 +370,14 @@ function _coda_civix_insert_navigation_menu(&$menu, $path, $item) {
}
else {
// Find an recurse into the next level down
$found = false;
$found = FALSE;
$path = explode('/', $path);
$first = array_shift($path);
foreach ($menu as $key => &$entry) {
if ($entry['attributes']['name'] == $first) {
if (!$entry['child']) $entry['child'] = array();
if (!isset($entry['child'])) {
$entry['child'] = array();
}
$found = _coda_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key);
}
}
Expand All @@ -305,7 +404,7 @@ function _coda_civix_fixNavigationMenu(&$nodes) {
if ($key === 'navID') {
$maxNavID = max($maxNavID, $item);
}
});
});
_coda_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL);
}

Expand Down Expand Up @@ -342,7 +441,20 @@ function _coda_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
$configured = TRUE;

$settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings';
if(is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) {
if (is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) {
$metaDataFolders[] = $settingsDir;
}
}

/**
* (Delegated) Implements hook_civicrm_entityTypes().
*
* Find any *.entityType.php files, merge their content, and return.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/

function _coda_civix_civicrm_entityTypes(&$entityTypes) {
$entityTypes = array_merge($entityTypes, array (
));
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>
Loading

0 comments on commit 00418e3

Please sign in to comment.