Various code reshuffelling.

This commit is contained in:
James Cole
2021-03-12 06:20:01 +01:00
parent 97561ab9c9
commit 748d61fb8f
51 changed files with 1874 additions and 1873 deletions

View File

@@ -277,6 +277,27 @@ class AccountRepository implements AccountRepositoryInterface
return $query->get(['accounts.*']);
}
/**
* @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());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
/**
* @return Account
*
@@ -293,6 +314,38 @@ class AccountRepository implements AccountRepositoryInterface
return $factory->findOrCreate('Cash account', $type->type);
}
/**
* @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');
}]
);
if (!empty($types)) {
$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
{
return $account->locations()->first();
}
/**
* Return meta value for account. Null if not found.
*
@@ -463,6 +516,22 @@ class AccountRepository implements AccountRepositoryInterface
return $factory->create($data);
}
/**
* @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();
}
/**
* @param Account $account
*
@@ -514,6 +583,22 @@ class AccountRepository implements AccountRepositoryInterface
return $result;
}
/**
* @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();
}
}
/**
* @param string $query
* @param array $types
@@ -546,129 +631,6 @@ class AccountRepository implements AccountRepositoryInterface
return $dbQuery->take($limit)->get(['accounts.*']);
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
*
* @return Account
* @throws FireflyException
*/
public function store(array $data): Account
{
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->create($data);
}
/**
* @param Account $account
* @param array $data
*
* @return Account
* @throws FireflyException
*/
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
/**
* @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');
}]
);
if (!empty($types)) {
$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
{
return $account->locations()->first();
}
/**
* @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());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
/**
* @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();
}
/**
* @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();
}
}
/**
* @inheritDoc
*/
@@ -707,6 +669,14 @@ class AccountRepository implements AccountRepositoryInterface
return $dbQuery->take($limit)->get(['accounts.*']);
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
@@ -748,9 +718,39 @@ class AccountRepository implements AccountRepositoryInterface
// set the rest to zero:
$this->user->accounts()
->leftJoin('account_types', 'accounts.account_type_id', 'account_types.id')
->whereNotIn('account_types.type', [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE])
->update(['order' => '0']);
->leftJoin('account_types', 'accounts.account_type_id', 'account_types.id')
->whereNotIn('account_types.type', [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE])
->update(['order' => '0']);
}
/**
* @param array $data
*
* @return Account
* @throws FireflyException
*/
public function store(array $data): Account
{
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->create($data);
}
/**
* @param Account $account
* @param array $data
*
* @return Account
* @throws FireflyException
*/
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
}

View File

@@ -47,41 +47,6 @@ interface AccountRepositoryInterface
*/
public function count(array $types): int;
/**
* Reset order types of the mentioned accounts.
*
* @param array $types
*/
public function resetAccountOrder(array $types): void;
/**
* @param Account $account
*
* @return Collection
*/
public function getUsedCurrencies(Account $account): Collection;
/**
* Sort accounts (and fix the sort if necessary).
*/
public function sortAccounts(): void;
/**
* @param Account $account
*
* @return Collection
*/
public function getAttachments(Account $account): Collection;
/**
* Get account location, if any.
*
* @param Account $account
*
* @return Location|null
*/
public function getLocation(Account $account): ?Location;
/**
* Moved here from account CRUD.
*
@@ -161,6 +126,18 @@ interface AccountRepositoryInterface
*/
public function getActiveAccountsByType(array $types): Collection;
/**
* @param Account $account
*
* @return Collection
*/
public function getAttachments(Account $account): Collection;
/**
* @return Account
*/
public function getCashAccount(): Account;
/**
* @param array $types
*
@@ -169,9 +146,13 @@ interface AccountRepositoryInterface
public function getInactiveAccountsByType(array $types): Collection;
/**
* @return Account
* Get account location, if any.
*
* @param Account $account
*
* @return Location|null
*/
public function getCashAccount(): Account;
public function getLocation(Account $account): ?Location;
/**
* Return meta value for account. Null if not found.
@@ -241,6 +222,12 @@ interface AccountRepositoryInterface
*/
public function getReconciliation(Account $account): ?Account;
/**
* @param Account $account
*
* @return Collection
*/
public function getUsedCurrencies(Account $account): Collection;
/**
* @param Account $account
@@ -249,7 +236,6 @@ interface AccountRepositoryInterface
*/
public function isLiability(Account $account): bool;
/**
* Returns the date of the very first transaction in this account.
*
@@ -268,6 +254,13 @@ interface AccountRepositoryInterface
*/
public function oldestJournalDate(Account $account): ?Carbon;
/**
* Reset order types of the mentioned accounts.
*
* @param array $types
*/
public function resetAccountOrder(array $types): void;
/**
* @param string $query
* @param array $types
@@ -291,6 +284,11 @@ interface AccountRepositoryInterface
*/
public function setUser(User $user);
/**
* Sort accounts (and fix the sort if necessary).
*/
public function sortAccounts(): void;
/**
* @param array $data
*

View File

@@ -33,16 +33,16 @@ interface AccountTaskerInterface
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
@@ -50,8 +50,8 @@ interface AccountTaskerInterface
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array

View File

@@ -24,13 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
*
@@ -163,8 +161,8 @@ class OperationsRepository implements OperationsRepositoryInterface
/**
* @inheritDoc
*/
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $expense = null, ?TransactionCurrency $currency = null): array
{
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $expense = null, ?TransactionCurrency $currency = null
): array {
$start->startOfDay();
$end->endOfDay();
@@ -178,7 +176,7 @@ class OperationsRepository implements OperationsRepositoryInterface
if (null !== $accounts) {
$collector->setSourceAccounts($accounts);
}
if(null !== $expense) {
if (null !== $expense) {
$collector->setDestinationAccounts($expense);
}
if (null !== $currency) {
@@ -233,15 +231,17 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->negative($journal['foreign_amount']));
}
}
return $array;
}
/**
* // TODO same as income but copied.
*
* @inheritDoc
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $revenue = null, ?TransactionCurrency $currency = null): array
{
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $revenue = null, ?TransactionCurrency $currency = null
): array {
$start->startOfDay();
$end->endOfDay();
@@ -255,7 +255,7 @@ class OperationsRepository implements OperationsRepositoryInterface
if (null !== $accounts) {
$collector->setDestinationAccounts($accounts);
}
if(null !== $revenue) {
if (null !== $revenue) {
$collector->setSourceAccounts($revenue);
}
if (null !== $currency) {
@@ -273,7 +273,7 @@ class OperationsRepository implements OperationsRepositoryInterface
if (null !== $accounts) {
$collector->setDestinationAccounts($accounts);
}
if(null !== $revenue) {
if (null !== $revenue) {
$collector->setSourceAccounts($revenue);
}
$result = $collector->getExtractedJournals();
@@ -309,6 +309,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->positive($journal['foreign_amount']));
}
}
return $array;
}
@@ -375,6 +376,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->positive($journal['foreign_amount']));
}
}
return $array;
}
}

View File

@@ -38,8 +38,8 @@ interface OperationsRepositoryInterface
* which have the specified accounts. 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 Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
@@ -75,7 +75,8 @@ interface OperationsRepositoryInterface
*
* @return array
*/
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $expense = null, ?TransactionCurrency $currency = null): array;
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $expense = null, ?TransactionCurrency $currency = null
): array;
/**
* Sum of income journals in period for a set of accounts, grouped per currency. Amounts are always positive.
@@ -88,7 +89,8 @@ interface OperationsRepositoryInterface
*
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $revenue = null, ?TransactionCurrency $currency = null): array;
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $revenue = null, ?TransactionCurrency $currency = null
): array;
/**
* Sum of transfers in period for a set of accounts, grouped per currency. Amounts are always positive.