Skip to content

Commit

Permalink
Date of payment and date of statement is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
Vítězslav Dvořák committed Nov 6, 2024
1 parent 2f64f7c commit 5fbde5e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ output.xml
/debian/pohoda-raiffeisenbank.debhelper.log
/.build/
/composer.lock
/.vscode/
22 changes: 11 additions & 11 deletions src/Pohoda/RaiffeisenBank/PohodaBankClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct($bankAccount, $options = [])
*/
public function sourceString()
{
return substr(__FILE__ . '@' . gethostname(), -50);
return substr(__FILE__.'@'.gethostname(), -50);
}

/**
Expand All @@ -71,7 +71,7 @@ public function sourceString()
public static function checkCertificatePresence($certFile): void
{
if ((file_exists($certFile) === false) || (is_readable($certFile) === false)) {
fwrite(\STDERR, 'Cannot read specified certificate file: ' . $certFile . \PHP_EOL);
fwrite(\STDERR, 'Cannot read specified certificate file: '.$certFile.\PHP_EOL);

exit(1);
}
Expand Down Expand Up @@ -123,8 +123,8 @@ public function setScope($scope): void

break;
case 'this_year':
$this->since = new \DateTime('first day of January ' . date('Y'));
$this->until = new \DateTime('last day of December' . date('Y'));
$this->since = new \DateTime('first day of January '.date('Y'));
$this->until = new \DateTime('last day of December'.date('Y'));

break;
case 'January': // 1
Expand All @@ -139,8 +139,8 @@ public function setScope($scope): void
case 'October': // 10
case 'November': // 11
case 'December': // 12
$this->since = new \DateTime('first day of ' . $scope . ' ' . date('Y'));
$this->until = new \DateTime('last day of ' . $scope . ' ' . date('Y'));
$this->since = new \DateTime('first day of '.$scope.' '.date('Y'));
$this->until = new \DateTime('last day of '.$scope.' '.date('Y'));

break;
case 'auto':
Expand All @@ -158,7 +158,7 @@ public function setScope($scope): void
break;

default:
throw new \Exception('Unknown scope ' . $scope);
throw new \Exception('Unknown scope '.$scope);

break;
}
Expand All @@ -176,7 +176,7 @@ public function setScope($scope): void
*/
public function getxRequestId()
{
return $this->getDataValue('account') . time();
return $this->getDataValue('account').time();
}

/**
Expand Down Expand Up @@ -205,8 +205,8 @@ public function checkForTransactionPresence()
public function ensureKSExists($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->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;
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ public function insertTransactionToPohoda($success)
$producedAction = 'n/a';
}

$this->addStatusMessage('#' . $producedId . ' ' . $producedAction . ' ' . $producedNumber, $result ? 'success' : 'error'); // TODO: Parse response for docID
$this->addStatusMessage('#'.$producedId.' '.$producedAction.' '.$producedNumber, $result ? 'success' : 'error'); // TODO: Parse response for docID
} else {
$this->addStatusMessage('Record with remoteNumber TODO already present in Pohoda', 'warning');
}
Expand Down
40 changes: 22 additions & 18 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct($bankAccount, $options = [])

$this->obtainer->setScope(\Ease\Shared::cfg('STATEMENT_IMPORT_SCOPE', 'last_month'));

$this->statementsDir = \Ease\Functions::cfg('STATEMENT_SAVE_DIR', sys_get_temp_dir() . '/rb');
$this->statementsDir = \Ease\Functions::cfg('STATEMENT_SAVE_DIR', sys_get_temp_dir().'/rb');

if (file_exists($this->statementsDir) === false) {
mkdir($this->statementsDir, 0777, true);
Expand All @@ -70,21 +70,24 @@ public function importXML(string $xmlFile)
return $this->import();
}

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

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

/**
* @return array
*/
public function importOnline()
{
$this->downloadXML();
$this->downloadPDF();

return $this->import();
}

Expand All @@ -105,7 +108,7 @@ public function import()
$this->dataReset();
$this->setData($this->entryToPohoda($entry));
[$statementNumber, $statementYear] = explode('_', $pos);
$this->setDataValue('statementNumber', ['statementNumber' => $statementNumber . '/' . $statementYear]);
$this->setDataValue('statementNumber', ['statementNumber' => $statementNumber.'/'.$statementYear]);
// $this->setDataValue('account', current((array) $entry->NtryRef));
// $this->setDataValue('vypisCisDokl', $statementXML->BkToCstmrStmt->Stmt->Id);
// $this->setDataValue('cisSouhrnne', $statementXML->BkToCstmrStmt->Stmt->LglSeqNb);
Expand All @@ -120,7 +123,7 @@ public function import()
}
}

$this->addStatusMessage($statementNumberLong . ' Import done. ' . $success . ' of ' . \count($this->statementsXML) . ' imported');
$this->addStatusMessage($statementNumberLong.' Import done. '.$success.' of '.\count($this->statementsXML).' imported');

return $inserted;
}
Expand All @@ -139,12 +142,12 @@ public function import()
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();
$data['datePayment'] = current((array) $entry->BookgDt->DtTm);
$data['dateStatement'] = current((array) $entry->ValDt->DtTm);
$data['intNote'] = 'Import Job '.\Ease\Shared::cfg('JOB_ID', 'n/a');
$data['note'] = 'Imported by '.\Ease\Shared::AppName().' '.\Ease\Shared::AppVersion();
$data['datePayment'] = current((array) $entry->BookgDt->DtTm); // current((array) $entry->ValDt->DtTm);
$data['dateStatement'] = current((array) $entry->BookgDt->DtTm) ;
$moveTrans = ['DBIT' => 'expense', 'CRDT' => 'receipt'];
$data['bankType'] = $moveTrans[trim((string)$entry->CdtDbtInd)];
$data['bankType'] = $moveTrans[trim((string) $entry->CdtDbtInd)];
// $data['cisDosle', strval($entry->NtryRef));
// $data['datVyst', new \DateTime($entry->BookgDt->DtTm));
$data['homeCurrency'] = ['priceNone' => abs((float) $entry->Amt)]; // "price3", "price3Sum", "price3VAT", "priceHigh", "priceHighSum", "priceHighVAT", "priceLow", "priceLowSum", "priceLowVAT", "priceNone", "round"
Expand Down Expand Up @@ -204,12 +207,13 @@ public function entryToPohoda($entry)
}

if (property_exists($entry->NtryDtls->TxDtls, 'RltdAgts')) {
if(property_exists($entry->NtryDtls->TxDtls->RltdAgts, 'DbtrAgt')){
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')) {
if (property_exists($entry->NtryDtls->TxDtls->RltdAgts->CdtrAgt, 'FinInstnId')) {
$paymentAccount['bankCode'] = current((array) $entry->NtryDtls->TxDtls->RltdAgts->CdtrAgt->FinInstnId->Othr->Id);
}
Expand Down Expand Up @@ -277,8 +281,8 @@ public function setScope($scope): void

break;
case 'this_year':
$this->since = new \DateTime('first day of January ' . date('Y'));
$this->until = new \DateTime('last day of December' . date('Y'));
$this->since = new \DateTime('first day of January '.date('Y'));
$this->until = new \DateTime('last day of December'.date('Y'));

break;
case 'January': // 1
Expand All @@ -293,8 +297,8 @@ public function setScope($scope): void
case 'October': // 10
case 'November': // 11
case 'December': // 12
$this->since = new \DateTime('first day of ' . $scope . ' ' . date('Y'));
$this->until = new \DateTime('last day of ' . $scope . ' ' . date('Y'));
$this->since = new \DateTime('first day of '.$scope.' '.date('Y'));
$this->until = new \DateTime('last day of '.$scope.' '.date('Y'));

break;
case 'auto':
Expand All @@ -312,7 +316,7 @@ public function setScope($scope): void
break;

default:
throw new \Exception('Unknown scope ' . $scope);
throw new \Exception('Unknown scope '.$scope);

break;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Pohoda/RaiffeisenBank/Transactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
class Transactor extends PohodaBankClient
{

/**
* Transaction Handler.
*
Expand Down Expand Up @@ -73,7 +72,7 @@ public function getTransactions()

$this->addStatusMessage('Exception when calling GetTransactionListApi->getTransactionList: '.$errorMessage, 'error', $apiInstance);

exit(intval($errorCode));
exit((int) $errorCode);
}

return $transactions;
Expand All @@ -86,7 +85,7 @@ public function import(): void
{
// $allMoves = $this->getColumnsFromPohoda('id', ['limit' => 0, 'banka' => $this->bank]);
$allTransactions = $this->getTransactions();
$this->addStatusMessage(\count($allTransactions) . ' transactions obtained via API', 'debug');
$this->addStatusMessage(\count($allTransactions).' transactions obtained via API', 'debug');
$success = 0;

foreach ($allTransactions as $transaction) {
Expand All @@ -96,7 +95,7 @@ public function import(): void
$this->reset();
}

$this->addStatusMessage('Import done. ' . $success . ' of ' . \count($allTransactions) . ' imported');
$this->addStatusMessage('Import done. '.$success.' of '.\count($allTransactions).' imported');
}

/**
Expand All @@ -114,7 +113,7 @@ public function takeTransactionData($transactionData): void
$this->setDataValue('bankType', $moveTrans[$transactionData->creditDebitIndication]);
$this->setDataValue('account', \Ease\Shared::cfg('POHODA_BANK_IDS')); // RB
$this->setDataValue('datePayment', (new \DateTime($transactionData->valueDate))->format('Y-m-d'));
$this->setDataValue('intNote', _('Automatic Import') . ': ' . \Ease\Shared::appName() . ' ' . \Ease\Shared::appVersion() . ' ' . $transactionData->entryReference);
$this->setDataValue('intNote', _('Automatic Import').': '.\Ease\Shared::appName().' '.\Ease\Shared::appVersion().' '.$transactionData->entryReference);
$this->setDataValue('statementNumber', ['statementNumber' => $transactionData->bankTransactionCode->code]);
$this->setDataValue('symPar', (string) $transactionData->entryReference);

Expand Down Expand Up @@ -169,7 +168,7 @@ public function takeTransactionData($transactionData): void
$this->setDataValue('text', $transactionData->entryDetails->transactionDetails->remittanceInformation->originatorMessage);
}

$this->setDataValue('note', 'Import Job ' . \Ease\Shared::cfg('JOB_ID', 'n/a'));
$this->setDataValue('note', 'Import Job '.\Ease\Shared::cfg('JOB_ID', 'n/a'));

if (property_exists($transactionData->entryDetails->transactionDetails->relatedParties, 'counterParty')) {
$counterAccount = $transactionData->entryDetails->transactionDetails->relatedParties->counterParty;
Expand All @@ -181,7 +180,7 @@ public function takeTransactionData($transactionData): void
$counterAccountNumber = $counterAccount->account->accountNumber;

if (property_exists($counterAccount->account, 'accountNumberPrefix')) {
$accountNumber = $counterAccount->account->accountNumberPrefix . '-' . $counterAccountNumber;
$accountNumber = $counterAccount->account->accountNumberPrefix.'-'.$counterAccountNumber;
} else {
$accountNumber = $counterAccountNumber;
}
Expand Down Expand Up @@ -220,6 +219,7 @@ public function setScope($scope): void
$this->since = (new \DateTime('yesterday'))->setTime(0, 0);
$this->until = (new \DateTime('yesterday'))->setTime(23, 59, 59, 999);

// no break
case 'last_week':
$this->since = new \DateTime('first day of last week');
$this->until = new \DateTime('last day of last week');
Expand All @@ -240,7 +240,7 @@ public function setScope($scope): void
break;

default:
throw new \Exception('Unknown scope ' . $scope);
throw new \Exception('Unknown scope '.$scope);

break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pohoda-raiffeisenbank-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
$banker->addStatusMessage(sprintf('Account %s already exists in pohoda as %s', $account->friendlyName, $currentAccounts[$account->iban]['kod']));
} else {
$banker->dataReset();
$banker->setDataValue('kod', 'RB' . $account->accountId);
$banker->setDataValue('kod', 'RB'.$account->accountId);
$banker->setDataValue('nazev', $account->accountName);
$banker->setDataValue('buc', $account->accountNumber);
$banker->setDataValue('nazBanky', 'Raiffeisenbank');
Expand Down
4 changes: 1 addition & 3 deletions src/pohoda-raiffeisenbank-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

namespace Pohoda\RaiffeisenBank;

use Ease\Shared;

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

\define('APP_NAME', 'Pohoda RaiffeisenBank Statements');
Expand All @@ -28,7 +26,7 @@
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);
$engine->logBanner('', 'Scope: '.$engine->scope);

$engine->downloadXML();
$inserted = $engine->import();
8 changes: 4 additions & 4 deletions src/pohoda-raiffeisenbank-xml-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
$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);
$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) {
Expand All @@ -66,12 +66,12 @@
try {
$ctx->executeQuery();
} catch (Exception $exc) {
fwrite(fopen('php://stderr', 'wb'), $exc->getMessage() . \PHP_EOL);
fwrite(fopen('php://stderr', 'wb'), $exc->getMessage().\PHP_EOL);

exit(1);
}

$fileUrl = $ctx->getBaseUrl() . '/_layouts/15/download.aspx?SourceUrl=' . urlencode($uploadFile->getServerRelativeUrl());
$fileUrl = $ctx->getBaseUrl().'/_layouts/15/download.aspx?SourceUrl='.urlencode($uploadFile->getServerRelativeUrl());
}

$doc = new \SpojeNet\PohodaSQL\DOC();
Expand All @@ -81,5 +81,5 @@
$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');
$doc->addStatusMessage($importInfo['number'].' '.$fileUrl, null === $result ? 'error' : 'success');
}
18 changes: 9 additions & 9 deletions src/pohodaSQL-raiffeisenbank-statements-sharepoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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);
$engine->logBanner('', 'Scope: '.$engine->scope);

$inserted = $engine->importOnline();

Expand All @@ -52,34 +52,34 @@
// )
//
sleep(5);

$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');
$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('using ' . $ctx->getServiceRootUrl(), 'debug');
$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);
fwrite(fopen('php://stderr', 'wb'), $exc->getMessage().\PHP_EOL);

exit(1);
}

$fileUrl = $ctx->getBaseUrl() . '/_layouts/15/download.aspx?SourceUrl=' . urlencode($uploadFile->getServerRelativeUrl());
$fileUrl = $ctx->getBaseUrl().'/_layouts/15/download.aspx?SourceUrl='.urlencode($uploadFile->getServerRelativeUrl());
}

$doc = new \SpojeNet\PohodaSQL\DOC();
Expand All @@ -89,6 +89,6 @@
$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');
$doc->addStatusMessage($importInfo['number'].' '.$fileUrl, null === $result ? 'error' : 'success');
}
}

0 comments on commit 5fbde5e

Please sign in to comment.