Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
More code for previous.
James Cole114788567dca
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.phpindex 9d3f513b1f..156c2396b3 100644--- a/app/Helpers/Report/ReportHelper.php+++ b/app/Helpers/Report/ReportHelper.php@@ -7,7 +7,6 @@ use Carbon\Carbon;use FireflyIII\Models\Account;use Illuminate\Database\Query\JoinClause;use Illuminate\Support\Collection;-use Session;/*** Class ReportHelper@@ -87,7 +86,7 @@ class ReportHelper implements ReportHelperInterface$end = Carbon::now();$months = [];while ($start <= $end) {- $year = $start->format('Y');+ $year = $start->format('Y');$months[$year][] = ['formatted' => $start->format('F Y'),'month' => intval($start->format('m')),@@ -119,33 +118,37 @@ class ReportHelper implements ReportHelperInterface/*** @param Carbon $date+ * @param bool $showSharedReports** @return array*/- public function yearBalanceReport(Carbon $date)+ public function yearBalanceReport(Carbon $date, $showSharedReports = false){- $start = clone $date;- $end = clone $date;- $sharedAccounts = [];- $sharedCollection = \Auth::user()->accounts()- ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')- ->where('account_meta.name', '=', 'accountRole')- ->where('account_meta.data', '=', json_encode('sharedAsset'))- ->get(['accounts.id']);-- foreach ($sharedCollection as $account) {- $sharedAccounts[] = $account->id;+ $start = clone $date;+ $end = clone $date;+ $sharedAccounts = [];+ if ($showSharedReports === false) {+ $sharedCollection = \Auth::user()->accounts()+ ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')+ ->where('account_meta.name', '=', 'accountRole')+ ->where('account_meta.data', '=', json_encode('sharedAsset'))+ ->get(['accounts.id']);++ foreach ($sharedCollection as $account) {+ $sharedAccounts[] = $account->id;+ }}- $accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name','ASC')->get(['accounts.*'])->filter(- function (Account $account) use ($sharedAccounts) {- if (!in_array($account->id, $sharedAccounts)) {- return $account;- }+ $accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*'])+ ->filter(+ function (Account $account) use ($sharedAccounts) {+ if (!in_array($account->id, $sharedAccounts)) {+ return $account;+ }- return null;- }- );+ return null;+ }+ );$report = [];$start->startOfYear()->subDay();$end->endOfYear();diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.phpindex 49b9b541ac..dedc6b38cb 100644--- a/app/Helpers/Report/ReportHelperInterface.php+++ b/app/Helpers/Report/ReportHelperInterface.php@@ -15,43 +15,44 @@ interface ReportHelperInterface/**- * @param Carbon $date+ * This methods fails to take in account transfers FROM shared accounts.*- * @return array+ * @param Carbon $start+ * @param Carbon $end+ * @param int $limit+ *+ * @return Collection*/- public function listOfMonths(Carbon $date);+ public function expensesGroupedByAccount(Carbon $start, Carbon $end, $limit = 15);/**+ * This method gets some kind of list for a monthly overview.+ ** @param Carbon $date*- * @return array+ * @return Collection*/- public function listOfYears(Carbon $date);+ public function getBudgetsForMonth(Carbon $date);/*** @param Carbon $date** @return array*/- public function yearBalanceReport(Carbon $date);+ public function listOfMonths(Carbon $date);/**- * This methods fails to take in account transfers FROM shared accounts.- *- * @param Carbon $start- * @param Carbon $end- * @param int $limit+ * @param Carbon $date*- * @return Collection+ * @return array*/- public function expensesGroupedByAccount(Carbon $start, Carbon $end, $limit = 15);+ public function listOfYears(Carbon $date);/**- * This method gets some kind of list for a monthly overview.- ** @param Carbon $date+ * @param bool $showSharedReports*- * @return Collection+ * @return array*/- public function getBudgetsForMonth(Carbon $date);+ public function yearBalanceReport(Carbon $date, $showSharedReports = false);}\ No newline at end of filediff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.phpindex be762c34e8..a465799d03 100644--- a/app/Helpers/Report/ReportQuery.php+++ b/app/Helpers/Report/ReportQuery.php@@ -447,55 +447,60 @@ class ReportQuery implements ReportQueryInterface** @param Carbon $start* @param Carbon $end+ * @param bool $showSharedReports** @return Collection*/- public function journalsByRevenueAccount(Carbon $start, Carbon $end)+ public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false){- return TransactionJournal::+ $query = TransactionJournal::leftJoin('transactions as t_from', function (JoinClause $join) {$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);})- ->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')- ->leftJoin(- 'account_meta as acm_from', function (JoinClause $join) {- $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');- }- )- ->leftJoin(- 'transactions as t_to', function (JoinClause $join) {- $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);- }- )- ->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')- ->leftJoin(- 'account_meta as acm_to', function (JoinClause $join) {- $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');- }- )- ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')- ->where(- function ($query) {- $query->where(- function ($q) {- $q->where('transaction_types.type', 'Deposit');- $q->where('acm_to.data', '!=', '"sharedAsset"');- }- );- $query->orWhere(- function ($q) {- $q->where('transaction_types.type', 'Transfer');- $q->where('acm_from.data', '=', '"sharedAsset"');- }- );- }- )- ->before($end)->after($start)- ->where('transaction_journals.user_id', Auth::user()->id)- ->groupBy('t_from.account_id')->orderBy('amount')- ->get(['t_from.account_id as account_id', 'ac_from.name as name', DB::Raw('SUM(t_from.amount) as `amount`')]);+ ->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')+ ->leftJoin(+ 'account_meta as acm_from', function (JoinClause $join) {+ $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');+ }+ )+ ->leftJoin(+ 'transactions as t_to', function (JoinClause $join) {+ $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);+ }+ )+ ->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')+ ->leftJoin(+ 'account_meta as acm_to', function (JoinClause $join) {+ $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');+ }+ )+ ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');+ if ($showSharedReports === false) {++ $query->where(+ function ($query) {+ $query->where(+ function ($q) {+ $q->where('transaction_types.type', 'Deposit');+ $q->where('acm_to.data', '!=', '"sharedAsset"');+ }+ );+ $query->orWhere(+ function ($q) {+ $q->where('transaction_types.type', 'Transfer');+ $q->where('acm_from.data', '=', '"sharedAsset"');+ }+ );+ }+ );+ }+ $query->before($end)->after($start)+ ->where('transaction_journals.user_id', Auth::user()->id)+ ->groupBy('t_from.account_id')->orderBy('amount');++ return $query->get(['t_from.account_id as account_id', 'ac_from.name as name', DB::Raw('SUM(t_from.amount) as `amount`')]);}/**diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.phpindex 9ae250b8cf..e67581a047 100644--- a/app/Helpers/Report/ReportQueryInterface.php+++ b/app/Helpers/Report/ReportQueryInterface.php@@ -136,10 +136,11 @@ interface ReportQueryInterface** @param Carbon $start* @param Carbon $end+ * @param bool $showSharedReports** @return Collection*/- public function journalsByRevenueAccount(Carbon $start, Carbon $end);+ public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false);/*** With an equally misleading name, this query returns are transfers to shared accounts. These are considereddiff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.phpindex a8d3bf1912..f54ef7c911 100644--- a/app/Http/Controllers/ReportController.php+++ b/app/Http/Controllers/ReportController.php@@ -363,16 +363,19 @@ class ReportController extends Controller} catch (Exception $e) {return view('error')->with('message', 'Invalid date.');}- $date = new Carbon('01-01-' . $year);- $end = clone $date;++ $pref = Preferences::get('showSharedReports', false);+ $showSharedReports = $pref->data;+ $date = new Carbon('01-01-' . $year);+ $end = clone $date;$end->endOfYear();$title = 'Reports';$subTitle = $year;$subTitleIcon = 'fa-bar-chart';$mainTitleIcon = 'fa-line-chart';- $balances = $helper->yearBalanceReport($date);- $groupedIncomes = $query->journalsByRevenueAccount($date, $end);- $groupedExpenses = $query->journalsByExpenseAccount($date, $end);+ $balances = $helper->yearBalanceReport($date, $showSharedReports);+ $groupedIncomes = $query->journalsByRevenueAccount($date, $end, $showSharedReports);+ $groupedExpenses = $query->journalsByExpenseAccount($date, $end, $showSharedReports);//$groupedExpenses = $helper-> expensesGroupedByAccount($date, $end, 15);