2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* AccountRepository.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 01:57:45 -05:00
|
|
|
|
2015-02-09 00:23:39 -06:00
|
|
|
namespace FireflyIII\Repositories\Account;
|
|
|
|
|
2015-02-21 05:16:41 -06:00
|
|
|
use Carbon\Carbon;
|
2018-07-23 14:49:15 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-02-21 14:06:59 -06:00
|
|
|
use FireflyIII\Factory\AccountFactory;
|
2015-02-09 00:23:39 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2019-08-24 00:56:08 -05:00
|
|
|
use FireflyIII\Models\AccountMeta;
|
2016-10-10 01:03:03 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2020-05-06 23:44:01 -05:00
|
|
|
use FireflyIII\Models\Attachment;
|
2019-12-30 05:12:08 -06:00
|
|
|
use FireflyIII\Models\Location;
|
2018-12-15 00:59:49 -06:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2019-06-16 06:16:04 -05:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
2016-10-10 01:03:03 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-02-21 11:42:15 -06:00
|
|
|
use FireflyIII\Services\Internal\Destroy\AccountDestroyService;
|
2018-02-21 13:34:24 -06:00
|
|
|
use FireflyIII\Services\Internal\Update\AccountUpdateService;
|
2024-05-17 23:42:09 -05:00
|
|
|
use FireflyIII\Support\Facades\Steam;
|
2016-03-20 05:47:10 -05:00
|
|
|
use FireflyIII\User;
|
2023-02-19 01:43:28 -06:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2021-03-07 09:19:14 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2018-07-24 13:49:25 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2018-07-23 14:49:15 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-09 00:23:39 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AccountRepository.
|
2015-02-09 00:23:39 -06:00
|
|
|
*/
|
|
|
|
class AccountRepository implements AccountRepositoryInterface
|
|
|
|
{
|
2020-08-20 10:59:44 -05:00
|
|
|
private User $user;
|
2019-08-24 00:56:08 -05:00
|
|
|
|
2016-10-09 01:20:29 -05:00
|
|
|
/**
|
|
|
|
* Moved here from account CRUD.
|
|
|
|
*/
|
2018-02-23 09:21:28 -06:00
|
|
|
public function destroy(Account $account, ?Account $moveTo): bool
|
2016-10-09 01:20:29 -05:00
|
|
|
{
|
2018-02-21 11:42:15 -06:00
|
|
|
/** @var AccountDestroyService $service */
|
|
|
|
$service = app(AccountDestroyService::class);
|
|
|
|
$service->destroy($account, $moveTo);
|
2016-10-09 01:20:29 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:35:41 -05:00
|
|
|
/**
|
|
|
|
* Find account with same name OR same IBAN or both, but not the same type or ID.
|
|
|
|
*/
|
|
|
|
public function expandWithDoubles(Collection $accounts): Collection
|
|
|
|
{
|
2022-10-30 08:24:28 -05:00
|
|
|
$result = new Collection();
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-09-03 15:35:41 -05:00
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$byName = $this->user->accounts()->where('name', $account->name)
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('id', '!=', $account->id)->first()
|
|
|
|
;
|
2019-09-03 15:35:41 -05:00
|
|
|
if (null !== $byName) {
|
|
|
|
$result->push($account);
|
|
|
|
$result->push($byName);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-09-03 15:35:41 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (null !== $account->iban) {
|
|
|
|
$byIban = $this->user->accounts()->where('iban', $account->iban)
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('id', '!=', $account->id)->first()
|
|
|
|
;
|
2019-09-03 15:35:41 -05:00
|
|
|
if (null !== $byIban) {
|
|
|
|
$result->push($account);
|
|
|
|
$result->push($byIban);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-09-03 15:35:41 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result->push($account);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
public function findByAccountNumber(string $number, array $types): ?Account
|
|
|
|
{
|
|
|
|
$dbQuery = $this->user
|
|
|
|
->accounts()
|
|
|
|
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
|
|
|
->where('accounts.active', true)
|
|
|
|
->where(
|
2023-12-20 22:07:26 -06:00
|
|
|
static function (EloquentBuilder $q1) use ($number): void { // @phpstan-ignore-line
|
2021-09-18 03:26:12 -05:00
|
|
|
$json = json_encode($number);
|
|
|
|
$q1->where('account_meta.name', '=', 'account_number');
|
|
|
|
$q1->where('account_meta.data', '=', $json);
|
|
|
|
}
|
2024-01-04 04:41:03 -06:00
|
|
|
)
|
|
|
|
;
|
2021-09-18 03:26:12 -05:00
|
|
|
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2021-09-18 03:26:12 -05:00
|
|
|
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
|
|
|
$dbQuery->whereIn('account_types.type', $types);
|
|
|
|
}
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
// @var Account|null
|
2021-09-18 03:26:12 -05:00
|
|
|
return $dbQuery->first(['accounts.*']);
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
public function findByIbanNull(string $iban, array $types): ?Account
|
|
|
|
{
|
2024-05-17 23:49:29 -05:00
|
|
|
$iban = Steam::filterSpaces($iban);
|
2018-07-23 14:49:15 -05:00
|
|
|
$query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban');
|
|
|
|
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
|
|
|
$query->whereIn('account_types.type', $types);
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
// @var Account|null
|
2021-06-29 23:17:38 -05:00
|
|
|
return $query->where('iban', $iban)->first(['accounts.*']);
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function findByName(string $name, array $types): ?Account
|
|
|
|
{
|
2024-01-04 04:41:03 -06:00
|
|
|
$query = $this->user->accounts();
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
|
|
|
$query->whereIn('account_types.type', $types);
|
|
|
|
}
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2021-07-10 00:29:39 -05:00
|
|
|
$query->where('accounts.name', $name);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
/** @var null|Account $account */
|
2021-07-10 00:29:39 -05:00
|
|
|
$account = $query->first(['accounts.*']);
|
|
|
|
if (null === $account) {
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('There is no account with name "%s" of types', $name), $types);
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2021-07-10 00:29:39 -05:00
|
|
|
return null;
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2021-07-10 00:29:39 -05:00
|
|
|
return $account;
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
/**
|
|
|
|
* Return account type or null if not found.
|
|
|
|
*/
|
|
|
|
public function getAccountTypeByType(string $type): ?AccountType
|
|
|
|
{
|
2021-04-05 03:56:08 -05:00
|
|
|
return AccountType::whereType(ucfirst($type))->first();
|
2018-08-04 10:30:47 -05:00
|
|
|
}
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
public function getAccountsById(array $accountIds): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user->accounts();
|
|
|
|
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($accountIds)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->whereIn('accounts.id', $accountIds);
|
|
|
|
}
|
2020-07-24 09:41:58 -05:00
|
|
|
$query->orderBy('accounts.order', 'ASC');
|
2019-06-22 22:53:01 -05:00
|
|
|
$query->orderBy('accounts.active', 'DESC');
|
2019-06-10 13:14:00 -05:00
|
|
|
$query->orderBy('accounts.name', 'ASC');
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2020-07-24 09:41:58 -05:00
|
|
|
return $query->get(['accounts.*']);
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getActiveAccountsByType(array $types): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user->accounts()->with(
|
2022-12-29 12:42:26 -06:00
|
|
|
[
|
2023-12-20 22:07:26 -06:00
|
|
|
'accountmeta' => static function (HasMany $query): void {
|
2022-12-29 12:42:26 -06:00
|
|
|
$query->where('name', 'account_role');
|
|
|
|
},
|
|
|
|
'attachments',
|
|
|
|
]
|
2018-07-23 14:49:15 -05:00
|
|
|
);
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
2021-05-01 13:04:58 -05:00
|
|
|
$query->where('active', true);
|
2019-06-10 13:14:00 -05:00
|
|
|
$query->orderBy('accounts.account_type_id', 'ASC');
|
2020-07-24 09:41:58 -05:00
|
|
|
$query->orderBy('accounts.order', 'ASC');
|
2019-06-10 13:14:00 -05:00
|
|
|
$query->orderBy('accounts.name', 'ASC');
|
2018-07-23 14:49:15 -05:00
|
|
|
|
2019-10-19 12:17:14 -05:00
|
|
|
return $query->get(['accounts.*']);
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getAttachments(Account $account): Collection
|
|
|
|
{
|
2024-01-04 04:41:03 -06:00
|
|
|
$set = $account->attachments()->get();
|
2021-03-11 23:20:01 -06:00
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var \Storage $disk */
|
|
|
|
$disk = \Storage::disk('upload');
|
2021-03-11 23:20:01 -06:00
|
|
|
|
|
|
|
return $set->each(
|
|
|
|
static function (Attachment $attachment) use ($disk) {
|
|
|
|
$notes = $attachment->notes()->first();
|
|
|
|
$attachment->file_exists = $disk->exists($attachment->fileName());
|
2023-11-05 02:40:45 -06:00
|
|
|
$attachment->notes_text = null !== $notes ? $notes->text : '';
|
2021-03-11 23:20:01 -06:00
|
|
|
|
|
|
|
return $attachment;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/**
|
|
|
|
* @throws FireflyException
|
2023-12-22 13:12:38 -06:00
|
|
|
*/
|
2018-07-23 14:49:15 -05:00
|
|
|
public function getCashAccount(): Account
|
|
|
|
{
|
|
|
|
/** @var AccountType $type */
|
2024-01-04 04:41:03 -06:00
|
|
|
$type = AccountType::where('type', AccountType::CASH)->first();
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/** @var AccountFactory $factory */
|
|
|
|
$factory = app(AccountFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
|
|
|
|
return $factory->findOrCreate('Cash account', $type->type);
|
|
|
|
}
|
|
|
|
|
2024-01-04 04:41:03 -06:00
|
|
|
public function setUser(null|Authenticatable|User $user): void
|
2023-06-21 05:34:58 -05:00
|
|
|
{
|
2023-10-30 13:49:40 -05:00
|
|
|
if ($user instanceof User) {
|
2023-06-21 05:34:58 -05:00
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
public function getCreditTransactionGroup(Account $account): ?TransactionGroup
|
|
|
|
{
|
2022-10-30 08:24:28 -05:00
|
|
|
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('transactions.account_id', $account->id)
|
|
|
|
->transactionTypes([TransactionType::LIABILITY_CREDIT])
|
|
|
|
->first(['transaction_journals.*'])
|
|
|
|
;
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2023-06-03 23:30:22 -05:00
|
|
|
return $journal?->transactionGroup;
|
2021-09-18 03:26:12 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getInactiveAccountsByType(array $types): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user->accounts()->with(
|
2022-12-29 12:42:26 -06:00
|
|
|
[
|
2023-12-20 22:07:26 -06:00
|
|
|
'accountmeta' => static function (HasMany $query): void {
|
2022-12-29 12:42:26 -06:00
|
|
|
$query->where('name', 'account_role');
|
|
|
|
},
|
|
|
|
]
|
2021-03-11 23:20:01 -06:00
|
|
|
);
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2021-03-11 23:20:01 -06:00
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
|
|
|
$query->where('active', 0);
|
|
|
|
$query->orderBy('accounts.account_type_id', 'ASC');
|
|
|
|
$query->orderBy('accounts.order', 'ASC');
|
|
|
|
$query->orderBy('accounts.name', 'ASC');
|
|
|
|
|
|
|
|
return $query->get(['accounts.*']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLocation(Account $account): ?Location
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
// @var Location|null
|
2021-03-11 23:20:01 -06:00
|
|
|
return $account->locations()->first();
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:39:02 -05:00
|
|
|
/**
|
|
|
|
* Get note text or null.
|
|
|
|
*/
|
|
|
|
public function getNoteText(Account $account): ?string
|
|
|
|
{
|
2023-06-03 23:30:22 -05:00
|
|
|
return $account->notes()->first()?->text;
|
2018-03-19 13:39:02 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 15:14:53 -06:00
|
|
|
/**
|
|
|
|
* Returns the amount of the opening balance for this account.
|
|
|
|
*/
|
|
|
|
public function getOpeningBalanceAmount(Account $account): ?string
|
|
|
|
{
|
2024-01-04 04:41:03 -06:00
|
|
|
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transactions.account_id', $account->id)
|
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE, TransactionType::LIABILITY_CREDIT])
|
|
|
|
->first(['transaction_journals.*'])
|
|
|
|
;
|
2018-02-16 15:14:53 -06:00
|
|
|
if (null === $journal) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
|
|
|
if (null === $transaction) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-11-05 12:41:37 -06:00
|
|
|
return $transaction->amount;
|
2018-02-16 15:14:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return date of opening balance as string or null.
|
|
|
|
*/
|
|
|
|
public function getOpeningBalanceDate(Account $account): ?string
|
|
|
|
{
|
2023-06-03 23:30:22 -05:00
|
|
|
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('transactions.account_id', $account->id)
|
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE, TransactionType::LIABILITY_CREDIT])
|
|
|
|
->first(['transaction_journals.*'])?->date->format('Y-m-d H:i:s')
|
|
|
|
;
|
2018-02-16 15:14:53 -06:00
|
|
|
}
|
|
|
|
|
2019-08-24 00:56:08 -05:00
|
|
|
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup
|
|
|
|
{
|
|
|
|
$journal = $this->getOpeningBalance($account);
|
|
|
|
|
2021-12-17 10:27:01 -06:00
|
|
|
return $journal?->transactionGroup;
|
2019-08-24 00:56:08 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
public function getOpeningBalance(Account $account): ?TransactionJournal
|
|
|
|
{
|
|
|
|
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('transactions.account_id', $account->id)
|
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE])
|
|
|
|
->first(['transaction_journals.*'])
|
|
|
|
;
|
2023-06-21 05:34:58 -05:00
|
|
|
}
|
|
|
|
|
2018-12-09 06:09:43 -06:00
|
|
|
public function getPiggyBanks(Account $account): Collection
|
|
|
|
{
|
|
|
|
return $account->piggyBanks()->get();
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/**
|
|
|
|
* @throws FireflyException
|
2023-12-22 13:12:38 -06:00
|
|
|
*/
|
2018-07-23 14:49:15 -05:00
|
|
|
public function getReconciliation(Account $account): ?Account
|
|
|
|
{
|
|
|
|
if (AccountType::ASSET !== $account->accountType->type) {
|
|
|
|
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
|
|
|
|
}
|
2020-05-05 09:02:06 -05:00
|
|
|
$currency = $this->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
|
2020-08-09 01:56:15 -05:00
|
|
|
$name = trans('firefly.reconciliation_account_name', ['name' => $account->name, 'currency' => $currency->code]);
|
2020-02-29 06:58:34 -06:00
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/** @var AccountType $type */
|
2024-01-04 04:41:03 -06:00
|
|
|
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
|
2023-11-04 00:52:40 -05:00
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|Account $current */
|
2024-01-04 04:41:03 -06:00
|
|
|
$current = $this->user->accounts()->where('account_type_id', $type->id)
|
|
|
|
->where('name', $name)
|
|
|
|
->first()
|
|
|
|
;
|
2019-08-03 12:17:59 -05:00
|
|
|
|
2020-05-05 09:02:06 -05:00
|
|
|
if (null !== $current) {
|
|
|
|
return $current;
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
2020-05-05 09:02:06 -05:00
|
|
|
|
2024-01-04 04:41:03 -06:00
|
|
|
$data = [
|
2021-04-05 14:52:55 -05:00
|
|
|
'account_type_id' => null,
|
|
|
|
'account_type_name' => AccountType::RECONCILIATION,
|
|
|
|
'active' => true,
|
|
|
|
'name' => $name,
|
|
|
|
'currency_id' => $currency->id,
|
|
|
|
'currency_code' => $currency->code,
|
2020-05-05 09:02:06 -05:00
|
|
|
];
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/** @var AccountFactory $factory */
|
2024-01-04 04:41:03 -06:00
|
|
|
$factory = app(AccountFactory::class);
|
2018-07-23 14:49:15 -05:00
|
|
|
$factory->setUser($account->user);
|
2020-05-05 09:02:06 -05:00
|
|
|
|
2020-05-05 13:32:04 -05:00
|
|
|
return $factory->create($data);
|
2018-07-23 14:49:15 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
|
|
|
{
|
2024-01-04 04:41:03 -06:00
|
|
|
$type = $account->accountType->type;
|
|
|
|
$list = config('firefly.valid_currency_account_types');
|
2023-06-21 05:34:58 -05:00
|
|
|
|
|
|
|
// return null if not in this list.
|
|
|
|
if (!in_array($type, $list, true)) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-02-22 13:11:09 -06:00
|
|
|
$currencyId = (int)$this->getMetaValue($account, 'currency_id');
|
2023-06-21 05:34:58 -05:00
|
|
|
if ($currencyId > 0) {
|
|
|
|
return TransactionCurrency::find($currencyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return meta value for account. Null if not found.
|
|
|
|
*/
|
|
|
|
public function getMetaValue(Account $account, string $field): ?string
|
|
|
|
{
|
|
|
|
$result = $account->accountMeta->filter(
|
2023-11-04 08:18:49 -05:00
|
|
|
static function (AccountMeta $meta) use ($field) {
|
2023-06-21 05:34:58 -05:00
|
|
|
return strtolower($meta->name) === strtolower($field);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (0 === $result->count()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (1 === $result->count()) {
|
2024-02-22 13:11:09 -06:00
|
|
|
return (string)$result->first()->data;
|
2023-06-21 05:34:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count(array $types): int
|
|
|
|
{
|
|
|
|
return $this->user->accounts()->accountTypeIn($types)->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find(int $accountId): ?Account
|
|
|
|
{
|
|
|
|
return $this->user->accounts()->find($accountId);
|
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getUsedCurrencies(Account $account): Collection
|
|
|
|
{
|
|
|
|
$info = $account->transactions()->get(['transaction_currency_id', 'foreign_currency_id'])->toArray();
|
|
|
|
$currencyIds = [];
|
|
|
|
foreach ($info as $entry) {
|
2024-02-22 13:11:09 -06:00
|
|
|
$currencyIds[] = (int)$entry['transaction_currency_id'];
|
|
|
|
$currencyIds[] = (int)$entry['foreign_currency_id'];
|
2021-03-11 23:20:01 -06:00
|
|
|
}
|
|
|
|
$currencyIds = array_unique($currencyIds);
|
|
|
|
|
|
|
|
return TransactionCurrency::whereIn('id', $currencyIds)->get();
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:18:15 -05:00
|
|
|
public function isLiability(Account $account): bool
|
|
|
|
{
|
2019-06-21 12:10:02 -05:00
|
|
|
return in_array($account->accountType->type, [AccountType::CREDITCARD, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true);
|
2018-08-22 14:18:15 -05:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
public function maxOrder(string $type): int
|
|
|
|
{
|
2024-01-04 04:41:03 -06:00
|
|
|
$sets = [
|
2021-03-21 03:15:40 -05:00
|
|
|
AccountType::ASSET => [AccountType::DEFAULT, AccountType::ASSET],
|
|
|
|
AccountType::EXPENSE => [AccountType::EXPENSE, AccountType::BENEFICIARY],
|
|
|
|
AccountType::REVENUE => [AccountType::REVENUE],
|
|
|
|
AccountType::LOAN => [AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
|
|
|
|
AccountType::DEBT => [AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
|
|
|
|
AccountType::MORTGAGE => [AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
|
|
|
|
];
|
|
|
|
if (array_key_exists(ucfirst($type), $sets)) {
|
2024-02-22 13:11:09 -06:00
|
|
|
$order = (int)$this->getAccountsByType($sets[ucfirst($type)])->max('order');
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Return max order of "%s" set: %d', $type, $order));
|
2021-03-21 03:15:40 -05:00
|
|
|
|
|
|
|
return $order;
|
|
|
|
}
|
|
|
|
$specials = [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION];
|
|
|
|
|
2024-02-22 13:11:09 -06:00
|
|
|
$order = (int)$this->getAccountsByType($specials)->max('order');
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Return max order of "%s" set (specials!): %d', $type, $order));
|
2021-03-21 03:15:40 -05:00
|
|
|
|
|
|
|
return $order;
|
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
public function getAccountsByType(array $types, ?array $sort = []): Collection
|
|
|
|
{
|
|
|
|
$res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types);
|
|
|
|
$query = $this->user->accounts();
|
|
|
|
if (0 !== count($types)) {
|
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add sort parameters. At this point they're filtered to allowed fields to sort by:
|
|
|
|
if (0 !== count($sort)) {
|
|
|
|
foreach ($sort as $param) {
|
|
|
|
$query->orderBy($param[0], $param[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 === count($sort)) {
|
|
|
|
if (0 !== count($res)) {
|
|
|
|
$query->orderBy('accounts.order', 'ASC');
|
|
|
|
}
|
|
|
|
$query->orderBy('accounts.active', 'DESC');
|
|
|
|
$query->orderBy('accounts.name', 'ASC');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->get(['accounts.*']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date of the very first transaction in this account.
|
|
|
|
*/
|
|
|
|
public function oldestJournalDate(Account $account): ?Carbon
|
|
|
|
{
|
|
|
|
$journal = $this->oldestJournal($account);
|
|
|
|
|
|
|
|
return $journal?->date;
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:59:58 -05:00
|
|
|
/**
|
|
|
|
* Returns the date of the very first transaction in this account.
|
|
|
|
*/
|
|
|
|
public function oldestJournal(Account $account): ?TransactionJournal
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $first */
|
2022-03-29 07:59:58 -05:00
|
|
|
$first = $account->transactions()
|
2024-01-04 04:41:03 -06:00
|
|
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->orderBy('transaction_journals.date', 'ASC')
|
|
|
|
->orderBy('transaction_journals.order', 'DESC')
|
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
|
|
|
->orderBy('transaction_journals.id', 'ASC')
|
|
|
|
->first(['transaction_journals.id'])
|
|
|
|
;
|
2022-03-29 07:59:58 -05:00
|
|
|
if (null !== $first) {
|
2023-11-05 12:41:37 -06:00
|
|
|
return TransactionJournal::find($first->id);
|
2022-03-29 07:59:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:01:01 -06:00
|
|
|
public function resetAccountOrder(): void
|
|
|
|
{
|
|
|
|
$sets = [
|
|
|
|
[AccountType::DEFAULT, AccountType::ASSET],
|
2023-12-20 12:35:52 -06:00
|
|
|
// [AccountType::EXPENSE, AccountType::BENEFICIARY],
|
|
|
|
// [AccountType::REVENUE],
|
2021-03-13 05:01:01 -06:00
|
|
|
[AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
|
2023-12-20 12:35:52 -06:00
|
|
|
// [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
|
2021-03-13 05:01:01 -06:00
|
|
|
];
|
|
|
|
foreach ($sets as $set) {
|
|
|
|
$list = $this->getAccountsByType($set);
|
|
|
|
$index = 1;
|
|
|
|
foreach ($list as $account) {
|
2021-03-28 04:46:23 -05:00
|
|
|
if (false === $account->active) {
|
2021-03-27 09:28:11 -05:00
|
|
|
$account->order = 0;
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2021-03-27 09:28:11 -05:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-22 13:11:09 -06:00
|
|
|
if ($index !== (int)$account->order) {
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Account #%d ("%s"): order should %d be but is %d.', $account->id, $account->name, $index, $account->order));
|
2021-03-13 05:01:01 -06:00
|
|
|
$account->order = $index;
|
|
|
|
$account->save();
|
|
|
|
}
|
2023-12-20 12:35:52 -06:00
|
|
|
++$index;
|
2021-03-13 05:01:01 -06:00
|
|
|
}
|
2021-03-11 23:20:01 -06:00
|
|
|
}
|
2024-07-25 20:57:35 -05:00
|
|
|
// reset the rest to zero.
|
2024-07-28 22:06:54 -05:00
|
|
|
$all = [AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE];
|
|
|
|
$this->user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
2024-07-25 20:57:35 -05:00
|
|
|
->whereNotIn('account_types.type', $all)
|
2024-07-28 22:06:54 -05:00
|
|
|
->update(['order' => 0])
|
|
|
|
;
|
2021-03-11 23:20:01 -06:00
|
|
|
}
|
|
|
|
|
2020-07-20 23:20:31 -05:00
|
|
|
public function searchAccount(string $query, array $types, int $limit): Collection
|
2019-03-02 07:12:09 -06:00
|
|
|
{
|
2019-08-29 10:53:25 -05:00
|
|
|
$dbQuery = $this->user->accounts()
|
2024-01-04 04:41:03 -06:00
|
|
|
->where('active', true)
|
|
|
|
->orderBy('accounts.order', 'ASC')
|
|
|
|
->orderBy('accounts.account_type_id', 'ASC')
|
|
|
|
->orderBy('accounts.name', 'ASC')
|
|
|
|
->with(['accountType'])
|
|
|
|
;
|
2019-05-04 13:58:43 -05:00
|
|
|
if ('' !== $query) {
|
2019-11-10 00:26:49 -06:00
|
|
|
// split query on spaces just in case:
|
|
|
|
$parts = explode(' ', $query);
|
2020-08-09 01:56:15 -05:00
|
|
|
foreach ($parts as $part) {
|
2019-11-10 00:26:49 -06:00
|
|
|
$search = sprintf('%%%s%%', $part);
|
2024-10-09 23:30:05 -05:00
|
|
|
$dbQuery->whereLike('name', $search);
|
2019-11-10 00:26:49 -06:00
|
|
|
}
|
2019-05-04 13:58:43 -05:00
|
|
|
}
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2019-03-02 07:12:09 -06:00
|
|
|
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
|
|
|
$dbQuery->whereIn('account_types.type', $types);
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:20:31 -05:00
|
|
|
return $dbQuery->take($limit)->get(['accounts.*']);
|
2019-03-02 07:12:09 -06:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:24:01 -05:00
|
|
|
public function searchAccountNr(string $query, array $types, int $limit): Collection
|
|
|
|
{
|
|
|
|
$dbQuery = $this->user->accounts()->distinct()
|
2024-01-04 04:41:03 -06:00
|
|
|
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
|
|
|
->where('accounts.active', true)
|
|
|
|
->orderBy('accounts.order', 'ASC')
|
|
|
|
->orderBy('accounts.account_type_id', 'ASC')
|
|
|
|
->orderBy('accounts.name', 'ASC')
|
|
|
|
->with(['accountType', 'accountMeta'])
|
|
|
|
;
|
2020-08-22 05:24:01 -05:00
|
|
|
if ('' !== $query) {
|
|
|
|
// split query on spaces just in case:
|
|
|
|
$parts = explode(' ', $query);
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$search = sprintf('%%%s%%', $part);
|
2021-03-07 09:19:14 -06:00
|
|
|
$dbQuery->where(
|
2023-12-20 22:07:26 -06:00
|
|
|
static function (EloquentBuilder $q1) use ($search): void { // @phpstan-ignore-line
|
2024-10-13 22:14:52 -05:00
|
|
|
$q1->whereLike('accounts.iban', $search);
|
2021-03-07 09:19:14 -06:00
|
|
|
$q1->orWhere(
|
2023-12-20 22:07:26 -06:00
|
|
|
static function (EloquentBuilder $q2) use ($search): void {
|
2021-03-07 09:19:14 -06:00
|
|
|
$q2->where('account_meta.name', '=', 'account_number');
|
2024-10-09 23:30:05 -05:00
|
|
|
$q2->whereLike('account_meta.data', $search);
|
2021-03-07 09:19:14 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-08-22 05:24:01 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 23:11:05 -05:00
|
|
|
if (0 !== count($types)) {
|
2020-08-22 05:24:01 -05:00
|
|
|
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
|
|
|
$dbQuery->whereIn('account_types.type', $types);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dbQuery->take($limit)->get(['accounts.*']);
|
|
|
|
}
|
2021-03-07 09:19:14 -06:00
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
/**
|
2021-03-11 23:20:01 -06:00
|
|
|
* @throws FireflyException
|
2023-12-22 13:12:38 -06:00
|
|
|
*/
|
2021-03-11 23:20:01 -06:00
|
|
|
public function store(array $data): Account
|
|
|
|
{
|
|
|
|
/** @var AccountFactory $factory */
|
|
|
|
$factory = app(AccountFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
|
|
|
|
return $factory->create($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-18 03:21:29 -05:00
|
|
|
* @throws FireflyException
|
2023-12-22 13:12:38 -06:00
|
|
|
*/
|
2021-03-11 23:20:01 -06:00
|
|
|
public function update(Account $account, array $data): Account
|
|
|
|
{
|
|
|
|
/** @var AccountUpdateService $service */
|
|
|
|
$service = app(AccountUpdateService::class);
|
|
|
|
|
|
|
|
return $service->update($account, $data);
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|