mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-14 08:11:20 +00:00
Improve report sums.
This commit is contained in:
@@ -16,9 +16,8 @@ namespace FireflyIII\Http\Controllers\Chart;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
@@ -166,6 +165,8 @@ class ReportController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$source = $this->getChartData($accounts, $start, $end);
|
$source = $this->getChartData($accounts, $start, $end);
|
||||||
$numbers = [
|
$numbers = [
|
||||||
'sum_earned' => '0',
|
'sum_earned' => '0',
|
||||||
@@ -249,31 +250,38 @@ class ReportController extends Controller
|
|||||||
$cache->addProperty($accounts);
|
$cache->addProperty($accounts);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
// return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$currentStart = clone $start;
|
$currentStart = clone $start;
|
||||||
$spentArray = [];
|
$spentArray = [];
|
||||||
$earnedArray = [];
|
$earnedArray = [];
|
||||||
|
|
||||||
|
/** @var AccountTaskerInterface $tasker */
|
||||||
|
$tasker = app(AccountTaskerInterface::class);
|
||||||
|
|
||||||
while ($currentStart <= $end) {
|
while ($currentStart <= $end) {
|
||||||
|
|
||||||
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
||||||
|
$earned = strval(
|
||||||
|
array_sum(
|
||||||
|
array_map(
|
||||||
|
function ($item) {
|
||||||
|
return $item['sum'];
|
||||||
|
}, $tasker->getIncomeReport($currentStart, $currentEnd, $accounts)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
// try a collector for income:
|
$spent = strval(
|
||||||
/** @var JournalCollectorInterface $collector */
|
array_sum(
|
||||||
$collector = app(JournalCollectorInterface::class);
|
array_map(
|
||||||
$collector->setAccounts($accounts)->setRange($currentStart, $currentEnd)
|
function ($item) {
|
||||||
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
return $item['sum'];
|
||||||
->withOpposingAccount();
|
}, $tasker->getExpenseReport($currentStart, $currentEnd, $accounts)
|
||||||
$earned = strval($collector->getJournals()->sum('transaction_amount'));
|
)
|
||||||
|
)
|
||||||
// try a collector for expenses:
|
);
|
||||||
/** @var JournalCollectorInterface $collector */
|
|
||||||
$collector = app(JournalCollectorInterface::class);
|
|
||||||
$collector->setAccounts($accounts)->setRange($currentStart, $currentEnd)
|
|
||||||
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
|
||||||
->withOpposingAccount();
|
|
||||||
$spent = strval($collector->getJournals()->sum('transaction_amount'));
|
|
||||||
|
|
||||||
|
|
||||||
$label = $currentStart->format('Y-m') . '-01';
|
$label = $currentStart->format('Y-m') . '-01';
|
||||||
|
@@ -19,6 +19,7 @@ use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
|||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
@@ -31,13 +32,14 @@ class OperationsController extends Controller
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param AccountTaskerInterface $tasker
|
||||||
* @param Carbon $start
|
* @param Collection $accounts
|
||||||
* @param Carbon $end
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return mixed|string
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
|
public function expenses(AccountTaskerInterface $tasker, Collection $accounts, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
@@ -48,7 +50,7 @@ class OperationsController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$entries = $this->getExpenseReport($start, $end, $accounts);
|
$entries = $tasker->getExpenseReport($start, $end, $accounts);
|
||||||
$type = 'expense-entry';
|
$type = 'expense-entry';
|
||||||
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
|
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
|
||||||
$cache->store($result);
|
$cache->store($result);
|
||||||
@@ -58,13 +60,14 @@ class OperationsController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param AccountTaskerInterface $tasker
|
||||||
* @param Carbon $start
|
* @param Collection $accounts
|
||||||
* @param Carbon $end
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function income(Collection $accounts, Carbon $start, Carbon $end)
|
public function income(AccountTaskerInterface $tasker, Collection $accounts, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
@@ -75,7 +78,7 @@ class OperationsController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$entries = $this->getIncomeReport($start, $end, $accounts);
|
$entries = $tasker->getIncomeReport($start, $end, $accounts);
|
||||||
$type = 'income-entry';
|
$type = 'income-entry';
|
||||||
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
|
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
|
||||||
|
|
||||||
@@ -86,13 +89,14 @@ class OperationsController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param AccountTaskerInterface $tasker
|
||||||
* @param Carbon $start
|
* @param Collection $accounts
|
||||||
* @param Carbon $end
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return mixed|string
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function operations(Collection $accounts, Carbon $start, Carbon $end)
|
public function operations(AccountTaskerInterface $tasker, Collection $accounts, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
@@ -104,8 +108,8 @@ class OperationsController extends Controller
|
|||||||
return $cache->get(); // @codeCoverageIgnore
|
return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$incomes = $this->getIncomeReport($start, $end, $accounts);
|
$incomes = $tasker->getIncomeReport($start, $end, $accounts);
|
||||||
$expenses = $this->getExpenseReport($start, $end, $accounts);
|
$expenses = $tasker->getExpenseReport($start, $end, $accounts);
|
||||||
$incomeSum = array_sum(
|
$incomeSum = array_sum(
|
||||||
array_map(
|
array_map(
|
||||||
function ($item) {
|
function ($item) {
|
||||||
@@ -129,125 +133,4 @@ class OperationsController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
* @param Collection $accounts
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array
|
|
||||||
{
|
|
||||||
// get all expenses for the given accounts in the given period!
|
|
||||||
// also transfers!
|
|
||||||
// get all transactions:
|
|
||||||
/** @var JournalCollectorInterface $collector */
|
|
||||||
$collector = app(JournalCollectorInterface::class);
|
|
||||||
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
||||||
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
|
||||||
->withOpposingAccount()
|
|
||||||
->enableInternalFilter();
|
|
||||||
$transactions = $collector->getJournals();
|
|
||||||
$transactions = $transactions->filter(
|
|
||||||
function (Transaction $transaction) {
|
|
||||||
// return negative amounts only.
|
|
||||||
if (bccomp($transaction->transaction_amount, '0') === -1) {
|
|
||||||
return $transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
$expenses = $this->groupByOpposing($transactions);
|
|
||||||
|
|
||||||
// sort the result
|
|
||||||
// Obtain a list of columns
|
|
||||||
$sum = [];
|
|
||||||
foreach ($expenses as $accountId => $row) {
|
|
||||||
$sum[$accountId] = floatval($row['sum']);
|
|
||||||
}
|
|
||||||
|
|
||||||
array_multisort($sum, SORT_ASC, $expenses);
|
|
||||||
|
|
||||||
return $expenses;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
* @param Collection $accounts
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
|
|
||||||
{
|
|
||||||
// get all expenses for the given accounts in the given period!
|
|
||||||
// also transfers!
|
|
||||||
// get all transactions:
|
|
||||||
/** @var JournalCollectorInterface $collector */
|
|
||||||
$collector = app(JournalCollectorInterface::class);
|
|
||||||
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
||||||
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
|
||||||
->withOpposingAccount()
|
|
||||||
->enableInternalFilter();
|
|
||||||
$transactions = $collector->getJournals();
|
|
||||||
$transactions = $transactions->filter(
|
|
||||||
function (Transaction $transaction) {
|
|
||||||
// return positive amounts only.
|
|
||||||
if (bccomp($transaction->transaction_amount, '0') === 1) {
|
|
||||||
return $transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
$income = $this->groupByOpposing($transactions);
|
|
||||||
|
|
||||||
// sort the result
|
|
||||||
// Obtain a list of columns
|
|
||||||
$sum = [];
|
|
||||||
foreach ($income as $accountId => $row) {
|
|
||||||
$sum[$accountId] = floatval($row['sum']);
|
|
||||||
}
|
|
||||||
|
|
||||||
array_multisort($sum, SORT_DESC, $income);
|
|
||||||
|
|
||||||
return $income;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $transactions
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function groupByOpposing(Collection $transactions): array
|
|
||||||
{
|
|
||||||
$expenses = [];
|
|
||||||
// join the result together:
|
|
||||||
foreach ($transactions as $transaction) {
|
|
||||||
$opposingId = $transaction->opposing_account_id;
|
|
||||||
$name = $transaction->opposing_account_name;
|
|
||||||
if (!isset($expenses[$opposingId])) {
|
|
||||||
$expenses[$opposingId] = [
|
|
||||||
'id' => $opposingId,
|
|
||||||
'name' => $name,
|
|
||||||
'sum' => '0',
|
|
||||||
'average' => '0',
|
|
||||||
'count' => 0,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
$expenses[$opposingId]['sum'] = bcadd($expenses[$opposingId]['sum'], $transaction->transaction_amount);
|
|
||||||
$expenses[$opposingId]['count']++;
|
|
||||||
}
|
|
||||||
// do averages:
|
|
||||||
foreach ($expenses as $key => $entry) {
|
|
||||||
if ($expenses[$key]['count'] > 1) {
|
|
||||||
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], strval($expenses[$key]['count']));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return $expenses;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -14,9 +14,10 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Repositories\Account;
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Database\Query\JoinClause;
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
use Steam;
|
use Steam;
|
||||||
@@ -89,6 +90,92 @@ class AccountTasker implements AccountTaskerInterface
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param Collection $accounts
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
|
{
|
||||||
|
// get all expenses for the given accounts in the given period!
|
||||||
|
// also transfers!
|
||||||
|
// get all transactions:
|
||||||
|
/** @var JournalCollectorInterface $collector */
|
||||||
|
$collector = app(JournalCollectorInterface::class);
|
||||||
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
||||||
|
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||||
|
->withOpposingAccount()
|
||||||
|
->enableInternalFilter();
|
||||||
|
$transactions = $collector->getJournals();
|
||||||
|
$transactions = $transactions->filter(
|
||||||
|
function (Transaction $transaction) {
|
||||||
|
// return negative amounts only.
|
||||||
|
if (bccomp($transaction->transaction_amount, '0') === -1) {
|
||||||
|
return $transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$expenses = $this->groupByOpposing($transactions);
|
||||||
|
|
||||||
|
// sort the result
|
||||||
|
// Obtain a list of columns
|
||||||
|
$sum = [];
|
||||||
|
foreach ($expenses as $accountId => $row) {
|
||||||
|
$sum[$accountId] = floatval($row['sum']);
|
||||||
|
}
|
||||||
|
|
||||||
|
array_multisort($sum, SORT_ASC, $expenses);
|
||||||
|
|
||||||
|
return $expenses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param Collection $accounts
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
|
{
|
||||||
|
// get all expenses for the given accounts in the given period!
|
||||||
|
// also transfers!
|
||||||
|
// get all transactions:
|
||||||
|
/** @var JournalCollectorInterface $collector */
|
||||||
|
$collector = app(JournalCollectorInterface::class);
|
||||||
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
||||||
|
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||||
|
->withOpposingAccount()
|
||||||
|
->enableInternalFilter();
|
||||||
|
$transactions = $collector->getJournals();
|
||||||
|
$transactions = $transactions->filter(
|
||||||
|
function (Transaction $transaction) {
|
||||||
|
// return positive amounts only.
|
||||||
|
if (bccomp($transaction->transaction_amount, '0') === 1) {
|
||||||
|
return $transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$income = $this->groupByOpposing($transactions);
|
||||||
|
|
||||||
|
// sort the result
|
||||||
|
// Obtain a list of columns
|
||||||
|
$sum = [];
|
||||||
|
foreach ($income as $accountId => $row) {
|
||||||
|
$sum[$accountId] = floatval($row['sum']);
|
||||||
|
}
|
||||||
|
|
||||||
|
array_multisort($sum, SORT_DESC, $income);
|
||||||
|
|
||||||
|
return $income;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
@@ -97,4 +184,38 @@ class AccountTasker implements AccountTaskerInterface
|
|||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $transactions
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function groupByOpposing(Collection $transactions): array
|
||||||
|
{
|
||||||
|
$expenses = [];
|
||||||
|
// join the result together:
|
||||||
|
foreach ($transactions as $transaction) {
|
||||||
|
$opposingId = $transaction->opposing_account_id;
|
||||||
|
$name = $transaction->opposing_account_name;
|
||||||
|
if (!isset($expenses[$opposingId])) {
|
||||||
|
$expenses[$opposingId] = [
|
||||||
|
'id' => $opposingId,
|
||||||
|
'name' => $name,
|
||||||
|
'sum' => '0',
|
||||||
|
'average' => '0',
|
||||||
|
'count' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$expenses[$opposingId]['sum'] = bcadd($expenses[$opposingId]['sum'], $transaction->transaction_amount);
|
||||||
|
$expenses[$opposingId]['count']++;
|
||||||
|
}
|
||||||
|
// do averages:
|
||||||
|
foreach ($expenses as $key => $entry) {
|
||||||
|
if ($expenses[$key]['count'] > 1) {
|
||||||
|
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], strval($expenses[$key]['count']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $expenses;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,6 +33,24 @@ interface AccountTaskerInterface
|
|||||||
*/
|
*/
|
||||||
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
|
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param Collection $accounts
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param Collection $accounts
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user