From 6b653ccd45dd17ef49d7201a95c1d8d31db507d5 Mon Sep 17 00:00:00 2001 From: CyberVitexus Date: Wed, 3 Jul 2024 19:01:08 +0200 Subject: [PATCH] xml file importer added empty partnerIdentity name crash fixed --- README.md | 9 ++- bin/pohoda-raiffeisenbank-xml-statement | 3 + src/Pohoda/RaiffeisenBank/Statementor.php | 65 +++++++++++------ src/pohoda-raiffeisenbank-statements.php | 4 +- src/pohoda-raiffeisenbank-xml-statement.php | 80 +++++++++++++++++++++ 5 files changed, 133 insertions(+), 28 deletions(-) create mode 100644 bin/pohoda-raiffeisenbank-xml-statement create mode 100644 src/pohoda-raiffeisenbank-xml-statement.php diff --git a/README.md b/README.md index 3c11754..5351ee7 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,10 @@ Please use the .deb packages. The repository is availble: Po instalaci balíku jsou v systému k dispozici tyto nové příkazy: - * **abraflexi-raiffeisenbank-setup** - check and/or prepare Bank account setup in AbraFlexi - * **abraflexi-raiffeisenbank-transactions** - Import transactions. From latest imported or within the given scope - * **abraflexi-raiffeisenbank-statements** - Import transactions from Account Statements. + * **abraflexi-raiffeisenbank-setup** - check and/or prepare Bank account setup in AbraFlexi + * **abraflexi-raiffeisenbank-transactions** - Import transactions. From latest imported or within the given scope + * **abraflexi-raiffeisenbank-statements** - Import transactions from Account Statements. + * **abraflexi-raiffeisenbank-xml-statement** - Import transactions from XML Statements file. Configuration ------------- @@ -69,8 +70,6 @@ DB_DATABASE=StwPh_12345678_2023 DB_USERNAME=pohodaSQLuser DB_PASSWORD=pohodaSQLpassword DB_SETTINGS=encrypt=false - - ``` Sharepoint Integration diff --git a/bin/pohoda-raiffeisenbank-xml-statement b/bin/pohoda-raiffeisenbank-xml-statement new file mode 100644 index 0000000..7006495 --- /dev/null +++ b/bin/pohoda-raiffeisenbank-xml-statement @@ -0,0 +1,3 @@ +#!/bin/bash +php -f /usr/lib/pohoda-raiffeisenbank/pohoda-raiffeisenbank-xml-statement.php -- $@ + diff --git a/src/Pohoda/RaiffeisenBank/Statementor.php b/src/Pohoda/RaiffeisenBank/Statementor.php index cb61602..b29a140 100644 --- a/src/Pohoda/RaiffeisenBank/Statementor.php +++ b/src/Pohoda/RaiffeisenBank/Statementor.php @@ -14,8 +14,8 @@ * * @author vitex */ -class Statementor extends PohodaBankClient -{ +class Statementor extends PohodaBankClient { + /** * * @var \VitexSoftware\Raiffeisenbank\Statementor @@ -51,8 +51,7 @@ class Statementor extends PohodaBankClient * @param string $bankAccount * @param array $options */ - public function __construct($bankAccount, $options = []) - { + public function __construct($bankAccount, $options = []) { parent::__construct($bankAccount, $options); $this->obtainer = new \VitexSoftware\Raiffeisenbank\Statementor($bankAccount); @@ -66,14 +65,36 @@ public function __construct($bankAccount, $options = []) } /** - * + * + * @param string $xmlFile + * * @return array */ - public function import() - { - $inserted = []; + public function importXML(string $xmlFile) { + $this->statementsXML[basename($xmlFile)] = $xmlFile; + $pdfFile = str_replace('.xml', '.pdf', $xmlFile); + if (file_exists($pdfFile)) { + $this->statementsPDF[basename($pdfFile)] = $pdfFile; + } + return $this->import(); + } + + /** + * + * @return array + */ + public function importOnline() { $this->statementsXML = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'xml'); $this->statementsPDF = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'pdf'); + return $this->import(); + } + + /** + * + * @return array + */ + public function import() { + $inserted = []; $this->account = \Ease\Shared::cfg('POHODA_BANK_IDS', 'RB'); //TODO!!! $success = 0; foreach ($this->statementsXML as $pos => $statement) { @@ -111,8 +132,7 @@ public function import() * * @return array */ - public function entryToPohoda($entry) - { + public function entryToPohoda($entry) { $data['symPar'] = current((array) $entry->NtryRef); $data['intNote'] = 'Import Job ' . \Ease\Shared::cfg('JOB_ID', 'n/a'); $data['note'] = 'Imported by ' . \Ease\Shared::AppName() . ' ' . \Ease\Shared::AppVersion(); @@ -157,15 +177,21 @@ public function entryToPohoda($entry) if (property_exists($entry->NtryDtls->TxDtls->RltdPties, 'CdtrAcct')) { $paymentAccount['accountNo'] = current((array) $entry->NtryDtls->TxDtls->RltdPties->CdtrAcct->Id->Othr->Id); - $data['partnerIdentity'] = [ - 'address' => [ - 'name' => current((array) $entry->NtryDtls->TxDtls->RltdPties->CdtrAcct->Nm) - ] - ]; + + if (property_exists($entry->NtryDtls->TxDtls->RltdPties->CdtrAcct, 'Nm')) { + $data['partnerIdentity'] = [ + 'address' => [ + 'name' => current((array) $entry->NtryDtls->TxDtls->RltdPties->CdtrAcct->Nm) + ] + ]; + } else { + $this->addStatusMessage(sprintf(_('%s payment without partnerIdentity name'), $paymentAccount['accountNo'])); + } } } else { echo ''; // No Related party ? } + if (property_exists($entry->NtryDtls->TxDtls, 'RltdAgts')) { if (property_exists($entry->NtryDtls->TxDtls->RltdAgts->DbtrAgt, 'FinInstnId')) { $paymentAccount['bankCode'] = current((array) $entry->NtryDtls->TxDtls->RltdAgts->DbtrAgt->FinInstnId->Othr->Id); @@ -193,8 +219,7 @@ public function entryToPohoda($entry) * * @throws \Exception */ - function setScope($scope) - { + function setScope($scope) { switch ($scope) { case 'yesterday': $this->since = (new \DateTime('yesterday'))->setTime(0, 0); @@ -267,8 +292,7 @@ function setScope($scope) * List of downloaded PDF statements * @return array */ - public function getPdfStatements() - { + public function getPdfStatements() { return $this->statementsPDF; } @@ -276,8 +300,7 @@ public function getPdfStatements() * List of downloaded XML statements * @return array */ - public function getXmlStatements() - { + public function getXmlStatements() { return $this->statementsXML; } } diff --git a/src/pohoda-raiffeisenbank-statements.php b/src/pohoda-raiffeisenbank-statements.php index cb5337d..6b61dcc 100644 --- a/src/pohoda-raiffeisenbank-statements.php +++ b/src/pohoda-raiffeisenbank-statements.php @@ -1,7 +1,7 @@ * @copyright (C) 2023-2024 Spoje.Net @@ -26,7 +26,7 @@ PohodaBankClient::checkCertificatePresence(\Ease\Shared::cfg('CERT_FILE')); $engine = new Statementor(\Ease\Shared::cfg('ACCOUNT_NUMBER')); $engine->setScope(\Ease\Shared::cfg('STATEMENT_IMPORT_SCOPE', 'last_month')); -$inserted = $engine->import(); +$inserted = $engine->importOnline(); // // [243] => Array diff --git a/src/pohoda-raiffeisenbank-xml-statement.php b/src/pohoda-raiffeisenbank-xml-statement.php new file mode 100644 index 0000000..cbd1825 --- /dev/null +++ b/src/pohoda-raiffeisenbank-xml-statement.php @@ -0,0 +1,80 @@ + + * @copyright (C) 2023-2024 Spoje.Net + */ + +namespace Pohoda\RaiffeisenBank; + +use SpojeNet\PohodaSQL\Agenda; +use Ease\Shared; +use Office365\Runtime\Auth\ClientCredential; +use Office365\Runtime\Auth\UserCredentials; +use Office365\SharePoint\ClientContext; + +require_once('../vendor/autoload.php'); + +define('APP_NAME', 'Pohoda RaiffeisenBank Statements'); + +/** + * Get today's tramsactons list + */ +\Ease\Shared::init(['POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO', 'ACCOUNT_NUMBER'], isset($argv[2]) ? $argv[2] : '../.env'); +$xmlFile = \Ease\Shared::cfg('STATEMENT_FILE', isset($argv[1]) ? $argv[1] : ''); + +$engine = new Statementor(\Ease\Shared::cfg('ACCOUNT_NUMBER')); +$inserted = $engine->importXML($xmlFile); + +// +// [243] => Array +// ( +// [id] => 243 +// [number] => KB102023 +// [actionType] => add +// ) +// +// [244] => Array +// ( +// [id] => 244 +// [number] => KB102023 +// [actionType] => add +// ) +// + + +$pdfs = $engine->getPdfStatements(); + +if (Shared::cfg('OFFICE365_USERNAME', false) && Shared::cfg('OFFICE365_PASSWORD', false)) { + $credentials = new UserCredentials(Shared::cfg('OFFICE365_USERNAME'), Shared::cfg('OFFICE365_PASSWORD')); +} else { + $credentials = new ClientCredential(Shared::cfg('OFFICE365_CLIENTID'), Shared::cfg('OFFICE365_CLSECRET')); +} + +$ctx = (new ClientContext('https://' . Shared::cfg('OFFICE365_TENANT') . '.sharepoint.com/sites/' . Shared::cfg('OFFICE365_SITE')))->withCredentials($credentials); +$targetFolder = $ctx->getWeb()->getFolderByServerRelativeUrl(Shared::cfg('OFFICE365_PATH')); + +foreach ($pdfs as $filename) { + $uploadFile = $targetFolder->uploadFile(basename($filename), file_get_contents($filename)); + try { + $ctx->executeQuery(); + } catch (Exception $exc) { + fwrite(fopen('php://stderr', 'wb'), $exc->getMessage() . PHP_EOL); + exit(1); + } + $fileUrl = $ctx->getBaseUrl() . '/_layouts/15/download.aspx?SourceUrl=' . urlencode($uploadFile->getServerRelativeUrl()); +} + + + +$doc = new \SpojeNet\PohodaSQL\DOC(); +$doc->setDataValue('RelAgID', \SpojeNet\PohodaSQL\Agenda::BANK); //Bank + +foreach ($inserted as $id => $importInfo) { + $statement = current($pdfs); + //$url = \Ease\Shared::cfg('DOWNLOAD_LINK_PREFIX') . urlencode(basename($statement)); + $result = $doc->urlAttachment($id, $fileUrl, basename($statement)); + $doc->addStatusMessage($importInfo['number'] . ' ' . $fileUrl, is_null($result) ? 'error' : 'success'); +}