Skip to content

Commit

Permalink
xml file importer added
Browse files Browse the repository at this point in the history
empty partnerIdentity name crash fixed
  • Loading branch information
Vitexus committed Jul 3, 2024
1 parent e529e1d commit 6b653cc
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down Expand Up @@ -69,8 +70,6 @@ DB_DATABASE=StwPh_12345678_2023
DB_USERNAME=pohodaSQLuser
DB_PASSWORD=pohodaSQLpassword
DB_SETTINGS=encrypt=false
```

Sharepoint Integration
Expand Down
3 changes: 3 additions & 0 deletions bin/pohoda-raiffeisenbank-xml-statement
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
php -f /usr/lib/pohoda-raiffeisenbank/pohoda-raiffeisenbank-xml-statement.php -- $@

65 changes: 44 additions & 21 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*
* @author vitex
*/
class Statementor extends PohodaBankClient
{
class Statementor extends PohodaBankClient {

/**
*
* @var \VitexSoftware\Raiffeisenbank\Statementor
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -267,17 +292,15 @@ function setScope($scope)
* List of downloaded PDF statements
* @return array
*/
public function getPdfStatements()
{
public function getPdfStatements() {
return $this->statementsPDF;
}

/**
* List of downloaded XML statements
* @return array
*/
public function getXmlStatements()
{
public function getXmlStatements() {
return $this->statementsXML;
}
}
4 changes: 2 additions & 2 deletions src/pohoda-raiffeisenbank-statements.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* RaiffeisenBank - Statements importer.
* RaiffeisenBank - Online Statements importer.
*
* @author Vítězslav Dvořák <[email protected]>
* @copyright (C) 2023-2024 Spoje.Net
Expand All @@ -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
Expand Down
80 changes: 80 additions & 0 deletions src/pohoda-raiffeisenbank-xml-statement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* RaiffeisenBank - XML Statement importer.
*
* @author Vítězslav Dvořák <[email protected]>
* @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');
}

0 comments on commit 6b653cc

Please sign in to comment.