Skip to content

Commit 00cda45

Browse files
committed
Merge branch 'release/0.8.0'
2 parents 02b059c + 0894230 commit 00cda45

20 files changed

Lines changed: 929 additions & 683 deletions

app/Events/ImportedTransactions.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,36 @@ class ImportedTransactions
4646
*/
4747
public function __construct(array $messages, array $warnings, array $errors)
4848
{
49-
app('log')->debug('Created event ImportedTransactions');
50-
$this->messages = $messages;
51-
$this->warnings = $warnings;
52-
$this->errors = $errors;
49+
app('log')->debug('Created event ImportedTransactions with filtering (2)');
5350

51+
// filter messages:
52+
$this->messages = $this->filterArray($messages);
53+
$this->warnings = $this->filterArray($warnings);
54+
$this->errors = $this->filterArray($errors);
55+
}
56+
57+
/**
58+
* @param array $collection
59+
* @return array
60+
*/
61+
private function filterArray(array $collection): array
62+
{
63+
$count = 0;
64+
$newCollection = [];
65+
foreach ($collection as $index => $set) {
66+
$newSet = [];
67+
foreach ($set as $line) {
68+
$line = (string) $line;
69+
if ('' !== $line) {
70+
$newSet[] = $line;
71+
$count++;
72+
}
73+
}
74+
if (count($newSet) > 0) {
75+
$newCollection[$index] = $newSet;
76+
}
77+
}
78+
app('log')->debug(sprintf('Array contains %d line(s)', $count));
79+
return $newCollection;
5480
}
5581
}

app/Handlers/Events/ImportedTransactionsEventHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function sendReportOverMail(ImportedTransactions $event): void
4848
app('log')->info('No mail receiver configured, will not mail.');
4949
return;
5050
}
51+
if(false === config('mail.enable_mail_report')) {
52+
app('log')->info('Configuration does not allow mail, will not mail.');
53+
return;
54+
}
5155

5256
$log = [
5357
'messages' => $event->messages,
@@ -56,6 +60,9 @@ public function sendReportOverMail(ImportedTransactions $event): void
5660
];
5761
if (count($event->messages) > 0 || count($event->warnings) > 0 || count($event->errors) > 0) {
5862
app('log')->info('Will send report message.');
63+
app('log')->debug(sprintf('Messages count: %s', count($event->messages)));
64+
app('log')->debug(sprintf('Warnings count: %s', count($event->warnings)));
65+
app('log')->debug(sprintf('Errors count : %s', count($event->errors)));
5966
app('log')->debug('If no error below this line, mail was sent!');
6067
Mail::to(config('mail.destination'))->send(new ImportReportMail($log));
6168
app('log')->debug('If no error above this line, mail was sent!');

app/Http/Controllers/Import/Nordigen/LinkController.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace App\Http\Controllers\Import\Nordigen;
2727

2828
use App\Exceptions\ImporterErrorException;
29+
use App\Exceptions\ImporterHttpException;
2930
use App\Http\Controllers\Controller;
3031
use App\Http\Middleware\LinkControllerMiddleware;
3132
use App\Services\Nordigen\Request\GetRequisitionRequest;
@@ -90,22 +91,20 @@ public function build()
9091
return redirect(route('004-configure.index'));
9192
}
9293

93-
// create and save local reference:
94-
$uuid = Uuid::uuid4()->toString();
95-
94+
$uuid = (string) Uuid::uuid4()->toString();
9695
$url = config('nordigen.url');
9796
$accessToken = TokenManager::getAccessToken();
9897
$request = new PostNewRequisitionRequest($url, $accessToken);
9998
$request->setTimeOut(config('importer.connection.timeout'));
10099
$request->setBank($configuration->getNordigenBank());
101100
$request->setReference($uuid);
102101

103-
app('log')->debug(sprintf('Reference is %s', $uuid));
102+
app('log')->debug(sprintf('Reference is "%s"', $uuid));
104103

105104
/** @var NewRequisitionResponse $response */
106105
$response = $request->post();
107-
app('log')->debug(sprintf('Got a new requisition with id %s', $response->id));
108-
app('log')->debug(sprintf('Status: %s, returned reference: %s', $response->status, $response->reference));
106+
app('log')->debug(sprintf('Got a new requisition with id "%s"', $response->id));
107+
app('log')->debug(sprintf('Status: %s, returned reference: "%s"', $response->status, $response->reference));
109108
app('log')->debug(sprintf('Will now redirect the user to %s', $response->link));
110109

111110
// save config!
@@ -121,14 +120,19 @@ public function build()
121120
* @param Request $request
122121
* @return Application|RedirectResponse|Redirector
123122
* @throws ImporterErrorException
123+
* @throws ImporterHttpException
124124
* @throws ContainerExceptionInterface
125125
* @throws NotFoundExceptionInterface
126126
*/
127127
public function callback(Request $request)
128128
{
129-
$reference = $request->get('ref');
129+
$reference = (string) $request->get('ref');
130130
app('log')->debug(sprintf('Now at %s', __METHOD__));
131-
app('log')->debug(sprintf('Reference is %s', $reference));
131+
app('log')->debug(sprintf('Reference is "%s"', $reference));
132+
133+
if('' === $reference) {
134+
throw new ImporterHttpException('The reference returned by Nordigen was unexpectedly empty.');
135+
}
132136

133137
// create a new config thing
134138
$configuration = $this->restoreConfiguration();

app/Http/Controllers/TokenController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function callback(Request $request)
6868
$vanityURL = (string) $request->session()->pull('form_vanity_url');
6969
$code = $request->get('code');
7070

71-
if (0 === strlen($state) || $state !== $request->state) {
71+
if ($state !== $request->state) {
7272
app('log')->error(sprintf('State according to session: "%s"', $state));
7373
app('log')->error(sprintf('State returned in request : "%s"', $request->state));
7474
throw new ImporterErrorException('The "state" returned from your server doesn\'t match the state that was sent.');

app/Http/Middleware/IsReadyForStep.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ private function redirectToCorrectNordigenStep(): RedirectResponse
426426
return redirect($route);
427427
}
428428
throw new ImporterErrorException(sprintf('redirectToCorrectNordigenStep: Cannot handle Nordigen step "%s" [1]', self::STEP));
429+
case 'submit':
430+
$route = route('007-convert.index');
431+
app('log')->debug(sprintf('Return redirect to "%s"', $route));
432+
return redirect($route);
433+
429434
}
430435
}
431436

app/Services/CSV/Conversion/Routine/PseudoTransactionProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function processPseudo(array $lines): array
139139
foreach ($lines as $index => $line) {
140140
app('log')->info(sprintf('Now processing line %d/%d.', ($index + 1), $count));
141141
$processed[] = $this->processPseudoLine($line);
142-
$this->addMessage($index, sprintf('Converted CSV line %d into a transaction.', $index + 1));
142+
// $this->addMessage($index, sprintf('Converted CSV line %d into a transaction.', $index + 1));
143143
}
144144
app('log')->info(sprintf('Done converting %d lines into transactions.', $count));
145145

app/Services/Nordigen/Conversion/Routine/GenerateTransactions.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public function collectTargetAccounts(): void
9393
if (in_array($type, ['reconciliation', 'initial-balance', 'expense', 'revenue'], true)) {
9494
continue;
9595
}
96-
$iban = $this->filterSpaces((string) $entry->iban);
96+
$iban = $this->filterSpaces((string)$entry->iban);
9797

9898
if ('' !== $iban) {
9999
app('log')->debug(sprintf('Collected IBAN "%s" (%s) under ID #%d', $iban, $entry->type, $entry->id));
100100
$return[$iban] = $entry->id;
101101
$types[$iban] = $entry->type;
102102
}
103-
$number = sprintf('%s.', (string) $entry->number);
103+
$number = sprintf('%s.', (string)$entry->number);
104104
if ('.' !== $number) {
105105
app('log')->debug(sprintf('Collected account nr "%s" (%s) under ID #%d', $number, $entry->type, $entry->id));
106106
$return[$number] = $entry->id;
@@ -213,12 +213,14 @@ private function generateTransaction(string $accountId, Transaction $entry): arr
213213
'error_if_duplicate_hash' => $this->configuration->isIgnoreDuplicateTransactions(),
214214
'transactions' => [],
215215
];
216+
$valueDate = $entry->getValueDate();
216217
$transaction = [
217218
'type' => 'withdrawal',
218219
'date' => $entry->getDate()->format('Y-m-d'),
219220
'datetime' => $entry->getDate()->toW3cString(),
220221
'amount' => $entry->transactionAmount,
221222
'description' => $entry->getDescription(),
223+
'payment_date' => is_null($valueDate) ? '' : $valueDate->format('Y-m-d'),
222224
'order' => 0,
223225
'currency_code' => $entry->currencyCode,
224226
'tags' => [],
@@ -253,7 +255,7 @@ private function generateTransaction(string $accountId, Transaction $entry): arr
253255
private function getMappedAccountId(string $name): ?int
254256
{
255257
if (isset($this->configuration->getMapping()['accounts'][$name])) {
256-
return (int) $this->configuration->getMapping()['accounts'][$name];
258+
return (int)$this->configuration->getMapping()['accounts'][$name];
257259
}
258260

259261
return null;
@@ -345,9 +347,11 @@ public function setConfiguration(Configuration $configuration): void
345347

346348
/**
347349
* Handle transaction information when the amount is positive, and this is probably a deposit or a transfer.
350+
*
348351
* @param string $accountId
349352
* @param array $transaction
350353
* @param Transaction $entry
354+
*
351355
* @return array
352356
* @throws ImporterHttpException
353357
*/
@@ -358,7 +362,7 @@ private function appendPositiveAmountInfo(string $accountId, array $transaction,
358362
$transaction['amount'] = $entry->transactionAmount;
359363

360364
// destination is a Nordigen account (has to be!)
361-
$transaction['destination_id'] = (int) $this->accounts[$accountId];
365+
$transaction['destination_id'] = (int)$this->accounts[$accountId];
362366
app('log')->debug(sprintf('Destination ID is now #%d, which should be a Firefly III asset account.', $transaction['destination_id']));
363367

364368
// append source iban and number (if present)
@@ -390,22 +394,25 @@ private function appendPositiveAmountInfo(string $accountId, array $transaction,
390394
$transaction['source_name'] = $originalSourceName;
391395
}
392396
}
397+
393398
return $transaction;
394399
}
395400

396401
/**
397402
* Handle transaction information when the amount is negative, and this is probably a withdrawal or a transfer.
403+
*
398404
* @param string $accountId
399405
* @param array $transaction
400406
* @param Transaction $entry
407+
*
401408
* @return array
402409
* @throws ImporterHttpException
403410
*/
404411
private function appendNegativeAmountInfo(string $accountId, array $transaction, Transaction $entry): array
405412
{
406413

407414
$transaction['amount'] = bcmul($entry->transactionAmount, '-1');
408-
$transaction['source_id'] = (int) $this->accounts[$accountId]; // TODO entry may not exist, then what?
415+
$transaction['source_id'] = (int)$this->accounts[$accountId]; // TODO entry may not exist, then what?
409416
410417
// append source iban and number (if present)
411418
$transaction = $this->appendAccountFields($transaction, $entry, 'destination');
@@ -436,13 +443,15 @@ private function appendNegativeAmountInfo(string $accountId, array $transaction,
436443
$transaction['destination_name'] = $originalDestName;
437444
}
438445
}
446+
439447
return $transaction;
440448
}
441449

442450
/**
443451
* @param array $transaction
444452
* @param Transaction $entry
445453
* @param string $direction
454+
*
446455
* @return array
447456
*/
448457
private function appendAccountFields(array $transaction, Transaction $entry, string $direction): array
@@ -473,51 +482,53 @@ private function appendAccountFields(array $transaction, Transaction $entry, str
473482
$numberSearch = sprintf('%s.', $number);
474483

475484
// IBAN is also an ID, so use that!
476-
if ('' !== (string) $iban && array_key_exists((string) $iban, $this->targetAccounts)) {
485+
if ('' !== (string)$iban && array_key_exists((string)$iban, $this->targetAccounts)) {
477486
app('log')->debug(sprintf('Recognized %s (IBAN) as a Firefly III asset account so this is a transfer.', $iban));
478487
$transaction[$idKey] = $this->targetAccounts[$iban];
479488
$transaction['type'] = 'transfer';
480489
}
481490

482491
// IBAN is not an ID, so just submit it as a field.
483-
if ('' === (string) $iban || !array_key_exists((string) $iban, $this->targetAccounts)) {
492+
if ('' === (string)$iban || !array_key_exists((string)$iban, $this->targetAccounts)) {
484493
app('log')->debug(sprintf('"%s" is not a valid IBAN OR not recognized as Firefly III asset account so submitted as-is.', $iban));
485494
// source is the other side:
486495
$transaction[$nameKey] = $name ?? sprintf('(unknown %s account)', $direction);
487-
if ('' !== (string) $iban) {
496+
if ('' !== (string)$iban) {
488497
app('log')->debug(sprintf('Set field "%s" to "%s".', $ibanKey, $iban));
489498
$transaction[$ibanKey] = $iban;
490499
}
491-
if ('' === (string) $iban) {
500+
if ('' === (string)$iban) {
492501
app('log')->debug(sprintf('IBAN is "%s", so leave field "%s" empty.', $iban, $ibanKey));
493502
}
494503
}
495504

496505
// source is also an ID, so use it!
497-
if ('' !== (string) $number && '.' !== $numberSearch && array_key_exists($numberSearch, $this->targetAccounts)) {
506+
if ('' !== (string)$number && '.' !== $numberSearch && array_key_exists($numberSearch, $this->targetAccounts)) {
498507
app('log')->debug(sprintf('Recognized "%s" (number) as a Firefly III asset account so this is a transfer.', $number));
499508
$transaction[$idKey] = $this->targetAccounts[$numberSearch];
500509
$transaction['type'] = 'transfer';
501510
}
502511

503-
if ('' === (string) $number || '.' === $numberSearch || !array_key_exists($numberSearch, $this->targetAccounts)) {
512+
if ('' === (string)$number || '.' === $numberSearch || !array_key_exists($numberSearch, $this->targetAccounts)) {
504513
app('log')->debug(sprintf('"%s" is not a valid account nr OR not recognized as Firefly III asset account so submitted as-is.', $number));
505514
// source is the other side:
506515
$transaction[$nameKey] = $name ?? sprintf('(unknown %s account)', $direction);
507-
if ('' !== (string) $number) {
516+
if ('' !== (string)$number) {
508517
app('log')->debug(sprintf('Set field "%s" to "%s".', $numberKey, $number));
509518
$transaction[$numberKey] = $number;
510519
}
511-
if ('' === (string) $number) {
520+
if ('' === (string)$number) {
512521
app('log')->debug(sprintf('Account number is "%s", so leave field "%s" empty.', $number, $numberKey));
513522
}
514523
}
515524
app('log')->debug(sprintf('End of %s', __METHOD__));
525+
516526
return $transaction;
517527
}
518528

519529
/**
520530
* @param string $iban
531+
*
521532
* @return string
522533
*/
523534
private function filterSpaces(string $iban): string
@@ -570,6 +581,7 @@ private function filterSpaces(string $iban): string
570581
"\u{FEFF}", // zero width no -break space
571582
"\x20", // plain old normal space
572583
];
584+
573585
return str_replace($search, '', $iban);
574586
}
575587
}

app/Services/Nordigen/Conversion/Routine/TransactionProcessor.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
use App\Exceptions\ImporterErrorException;
2828
use App\Exceptions\ImporterHttpException;
29+
use App\Services\Nordigen\Model\Account;
2930
use App\Services\Nordigen\Request\GetTransactionsRequest;
3031
use App\Services\Nordigen\Response\GetTransactionsResponse;
32+
use App\Services\Nordigen\Services\AccountInformationCollector;
3133
use App\Services\Nordigen\TokenManager;
3234
use App\Services\Shared\Configuration\Configuration;
3335
use Carbon\Carbon;
@@ -65,8 +67,15 @@ public function download(): array
6567

6668
$return = [];
6769
foreach ($accounts as $key => $account) {
68-
app('log')->debug(sprintf('Going to download transactions for account #%d "%s"', $key, $account));
6970
$account = (string) $account;
71+
app('log')->debug(sprintf('Going to download transactions for account #%d "%s"', $key, $account));
72+
73+
app('log')->debug(sprintf('Will also download information on the account for debug purposes.'));
74+
$object = new Account();
75+
$object->setIdentifier($account);
76+
AccountInformationCollector::collectInformation($object);
77+
app('log')->debug(sprintf('Done downloading information for debug purposes.'));
78+
7079
$accessToken = TokenManager::getAccessToken();
7180
$url = config('nordigen.url');
7281
$request = new GetTransactionsRequest($url, $accessToken, $account);

app/Services/Nordigen/Model/Transaction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ public function getDate(): Carbon
246246
return new Carbon(config('app.timezone'));
247247
}
248248

249+
/**
250+
* @return Carbon|null
251+
*/
252+
public function getValueDate(): ?Carbon
253+
{
254+
if (null !== $this->valueDate) {
255+
app('log')->debug('Returning value date for getValueDate');
256+
return $this->valueDate;
257+
}
258+
app('log')->warning('Transaction has no valueDate, return NULL.');
259+
return null;
260+
}
261+
249262
/**
250263
* Return transaction description, which depends on the values in the object:
251264
* @return string

0 commit comments

Comments
 (0)