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);
}
}

View File

@@ -41,15 +41,6 @@ interface AccountRepositoryInterface
*/
public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
/**
* 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
@@ -160,27 +151,4 @@ interface AccountRepositoryInterface
*/
public function openingBalanceTransaction(Account $account) : TransactionJournal;
/**
* 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;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
}

View File

@@ -14,10 +14,12 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\User;
use Illuminate\Support\Collection;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\User;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Log;
use Steam;
@@ -26,7 +28,7 @@ use Steam;
*
* @package FireflyIII\Repositories\Account
*/
class AccountTasker
class AccountTasker implements AccountTaskerInterface
{
/** @var User */
private $user;
@@ -41,6 +43,64 @@ class AccountTasker
$this->user = $user;
}
/**
* @see self::amountInPeriod
*
* @param Collection $accounts
* @param Collection $excluded
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string
{
$idList = [
'accounts' => $accounts->pluck('id')->toArray(),
'exclude' => $excluded->pluck('id')->toArray(),
];
Log::debug(
'Now calling amountInInPeriod.',
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
]
);
return $this->amountInPeriod($idList, $start, $end, true);
}
/**
* @see self::amountInPeriod
*
* @param Collection $accounts
* @param Collection $excluded
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string
{
$idList = [
'accounts' => $accounts->pluck('id')->toArray(),
'exclude' => $excluded->pluck('id')->toArray(),
];
Log::debug(
'Now calling amountOutInPeriod.',
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
]
);
return $this->amountInPeriod($idList, $start, $end, false);
}
/**
* @param Carbon $start
* @param Carbon $end
@@ -96,4 +156,62 @@ class AccountTasker
return $object;
}
/**
* Will return how much money has been going out (ie. spent) by the given account(s).
* Alternatively, will return how much money has been coming in (ie. earned) by the given accounts.
*
* Enter $incoming=true for any money coming in (income)
* Enter $incoming=false for any money going out (expenses)
*
* This means any money going out or in. You can also submit accounts to exclude,
* so transfers between accounts are not included.
*
* As a general rule:
*
* - Asset accounts should return both expenses and earnings. But could return 0.
* - Expense accounts (where money is spent) should only return earnings (the account gets money).
* - Revenue accounts (where money comes from) should only return expenses (they spend money).
*
* @param array $accounts
* @param Carbon $start
* @param Carbon $end
* @param bool $incoming
*
* @return string
*/
protected function amountInPeriod(array $accounts, Carbon $start, Carbon $end, bool $incoming): string
{
$joinModifier = $incoming ? '<' : '>';
$selection = $incoming ? '>' : '<';
$query = Transaction
::distinct()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin(
'transactions as other_side', function (JoinClause $join) use ($joinModifier) {
$join->on('transaction_journals.id', '=', 'other_side.transaction_journal_id')->where('other_side.amount', $joinModifier, 0);
}
)
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->where('transaction_journals.user_id', $this->user->id)
->whereNull('transactions.deleted_at')
->whereNull('transaction_journals.deleted_at')
->whereIn('transactions.account_id', $accounts['accounts'])
->where('transactions.amount', $selection, 0);
if (count($accounts['exclude']) > 0) {
$query->whereNotIn('other_side.account_id', $accounts['exclude']);
}
$result = $query->get(['transactions.id', 'transactions.amount']);
$sum = strval($result->sum('amount'));
if (strlen($sum) === 0) {
Log::debug('Sum is empty.');
$sum = '0';
}
Log::debug(sprintf('Result is %s', $sum));
return $sum;
}
}

View File

@@ -25,6 +25,30 @@ use Illuminate\Support\Collection;
interface AccountTaskerInterface
{
/**
* @param Collection $accounts
* @param Collection $excluded
* @param Carbon $start
* @param Carbon $end
*
* @see AccountTasker::amountInPeriod()
*
* @return string
*/
public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string;
/**
* @param Collection $accounts
* @param Collection $excluded
* @param Carbon $start
* @param Carbon $end
*
* @see AccountTasker::amountInPeriod()
*
* @return string
*/
public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string;
/**
* @param Carbon $start
* @param Carbon $end

View File

@@ -42,6 +42,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
* @param string $fileType
*
* @return ImportJob
* @throws FireflyException
*/
public function create(string $fileType): ImportJob
{