From 6ee57287d9f05a7a56631cea5187fb3f7f94db64 Mon Sep 17 00:00:00 2001 From: atietz Date: Mon, 22 Jul 2024 16:59:13 +0200 Subject: [PATCH] re implement parser for DE https://github.com/Project60/org.project60.bic/issues/73 --- CRM/Bic/Parser/DE.php | 116 ++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/CRM/Bic/Parser/DE.php b/CRM/Bic/Parser/DE.php index 8a1c7e3..2f198d9 100644 --- a/CRM/Bic/Parser/DE.php +++ b/CRM/Bic/Parser/DE.php @@ -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), - ); - } - }