mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fixed chart. [skip ci]
This commit is contained in:
parent
5a43e6cb9f
commit
fc4ab29244
@ -270,8 +270,8 @@ class ReportController extends Controller
|
|||||||
while ($start < $end) {
|
while ($start < $end) {
|
||||||
// total income and total expenses:
|
// total income and total expenses:
|
||||||
$date = $start->format('Y-m');
|
$date = $start->format('Y-m');
|
||||||
$incomeSum = isset($earned[$date]) ? $earned[$date] * -1 : 0;
|
$incomeSum = isset($earned[$date]) ? $earned[$date] : 0;
|
||||||
$expenseSum = isset($spent[$date]) ? $spent[$date] * -1 : 0;
|
$expenseSum = isset($spent[$date]) ? $spent[$date] : 0;
|
||||||
|
|
||||||
$entries->push([clone $start, $incomeSum, $expenseSum]);
|
$entries->push([clone $start, $incomeSum, $expenseSum]);
|
||||||
$start->addMonth();
|
$start->addMonth();
|
||||||
@ -297,8 +297,8 @@ class ReportController extends Controller
|
|||||||
$count = 0;
|
$count = 0;
|
||||||
while ($start < $end) {
|
while ($start < $end) {
|
||||||
$date = $start->format('Y-m');
|
$date = $start->format('Y-m');
|
||||||
$currentIncome = $earned[$date] ?? '0';
|
$currentIncome = isset($earned[$date]) ? $earned[$date] : 0;
|
||||||
$currentExpense = $spent[$date] ??'0';
|
$currentExpense = isset($spent[$date]) ? $spent[$date] : 0;
|
||||||
$income = bcadd($income, $currentIncome);
|
$income = bcadd($income, $currentIncome);
|
||||||
$expense = bcadd($expense, $currentExpense);
|
$expense = bcadd($expense, $currentExpense);
|
||||||
|
|
||||||
|
@ -82,13 +82,55 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||||
{
|
{
|
||||||
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
|
$incomes = $this->incomesInPeriod($accounts, $start, $end);
|
||||||
$sum = bcmul($this->sumInPeriod($accounts, $types, $start, $end), '-1');
|
$sum = '0';
|
||||||
|
foreach ($incomes as $entry) {
|
||||||
|
$amount = TransactionJournal::amount($entry);
|
||||||
|
$sum = bcadd($sum, $amount);
|
||||||
|
}
|
||||||
|
|
||||||
return $sum;
|
return $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
|
||||||
|
* 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 $journal;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* 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 $journal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $journals;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $accountId
|
* @param $accountId
|
||||||
*
|
*
|
||||||
@ -319,7 +361,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
$journals = $this->journalsInPeriod($accounts, $types, $start, $end);
|
$journals = $this->journalsInPeriod($accounts, $types, $start, $end);
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
$accountIds = $accounts->pluck('id')->toArray();
|
||||||
|
|
||||||
// filter because some of these journals are
|
// filter because some of these journals are still too much.
|
||||||
$journals = $journals->filter(
|
$journals = $journals->filter(
|
||||||
function (TransactionJournal $journal) use ($accountIds) {
|
function (TransactionJournal $journal) use ($accountIds) {
|
||||||
if ($journal->transaction_type_type == TransactionType::DEPOSIT) {
|
if ($journal->transaction_type_type == TransactionType::DEPOSIT) {
|
||||||
@ -478,8 +520,13 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||||
{
|
{
|
||||||
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
|
$incomes = $this->expensesInPeriod($accounts, $start, $end);
|
||||||
$sum = $this->sumInPeriod($accounts, $types, $start, $end);
|
$sum = '0';
|
||||||
|
foreach ($incomes as $entry) {
|
||||||
|
$amount = TransactionJournal::amountPositive($entry);
|
||||||
|
Log::debug('spentInPeriod amount: ' . $amount);
|
||||||
|
$sum = bcadd($sum, $amount);
|
||||||
|
}
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
@ -740,63 +787,4 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param array $types
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function sumInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): string
|
|
||||||
{
|
|
||||||
// first collect incoming transaction journals (where the $accounts receive the money).
|
|
||||||
$query = $this->user
|
|
||||||
->transactionjournals()
|
|
||||||
->distinct()
|
|
||||||
->transactionTypes($types)
|
|
||||||
->leftJoin(
|
|
||||||
'transactions as t', function (JoinClause $join) {
|
|
||||||
$join->on('t.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '>', 0);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($end >= $start) {
|
|
||||||
$query->before($end)->after($start);
|
|
||||||
}
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
|
||||||
$query->whereIn('t.account_id', $accountIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// that should do it:
|
|
||||||
$first = strval($query->sum('t.amount'));
|
|
||||||
|
|
||||||
// the the other way around:
|
|
||||||
$query = $this->user
|
|
||||||
->transactionjournals()
|
|
||||||
->distinct()
|
|
||||||
->transactionTypes($types)
|
|
||||||
->leftJoin(
|
|
||||||
'transactions as t', function (JoinClause $join) {
|
|
||||||
$join->on('t.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '<', 0);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($end >= $start) {
|
|
||||||
$query->before($end)->after($start);
|
|
||||||
}
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
|
||||||
$query->whereIn('t.account_id', $accountIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// that should do it:
|
|
||||||
$second = strval($query->sum('t.amount'));
|
|
||||||
$sum = bcadd($first, $second);
|
|
||||||
|
|
||||||
|
|
||||||
return $sum;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -107,9 +107,24 @@ interface AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection;
|
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will call AccountRepositoryInterface::journalsInPeriod and get all deposits made to the given $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
|
* as well as the transfers that move to to those $accounts. This is a slightly sharper selection
|
||||||
* than made by journalsInPeriod itself.
|
* than made by journalsInPeriod itself.
|
||||||
*
|
*
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
|
Loading…
Reference in New Issue
Block a user