Skip to content

Commit d117cdc

Browse files
committed
minimal supported php is 8.1
Extended statements request parameters Initial Statementor class introduced Examples Cleanup
1 parent caa7945 commit d117cdc

22 files changed

Lines changed: 579 additions & 1072 deletions

.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ lib/Model/StatementListAccountStatementsInner.php
127127
lib/Model/TransactionList.php
128128
lib/Model/TransactionListTransactionsInner.php
129129
lib/ObjectSerializer.php
130+
lib/Statementor.php
130131
phpunit.xml.dist
131132
test/Api/DefaultApiTest.php
132133
test/Model/AccountBalanceTest.php

.openapi-generator/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ files:
2828
ApiClient.mustache:
2929
folder: lib
3030
destinationFilename: ApiClient.php
31+
Statementor.mustache:
32+
folder: lib
33+
destinationFilename: Statementor.php
3134

.openapi-generator/templates/.php-cs-fixer.dist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Ergebnis\PhpCsFixer\Config\Factory;
1717
use Ergebnis\PhpCsFixer\Config\Rules;
18-
use Ergebnis\PhpCsFixer\Config\RuleSet\Php74;
18+
use Ergebnis\PhpCsFixer\Config\RuleSet\Php81;
1919

2020
$header = <<<'HEADER'
2121
This file is part of the CSasWebApi package
@@ -28,7 +28,7 @@
2828
file that was distributed with this source code.
2929
HEADER;
3030

31-
$ruleSet = Php74::create()->withHeader($header)->withRules(Rules::fromArray([
31+
$ruleSet = Php81::create()->withHeader($header)->withRules(Rules::fromArray([
3232
'blank_line_before_statement' => [
3333
'statements' => [
3434
'break',
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the MultiFlexi package
7+
*
8+
* https://github.com/VitexSoftware/php-vitexsoftware-rbczpremiumapi
9+
*
10+
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
namespace SpojeNET\CSas;
17+
18+
/**
19+
* Description of Statementor.
20+
*
21+
* @author vitex
22+
*/
23+
class Statementor extends \Ease\Sand
24+
{
25+
use \Ease\Logger\Logging;
26+
public \DateTime $since;
27+
public \DateTime $until;
28+
29+
/**
30+
* DateTime Formating eg. 2021-08-01T10:00:00.0Z.
31+
*/
32+
public static string $dateTimeFormat = 'Y-m-d\\TH:i:s.0\\Z';
33+
34+
/**
35+
* DateTime Formating eg. 2021-08-01T10:00:00.0Z.
36+
*/
37+
public static string $dateFormat = 'Y-m-d';
38+
private string $accountNumber = '';
39+
40+
public function __construct(string $accountNumber = '', string $scope = '')
41+
{
42+
if ($accountNumber) {
43+
$this->setAccountNumber($accountNumber);
44+
}
45+
46+
if ($scope) {
47+
$this->setScope($scope);
48+
}
49+
}
50+
51+
/**
52+
* Set AccountNumber for further operations.
53+
*
54+
* @param string $accountNumber
55+
*
56+
* @return Statementor
57+
*/
58+
public function setAccountNumber($accountNumber)
59+
{
60+
$this->accountNumber = $accountNumber;
61+
$this->setObjectName($accountNumber.'@'.\get_class($this));
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* Obtain Statements from RB.
68+
*
69+
* @param string $currencyCode CZK,USD etc
70+
* @param string $statementLine
71+
*/
72+
public function getStatements($currencyCode = 'CZK', $statementLine = 'MAIN'): array
73+
{
74+
$apiInstance = new \SpojeNET\CSas\Accounts\DefaultApi();
75+
$page = 0;
76+
$statements = [];
77+
$this->addStatusMessage(sprintf(_('Request statements from %s to %s'), $this->since->format(self::$dateFormat), $this->until->format(self::$dateFormat)), 'debug');
78+
79+
try {
80+
do {
81+
$requestBody = new Model\GetStatementsRequest([
82+
'accountNumber' => $this->accountNumber,
83+
'page' => ++$page,
84+
'size' => 60,
85+
'currency' => $currencyCode,
86+
'statementLine' => $statementLine,
87+
'dateFrom' => $this->since->format(self::$dateFormat),
88+
'dateTo' => $this->until->format(self::$dateFormat)]);
89+
90+
$result = $apiInstance->getAccountStatements($requestBody, $page);
91+
92+
if (empty($result)) {
93+
$this->addStatusMessage(sprintf(_('No transactions from %s to %s'), $this->since->format(self::$dateFormat), $this->until->format(self::$dateFormat)));
94+
$result['lastPage'] = true;
95+
$result['last'] = true;
96+
}
97+
98+
if (\array_key_exists('statements', $result)) {
99+
$statements = array_merge($statements, $result['statements']);
100+
}
101+
102+
sleep(1);
103+
} while ($result['last'] === false);
104+
} catch (\Ease\Exception $e) {
105+
echo 'Exception when calling GetTransactionListApi->getTransactionList: ', $e->getMessage(), \PHP_EOL;
106+
}
107+
108+
return $statements;
109+
}
110+
111+
/**
112+
* Prepare processing interval.
113+
*
114+
* @param mixed $scope
115+
*
116+
* @throws \Exception
117+
*/
118+
public function setScope($scope): \DatePeriod
119+
{
120+
switch ($scope) {
121+
case 'yesterday':
122+
$this->since = (new \DateTime('yesterday'))->setTime(0, 0);
123+
$this->until = (new \DateTime('yesterday'))->setTime(23, 59);
124+
125+
break;
126+
case 'current_month':
127+
$this->since = new \DateTime('first day of this month');
128+
$this->until = new \DateTime();
129+
130+
break;
131+
case 'last_month':
132+
$this->since = new \DateTime('first day of last month');
133+
$this->until = new \DateTime('last day of last month');
134+
135+
break;
136+
case 'last_week':
137+
$this->since = new \DateTime('first day of last week');
138+
$this->until = new \DateTime('last day of last week');
139+
140+
break;
141+
case 'last_two_months':
142+
$this->since = (new \DateTime('first day of last month'))->modify('-1 month');
143+
$this->until = (new \DateTime('last day of last month'));
144+
145+
break;
146+
case 'previous_month':
147+
$this->since = new \DateTime('first day of -2 month');
148+
$this->until = new \DateTime('last day of -2 month');
149+
150+
break;
151+
case 'two_months_ago':
152+
$this->since = new \DateTime('first day of -3 month');
153+
$this->until = new \DateTime('last day of -3 month');
154+
155+
break;
156+
case 'this_year':
157+
$this->since = new \DateTime('first day of January '.date('Y'));
158+
$this->until = new \DateTime('last day of December'.date('Y'));
159+
160+
break;
161+
case 'January': // 1
162+
case 'February': // 2
163+
case 'March': // 3
164+
case 'April': // 4
165+
case 'May': // 5
166+
case 'June': // 6
167+
case 'July': // 7
168+
case 'August': // 8
169+
case 'September':// 9
170+
case 'October': // 10
171+
case 'November': // 11
172+
case 'December': // 12
173+
$this->since = new \DateTime('first day of '.$scope.' '.date('Y'));
174+
$this->until = new \DateTime('last day of '.$scope.' '.date('Y'));
175+
176+
break;
177+
case 'auto':
178+
$latestRecord = $this->getColumnsFromPohoda(['id', 'lastUpdate'], ['limit' => 1, 'order' => 'lastUpdate@A', 'source' => $this->sourceString(), 'banka' => $this->bank]);
179+
180+
if (\array_key_exists(0, $latestRecord) && \array_key_exists('lastUpdate', $latestRecord[0])) {
181+
$this->since = $latestRecord[0]['lastUpdate'];
182+
} else {
183+
$this->addStatusMessage('Previous record for "auto since" not found. Defaulting to today\'s 00:00', 'warning');
184+
$this->since = (new \DateTime())->setTime(0, 0);
185+
}
186+
187+
$this->until = new \DateTime(); // Now
188+
189+
break;
190+
191+
default:
192+
if (strstr($scope, '>')) {
193+
[$begin, $end] = explode('>', $scope);
194+
$this->since = new \DateTime($begin);
195+
$this->until = new \DateTime($end);
196+
} else {
197+
if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $scope)) {
198+
$this->since = new \DateTime($scope);
199+
$this->until = (new \DateTime($scope))->setTime(23, 59, 59, 999);
200+
}
201+
202+
throw new \InvalidArgumentException('Unknown scope '.$scope);
203+
}
204+
205+
break;
206+
}
207+
208+
if ($scope !== 'auto' && $scope !== 'today' && $scope !== 'yesterday') {
209+
$this->since = $this->since->setTime(0, 0);
210+
$this->until = $this->until->setTime(23, 59, 59, 999);
211+
}
212+
}
213+
214+
/**
215+
* Save Statement PDF files.
216+
*
217+
* @param array<mixed> $statements - produced by getStatements() function
218+
* @param string $format pdf|xml
219+
*/
220+
public function download(string $saveTo, array $statements, string $format = 'pdf', string $currencyCode = 'CZK'): array
221+
{
222+
$saved = [];
223+
$apiInstance = new PremiumAPI\DownloadStatementApi();
224+
$success = 0;
225+
226+
foreach ($statements as $statement) {
227+
$statementFilename = str_replace('/', '_', $statement->statementNumber).'_'.
228+
$statement->accountNumber.'_'.
229+
$statement->accountId.'_'.
230+
$statement->currency.'_'.$statement->dateFrom.'.'.$format;
231+
$requestBody = new \VitexSoftware\Raiffeisenbank\Model\DownloadStatementRequest([
232+
'accountNumber' => $this->accountNumber,
233+
'currency' => $currencyCode,
234+
'statementId' => $statement->statementId,
235+
'statementFormat' => $format]);
236+
$pdfStatementRaw = $apiInstance->downloadStatement(ApiClient::getxRequestId(), 'cs', $requestBody);
237+
sleep(1);
238+
239+
if (file_put_contents($saveTo.'/'.$statementFilename, $pdfStatementRaw->fread($pdfStatementRaw->getSize()))) {
240+
$saved[$statementFilename] = $saveTo.'/'.$statementFilename;
241+
$this->addStatusMessage($statementFilename.' saved', 'success');
242+
unset($pdfStatementRaw);
243+
++$success;
244+
}
245+
}
246+
247+
$this->addStatusMessage('Download done. '.$success.' of '.\count($statements).' saved');
248+
249+
return $saved;
250+
}
251+
252+
/**
253+
* Since time getter.
254+
*/
255+
public function getSince(): \DateTime
256+
{
257+
return $this->since;
258+
}
259+
260+
/**
261+
* Until time getter.
262+
*/
263+
public function getUntil(): \DateTime
264+
{
265+
return $this->until;
266+
}
267+
}

.openapi-generator/templates/composer.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
],
3030
"require": {
31-
"php": "^7.4 || ^8.0",
31+
"php": "^8.1 || ^8.2 || ^8.3 || ^8.4",
3232
"ext-curl": "*",
3333
"ext-json": "*",
3434
"ext-mbstring": "*",

.php-cs-fixer.dist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Ergebnis\PhpCsFixer\Config\Factory;
1717
use Ergebnis\PhpCsFixer\Config\Rules;
18-
use Ergebnis\PhpCsFixer\Config\RuleSet\Php74;
18+
use Ergebnis\PhpCsFixer\Config\RuleSet\Php81;
1919

2020
$header = <<<'HEADER'
2121
This file is part of the CSasWebApi package
@@ -28,7 +28,7 @@
2828
file that was distributed with this source code.
2929
HEADER;
3030

31-
$ruleSet = Php74::create()->withHeader($header)->withRules(Rules::fromArray([
31+
$ruleSet = Php81::create()->withHeader($header)->withRules(Rules::fromArray([
3232
'blank_line_before_statement' => [
3333
'statements' => [
3434
'break',

Examples/accounts.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
require_once \dirname(__DIR__).'/vendor/autoload.php';
1919

20-
Shr::init([], \dirname(__DIR__).'/.env');
20+
Shr::init(['CSAS_API_KEY', 'CSAS_ACCESS_TOKEN', 'CSAS_API_DEBUG', 'CSAS_SANDBOX_MODE'], \dirname(__DIR__).'/.env');
2121

2222
$apiInstance = new \SpojeNET\CSas\Accounts\DefaultApi(new SpojeNET\CSas\ApiClient(
2323
[
@@ -117,15 +117,15 @@
117117
118118
[container:protected] => Array
119119
(
120-
[aISP] =>
121-
[pISP] =>
120+
[aISP] =>
121+
[pISP] =>
122122
)
123123
124124
)
125125
126-
[status] =>
127-
[relatedAgents] =>
128-
[currencyExchange] =>
126+
[status] =>
127+
[relatedAgents] =>
128+
[currencyExchange] =>
129129
)
130130
131131
)
@@ -184,7 +184,7 @@
184184
185185
[container:protected] => Array
186186
(
187-
[isOwner] =>
187+
[isOwner] =>
188188
)
189189
190190
)
@@ -197,15 +197,15 @@
197197
198198
[container:protected] => Array
199199
(
200-
[aISP] =>
201-
[pISP] =>
200+
[aISP] =>
201+
[pISP] =>
202202
)
203203
204204
)
205205
206-
[status] =>
207-
[relatedAgents] =>
208-
[currencyExchange] =>
206+
[status] =>
207+
[relatedAgents] =>
208+
[currencyExchange] =>
209209
)
210210
211211
)
@@ -216,4 +216,4 @@
216216
217217
)
218218
Done.
219-
*/
219+
*/

0 commit comments

Comments
 (0)