2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
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;
|
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;
|
2020-08-22 05:24:01 -05:00
|
|
|
use \Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2018-07-23 14:49:15 -05:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Log;
|
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;
|
2019-08-24 00:56:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function count(array $types): int
|
|
|
|
{
|
|
|
|
return $this->user->accounts()->accountTypeIn($types)->count();
|
|
|
|
}
|
|
|
|
|
2016-10-09 01:20:29 -05:00
|
|
|
/**
|
|
|
|
* Moved here from account CRUD.
|
|
|
|
*
|
2019-08-24 00:56:08 -05:00
|
|
|
* @param Account $account
|
2018-02-23 09:21:28 -06:00
|
|
|
* @param Account|null $moveTo
|
2016-10-09 01:20:29 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2018-04-02 08:10:40 -05:00
|
|
|
|
2016-10-09 01:20:29 -05:00
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function expandWithDoubles(Collection $accounts): Collection
|
|
|
|
{
|
|
|
|
$result = new Collection;
|
|
|
|
/** @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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/**
|
|
|
|
* @param string $iban
|
2019-08-24 00:56:08 -05:00
|
|
|
* @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');
|
|
|
|
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($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);
|
|
|
|
}
|
|
|
|
|
2019-08-05 22:39:05 -05:00
|
|
|
// TODO a loop like this is no longer necessary
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
$accounts = $query->get(['accounts.*']);
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
if ($account->iban === $iban) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
2019-08-24 00:56:08 -05:00
|
|
|
* @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();
|
|
|
|
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($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);
|
|
|
|
}
|
|
|
|
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.*']);
|
2019-08-03 12:17:59 -05:00
|
|
|
|
|
|
|
// TODO no longer need to loop like this
|
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/** @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-12-15 00:59:49 -06:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return TransactionCurrency|null
|
|
|
|
*/
|
|
|
|
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
|
|
|
{
|
2020-08-09 01:56:15 -05:00
|
|
|
$currencyId = (int) $this->getMetaValue($account, 'currency_id');
|
2018-12-15 00:59:49 -06:00
|
|
|
if ($currencyId > 0) {
|
|
|
|
return TransactionCurrency::find($currencyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAccountsByType(array $types): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $result */
|
|
|
|
$query = $this->user->accounts();
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($types)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
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');
|
2019-05-04 13:58:43 -05:00
|
|
|
|
2020-07-24 09:41:58 -05:00
|
|
|
return $query->get(['accounts.*']);
|
2018-07-23 14:49:15 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getActiveAccountsByType(array $types): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $result */
|
|
|
|
$query = $this->user->accounts()->with(
|
|
|
|
['accountmeta' => function (HasMany $query) {
|
2019-07-05 12:43:16 -05:00
|
|
|
$query->where('name', 'account_role');
|
2020-03-19 12:20:23 -05:00
|
|
|
}, 'attachments']
|
2018-07-23 14:49:15 -05:00
|
|
|
);
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($types)) {
|
2018-07-23 14:49:15 -05:00
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
|
|
|
$query->where('active', 1);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2019-08-24 00:56:08 -05:00
|
|
|
* @param string $field
|
2018-03-19 02:17:31 -05:00
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getMetaValue(Account $account, string $field): ?string
|
|
|
|
{
|
2020-08-09 01:56:15 -05:00
|
|
|
$result = $account->accountMeta->filter(function (AccountMeta $meta) use ($field) {
|
|
|
|
return strtolower($meta->name) === strtolower($field);
|
|
|
|
});
|
|
|
|
if (0 === $result->count()) {
|
2019-08-24 00:56:08 -05:00
|
|
|
return null;
|
2018-03-19 02:17:31 -05:00
|
|
|
}
|
2020-08-09 01:56:15 -05:00
|
|
|
if (1 === $result->count()) {
|
2020-08-22 05:24:01 -05:00
|
|
|
return (string) $result->first()->data;
|
2020-08-09 01:56:15 -05:00
|
|
|
}
|
|
|
|
return null;
|
2018-03-19 02:17:31 -05:00
|
|
|
}
|
|
|
|
|
2018-03-19 13:39:02 -05:00
|
|
|
/**
|
|
|
|
* 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) {
|
2018-03-19 13:39:02 -05:00
|
|
|
return null;
|
|
|
|
}
|
2018-08-06 12:14:30 -05:00
|
|
|
|
2018-03-19 13:39:02 -05:00
|
|
|
return $note->text;
|
|
|
|
}
|
|
|
|
|
2019-08-24 00:56:08 -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.*']);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-09 01:56:15 -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');
|
|
|
|
}
|
|
|
|
|
2019-08-24 00:56:08 -05:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return TransactionGroup|null
|
|
|
|
*/
|
|
|
|
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup
|
|
|
|
{
|
|
|
|
$journal = $this->getOpeningBalance($account);
|
|
|
|
$group = null;
|
|
|
|
if (null !== $journal) {
|
|
|
|
$group = $journal->transactionGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
2018-12-09 06:09:43 -06:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPiggyBanks(Account $account): Collection
|
|
|
|
{
|
|
|
|
return $account->piggyBanks()->get();
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
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 */
|
2020-05-05 09:02:06 -05:00
|
|
|
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
|
|
|
|
$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
|
|
|
|
2018-07-23 14:49:15 -05:00
|
|
|
/** @var Account $current */
|
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
|
|
|
|
|
|
|
$data = [
|
|
|
|
'account_type_id' => null,
|
|
|
|
'account_type' => 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 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
|
|
|
}
|
|
|
|
|
2018-08-22 14:18:15 -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);
|
2018-08-22 14:18:15 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-01-19 14:54:27 -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')
|
2017-01-19 14:54:27 -06:00
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
2016-11-21 13:23:25 -06:00
|
|
|
->orderBy('transaction_journals.id', 'ASC')
|
2017-01-19 14:54:27 -06:00
|
|
|
->first(['transaction_journals.id']);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $first) {
|
2020-08-09 01:56:15 -05:00
|
|
|
return TransactionJournal::find((int) $first->id);
|
2017-01-19 14:54:27 -06:00
|
|
|
}
|
|
|
|
|
2018-07-24 13:30:52 -05:00
|
|
|
return null;
|
2017-01-19 14:54:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date of the very first transaction in this account.
|
|
|
|
*
|
|
|
|
* @param Account $account
|
|
|
|
*
|
2018-08-07 12:29:10 -05:00
|
|
|
* @return Carbon|null
|
2017-01-19 14:54:27 -06:00
|
|
|
*/
|
2018-08-07 12:29:10 -05:00
|
|
|
public function oldestJournalDate(Account $account): ?Carbon
|
2017-01-19 14:54:27 -06:00
|
|
|
{
|
2018-08-07 12:29:10 -05:00
|
|
|
$result = null;
|
2017-01-19 14:54:27 -06:00
|
|
|
$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;
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:01:47 -05:00
|
|
|
return $result;
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:12:09 -06:00
|
|
|
/**
|
|
|
|
* @param string $query
|
2019-08-24 00:56:08 -05:00
|
|
|
* @param array $types
|
2020-08-09 01:56:15 -05:00
|
|
|
* @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()
|
|
|
|
->where('active', 1)
|
2020-07-24 09:41:58 -05:00
|
|
|
->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
|
|
|
}
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($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
|
|
|
}
|
|
|
|
|
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
|
2019-07-31 09:53:09 -05:00
|
|
|
* @throws FireflyException
|
2016-10-10 01:03:03 -05:00
|
|
|
*/
|
|
|
|
public function store(array $data): Account
|
|
|
|
{
|
2018-02-21 14:06:59 -06:00
|
|
|
/** @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
|
2019-08-24 00:56:08 -05:00
|
|
|
* @param array $data
|
2016-10-10 01:03:03 -05:00
|
|
|
*
|
|
|
|
* @return Account
|
2019-07-31 09:53:09 -05:00
|
|
|
* @throws FireflyException
|
2016-10-10 01:03:03 -05:00
|
|
|
*/
|
|
|
|
public function update(Account $account, array $data): Account
|
|
|
|
{
|
2018-02-21 13:34:24 -06:00
|
|
|
/** @var AccountUpdateService $service */
|
|
|
|
$service = app(AccountUpdateService::class);
|
2020-10-23 12:11:25 -05:00
|
|
|
return $service->update($account, $data);
|
2016-10-10 01:03:03 -05:00
|
|
|
}
|
2019-10-19 12:17:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getInactiveAccountsByType(array $types): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $result */
|
|
|
|
$query = $this->user->accounts()->with(
|
|
|
|
['accountmeta' => function (HasMany $query) {
|
|
|
|
$query->where('name', 'account_role');
|
|
|
|
}]
|
|
|
|
);
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($types)) {
|
2019-10-19 12:17:14 -05:00
|
|
|
$query->accountTypeIn($types);
|
|
|
|
}
|
|
|
|
$query->where('active', 0);
|
|
|
|
$query->orderBy('accounts.account_type_id', 'ASC');
|
2020-07-24 09:41:58 -05:00
|
|
|
$query->orderBy('accounts.order', 'ASC');
|
2019-10-19 12:17:14 -05:00
|
|
|
$query->orderBy('accounts.name', 'ASC');
|
|
|
|
|
|
|
|
return $query->get(['accounts.*']);
|
|
|
|
}
|
2019-12-30 05:12:08 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getLocation(Account $account): ?Location
|
|
|
|
{
|
|
|
|
return $account->locations()->first();
|
|
|
|
}
|
2020-03-19 02:32:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAttachments(Account $account): Collection
|
|
|
|
{
|
2020-08-09 01:56:15 -05:00
|
|
|
$set = $account->attachments()->get();
|
2020-05-06 23:44:01 -05:00
|
|
|
|
|
|
|
/** @var Storage $disk */
|
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
|
2020-10-23 12:11:25 -05:00
|
|
|
return $set->each(
|
2020-05-06 23:44:01 -05:00
|
|
|
static function (Attachment $attachment) use ($disk) {
|
|
|
|
$notes = $attachment->notes()->first();
|
|
|
|
$attachment->file_exists = $disk->exists($attachment->fileName());
|
|
|
|
$attachment->notes = $notes ? $notes->text : '';
|
|
|
|
|
|
|
|
return $attachment;
|
|
|
|
}
|
|
|
|
);
|
2020-03-19 02:32:42 -05:00
|
|
|
}
|
2020-03-19 22:37:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getUsedCurrencies(Account $account): Collection
|
|
|
|
{
|
|
|
|
$info = $account->transactions()->get(['transaction_currency_id', 'foreign_currency_id'])->toArray();
|
|
|
|
$currencyIds = [];
|
|
|
|
foreach ($info as $entry) {
|
|
|
|
$currencyIds[] = (int) $entry['transaction_currency_id'];
|
|
|
|
$currencyIds[] = (int) $entry['foreign_currency_id'];
|
|
|
|
}
|
|
|
|
$currencyIds = array_unique($currencyIds);
|
|
|
|
|
|
|
|
return TransactionCurrency::whereIn('id', $currencyIds)->get();
|
|
|
|
}
|
2020-07-26 08:05:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function resetAccountOrder(array $types): void
|
|
|
|
{
|
|
|
|
$list = $this->getAccountsByType($types);
|
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @var Account $account
|
|
|
|
*/
|
|
|
|
foreach ($list as $index => $account) {
|
|
|
|
$account->order = $index + 1;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 05:24:01 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function searchAccountNr(string $query, array $types, int $limit): Collection
|
|
|
|
{
|
|
|
|
$dbQuery = $this->user->accounts()->distinct()
|
|
|
|
->leftJoin('account_meta', 'accounts.id', 'account_meta.account_id')
|
|
|
|
->where('accounts.active', 1)
|
|
|
|
->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(function (EloquentBuilder $q1) use ($search) {
|
|
|
|
$q1->where('accounts.iban', 'LIKE', $search);
|
|
|
|
$q1->orWhere(function (EloquentBuilder $q2) use ($search) {
|
|
|
|
$q2->where('account_meta.name', '=', 'account_number');
|
|
|
|
$q2->where('account_meta.data', 'LIKE', $search);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 13:15:57 -05:00
|
|
|
if (!empty($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.*']);
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|