Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fixed chart. [skip ci]
James Colefc4ab2924420
diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.phpindex 6daafd877b..2230c2872f 100644--- a/app/Http/Controllers/Chart/ReportController.php+++ b/app/Http/Controllers/Chart/ReportController.php@@ -270,8 +270,8 @@ class ReportController extends Controllerwhile ($start < $end) {// total income and total expenses:$date = $start->format('Y-m');- $incomeSum = isset($earned[$date]) ? $earned[$date] * -1 : 0;- $expenseSum = isset($spent[$date]) ? $spent[$date] * -1 : 0;+ $incomeSum = isset($earned[$date]) ? $earned[$date] : 0;+ $expenseSum = isset($spent[$date]) ? $spent[$date] : 0;$entries->push([clone $start, $incomeSum, $expenseSum]);$start->addMonth();@@ -297,8 +297,8 @@ class ReportController extends Controller$count = 0;while ($start < $end) {$date = $start->format('Y-m');- $currentIncome = $earned[$date] ?? '0';- $currentExpense = $spent[$date] ??'0';+ $currentIncome = isset($earned[$date]) ? $earned[$date] : 0;+ $currentExpense = isset($spent[$date]) ? $spent[$date] : 0;$income = bcadd($income, $currentIncome);$expense = bcadd($expense, $currentExpense);diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.phpindex 5d9d9d9851..b775164f47 100644--- a/app/Repositories/Account/AccountRepository.php+++ b/app/Repositories/Account/AccountRepository.php@@ -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;- }}diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.phpindex 79fd4d6321..7238a81086 100644--- a/app/Repositories/Account/AccountRepositoryInterface.php+++ b/app/Repositories/Account/AccountRepositoryInterface.php@@ -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