Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re implement parser for DE #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 55 additions & 61 deletions CRM/Bic/Parser/DE.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,69 +25,63 @@
*/
class CRM_Bic_Parser_DE extends CRM_Bic_Parser_Parser {

static $page_url = 'https://www.bundesbank.de/resource/blob/602630/1b5138f22cc648106b16a29ecc3117c1/mL/blz-aktuell-xls-data.xlsx';

static $country_code = 'DE';

public function update() {
// First, download the file
$file_name = sys_get_temp_dir() . '/DE-banks.xlsx';
$downloaded_file = $this->downloadFile(CRM_Bic_Parser_DE::$page_url);

if (empty($downloaded_file)) {
return $this->createParserOutdatedError(ts("Couldn't download data file"));
static $page_url = 'https://www.bundesbank.de/resource/blob/926192/07561adcd024c5752c46afa317d554af/mL/blz-aktuell-csv-data.csv';

static $country_code = 'DE';

public function update()
{
// First, download the file
$file_name = sys_get_temp_dir() . '/DE-banks.csv';
$downloaded_file = $this->downloadFile(CRM_Bic_Parser_DE::$page_url);

if (empty($downloaded_file)) {
return $this->createParserOutdatedError(ts("Couldn't download data file"));
}

// store file
file_put_contents($file_name, $downloaded_file);
unset($downloaded_file);

// Open and read CSV file
if (($handle = fopen($file_name, "r")) === false) {
return $this->createParserOutdatedError(ts("Couldn't open data file"));
}

// Skip header row
fgetcsv($handle, 1000, ';');

$banks = [];
while (($data = fgetcsv($handle, 1000, ';')) !== false) {
// skip entries with no bic
if (empty($data[7])) continue;
if ($data[1] != 1) continue;

// Process row
$bank = array(
'value' => $data[0],
'name' => $data[7],
'label' => $data[5],
'description' => $data[3] . ' ' . $data[4]
);
$banks[] = $bank;
}

fclose($handle);
unlink($file_name);

// Finally, update DB
return $this->updateEntries(CRM_Bic_Parser_DE::$country_code, $banks);
}

// store file
file_put_contents($file_name, $downloaded_file);
unset($downloaded_file);

// Automatically detect the correct reader to load for this file type
$excel_reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file_name);

// Set reader options
$excel_reader->setReadDataOnly(true);
$excel_reader->setLoadSheetsOnly(["Daten"]);

// Read Excel file
$excel_object = $excel_reader->load($file_name);
$excel_rows = $excel_object->getActiveSheet()->toArray();

// Process Excel data
//$skip_lines = 1;
$banks[] = array();
foreach($excel_rows as $excel_row) {
// skip entries with no bic
if (empty($excel_row[7])) continue;
if ($excel_row[1] != 1) continue;

// Process row
$bank = array(
'value' => $excel_row[0],
'name' => $excel_row[7],
'label' => $excel_row[5],
'description' => $excel_row[3] . ' ' . $excel_row[4]
);
$banks[] = $bank;
/*
* Extracts the National Bank Identifier from an IBAN.
*/
public function extractNBIDfromIBAN($iban)
{
return array(
substr($iban, 4, 8),
);
}

// clean up before importing
unset($excel_rows);
unset($excel_object);
unset($excel_reader);
unlink($file_name);

// Finally, update DB
return $this->updateEntries(CRM_Bic_Parser_DE::$country_code, $banks);
}

/*
* Extracts the National Bank Identifier from an IBAN.
*/
public function extractNBIDfromIBAN($iban) {
return array(
substr($iban, 4, 8),
);
}

}