Move some methods around, refactoring.

This commit is contained in:
James Cole
2016-10-09 07:58:27 +02:00
parent ea7ee7ee9a
commit c64771b76b
19 changed files with 207 additions and 343 deletions

View File

@@ -23,7 +23,6 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Steam;
@@ -51,108 +50,6 @@ class AccountRepository implements AccountRepositoryInterface
$this->user = $user;
}
/**
* This method is almost the same as ::earnedInPeriod, but only works for revenue accounts
* instead of the implied asset accounts for ::earnedInPeriod. ::earnedInPeriod will tell you
* how much money was earned by the given asset accounts. This method will tell you how much money
* these given revenue accounts sent. Ie. how much money was made FROM these revenue accounts.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
{
$query = $this->user->transactionJournals()->expanded()->sortCorrectly()
->transactionTypes([TransactionType::DEPOSIT]);
if ($end >= $start) {
$query->before($end)->after($start);
}
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$query->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
}
);
$query->whereIn('source.account_id', $accountIds);
$query->whereNull('source.deleted_at');
}
// remove group by
$query->getQuery()->getQuery()->groups = null;
// get id's
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
// that should do it:
$sum = $this->user->transactions()
->whereIn('transaction_journal_id', $ids)
->where('amount', '>', '0')
->whereNull('transactions.deleted_at')
->sum('amount');
return strval($sum);
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
{
$query = $this->user->transactionJournals()->expanded()->sortCorrectly()
->transactionTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]);
if ($end >= $start) {
$query->before($end)->after($start);
}
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$query->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
}
);
$query->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
}
);
$query->whereIn('destination.account_id', $accountIds);
$query->whereNotIn('source.account_id', $accountIds);
$query->whereNull('destination.deleted_at');
$query->whereNull('source.deleted_at');
}
// remove group by
$query->getQuery()->getQuery()->groups = null;
// get id's
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
// that should do it:
$sum = $this->user->transactions()
->whereIn('transaction_journal_id', $ids)
->where('amount', '>', '0')
->whereNull('transactions.deleted_at')
->sum('amount');
return strval($sum);
}
/**
* This method will call AccountRepositoryInterface::journalsInPeriod and get all withdrawaks made from the given $accounts,
* as well as the transfers that move away from those $accounts. This is a slightly sharper selection
@@ -514,98 +411,4 @@ class AccountRepository implements AccountRepositoryInterface
return $journal;
}
/**
* This method is almost the same as ::spentInPeriod, but only works for expense accounts
* instead of the implied asset accounts for ::spentInPeriod. ::spentInPeriod will tell you
* how much money was spent by the given asset accounts. This method will tell you how much money
* these given expense accounts received. Ie. how much money was spent AT these expense accounts.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentAtInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var HasMany $query */
$query = $this->user->transactionJournals()->expanded()
->transactionTypes([TransactionType::WITHDRAWAL]);
if ($end >= $start) {
$query->before($end)->after($start);
}
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$query->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
}
);
$query->whereIn('destination.account_id', $accountIds);
}
// remove group by
$query->getQuery()->getQuery()->groups = null;
// that should do it:
$sum = strval($query->sum('destination.amount'));
if (is_null($sum)) {
$sum = '0';
}
$sum = bcmul($sum, '-1');
return $sum;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var HasMany $query */
$query = $this->user->transactionJournals()->expanded()
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]);
if ($end >= $start) {
$query->before($end)->after($start);
}
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$query->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
}
);
$query->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
}
);
$query->whereIn('source.account_id', $accountIds);
$query->whereNotIn('destination.account_id', $accountIds);
$query->whereNull('source.deleted_at');
$query->whereNull('destination.deleted_at');
$query->distinct();
}
// remove group by
$query->getQuery()->getQuery()->groups = null;
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
$sum = $this->user->transactions()
->whereIn('transaction_journal_id', $ids)
->where('amount', '<', '0')
->whereNull('transactions.deleted_at')
->sum('amount');
return strval($sum);
}
}