Skip to content

Commit

Permalink
Importer update
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Oct 25, 2024
1 parent 5a931ff commit 5705821
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 67 deletions.
24 changes: 19 additions & 5 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,21 @@ public function importXML(string $xmlFile)
return $this->import();
}

function downloadXML() {
$this->statementsXML = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'xml');
}

function downloadPDF() {
$this->statementsPDF = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'pdf');
}

/**
* @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');

$this->downloadXML();
$this->downloadPDF();
return $this->import();
}

Expand Down Expand Up @@ -197,8 +204,15 @@ public function entryToPohoda($entry)
}

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);
if(property_exists($entry->NtryDtls->TxDtls->RltdAgts, 'DbtrAgt')){
if (property_exists($entry->NtryDtls->TxDtls->RltdAgts->DbtrAgt, 'FinInstnId')) {
$paymentAccount['bankCode'] = current((array) $entry->NtryDtls->TxDtls->RltdAgts->DbtrAgt->FinInstnId->Othr->Id);
}
}
if(property_exists($entry->NtryDtls->TxDtls->RltdAgts, 'CdtrAgt')){
if (property_exists($entry->NtryDtls->TxDtls->RltdAgts->CdtrAgt, 'FinInstnId')) {
$paymentAccount['bankCode'] = current((array) $entry->NtryDtls->TxDtls->RltdAgts->CdtrAgt->FinInstnId->Othr->Id);
}
}
}

Expand Down
93 changes: 93 additions & 0 deletions src/pohoda-raiffeisenbank-statements-sharepoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/**
* This file is part of the PohodaRaiffeisenbank package
*
* https://github.com/Spoje-NET/pohoda-raiffeisenbank
*
* (c) Spoje.Net IT s.r.o. <https://spojenet.cz>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pohoda\RaiffeisenBank;

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 Statements list.
*/
\Ease\Shared::init(['POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO', 'CERT_FILE', 'CERT_PASS', 'XIBMCLIENTID', 'ACCOUNT_NUMBER'], $argv[1] ?? '../.env');
PohodaBankClient::checkCertificatePresence(\Ease\Shared::cfg('CERT_FILE'));
$engine = new Statementor(\Ease\Shared::cfg('ACCOUNT_NUMBER'));
$engine->setScope(\Ease\Shared::cfg('IMPORT_SCOPE', 'last_month'));
$engine->logBanner('', 'Scope: ' . $engine->scope);

$inserted = $engine->importOnline();

if ($inserted) {
//
// [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'));
$engine->addStatusMessage('Using OFFICE365_USERNAME ' . Shared::cfg('OFFICE365_USERNAME') . ' and OFFICE365_PASSWORD', 'debug');
} else {
$credentials = new ClientCredential(Shared::cfg('OFFICE365_CLIENTID'), Shared::cfg('OFFICE365_CLSECRET'));
$engine->addStatusMessage('Using OFFICE365_CLIENTID ' . Shared::cfg('OFFICE365_CLIENTID') . ' and OFFICE365_CLSECRET', 'debug');
}

$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'));

$engine->addStatusMessage('using ' . $ctx->getServiceRootUrl(), 'debug');

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, null === $result ? 'error' : 'success');
}
}
63 changes: 2 additions & 61 deletions src/pohoda-raiffeisenbank-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
namespace Pohoda\RaiffeisenBank;

use Ease\Shared;
use Office365\Runtime\Auth\ClientCredential;
use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;

require_once '../vendor/autoload.php';

Expand All @@ -33,61 +30,5 @@
$engine->setScope(\Ease\Shared::cfg('IMPORT_SCOPE', 'last_month'));
$engine->logBanner('', 'Scope: ' . $engine->scope);

$inserted = $engine->importOnline();

if ($inserted) {
//
// [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'));
$engine->addStatusMessage('Using OFFICE365_USERNAME ' . Shared::cfg('OFFICE365_USERNAME') . ' and OFFICE365_PASSWORD', 'debug');
} else {
$credentials = new ClientCredential(Shared::cfg('OFFICE365_CLIENTID'), Shared::cfg('OFFICE365_CLSECRET'));
$engine->addStatusMessage('Using OFFICE365_CLIENTID ' . Shared::cfg('OFFICE365_CLIENTID') . ' and OFFICE365_CLSECRET', 'debug');
}

$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'));

$engine->addStatusMessage('using ' . $ctx->getServiceRootUrl(), 'debug');

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, null === $result ? 'error' : 'success');
}
}
$engine->downloadXML();
$inserted = $engine->import();
2 changes: 1 addition & 1 deletion src/pohoda-raiffeisenbank-xml-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
\define('APP_NAME', 'Pohoda RaiffeisenBank Statements');

/**
* Get today's tramsactons list.
* Get today's transactions list.
*/
\Ease\Shared::init(['POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO', 'ACCOUNT_NUMBER'], $argv[2] ?? '../.env');
$xmlFile = \Ease\Shared::cfg('STATEMENT_FILE', $argv[1] ?? '');
Expand Down

0 comments on commit 5705821

Please sign in to comment.