Skip to content

Commit 0246857

Browse files
committed
1 parent 13379fa commit 0246857

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

app/Services/CSV/Conversion/Task/Accounts.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ private function processTransaction(array $transaction): array
8686
$transaction = $this->setSource($transaction, $source);
8787
$transaction = $this->setDestination($transaction, $destination);
8888
$transaction['type'] = $this->determineType($source['type'], $destination['type']);
89+
Log::debug(sprintf('Transaction type is set to "%s"', $transaction['type']));
8990

90-
$amount = (string) $transaction['amount'];
91+
$amount = (string)$transaction['amount'];
9192
$amount = '' === $amount ? '0' : $amount;
9293

9394
if ('0' === $amount) {
@@ -97,18 +98,26 @@ private function processTransaction(array $transaction): array
9798
/*
9899
* If the amount is positive, the transaction is a deposit. We switch Source
99100
* and Destination and see if we can still handle the transaction, but only if the transaction
100-
* isn't already a deposit
101+
* isn't already a deposit.
102+
*
103+
*
101104
*/
102105
if ('deposit' !== $transaction['type'] && 1 === bccomp($amount, '0')) {
103106
// amount is positive
104-
Log::debug(sprintf('%s is positive.', $amount));
107+
Log::debug(sprintf('%s is positive and type is "%s".', $amount, $transaction['type']));
105108
$transaction = $this->setSource($transaction, $destination);
106109
$transaction = $this->setDestination($transaction, $source);
107110
$transaction['type'] = $this->determineType($destination['type'], $source['type']);
108111
}
112+
109113
if ('deposit' === $transaction['type'] && 1 === bccomp($amount, '0')) {
110114
Log::debug('Transaction is a deposit, and amount is positive. Will not change account types.');
111115
}
116+
if ('transfer' === $transaction['type'] && 1 === bccomp($amount, '0')) {
117+
Log::debug('Transaction is a transfer, and amount is positive, must reverse accounts again.');
118+
$transaction = $this->setSource($transaction, $source);
119+
$transaction = $this->setDestination($transaction, $destination);
120+
}
112121

113122
/*
114123
* Final check. If the type is "withdrawal" but the destination account found is "revenue"
@@ -212,7 +221,7 @@ private function findAccount(array $array, ?Account $defaultAccount): array
212221
// if the ID is set, at least search for the ID.
213222
if (is_int($array['id']) && $array['id'] > 0) {
214223
Log::debug('Find by ID field.');
215-
$result = $this->findById((string) $array['id']);
224+
$result = $this->findById((string)$array['id']);
216225
}
217226
if (null !== $result) {
218227
$return = $result->toArray();
@@ -222,10 +231,10 @@ private function findAccount(array $array, ?Account $defaultAccount): array
222231
}
223232

224233
// if the IBAN is set, search for the IBAN.
225-
if (isset($array['iban']) && '' !== (string) $array['iban']) {
234+
if (isset($array['iban']) && '' !== (string)$array['iban']) {
226235
Log::debug('Find by IBAN.');
227-
$transactionType = (string) ($array['transaction_type'] ?? null);
228-
$result = $this->findByIban((string) $array['iban'], $transactionType);
236+
$transactionType = (string)($array['transaction_type'] ?? null);
237+
$result = $this->findByIban((string)$array['iban'], $transactionType);
229238
}
230239
if (null !== $result) {
231240
$return = $result->toArray();
@@ -239,9 +248,9 @@ private function findAccount(array $array, ?Account $defaultAccount): array
239248

240249

241250
// find by name, return only if it's an asset or liability account.
242-
if (isset($array['name']) && '' !== (string) $array['name']) {
251+
if (isset($array['name']) && '' !== (string)$array['name']) {
243252
Log::debug('Find by name.');
244-
$result = $this->findByName((string) $array['name']);
253+
$result = $this->findByName((string)$array['name']);
245254
}
246255
if (null !== $result) {
247256
$return = $result->toArray();
@@ -257,14 +266,14 @@ private function findAccount(array $array, ?Account $defaultAccount): array
257266
$array['bic'] = $array['bic'] ?? null;
258267

259268
// Return ID or name if not null
260-
if (null !== $array['id'] || '' !== (string) $array['name']) {
269+
if (null !== $array['id'] || '' !== (string)$array['name']) {
261270
Log::debug('Array with account has some name info, return that.', $array);
262271

263272
return $array;
264273
}
265274

266275
// Return ID or IBAN if not null
267-
if (null !== $array['id'] || '' !== (string) $array['iban']) {
276+
if (null !== $array['id'] || '' !== (string)$array['iban']) {
268277
Log::debug('Array with account has some IBAN info, return that.', $array);
269278

270279
return $array;
@@ -385,6 +394,7 @@ private function findByIban(string $iban, string $transactionType): ?Account
385394

386395
if (2 === count($response)) {
387396
Log::debug('Found 2 results, Firefly III will have to make the correct decision.');
397+
388398
return null;
389399
}
390400
Log::debug(sprintf('Found %d result(s), Firefly III will have to make the correct decision.', count($response)));
@@ -421,7 +431,8 @@ private function findByName(string $name): ?Account
421431
}
422432
/** @var Account $account */
423433
foreach ($response as $account) {
424-
if (in_array($account->type, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true) && strtolower($account->name) === strtolower($name)) {
434+
if (in_array($account->type, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true)
435+
&& strtolower($account->name) === strtolower($name)) {
425436
Log::debug(sprintf('[b] Found "%s" account #%d based on name "%s"', $account->type, $account->id, $name));
426437

427438
return $account;

app/Services/CSV/File/FileReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function getReaderFromContent(string $content, bool $convert = fal
6464
{
6565
if (true === $convert) {
6666
$encoding = mb_detect_encoding($content, config('importer.encoding'), true);
67-
if (false !== $encoding && 'ASCII' !== $encoding) {
67+
if (false !== $encoding && 'ASCII' !== $encoding && 'UTF-8' !== $encoding) {
6868
Log::warning(sprintf('Content is detected as "%s" and will be converted to UTF-8. Your milage may vary.', $encoding));
6969
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
7070
}

app/Services/Storage/StorageService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static function getContent(string $name, bool $convert = false): string
100100
Log::warning('Tried to detect encoding but could not find valid encoding. Assume UTF-8.');
101101
return $content;
102102
}
103-
if ('ASCII' === $encoding) {
103+
if ('ASCII' === $encoding || 'UTF-8' === $encoding) {
104104
return $content;
105105
}
106106
Log::warning(sprintf('Content is detected as "%s" and will be converted to UTF-8. Your milage may vary.', $encoding));

0 commit comments

Comments
 (0)