mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 21:23:36 +00:00
Expand import routine.
This commit is contained in:
@@ -16,6 +16,7 @@ use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -36,6 +37,8 @@ class ImportAccount
|
||||
private $accountName = [];
|
||||
/** @var array */
|
||||
private $accountNumber = [];
|
||||
/** @var string */
|
||||
private $expectedType = '';
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var User */
|
||||
@@ -46,77 +49,10 @@ class ImportAccount
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->account = new Account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
$this->expectedType = AccountType::ASSET;
|
||||
$this->account = new Account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
Log::debug('Created ImportAccount.');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function convertToExpense(): bool
|
||||
{
|
||||
if ($this->getAccount()->accountType->type === AccountType::EXPENSE) {
|
||||
return true;
|
||||
}
|
||||
// maybe that an account of expense account type already exists?
|
||||
$expenseType = AccountType::whereType(AccountType::EXPENSE)->first();
|
||||
$this->account->account_type_id = $expenseType->id;
|
||||
$this->account->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function convertToRevenue(): bool
|
||||
{
|
||||
if ($this->getAccount()->accountType->type === AccountType::REVENUE) {
|
||||
return true;
|
||||
}
|
||||
// maybe that an account of revenue account type already exists?
|
||||
$revenueType = AccountType::whereType(AccountType::REVENUE)->first();
|
||||
$this->account->account_type_id = $revenueType->id;
|
||||
$this->account->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function createAccount(): Account
|
||||
{
|
||||
if (!is_null($this->account->id)) {
|
||||
return $this->account;
|
||||
}
|
||||
Log::debug('In createAccount()');
|
||||
// check if any of them is mapped:
|
||||
$mapped = $this->findMappedObject();
|
||||
|
||||
if (is_null($mapped->id)) {
|
||||
// none are, create new object!
|
||||
$data = [
|
||||
'accountType' => 'import',
|
||||
'name' => $this->accountName['value'] ?? '(no name)',
|
||||
'iban' => $this->accountIban['value'] ?? null,
|
||||
'active' => true,
|
||||
'virtualBalance' => null,
|
||||
];
|
||||
if (!is_null($data['iban']) && $data['name'] === '(no name)') {
|
||||
$data['name'] = $data['iban'];
|
||||
}
|
||||
Log::debug('Search for maps resulted in nothing, create new one based on', $data);
|
||||
$account = $this->repository->store($data);
|
||||
$this->account = $account;
|
||||
Log::info('Made new account.', ['input' => $data, 'new' => $account->toArray()]);
|
||||
|
||||
|
||||
return $account;
|
||||
}
|
||||
Log::debug('Mapped existing account.', ['new' => $mapped->toArray()]);
|
||||
$this->account = $mapped;
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,6 +60,10 @@ class ImportAccount
|
||||
*/
|
||||
public function getAccount(): Account
|
||||
{
|
||||
if (is_null($this->account->id)) {
|
||||
$this->store();
|
||||
}
|
||||
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
@@ -159,6 +99,14 @@ class ImportAccount
|
||||
$this->accountNumber = $accountNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $expectedType
|
||||
*/
|
||||
public function setExpectedType(string $expectedType)
|
||||
{
|
||||
$this->expectedType = $expectedType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
@@ -168,12 +116,88 @@ class ImportAccount
|
||||
$this->repository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findExistingObject(): Account
|
||||
{
|
||||
Log::debug('In findExistingObject() for Account');
|
||||
// 0: determin account type:
|
||||
/** @var AccountType $accountType */
|
||||
$accountType = AccountType::whereType($this->expectedType)->first();
|
||||
|
||||
// 1: find by ID, iban or name (and type)
|
||||
if (count($this->accountId) === 3) {
|
||||
Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value']));
|
||||
/** @var Account $account */
|
||||
$account = $this->user->accounts()->where('account_type_id', $accountType->id)->where('id', $this->accountId['value'])->first();
|
||||
if (!is_null($account)) {
|
||||
Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
/** @var Collection $accounts */
|
||||
$accounts = $this->repository->getAccountsByType([$accountType->type]);
|
||||
// 2: find by IBAN (and type):
|
||||
if (count($this->accountIban) === 3) {
|
||||
$iban = $this->accountIban['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban));
|
||||
$filtered = $accounts->filter(
|
||||
function (Account $account) use ($iban) {
|
||||
if ($account->iban === $iban) {
|
||||
Log::debug(
|
||||
sprintf('Found unmapped %s account by IBAN (#%d): %s (%s)', $this->expectedType, $account->id, $account->name, $account->iban)
|
||||
);
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
);
|
||||
if ($filtered->count() === 1) {
|
||||
return $filtered->first();
|
||||
}
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
|
||||
// 3: find by name (and type):
|
||||
if (count($this->accountName) === 3) {
|
||||
$name = $this->accountName['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name));
|
||||
$filtered = $accounts->filter(
|
||||
function (Account $account) use ($name) {
|
||||
if ($account->name === $name) {
|
||||
Log::debug(sprintf('Found unmapped %s account by name (#%d): %s', $this->expectedType, $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
);
|
||||
|
||||
if ($filtered->count() === 1) {
|
||||
return $filtered->first();
|
||||
}
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
|
||||
// 4: do not search by account number.
|
||||
Log::debug('Found NO existing accounts.');
|
||||
|
||||
return new Account;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findMappedObject(): Account
|
||||
{
|
||||
Log::debug('In findMappedObject()');
|
||||
Log::debug('In findMappedObject() for Account');
|
||||
$fields = ['accountId', 'accountIban', 'accountNumber', 'accountName'];
|
||||
foreach ($fields as $field) {
|
||||
$array = $this->$field;
|
||||
@@ -199,7 +223,7 @@ class ImportAccount
|
||||
*/
|
||||
private function getMappedObject(array $array): Account
|
||||
{
|
||||
Log::debug('In getMappedObject()');
|
||||
Log::debug('In getMappedObject() for Account');
|
||||
if (count($array) === 0) {
|
||||
Log::debug('Array is empty, nothing will come of this.');
|
||||
|
||||
@@ -212,7 +236,7 @@ class ImportAccount
|
||||
return new Account;
|
||||
}
|
||||
|
||||
Log::debug('Finding a mapped object based on', $array);
|
||||
Log::debug('Finding a mapped account based on', $array);
|
||||
|
||||
$search = intval($array['mapped']);
|
||||
$account = $this->repository->find($search);
|
||||
@@ -222,5 +246,54 @@ class ImportAccount
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function store(): bool
|
||||
{
|
||||
// 1: find mapped object:
|
||||
$mapped = $this->findMappedObject();
|
||||
if (!is_null($mapped->id)) {
|
||||
$this->account = $mapped;
|
||||
|
||||
return true;
|
||||
}
|
||||
// 2: find existing by given values:
|
||||
$found = $this->findExistingObject();
|
||||
if (!is_null($found->id)) {
|
||||
$this->account = $found;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 3: if found nothing, retry the search with an asset account:
|
||||
Log::debug('Will try to find an asset account just in case.');
|
||||
$oldExpectedType = $this->expectedType;
|
||||
$this->expectedType = AccountType::ASSET;
|
||||
$found = $this->findExistingObject();
|
||||
if (!is_null($found->id)) {
|
||||
Log::debug('Found asset account!');
|
||||
$this->account = $found;
|
||||
|
||||
return true;
|
||||
}
|
||||
$this->expectedType = $oldExpectedType;
|
||||
|
||||
Log::debug(sprintf('Found no account of type %s so must create one ourselves.', $this->expectedType));
|
||||
|
||||
$data = [
|
||||
'accountType' => config('firefly.shortNamesByFullName.' . $this->expectedType),
|
||||
'name' => $this->accountName['value'] ?? '(no name)',
|
||||
'iban' => $this->accountIban['value'] ?? null,
|
||||
'active' => true,
|
||||
'virtualBalance' => null,
|
||||
];
|
||||
|
||||
$this->account = $this->repository->store($data);
|
||||
Log::debug(sprintf('Successfully stored new account #%d: %s', $this->account->id, $this->account->name));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user