Fix issue with opposing account creation

This commit is contained in:
James Cole 2023-04-01 07:02:51 +02:00
parent 8ea32714f6
commit 259bad4487
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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'],