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

463 lines
13 KiB
PHP
Raw Normal View History

2015-02-09 00:23:39 -06:00
<?php
/**
* AccountRepository.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://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;
2016-10-10 01:03:03 -05:00
use FireflyIII\Models\AccountType;
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;
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;
use Log;
2015-02-09 00:23:39 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class AccountRepository.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2015-02-09 00:23:39 -06:00
*/
class AccountRepository implements AccountRepositoryInterface
{
2018-07-23 14:49:15 -05:00
2016-03-20 05:47:10 -05:00
/** @var User */
private $user;
2016-03-30 10:47:13 -05:00
/**
* @param array $types
*
* @return int
*/
2016-11-28 11:55:56 -06:00
public function count(array $types): int
{
2018-04-02 08:10:40 -05:00
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* Moved here from account CRUD.
*
2018-03-10 15:38:20 -06:00
* @param Account $account
2018-02-23 09:21:28 -06:00
* @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;
}
2018-07-23 14:49:15 -05:00
/**
* @param string $number
* @param array $types
*
* @return Account|null
*/
public function findByAccountNumber(string $number, array $types): ?Account
{
$query = $this->user->accounts()
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
->where('account_meta.name', 'accountNumber')
->where('account_meta.data', json_encode($number));
if (\count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
/** @var Collection $accounts */
$accounts = $query->get(['accounts.*']);
if ($accounts->count() > 0) {
return $accounts->first();
}
return null;
}
/**
* @param string $iban
* @param array $types
*
* @return Account|null
*/
public function findByIbanNull(string $iban, array $types): ?Account
{
$query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban');
if (\count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
$accounts = $query->get(['accounts.*']);
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->iban === $iban) {
return $account;
}
}
return null;
}
/**
* @param string $name
* @param array $types
*
* @return Account|null
*/
public function findByName(string $name, array $types): ?Account
{
$query = $this->user->accounts();
if (\count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
$accounts = $query->get(['accounts.*']);
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->name === $name) {
Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
return $account;
}
}
Log::debug(sprintf('There is no account with name "%s" of types', $name), $types);
return null;
}
2018-02-16 08:19:19 -06:00
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
2018-08-04 10:30:47 -05:00
/**
* Return account type or null if not found.
*
* @param string $type
*
* @return AccountType|null
*/
public function getAccountTypeByType(string $type): ?AccountType
{
return AccountType::whereType($type)->first();
}
2018-07-23 14:49:15 -05:00
/**
* @param array $accountIds
*
* @return Collection
*/
public function getAccountsById(array $accountIds): Collection
{
/** @var Collection $result */
$query = $this->user->accounts();
if (\count($accountIds) > 0) {
$query->whereIn('accounts.id', $accountIds);
}
$result = $query->get(['accounts.*']);
$result = $result->sortBy(
function (Account $account) {
return strtolower($account->name);
}
);
return $result;
}
/**
* @param array $types
*
* @return Collection
*/
public function getAccountsByType(array $types): Collection
{
/** @var Collection $result */
$query = $this->user->accounts();
if (\count($types) > 0) {
$query->accountTypeIn($types);
}
$result = $query->get(['accounts.*']);
$result = $result->sortBy(
function (Account $account) {
return strtolower($account->name);
}
);
return $result;
}
/**
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection
{
/** @var Collection $result */
$query = $this->user->accounts()->with(
['accountmeta' => function (HasMany $query) {
$query->where('name', 'accountRole');
}]
);
if (\count($types) > 0) {
$query->accountTypeIn($types);
}
$query->where('active', 1);
$result = $query->get(['accounts.*']);
$result = $result->sortBy(
function (Account $account) {
return strtolower($account->name);
}
);
return $result;
}
/**
* @return Account
*
* @throws FireflyException
*/
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);
}
2018-03-19 02:17:31 -05:00
/**
* 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
{
foreach ($account->accountMeta as $meta) {
if ($meta->name === $field) {
2018-04-02 08:10:40 -05:00
return (string)$meta->data;
2018-03-19 02:17:31 -05:00
}
}
return null;
}
/**
* Get note text or null.
*
* @param Account $account
*
* @return null|string
*/
public function getNoteText(Account $account): ?string
{
$note = $account->notes()->first();
2018-08-05 11:59:15 -05:00
2018-04-02 08:10:40 -05:00
if (null === $note) {
return null;
}
return $note->text;
}
2018-02-16 15:14:53 -06:00
/**
* Returns the amount of the opening balance for this account.
*
2018-02-28 14:32:59 -06:00
* @param Account $account
*
2018-02-16 15:14:53 -06:00
* @return string
*/
public function getOpeningBalanceAmount(Account $account): ?string
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (null === $journal) {
return null;
}
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
if (null === $transaction) {
return null;
}
2018-04-02 08:10:40 -05:00
return (string)$transaction->amount;
2018-02-16 15:14:53 -06:00
}
/**
* Return date of opening balance as string or null.
*
* @param Account $account
*
* @return null|string
*/
public function getOpeningBalanceDate(Account $account): ?string
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (null === $journal) {
return null;
}
return $journal->date->format('Y-m-d');
}
2018-07-23 14:49:15 -05:00
/**
* @param Account $account
*
* @return Account|null
*
* @throws FireflyException
*/
public function getReconciliation(Account $account): ?Account
{
if (AccountType::ASSET !== $account->accountType->type) {
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
}
$name = $account->name . ' reconciliation';
/** @var AccountType $type */
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get();
/** @var Account $current */
foreach ($accounts as $current) {
if ($current->name === $name) {
return $current;
}
}
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($account->user);
$account = $factory->findOrCreate($name, $type->type);
return $account;
}
2016-11-21 13:23:25 -06:00
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
2018-08-04 10:30:47 -05:00
*
2018-07-24 13:30:52 -05:00
* @return TransactionJournal|null
2016-11-21 13:23:25 -06:00
*/
2018-07-24 13:30:52 -05:00
public function oldestJournal(Account $account): ?TransactionJournal
2016-11-21 13:23:25 -06:00
{
$first = $account->transactions()
2016-11-21 13:23:25 -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)
2016-11-21 13:23:25 -06:00
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']);
2017-11-15 05:25:49 -06:00
if (null !== $first) {
2018-04-02 08:10:40 -05:00
return TransactionJournal::find((int)$first->id);
}
2018-07-24 13:30:52 -05:00
return null;
}
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return Carbon
*/
public function oldestJournalDate(Account $account): Carbon
{
2018-03-29 12:01:47 -05:00
$result = new Carbon;
$journal = $this->oldestJournal($account);
2018-07-24 13:30:52 -05:00
if (null !== $journal) {
2018-03-29 12:01:47 -05:00
$result = $journal->date;
}
2018-03-29 12:01:47 -05:00
return $result;
}
2017-01-30 09:42:58 -06:00
/**
* @param User $user
*/
2018-07-22 11:50:27 -05:00
public function setUser(User $user): void
2017-01-30 09:42:58 -06:00
{
$this->user = $user;
}
2016-10-10 01:03:03 -05:00
/**
* @param array $data
*
* @return Account
2018-07-22 11:50:27 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-10-10 01:03:03 -05:00
*/
public function store(array $data): Account
{
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
2017-12-30 14:04:04 -06:00
2018-04-02 08:10:40 -05:00
return $factory->create($data);
2016-10-10 01:03:03 -05:00
}
/**
* @param Account $account
* @param array $data
*
* @return Account
*/
public function update(Account $account, array $data): Account
{
2018-02-21 13:34:24 -06:00
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
2018-02-21 14:11:44 -06:00
$account = $service->update($account, $data);
2016-10-10 01:03:03 -05:00
return $account;
}
2015-03-29 01:14:32 -05:00
}