|
| 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 | +} |
0 commit comments