mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Fixed chart. [skip ci]
This commit is contained in:
@@ -82,13 +82,55 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
|
||||
$sum = bcmul($this->sumInPeriod($accounts, $types, $start, $end), '-1');
|
||||
$incomes = $this->incomesInPeriod($accounts, $start, $end);
|
||||
$sum = '0';
|
||||
foreach ($incomes as $entry) {
|
||||
$amount = TransactionJournal::amount($entry);
|
||||
$sum = bcadd($sum, $amount);
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
@@ -319,7 +361,7 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
$journals = $this->journalsInPeriod($accounts, $types, $start, $end);
|
||||
$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(
|
||||
function (TransactionJournal $journal) use ($accountIds) {
|
||||
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
|
||||
{
|
||||
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
|
||||
$sum = $this->sumInPeriod($accounts, $types, $start, $end);
|
||||
$incomes = $this->expensesInPeriod($accounts, $start, $end);
|
||||
$sum = '0';
|
||||
foreach ($incomes as $entry) {
|
||||
$amount = TransactionJournal::amountPositive($entry);
|
||||
Log::debug('spentInPeriod amount: ' . $amount);
|
||||
$sum = bcadd($sum, $amount);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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,
|
||||
* 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.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
|
Reference in New Issue
Block a user