mapped[$this->index][$this->value])) { $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); return $account; } // if not, search for it (or create it): $value = $this->value ?? ''; if (strlen($value) > 0) { // find or create new account: $account = $this->findAccount(); if (is_null($account->id)) { // create it if doesn't exist. $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); $accountData = [ 'name' => $this->value, 'accountType' => 'asset', 'virtualBalance' => 0, 'virtualBalanceCurrency' => 1, // hard coded. 'active' => true, 'user' => Auth::user()->id, 'iban' => null, 'accountNumber' => $this->value, 'accountRole' => null, 'openingBalance' => 0, 'openingBalanceDate' => new Carbon, 'openingBalanceCurrency' => 1, // hard coded. ]; $account = $repository->store($accountData); } return $account; } return null; } /** * @return Account */ protected function findAccount(): Account { $set = Auth::user()->accounts()->with(['accountmeta'])->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']); /** @var Account $entry */ foreach ($set as $entry) { $accountNumber = $entry->getMeta('accountNumber'); if ($accountNumber == $this->value) { return $entry; } } return new Account; } }