Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -1,6 +1,5 @@
<?php
/*
* AccountRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -40,9 +39,6 @@ class AccountRepository implements AccountRepositoryInterface
{
use UserGroupTrait;
/**
* @inheritDoc
*/
public function findByAccountNumber(string $number, array $types): ?Account
{
$dbQuery = $this->userGroup
@@ -55,22 +51,18 @@ class AccountRepository implements AccountRepositoryInterface
$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 */
// @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');
@@ -80,13 +72,10 @@ class AccountRepository implements AccountRepositoryInterface
$query->whereIn('account_types.type', $types);
}
/** @var Account|null */
// @var Account|null
return $query->where('iban', $iban)->first(['accounts.*']);
}
/**
* @inheritDoc
*/
public function findByName(string $name, array $types): ?Account
{
$query = $this->userGroup->accounts();
@@ -98,7 +87,8 @@ class AccountRepository implements AccountRepositoryInterface
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);
/** @var Account|null $account */
/** @var null|Account $account */
$account = $query->first(['accounts.*']);
if (null === $account) {
app('log')->debug(sprintf('There is no account with name "%s" of types', $name), $types);
@@ -110,11 +100,6 @@ class AccountRepository implements AccountRepositoryInterface
return $account;
}
/**
* @param Account $account
*
* @return TransactionCurrency|null
*/
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
$type = $account->accountType->type;
@@ -134,11 +119,6 @@ class AccountRepository implements AccountRepositoryInterface
/**
* 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
{
@@ -157,25 +137,16 @@ class AccountRepository implements AccountRepositoryInterface
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);
}
return $account;
}
/**
* @param array $accountIds
*
* @return Collection
*/
public function getAccountsById(array $accountIds): Collection
{
$query = $this->userGroup->accounts();
@@ -190,9 +161,6 @@ class AccountRepository implements AccountRepositoryInterface
return $query->get(['accounts.*']);
}
/**
* @inheritDoc
*/
public function getAccountsByType(array $types, ?array $sort = []): Collection
{
$res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types);
@@ -215,14 +183,10 @@ class AccountRepository implements AccountRepositoryInterface
$query->orderBy('accounts.active', 'DESC');
$query->orderBy('accounts.name', 'ASC');
}
return $query->get(['accounts.*']);
}
/**
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection
{
$query = $this->userGroup->accounts();
@@ -237,18 +201,16 @@ class AccountRepository implements AccountRepositoryInterface
return $query->get(['accounts.*']);
}
/**
* @inheritDoc
*/
public function searchAccount(string $query, array $types, int $limit): Collection
{
// 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']);
->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);

View File

@@ -1,6 +1,5 @@
<?php
/*
* AccountRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -36,98 +35,30 @@ use Illuminate\Support\Collection;
*/
interface AccountRepositoryInterface
{
/**
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account;
/**
* @param string $number
* @param array $types
*
* @return Account|null
*/
public function findByAccountNumber(string $number, array $types): ?Account;
/**
* @param string $iban
* @param array $types
*
* @return Account|null
*/
public function findByIbanNull(string $iban, array $types): ?Account;
/**
* @param string $name
* @param array $types
*
* @return Account|null
*/
public function findByName(string $name, array $types): ?Account;
/**
* @param Account $account
*
* @return TransactionCurrency|null
*/
public function getAccountCurrency(Account $account): ?TransactionCurrency;
/**
* @param array $accountIds
*
* @return Collection
*/
public function getAccountsById(array $accountIds): Collection;
/**
* @param array $types
* @param array|null $sort
*
* @return Collection
*/
public function getAccountsByType(array $types, ?array $sort = []): Collection;
/**
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection;
/**
* 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;
/**
* @param string $query
* @param array $types
* @param int $limit
*
* @return Collection
*/
public function searchAccount(string $query, array $types, int $limit): Collection;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
/**
* @param UserGroup $userGroup
*
* @return void
*/
public function setUserGroup(UserGroup $userGroup): void;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* BillRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -53,29 +52,25 @@ class BillRepository implements BillRepositoryInterface
$bill->order = $current;
$bill->save();
}
$current++;
++$current;
}
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->userGroup->bills()
->orderBy('bills.name', 'ASC')
->get(['bills.*']);
->orderBy('bills.name', 'ASC')
->get(['bills.*'])
;
}
/**
* @inheritDoc
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$default = app('amount')->getDefaultCurrency();
$return = [];
$converter = new ExchangeRateConverter();
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
@@ -100,7 +95,7 @@ class BillRepository implements BillRepositoryInterface
/** @var TransactionJournal $transactionJournal */
foreach ($set as $transactionJournal) {
/** @var Transaction|null $sourceTransaction */
/** @var null|Transaction $sourceTransaction */
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = $sourceTransaction->amount;
@@ -123,29 +118,26 @@ class BillRepository implements BillRepositoryInterface
}
}
}
return $return;
}
/**
* @return Collection
*/
public function getActiveBills(): Collection
{
return $this->userGroup->bills()
->where('active', true)
->orderBy('bills.name', 'ASC')
->get(['bills.*']);
->where('active', true)
->orderBy('bills.name', 'ASC')
->get(['bills.*'])
;
}
/**
* @inheritDoc
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
$default = app('amount')->getDefaultCurrency();
$converter = new ExchangeRateConverter();
/** @var Bill $bill */
foreach ($bills as $bill) {
$dates = $this->getPayDatesInRange($bill, $start, $end);
@@ -182,32 +174,26 @@ class BillRepository implements BillRepositoryInterface
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
* TODO duplicate of function in other billrepositoryinterface
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{
$set = new Collection();
$currentStart = clone $start;
//app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
//app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
// app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
// app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
while ($currentStart <= $end) {
//app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
// app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
//app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
// app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
break;
}
$set->push(clone $nextExpectedMatch);
//app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
// app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
$nextExpectedMatch->addDay();
//app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
// app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
$currentStart = clone $nextExpectedMatch;
}
@@ -220,11 +206,6 @@ class BillRepository implements BillRepositoryInterface
* transaction. Whether it is there already, is not relevant.
*
* TODO duplicate of other repos
*
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
public function nextDateMatch(Bill $bill, Carbon $date): Carbon
{

View File

@@ -1,6 +1,5 @@
<?php
/*
* BillRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -41,26 +40,14 @@ interface BillRepositoryInterface
*/
public function correctOrder(): void;
/**
* @return Collection
*/
public function getActiveBills(): Collection;
/**
* @return Collection
*/
public function getBills(): Collection;
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
*
* TODO duplicate of method in other billrepositoryinterface
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection;
@@ -69,38 +56,18 @@ interface BillRepositoryInterface
* transaction. Whether it is there already, is not relevant.
*
* TODO duplicate of method in other bill repos
*
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
public function nextDateMatch(Bill $bill, Carbon $date): Carbon;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
/**
* Collect multi-currency of sum of bills already paid.
*
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array;
/**
* Collect multi-currency of sum of bills yet to pay.
*
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* AvailableBudgetRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -37,20 +36,16 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
use UserGroupTrait;
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{
$return = [];
$converter = new ExchangeRateConverter();
$default = app('amount')->getDefaultCurrency();
$availableBudgets = $this->userGroup->availableBudgets()
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->get();
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->get()
;
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$currencyId = $availableBudget->transaction_currency_id;
@@ -72,6 +67,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
$return[$currencyId]['amount'] = bcadd($return[$currencyId]['amount'], $availableBudget->amount);
$return[$currencyId]['native_amount'] = bcadd($return[$currencyId]['native_amount'], $nativeAmount);
}
return $return;
}
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* AvailableBudgetRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -33,19 +32,7 @@ use FireflyIII\User;
*/
interface AvailableBudgetRepositoryInterface
{
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* BudgetRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -35,14 +34,12 @@ class BudgetRepository implements BudgetRepositoryInterface
{
use UserGroupTrait;
/**
* @inheritDoc
*/
public function getActiveBudgets(): Collection
{
return $this->userGroup->budgets()->where('active', true)
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')
->get();
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')
->get()
;
}
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* BudgetRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -34,22 +33,9 @@ use Illuminate\Support\Collection;
*/
interface BudgetRepositoryInterface
{
/**
* @return Collection
*/
public function getActiveBudgets(): Collection;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
/**
* @param UserGroup $userGroup
*
* @return void
*/
public function setUserGroup(UserGroup $userGroup): void;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* OperationsRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -40,7 +39,6 @@ class OperationsRepository implements OperationsRepositoryInterface
use UserGroupTrait;
/**
* @inheritDoc
* @throws FireflyException
*/
public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array
@@ -124,9 +122,6 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @return Collection
*/
private function getBudgets(): Collection
{
/** @var BudgetRepositoryInterface $repository */

View File

@@ -1,6 +1,5 @@
<?php
/*
* OperationsRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -38,20 +37,8 @@ interface OperationsRepositoryInterface
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified budget set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
*
* @return array
*/
public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* CurrencyRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -39,7 +38,6 @@ use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class CurrencyRepository
@@ -49,9 +47,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
use UserGroupTrait;
/**
* @param TransactionCurrency $currency
*
* @return bool
* @throws FireflyException
*/
public function currencyInUse(TransactionCurrency $currency): bool
@@ -62,9 +57,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
*
* @return string|null
* @throws FireflyException
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string
@@ -112,8 +104,9 @@ class CurrencyRepository implements CurrencyRepositoryInterface
// is being used in accounts (as integer)
$meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count();
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count()
;
if ($meta > 0) {
app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
@@ -157,28 +150,14 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return null;
}
/**
* @param TransactionCurrency $currency
*
* @return int
*/
private function countJournals(TransactionCurrency $currency): int
{
$count = $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
// also count foreign:
return $count + Transaction::where('foreign_currency_id', $currency->id)->count();
}
/**
* Returns ALL currencies, regardless of whether they are enabled or not.
*
* @return Collection
*/
public function getAll(): Collection
{
$all = TransactionCurrency::orderBy('code', 'ASC')->get();
$local = $this->get();
return $all->map(static function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(static function (TransactionCurrency $entry) use ($current) {
return $entry->id === $current->id;
@@ -188,29 +167,24 @@ class CurrencyRepository implements CurrencyRepositoryInterface
});
$current->userGroupEnabled = $hasId;
$current->userGroupDefault = $isDefault;
return $current;
});
}
/**
* @inheritDoc
*/
public function get(): Collection
{
$all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get();
$all->map(static function (TransactionCurrency $current) {
$current->userGroupEnabled = true;
$current->userGroupDefault = 1 === (int)$current->pivot->group_default;
return $current;
});
return $all;
}
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function destroy(TransactionCurrency $currency): bool
{
/** @var UserRepositoryInterface $repository */
@@ -226,8 +200,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void
{
@@ -236,9 +208,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$currency->save();
}
/**
* @inheritDoc
*/
public function findByName(string $name): ?TransactionCurrency
{
return TransactionCurrency::where('name', $name)->first();
@@ -247,12 +216,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
* @throws FireflyException
* @throws JsonException
* @throws \JsonException
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency
{
@@ -260,7 +225,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if (null === $result) {
app('log')->debug('Grabbing default currency for this user...');
/** @var TransactionCurrency|null $result */
/** @var null|TransactionCurrency $result */
$result = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
@@ -275,11 +241,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
@@ -299,10 +260,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function find(int $currencyId): ?TransactionCurrency
{
@@ -311,10 +268,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCode(string $currencyCode): ?TransactionCurrency
{
@@ -323,7 +276,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* @param TransactionCurrency $currency
* Enables a currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
@@ -332,27 +285,16 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$currency->save();
}
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->whereIn('id', $ids)->get();
}
/**
* @inheritDoc
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
/**
* @inheritDoc
*/
public function makeDefault(TransactionCurrency $currency): void
{
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id));
@@ -363,12 +305,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => true]]);
}
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
@@ -380,9 +316,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency
@@ -398,12 +331,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $result;
}
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency
{
app('log')->debug('Now in update()');
@@ -450,4 +377,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $service->update($currency, $data);
}
private function countJournals(TransactionCurrency $currency): int
{
$count = $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
// also count foreign:
return $count + Transaction::where('foreign_currency_id', $currency->id)->count();
}
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* CurrencyRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -32,83 +31,41 @@ use Illuminate\Support\Collection;
interface CurrencyRepositoryInterface
{
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function currencyInUse(TransactionCurrency $currency): bool;
/**
* Currency is in use where exactly.
*
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function destroy(TransactionCurrency $currency): bool;
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void;
/**
* Enables a currency
*
* @param TransactionCurrency $currency
*/
public function enable(TransactionCurrency $currency): void;
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function find(int $currencyId): ?TransactionCurrency;
/**
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCode(string $currencyCode): ?TransactionCurrency;
/**
* @param string $name
*
* @return TransactionCurrency|null
*/
public function findByName(string $name): ?TransactionCurrency;
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency;
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency;
@@ -121,60 +78,23 @@ interface CurrencyRepositoryInterface
/**
* Get ALL currencies.
*
* @return Collection
*/
public function getAll(): Collection;
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids): Collection;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool;
/**
* @param TransactionCurrency $currency
*
* @return void
*/
public function makeDefault(TransactionCurrency $currency): void;
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency;
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency;
}

View File

@@ -33,13 +33,11 @@ class JournalRepository implements JournalRepositoryInterface
{
use UserGroupTrait;
/**
* @inheritDoc
*/
public function searchJournalDescriptions(string $search, int $limit): Collection
{
$query = $this->userGroup->transactionJournals()
->orderBy('date', 'DESC');
->orderBy('date', 'DESC')
;
if ('' !== $search) {
$query->where('description', 'LIKE', sprintf('%%%s%%', $search));
}

View File

@@ -33,18 +33,8 @@ interface JournalRepositoryInterface
{
/**
* Search in journal descriptions.
*
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchJournalDescriptions(string $search, int $limit): Collection;
/**
* @param User $user
*
* @return void
*/
public function setUser(User $user): void;
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* PiggyBankRepository.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -35,18 +34,16 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
{
use UserGroupTrait;
/**
* @inheritDoc
*/
public function getPiggyBanks(): Collection
{
return $this->userGroup->piggyBanks()
->with(
[
'account',
'objectGroups',
]
)
->orderBy('order', 'ASC')->get();
->with(
[
'account',
'objectGroups',
]
)
->orderBy('order', 'ASC')->get()
;
}
}

View File

@@ -1,6 +1,5 @@
<?php
/*
* PiggyBankRepositoryInterface.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -34,8 +33,6 @@ interface PiggyBankRepositoryInterface
{
/**
* Return all piggy banks.
*
* @return Collection
*/
public function getPiggyBanks(): Collection;
}