Skip to content

Commit

Permalink
v1.1.1 release: code cleanup. PohodaSQL attachnent fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Dec 2, 2024
1 parent 04128f2 commit d3fe6be
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 60 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bin": ["src/pohoda-raiffeisenbank-transactions.php", "src/pohoda-raiffeisenbank-setup.php"],
"require": {
"vgrem/php-spo": "^3",
"vitexsoftware/pohoda-connector": "^0.4",
"vitexsoftware/pohoda-connector": "^0.5",
"vitexsoftware/rbczpremiumapi": "dev-main",
"spojenet/pohoda-sql": "dev-main"
},
Expand Down
10 changes: 8 additions & 2 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pohoda-raiffeisenbank (1.1.0) UNRELEASED; urgency=medium
pohoda-raiffeisenbank (1.1.1) UNRELEASED; urgency=medium

* code cleanup. PohodaSQL attachnent fix

-- vitex <info@vitexsoftware.cz> Tue, 03 Dec 2024 00:45:54 +0100

pohoda-raiffeisenbank (1.1.0) jammy; urgency=medium

* raiffeisenbank-statements-sharepoint-checker added

-- vitex <info@vitexsoftware.cz> Thu, 28 Nov 2024 17:58:36 +0100
-- vitex <info@vitexsoftware.cz> Tue, 03 Dec 2024 00:45:03 +0100

pohoda-raiffeisenbank (1.0.0) jammy; urgency=medium

Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Homepage: https://github.com/Spoje-NET/pohoda-raiffeisenbank
Package: pohoda-raiffeisenbank
Architecture: all
Multi-Arch: foreign
Depends: ${misc:Depends}, ${shlibs:Depends}, php-vitexsoftware-rbczpremiumapi, php-spojenet-pohoda-sql, php-vitexsoftware-pohoda-connector
Depends: ${misc:Depends}, ${shlibs:Depends}, php-vitexsoftware-rbczpremiumapi, php-spojenet-pohoda-sql, php-vitexsoftware-pohoda-connector (>= 0.5.0)
Description: Import Raiffeisenbank statements into Pohoda
See your Bank Account Statements in Pohoda

Expand Down
30 changes: 7 additions & 23 deletions src/Pohoda/RaiffeisenBank/PohodaBankClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Pohoda\RaiffeisenBank;

use Ease\Shared;

/**
* Description of ApiClient.
*
Expand All @@ -31,18 +33,9 @@ abstract class PohodaBankClient extends \mServer\Bank
* DateTime Formating eg. 2021-08-01T10:00:00.0Z.
*/
public static string $dateFormat = 'Y-m-d';

/**
* @var \Riesenia\Pohoda\Banka
*/
public ?\Riesenia\Pohoda\Agenda $requestXml;

// public Response $response; TODO: Update
protected $constantor;
protected $constSymbols;
protected \DateTime $since;
protected \DateTime $until;
protected $bank;
protected string $bank;

/**
* Transaction Handler.
Expand All @@ -69,7 +62,7 @@ public function sourceString()
}

/**
* Try to check certificate readibilty.
* Try to check certificate readability.
*
* @param string $certFile path to certificate
*/
Expand Down Expand Up @@ -149,7 +142,7 @@ public function setScope($scope): void

break;
case 'auto':
$latestRecord = $this->getColumnsFromPohoda(['id', 'lastUpdate'], ['limit' => 1, 'order' => 'lastUpdate@A', 'source' => $this->sourceString(), 'banka' => $this->bank]);
$latestRecord = $this->getColumnsFromPohoda(['id', 'lastUpdate'], ['limit' => 1, 'order' => 'lastUpdate@A', 'source' => $this->sourceString(), 'bank' => $this->bank]);

if (\array_key_exists(0, $latestRecord) && \array_key_exists('lastUpdate', $latestRecord[0])) {
$this->since = $latestRecord[0]['lastUpdate'];
Expand Down Expand Up @@ -204,7 +197,7 @@ public function getxRequestId()
*/
public function getCurrencyCode()
{
return \Ease\Shared::cfg('ACCOUNT_CURRENCY', 'CZK');
return Shared::cfg('ACCOUNT_CURRENCY', 'CZK');
}

/**
Expand All @@ -221,15 +214,6 @@ public function checkForTransactionPresence()
return false; // !empty($this->getColumnsFromPohoda('id', ['cisDosle' => $this->getDataValue('cisDosle')])); TODO
}

public function ensureKSExists(string $conSym): void
{
if (!\array_key_exists($conSym, $this->constSymbols)) {
$this->constantor->insertToPohoda(['kod' => $conSym, 'poznam' => 'Created by Raiffeisen Bank importer', 'nazev' => '?!?!? '.$conSym]);
$this->constantor->addStatusMessage('New constant '.$conSym.' created in flexibee', 'warning');
$this->constSymbols[$conSym] = $conSym;
}
}

/**
* Insert Transaction to Pohoda.
*
Expand Down Expand Up @@ -381,6 +365,6 @@ public function generateAutomaticLiquidationXML($producedNumber)

public function getCompanyId(): string
{
return \Ease\Shared::cfg('POHODA_ICO');
return Shared::cfg('POHODA_ICO');
}
}
47 changes: 29 additions & 18 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,25 @@ public function importXML(string $xmlFile)
return $this->import();
}



/**
* Get List of Statement files
*
* Get List of Statement files.
*
* @param string $format xml|pdf
*
* @return array<string>
*/
public function getStatementFilenames(string $format): array {
public function getStatementFilenames(string $format): array
{
foreach ($this->getStatements() as $statement) {
$statementFilenames[] = str_replace('/', '_', $statement->statementNumber) . '_' .
$statement->accountNumber . '_' .
$statement->accountId . '_' .
$statement->currency . '_' . $statement->dateFrom . '.' . $format;
$statementFilenames[] = str_replace('/', '_', $statement->statementNumber).'_'.
$statement->accountNumber.'_'.
$statement->accountId.'_'.
$statement->currency.'_'.$statement->dateFrom.'.'.$format;
}

return $statementFilenames;
}


public function getStatements()
{
return $this->obtainer->getStatements();
Expand All @@ -103,13 +102,23 @@ public function download($format)
return $this->obtainer->download($this->statementsDir, $this->getStatements(), $format);
}

/**
* Download Raiffeisen bank XML statement.
*
* @return array<string, string> List of downloaded XML files
*/
public function downloadXML(): array
{
$this->statementsXML = $this->download('xml');

return $this->statementsXML;
}

/**
* Download Raiffeisen bank PDF statement.
*
* @return array<string, string> List of downloaded PDF files
*/
public function downloadPDF(): array
{
$this->statementsPDF = $this->download('pdf');
Expand All @@ -118,7 +127,7 @@ public function downloadPDF(): array
}

/**
* @return array
* @return array<array<string, string>> List of inserted records
*/
public function importOnline()
{
Expand Down Expand Up @@ -158,7 +167,7 @@ public function import(): array
$lastInsert = $this->insertTransactionToPohoda();

if ($lastInsert) {
$inserted = array_merge($lastInsert);
$inserted[key($lastInsert)] = current($lastInsert);
++$success;
}
} catch (\Exception $exc) {
Expand All @@ -178,9 +187,9 @@ public function import(): array
* @see https://cbaonline.cz/upload/1425-standard-xml-cba-listopad-2020.pdf
* @see https://www.stormware.cz/xml/schema/version_2/bank.xsd
*
* @param \SimpleXMLElement $entry
* @return array<string, array<string, string>|string>
*/
public function entryToPohoda($entry): array
public function entryToPohoda(\SimpleXMLElement $entry): array
{
$data['symPar'] = current((array) $entry->NtryRef);
$data['intNote'] = 'Imported by '.\Ease\Shared::AppName().' '.\Ease\Shared::AppVersion().' Import Job '.\Ease\Shared::cfg('MULTIFLEXI_JOB_ID', \Ease\Shared::cfg('JOB_ID', 'n/a'));
Expand Down Expand Up @@ -404,12 +413,14 @@ public function getXmlStatements()
{
return $this->statementsXML;
}

public function getSince(): \DateTime {

public function getSince(): \DateTime
{
return $this->since;
}

public function getUntil(): \DateTime {

public function getUntil(): \DateTime
{
return $this->until;
}
}
6 changes: 5 additions & 1 deletion src/Pohoda/RaiffeisenBank/Transactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function import(): void
foreach ($allTransactions as $transaction) {
// $this->dataReset();
$this->takeTransactionData($transaction);
$success = $this->insertTransactionToPohoda($success);

if ($this->insertTransactionToPohoda()) {
++$success;
}

$this->reset();
}

Expand Down
2 changes: 1 addition & 1 deletion src/pohodaSQL-raiffeisenbank-statements-sharepoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
}
}

$written = file_put_contents($destination, json_encode($report, \Ease\Shared::cfg('DEBUG') ? \JSON_PRETTY_PRINT : 0));
$written = file_put_contents($destination, json_encode($report, Shared::cfg('DEBUG') ? \JSON_PRETTY_PRINT : 0));
$engine->addStatusMessage(sprintf(_('Saving result to %s'), $destination), $written ? 'success' : 'error');

exit($exitcode);
28 changes: 15 additions & 13 deletions src/raiffeisenbank-statements-sharepoint-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,54 @@
*/
$options = getopt('o::e::', ['output::environment::']);
Shared::init(
[
'CERT_FILE', 'CERT_PASS', 'XIBMCLIENTID', 'ACCOUNT_NUMBER',
'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD',
],
\array_key_exists('environment', $options) ? $options['environment'] : '../.env',
[
'CERT_FILE', 'CERT_PASS', 'XIBMCLIENTID', 'ACCOUNT_NUMBER',
'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD',
],
\array_key_exists('environment', $options) ? $options['environment'] : '../.env',
);
$destination = \array_key_exists('output', $options) ? $options['output'] : Shared::cfg('RESULT_FILE', 'php://stdout');

PohodaBankClient::checkCertificatePresence(Shared::cfg('CERT_FILE'));
$engine = new Statementor(Shared::cfg('ACCOUNT_NUMBER'));
$engine->setScope(Shared::cfg('IMPORT_SCOPE', 'last_month'));
$engine->logBanner('', 'Scope: ' . $engine->scope);
$engine->logBanner('', 'Scope: '.$engine->scope);
$exitcode = 0;
$fileUrls = [];
$report = [
'account' => Shared::cfg('ACCOUNT_NUMBER'),
'until' => $engine->getUntil()->format('Y-m-d'),
'since' => $engine->getSince()->format('Y-m-d'),
'missing' => [],
'existing' => []
'existing' => [],
];

$pdfStatements = $engine->getStatementFilenames('pdf');

if ($pdfStatements) {

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');
$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');
$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);
$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('ServiceRootUrl: ' . $ctx->getServiceRootUrl(), 'debug');
$engine->addStatusMessage('ServiceRootUrl: '.$ctx->getServiceRootUrl(), 'debug');

$sharepointFilesRaw = $targetFolder->getFiles()->get()->executeQuery();
$sharepointFiles = [];

// @phpstan-ignore foreach.nonIterable
foreach ($sharepointFilesRaw as $fileInSharepint) {
$sharepointFiles[$fileInSharepint->getName()] = $fileInSharepint->getServerRelativeUrl();
}

foreach ($pdfStatements as $pdfStatement) {
if (array_key_exists($pdfStatement, $sharepointFiles)) {
if (\array_key_exists($pdfStatement, $sharepointFiles)) {
$engine->addStatusMessage(sprintf('File %s exists in SharePoint', $pdfStatement), 'success');
$report['existing'][] = $pdfStatement;
} else {
Expand Down

0 comments on commit d3fe6be

Please sign in to comment.