firefly-iii/app/Repositories/Account/AccountRepository.php

784 lines
25 KiB
PHP
Raw Normal View History

2015-02-09 00:23:39 -06:00
<?php
2022-12-29 12:42:26 -06:00
/**
* AccountRepository.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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
*
* 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
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -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/>.
*/
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;
use FireflyIII\Factory\AccountFactory;
2015-02-09 00:23:39 -06:00
use FireflyIII\Models\Account;
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;
use FireflyIII\Models\Location;
use FireflyIII\Models\TransactionCurrency;
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;
2016-03-20 05:47:10 -05:00
use FireflyIII\User;
2023-02-19 01:43:28 -06:00
use Illuminate\Contracts\Auth\Authenticatable;
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;
2023-05-29 06:56:55 -05:00
use JsonException;
2020-05-06 23:44:01 -05:00
use Storage;
2015-02-09 00:23:39 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class AccountRepository.
2019-08-17 03:47:29 -05:00
*
2015-02-09 00:23:39 -06:00
*/
class AccountRepository implements AccountRepositoryInterface
{
2020-08-20 10:59:44 -05:00
private User $user;
/**
* Moved here from account CRUD.
*
2023-06-21 05:34:58 -05:00
* @param Account $account
* @param Account|null $moveTo
*
* @return bool
2017-12-22 11:32:43 -06:00
*
2018-04-02 08:10:40 -05:00
*/
2018-02-23 09:21:28 -06:00
public function destroy(Account $account, ?Account $moveTo): bool
{
2018-02-21 11:42:15 -06:00
/** @var AccountDestroyService $service */
$service = app(AccountDestroyService::class);
$service->destroy($account, $moveTo);
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.
*
2023-06-21 05:34:58 -05:00
* @param Collection $accounts
2019-09-03 15:35:41 -05:00
*
* @return Collection
*/
public function expandWithDoubles(Collection $accounts): Collection
{
2022-10-30 08:24:28 -05:00
$result = new Collection();
2019-09-03 15:35:41 -05:00
/** @var Account $account */
foreach ($accounts as $account) {
$byName = $this->user->accounts()->where('name', $account->name)
->where('id', '!=', $account->id)->first();
if (null !== $byName) {
$result->push($account);
$result->push($byName);
continue;
}
if (null !== $account->iban) {
$byIban = $this->user->accounts()->where('iban', $account->iban)
->where('id', '!=', $account->id)->first();
if (null !== $byIban) {
$result->push($account);
$result->push($byIban);
continue;
}
}
$result->push($account);
}
return $result;
}
/**
* @inheritDoc
*/
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-09 23:51:59 -06:00
static function (EloquentBuilder $q1) use ($number) { // @phpstan-ignore-line
$json = json_encode($number);
$q1->where('account_meta.name', '=', 'account_number');
$q1->where('account_meta.data', '=', $json);
}
);
2022-11-03 23:11:05 -05:00
if (0 !== count($types)) {
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$dbQuery->whereIn('account_types.type', $types);
}
2023-02-22 12:54:19 -06:00
/** @var Account|null */
return $dbQuery->first(['accounts.*']);
}
2018-07-23 14:49:15 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param string $iban
* @param array $types
2018-07-23 14:49:15 -05:00
*
* @return Account|null
*/
public function findByIbanNull(string $iban, array $types): ?Account
{
$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-02-22 12:54:19 -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
}
/**
2023-06-21 05:34:58 -05:00
* @param string $name
* @param array $types
2018-07-23 14:49:15 -05:00
*
* @return Account|null
*/
public function findByName(string $name, array $types): ?Account
{
$query = $this->user->accounts();
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-11-04 00:52:40 -05:00
/** @var Account|null $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.
*
2023-06-21 05:34:58 -05:00
* @param string $type
2018-08-04 10:30:47 -05:00
*
* @return AccountType|null
*/
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
/**
2023-06-21 05:34:58 -05:00
* @param array $accountIds
2018-07-23 14:49:15 -05:00
*
* @return Collection
*/
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);
}
$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
return $query->get(['accounts.*']);
2018-07-23 14:49:15 -05:00
}
2023-05-29 06:56:55 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param array $types
2018-07-23 14:49:15 -05:00
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection
{
$query = $this->user->accounts()->with(
2022-12-29 12:42:26 -06:00
[
2023-11-04 08:18:49 -05:00
'accountmeta' => static function (HasMany $query) {
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');
$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
/**
* @inheritDoc
*/
public function getAttachments(Account $account): Collection
{
$set = $account->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
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
/**
* @return Account
*
* @throws FireflyException
2023-02-22 11:03:31 -06:00
* @throws JsonException
2018-07-23 14:49:15 -05:00
*/
public function getCashAccount(): Account
{
/** @var AccountType $type */
$type = AccountType::where('type', AccountType::CASH)->first();
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate('Cash account', $type->type);
}
2023-06-21 05:34:58 -05:00
/**
* @param User|Authenticatable|null $user
*/
public function setUser(User | Authenticatable | null $user): void
{
2023-10-30 13:49:40 -05:00
if ($user instanceof User) {
2023-06-21 05:34:58 -05:00
$this->user = $user;
}
}
/**
* @inheritDoc
*/
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')
2022-12-29 12:42:26 -06:00
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::LIABILITY_CREDIT])
->first(['transaction_journals.*']);
2023-06-03 23:30:22 -05:00
return $journal?->transactionGroup;
}
2021-03-11 23:20:01 -06:00
/**
2023-06-21 05:34:58 -05:00
* @param array $types
2021-03-11 23:20:01 -06:00
*
* @return Collection
*/
public function getInactiveAccountsByType(array $types): Collection
{
$query = $this->user->accounts()->with(
2022-12-29 12:42:26 -06:00
[
2023-11-04 08:18:49 -05:00
'accountmeta' => static function (HasMany $query) {
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.*']);
}
/**
* @inheritDoc
*/
public function getLocation(Account $account): ?Location
{
2023-02-22 12:54:19 -06:00
/** @var Location|null */
2021-03-11 23:20:01 -06:00
return $account->locations()->first();
}
/**
* Get note text or null.
*
2023-06-21 05:34:58 -05:00
* @param Account $account
*
* @return null|string
*/
public function getNoteText(Account $account): ?string
{
2023-06-03 23:30:22 -05:00
return $account->notes()->first()?->text;
}
2018-02-16 15:14:53 -06:00
/**
* Returns the amount of the opening balance for this account.
*
2023-06-21 05:34:58 -05:00
* @param Account $account
2018-02-28 14:32:59 -06:00
*
2021-05-24 01:50:17 -05:00
* @return string|null
2018-02-16 15:14:53 -06:00
*/
public function getOpeningBalanceAmount(Account $account): ?string
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
2022-12-27 00:01:13 -06:00
->transactionTypes([TransactionType::OPENING_BALANCE, TransactionType::LIABILITY_CREDIT])
2018-02-16 15:14:53 -06:00
->first(['transaction_journals.*']);
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.
*
2023-06-21 05:34:58 -05:00
* @param Account $account
2018-02-16 15:14:53 -06:00
*
* @return null|string
*/
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')
->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
}
/**
2023-06-21 05:34:58 -05:00
* @param Account $account
*
* @return TransactionGroup|null
*/
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup
{
$journal = $this->getOpeningBalance($account);
2021-12-17 10:27:01 -06:00
return $journal?->transactionGroup;
}
2018-12-09 06:09:43 -06:00
/**
2023-06-21 05:34:58 -05:00
* @param Account $account
*
* @return TransactionJournal|null
*/
public function getOpeningBalance(Account $account): ?TransactionJournal
{
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
}
/**
* @param Account $account
2018-12-09 06:09:43 -06:00
*
* @return Collection
*/
public function getPiggyBanks(Account $account): Collection
{
return $account->piggyBanks()->get();
}
2018-07-23 14:49:15 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param Account $account
2018-07-23 14:49:15 -05:00
*
* @return Account|null
*
* @throws FireflyException
2022-03-29 08:10:05 -05:00
* @throws JsonException
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));
}
$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 */
2023-12-09 23:51:59 -06:00
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
2023-11-04 00:52:40 -05:00
/** @var Account|null $current */
$current = $this->user->accounts()->where('account_type_id', $type->id)
2020-08-09 01:56:15 -05:00
->where('name', $name)
->first();
2019-08-03 12:17:59 -05:00
if (null !== $current) {
return $current;
2018-07-23 14:49:15 -05: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,
];
2018-07-23 14:49:15 -05:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($account->user);
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
/**
* @param Account $account
*
* @return TransactionCurrency|null
*/
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
// return null if not in this list.
if (!in_array($type, $list, true)) {
return null;
}
$currencyId = (int)$this->getMetaValue($account, 'currency_id');
if ($currencyId > 0) {
return TransactionCurrency::find($currencyId);
}
return null;
}
/**
* Return meta value for account. Null if not found.
*
* @param Account $account
* @param string $field
*
* @return null|string
*/
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()) {
return (string)$result->first()->data;
}
return null;
}
/**
* @param array $types
*
* @return int
*/
public function count(array $types): int
{
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
2021-03-11 23:20:01 -06:00
/**
* @inheritDoc
*/
public function getUsedCurrencies(Account $account): Collection
{
$info = $account->transactions()->get(['transaction_currency_id', 'foreign_currency_id'])->toArray();
$currencyIds = [];
foreach ($info as $entry) {
2022-12-29 12:42:26 -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();
}
/**
2023-06-21 05:34:58 -05:00
* @param Account $account
*
* @return bool
*/
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);
}
2021-03-21 03:15:40 -05:00
/**
* @inheritDoc
*/
public function maxOrder(string $type): int
{
$sets = [
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)) {
2022-12-29 12:42:26 -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];
2022-12-29 12:42:26 -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
/**
* @param array $types
* @param array|null $sort
*
* @return Collection
*/
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.
*
* @param Account $account
*
* @return Carbon|null
*/
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.
*
2023-06-21 05:34:58 -05:00
* @param Account $account
2022-03-29 07:59:58 -05:00
*
* @return TransactionJournal|null
*/
public function oldestJournal(Account $account): ?TransactionJournal
{
2023-11-05 12:41:37 -06:00
/** @var TransactionJournal|null $first */
2022-03-29 07:59:58 -05:00
$first = $account->transactions()
->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']);
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-11 23:20:01 -06:00
/**
* @inheritDoc
*/
2021-03-13 05:01:01 -06:00
public function resetAccountOrder(): void
{
$sets = [
[AccountType::DEFAULT, AccountType::ASSET],
2021-03-13 23:20:23 -06:00
//[AccountType::EXPENSE, AccountType::BENEFICIARY],
//[AccountType::REVENUE],
2021-03-13 05:01:01 -06:00
[AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
2021-03-13 23:20:23 -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;
continue;
}
2022-12-29 12:42:26 -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();
}
$index++;
}
2021-03-11 23:20:01 -06:00
}
}
2019-03-02 07:12:09 -06:00
/**
2023-06-21 05:34:58 -05:00
* @param string $query
* @param array $types
* @param int $limit
2019-03-02 07:12:09 -06:00
*
* @return Collection
*/
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()
2021-05-01 13:04:58 -05:00
->where('active', true)
->orderBy('accounts.order', 'ASC')
2020-07-26 07:30:56 -05:00
->orderBy('accounts.account_type_id', 'ASC')
2019-10-27 12:12:30 -05:00
->orderBy('accounts.name', 'ASC')
2019-08-29 10:53:25 -05:00
->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);
$dbQuery->where('name', 'LIKE', $search);
}
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
/**
* @inheritDoc
*/
public function searchAccountNr(string $query, array $types, int $limit): Collection
{
$dbQuery = $this->user->accounts()->distinct()
2021-04-21 23:18:46 -05:00
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
2021-05-01 13:04:58 -05:00
->where('accounts.active', true)
2020-08-22 05:24:01 -05:00
->orderBy('accounts.order', 'ASC')
->orderBy('accounts.account_type_id', 'ASC')
->orderBy('accounts.name', 'ASC')
->with(['accountType', 'accountMeta']);
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);
foreach ($parts as $part) {
$search = sprintf('%%%s%%', $part);
$dbQuery->where(
2023-11-04 08:18:49 -05:00
static function (EloquentBuilder $q1) use ($search) { // @phpstan-ignore-line
$q1->where('accounts.iban', 'LIKE', $search);
$q1->orWhere(
2023-11-04 08:18:49 -05:00
static function (EloquentBuilder $q2) use ($search) {
$q2->where('account_meta.name', '=', 'account_number');
$q2->where('account_meta.data', 'LIKE', $search);
}
);
}
);
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.*']);
}
2023-05-29 06:56:55 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param array $data
2021-03-11 23:20:01 -06:00
*
* @return Account
* @throws FireflyException
2022-03-29 08:10:05 -05:00
* @throws JsonException
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);
}
/**
2023-06-21 05:34:58 -05:00
* @param Account $account
2023-07-15 09:02:42 -05:00
* @param array $data
2021-03-11 23:20:01 -06:00
*
* @return Account
2021-09-18 03:21:29 -05:00
* @throws FireflyException
2023-07-04 06:29:19 -05:00
* @throws JsonException
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
}