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

268 lines
8.4 KiB
PHP
Raw Normal View History

<?php
2023-07-15 16:02:42 +02:00
/*
* AccountRepository.php
* Copyright (c) 2023 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
2023-07-15 16:02:42 +02:00
declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Account;
2023-07-25 09:01:44 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
2023-11-28 17:18:31 +01:00
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
/**
* Class AccountRepository
*/
class AccountRepository implements AccountRepositoryInterface
{
use UserGroupTrait;
2023-12-10 06:51:59 +01:00
/**
* @inheritDoc
*/
public function findByAccountNumber(string $number, array $types): ?Account
{
$dbQuery = $this->userGroup
->accounts()
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('accounts.active', true)
->where(
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);
}
);
if (0 !== count($types)) {
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$dbQuery->whereIn('account_types.type', $types);
}
/** @var Account|null */
return $dbQuery->first(['accounts.*']);
}
/**
* @param string $iban
* @param array $types
*
* @return Account|null
*/
public function findByIbanNull(string $iban, array $types): ?Account
{
$query = $this->userGroup->accounts()->where('iban', '!=', '')->whereNotNull('iban');
if (0 !== count($types)) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
/** @var Account|null */
return $query->where('iban', $iban)->first(['accounts.*']);
}
/**
* @inheritDoc
*/
public function findByName(string $name, array $types): ?Account
{
$query = $this->userGroup->accounts();
if (0 !== count($types)) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
$query->where('accounts.name', $name);
2023-11-04 07:18:03 +01:00
/** @var Account|null $account */
$account = $query->first(['accounts.*']);
if (null === $account) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('There is no account with name "%s" of types', $name), $types);
return null;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
return $account;
}
/**
2023-08-01 09:27:39 +02:00
* @param Account $account
*
* @return TransactionCurrency|null
*/
2023-08-01 09:27:39 +02:00
public function getAccountCurrency(Account $account): ?TransactionCurrency
2023-07-17 20:33:26 +02:00
{
2023-08-01 09:27:39 +02:00
$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;
}
2023-08-01 09:27:39 +02:00
$currencyId = (int)$this->getMetaValue($account, 'currency_id');
if ($currencyId > 0) {
return TransactionCurrency::find($currencyId);
}
2023-08-01 09:27:39 +02:00
return null;
}
2023-07-25 09:01:44 +02:00
/**
2023-08-01 09:27:39 +02:00
* Return meta value for account. Null if not found.
*
* @param Account $account
* @param string $field
*
* @return null|string
2023-07-25 09:01:44 +02:00
*/
2023-08-01 09:27:39 +02:00
public function getMetaValue(Account $account, string $field): ?string
2023-07-25 09:01:44 +02:00
{
2023-08-01 09:27:39 +02:00
$result = $account->accountMeta->filter(
2023-11-04 14:18:49 +01:00
static function (AccountMeta $meta) use ($field) {
2023-08-01 09:27:39 +02:00
return strtolower($meta->name) === strtolower($field);
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
);
if (0 === $result->count()) {
return null;
}
if (1 === $result->count()) {
return (string)$result->first()->data;
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
return null;
}
/**
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account
{
$account = $this->user->accounts()->find($accountId);
if (null === $account) {
$account = $this->userGroup->accounts()->find($accountId);
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
return $account;
2023-07-25 09:01:44 +02:00
}
/**
* @param array $accountIds
*
* @return Collection
*/
public function getAccountsById(array $accountIds): Collection
{
$query = $this->userGroup->accounts();
if (0 !== count($accountIds)) {
$query->whereIn('accounts.id', $accountIds);
}
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.active', 'DESC');
$query->orderBy('accounts.name', 'ASC');
return $query->get(['accounts.*']);
}
/**
2023-08-01 09:27:39 +02:00
* @inheritDoc
2023-07-25 09:01:44 +02:00
*/
2023-08-01 09:27:39 +02:00
public function getAccountsByType(array $types, ?array $sort = []): Collection
2023-07-25 09:01:44 +02:00
{
2023-08-01 09:27:39 +02:00
$res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types);
$query = $this->userGroup->accounts();
if (0 !== count($types)) {
$query->accountTypeIn($types);
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
// 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]);
}
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
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.*']);
2023-07-25 09:01:44 +02:00
}
2023-08-06 11:22:36 +02:00
/**
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection
{
$query = $this->userGroup->accounts();
if (0 !== count($types)) {
$query->accountTypeIn($types);
}
$query->where('active', true);
$query->orderBy('accounts.account_type_id', 'ASC');
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.name', 'ASC');
return $query->get(['accounts.*']);
}
2023-07-25 09:01:44 +02:00
/**
2023-08-01 09:27:39 +02:00
* @inheritDoc
2023-07-25 09:01:44 +02:00
*/
2023-08-01 09:27:39 +02:00
public function searchAccount(string $query, array $types, int $limit): Collection
2023-07-25 09:01:44 +02:00
{
2023-08-01 09:27:39 +02:00
// search by group, not by user
$dbQuery = $this->userGroup->accounts()
->where('active', true)
->orderBy('accounts.order', 'ASC')
->orderBy('accounts.account_type_id', 'ASC')
->orderBy('accounts.name', 'ASC')
->with(['accountType']);
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);
foreach ($parts as $part) {
$search = sprintf('%%%s%%', $part);
$dbQuery->where('name', 'LIKE', $search);
2023-07-25 09:01:44 +02:00
}
}
2023-08-01 09:27:39 +02:00
if (0 !== count($types)) {
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$dbQuery->whereIn('account_types.type', $types);
2023-07-25 09:01:44 +02:00
}
2023-08-01 09:27:39 +02:00
return $dbQuery->take($limit)->get(['accounts.*']);
2023-07-25 09:01:44 +02:00
}
}