Skip to content

Firefly III: a personal finances manager

Fix issue with opposing account creation

James Cole259bad448741
diff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.php
index a57d16487f..70a9668b0c 100644
--- a/app/Services/Internal/Support/JournalServiceTrait.php
+++ b/app/Services/Internal/Support/JournalServiceTrait.php
@@ -35,7 +35,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\NullArrayObject;
-use Log;
+use Illuminate\Support\Facades\Log;
/**
* Trait JournalServiceTrait
@@ -86,9 +86,14 @@ trait JournalServiceTrait
Log::debug(sprintf('Search again using the new name, "%s".', $data['name']));
$result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]);
}
+
+ // the account that Firefly III creates must be "creatable", aka select the one we can create from the list just in case
+ $creatableType = $this->getCreatableType($expectedTypes[$transactionType]);
+
+
// if the result is NULL but the ID is set, an account could exist of the wrong type.
// that data can be used to create a new account of the right type.
- if (null === $result && null !== $data['id']) {
+ if (null === $result && null !== $data['id'] && null !== $creatableType) {
Log::debug(sprintf('Account #%d may exist and be of the wrong type, use data to create one of the right type.', $data['id']));
$temp = $this->findAccountById(['id' => $data['id']], []);
if (null !== $temp) {
@@ -98,12 +103,12 @@ trait JournalServiceTrait
'number' => null,
'bic' => null,
];
- $result = $this->createAccount(null, $tempData, $expectedTypes[$transactionType][0]);
+ $result = $this->createAccount(null, $tempData, $creatableType);
}
}
- if (null === $result) {
+ if (null === $result && null !== $creatableType) {
Log::debug('If nothing is found, create it.');
- $result = $this->createAccount($result, $data, $expectedTypes[$transactionType][0]);
+ $result = $this->createAccount($result, $data, $creatableType);
}
if (null === $result) {
Log::debug('If cant be created, return cash account.');
@@ -237,6 +242,24 @@ trait JournalServiceTrait
return null;
}
+ /**
+ * @param array $types
+ * @return null|string
+ */
+ private function getCreatableType(array $types): ?string
+ {
+ $result = null;
+ $list = config('firefly.dynamic_creation_allowed');
+ /** @var string $type */
+ foreach ($types as $type) {
+ if (true === in_array($type, $list, true)) {
+ $result = $type;
+ break;
+ }
+ }
+ return $result;
+ }
+
/**
* @param Account|null $account
* @param array $data
diff --git a/config/firefly.php b/config/firefly.php
index 442db08ccc..e5e44b3822 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -877,6 +877,7 @@ return [
],
'can_have_virtual_amounts' => [AccountType::ASSET],
'can_have_opening_balance' => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
+ 'dynamic_creation_allowed' => [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::RECONCILIATION],
'valid_asset_fields' => ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'],
'valid_cc_fields' => ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'],
'valid_account_fields' => ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth', 'liability_direction'],