mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 20:16:22 +00:00
Refactoring income and expense reports.
This commit is contained in:
@@ -84,46 +84,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* than made by journalsInPeriod itself.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountRepositoryInterface::journalsInPeriod
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function expensesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
|
||||
$journals = $this->journalsInPeriod($accounts, $types, $start, $end);
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
|
||||
// filter because some of these journals are still too much.
|
||||
$journals = $journals->filter(
|
||||
function (TransactionJournal $journal) use ($accountIds) {
|
||||
if ($journal->transaction_type_type == TransactionType::WITHDRAWAL) {
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* The source of a transfer must be one of the $accounts in order to
|
||||
* be included. Otherwise, it would not be an expense.
|
||||
*/
|
||||
if (in_array($journal->source_account_id, $accountIds)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
@@ -258,49 +218,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call AccountRepositoryInterface::journalsInPeriod and get all deposits made to the given $accounts,
|
||||
* as well as the transfers that move away to those $accounts. This is a slightly sharper selection
|
||||
* than made by journalsInPeriod itself.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountRepositoryInterface::journalsInPeriod
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function incomesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
|
||||
$journals = $this->journalsInPeriod($accounts, $types, $start, $end);
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
|
||||
// filter because some of these journals are still too much.
|
||||
$journals = $journals->filter(
|
||||
function (TransactionJournal $journal) use ($accountIds) {
|
||||
if ($journal->transaction_type_type == TransactionType::DEPOSIT) {
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* The destination of a transfer must be one of the $accounts in order to
|
||||
* be included. Otherwise, it would not be income.
|
||||
*/
|
||||
$destinations = TransactionJournal::destinationAccountList($journal)->pluck('id')->toArray();
|
||||
|
||||
if (count(array_intersect($destinations, $accountIds)) > 0) {
|
||||
// at least one of $target is in $haystack
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param array $types
|
||||
|
@@ -46,21 +46,6 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function destroy(Account $account, Account $moveTo): bool;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* than made by journalsInPeriod itself.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountRepositoryInterface::journalsInPeriod
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function expensesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
@@ -96,21 +81,6 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* This method will call AccountRepositoryInterface::journalsInPeriod and get all deposits made to the given $accounts,
|
||||
* as well as the transfers that move to to those $accounts. This is a slightly sharper selection
|
||||
* than made by journalsInPeriod itself.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountRepositoryInterface::journalsInPeriod
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function incomesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param array $types
|
||||
|
@@ -14,6 +14,8 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Repositories\Account;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Crypt;
|
||||
use DB;
|
||||
use FireflyIII\Helpers\Collection\Account as AccountCollection;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
@@ -21,6 +23,7 @@ use FireflyIII\User;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use stdClass;
|
||||
use Steam;
|
||||
|
||||
/**
|
||||
@@ -101,6 +104,35 @@ class AccountTasker implements AccountTaskerInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @see self::financialReport
|
||||
*
|
||||
*/
|
||||
public function expenseReport(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$idList = [
|
||||
'accounts' => $accounts->pluck('id')->toArray(),
|
||||
'exclude' => $excluded->pluck('id')->toArray(),
|
||||
];
|
||||
|
||||
Log::debug(
|
||||
'Now calling expenseReport.',
|
||||
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
|
||||
'start' => $start->format('Y-m-d'),
|
||||
'end' => $end->format('Y-m-d'),
|
||||
]
|
||||
);
|
||||
|
||||
return $this->financialReport($idList, $start, $end, false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -157,6 +189,35 @@ class AccountTasker implements AccountTaskerInterface
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountTasker::financialReport()
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
*/
|
||||
public function incomeReport(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$idList = [
|
||||
'accounts' => $accounts->pluck('id')->toArray(),
|
||||
'exclude' => $excluded->pluck('id')->toArray(),
|
||||
];
|
||||
|
||||
Log::debug(
|
||||
'Now calling expenseReport.',
|
||||
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
|
||||
'start' => $start->format('Y-m-d'),
|
||||
'end' => $end->format('Y-m-d'),
|
||||
]
|
||||
);
|
||||
|
||||
return $this->financialReport($idList, $start, $end, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -214,4 +275,86 @@ class AccountTasker implements AccountTaskerInterface
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This method will determin how much has flown (in the given period) from OR to $accounts to/from anywhere else,
|
||||
* except $excluded. This could be a list of incomes, or a list of expenses. This method shows
|
||||
* the name, the amount and the number of transactions. It is a summary, and only used in some reports.
|
||||
*
|
||||
* $incoming=true a list of incoming money (earnings)
|
||||
* $incoming=false a list of outgoing money (expenses).
|
||||
*
|
||||
* @param array $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param bool $incoming
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
protected function financialReport(array $accounts, Carbon $start, Carbon $end, bool $incoming): Collection
|
||||
{
|
||||
$collection = new Collection;
|
||||
$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);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts as other_account', 'other_account.id', '=', 'other_side.account_id')
|
||||
->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('other_side.amount', '=', DB::raw('transactions.amount * -1'))
|
||||
->where('transactions.amount', $selection, 0)
|
||||
->orderBy('transactions.amount');
|
||||
|
||||
if (count($accounts['exclude']) > 0) {
|
||||
$query->whereNotIn('other_side.account_id', $accounts['exclude']);
|
||||
}
|
||||
$set = $query->get(
|
||||
[
|
||||
'other_side.account_id',
|
||||
'other_account.name',
|
||||
'other_account.encrypted',
|
||||
'transactions.amount',
|
||||
]
|
||||
);
|
||||
// summarize ourselves:
|
||||
$temp = [];
|
||||
foreach ($set as $entry) {
|
||||
// save into $temp:
|
||||
$id = intval($entry->account_id);
|
||||
if (isset($temp[$id])) {
|
||||
$temp[$id]['count']++;
|
||||
$temp[$id]['amount'] = bcadd($temp[$id]['amount'], $entry->amount);
|
||||
}
|
||||
if (!isset($temp[$id])) {
|
||||
$temp[$id] = [
|
||||
'name' => intval($entry->encrypted) === 1 ? Crypt::decrypt($entry->name) : $entry->name,
|
||||
'amount' => $entry->amount,
|
||||
'count' => 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// loop $temp and create collection:
|
||||
foreach ($temp as $key => $entry) {
|
||||
$object = new stdClass();
|
||||
$object->id = $key;
|
||||
$object->name = $entry['name'];
|
||||
$object->count = $entry['count'];
|
||||
$object->amount = $entry['amount'];
|
||||
$collection->push($object);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
}
|
@@ -49,6 +49,19 @@ interface AccountTaskerInterface
|
||||
*/
|
||||
public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountTasker::financialReport()
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
*/
|
||||
public function expenseReport(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -58,4 +71,17 @@ interface AccountTaskerInterface
|
||||
*/
|
||||
public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts): AccountCollection;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountTasker::financialReport()
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
*/
|
||||
public function incomeReport(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
}
|
Reference in New Issue
Block a user