diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index bdff3f2c90..df80eaa951 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -452,4 +452,25 @@ class ReportHelper implements ReportHelperInterface return $object; } + + /** + * Get a full report on the users incomes during the period for the given accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Income + */ + public function getIncomeReportForList($start, $end, Collection $accounts) + { + $object = new Income; + $set = $this->query->incomeInPeriodCorrectedForList($start, $end, $accounts); + foreach ($set as $entry) { + $object->addToTotal($entry->amount_positive); + $object->addOrCreateIncome($entry); + } + + return $object; + } } diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index e4cc893e1c..dd4166352a 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -36,8 +36,8 @@ interface ReportHelperInterface * This method generates a full report for the given period on all * given accounts * - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * @param Collection $accounts * * @return AccountCollection @@ -104,6 +104,17 @@ interface ReportHelperInterface */ public function getIncomeReport($start, $end, $shared); + /** + * Get a full report on the users incomes during the period for the given accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Income + */ + public function getIncomeReportForList($start, $end, Collection $accounts); + /** * @param Carbon $date * diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index ce942b696b..da559b003b 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -104,6 +104,7 @@ class ReportQuery implements ReportQueryInterface ); } $set = $query->get(['accounts.*']); + return $set; } @@ -141,7 +142,7 @@ class ReportQuery implements ReportQueryInterface function (Builder $q) { $q->where('transaction_types.type', TransactionType::TRANSFER); $q->where('acm_from.data', '=', '"sharedAsset"'); - $q->where('acm_to.data','!=','"sharedAsset"'); + $q->where('acm_to.data', '!=', '"sharedAsset"'); } ); } @@ -261,4 +262,79 @@ class ReportQuery implements ReportQueryInterface return $query; } + + /** + * This method works the same way as ReportQueryInterface::incomeInPeriod does, but instead of returning results + * will simply list the transaction journals only. This should allow any follow up counting to be accurate with + * regards to tags. It will only get the incomes to the specified accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Collection + */ + public function incomeInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts) + { + $query = $this->queryJournalsWithTransactions($start, $end); + + $ids = []; + /** @var Account $account */ + foreach ($accounts as $account) { + $ids[] = $account->id; + } + + // only get deposits not to a shared account + // and transfers to a shared account. + + // OR is a deposit + // OR is a transfer FROM a shared account to NOT a shared account. + // + + $query->where( + function (Builder $query) { + $query->where( + function (Builder $q) { + $q->where('transaction_types.type', TransactionType::DEPOSIT); + } + ); + $query->orWhere( + function (Builder $q) { + $q->where('transaction_types.type', TransactionType::TRANSFER); + $q->where('acm_from.data', '=', '"sharedAsset"'); + $q->where('acm_to.data', '!=', '"sharedAsset"'); + } + ); + } + ); + + // only include selected accounts. + $query->whereIn('acm_to.id', $ids); + + $query->orderBy('transaction_journals.date'); + + // get everything + $data = $query->get( + ['transaction_journals.*', 'transaction_types.type', 'ac_from.name as name', 'ac_from.id as account_id', 'ac_from.encrypted as account_encrypted'] + ); + + $data->each( + function (TransactionJournal $journal) { + if (intval($journal->account_encrypted) == 1) { + $journal->name = Crypt::decrypt($journal->name); + } + } + ); + $data = $data->filter( + function (TransactionJournal $journal) { + if ($journal->amount != 0) { + return $journal; + } + + return null; + } + ); + + return $data; + } } diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.php index c48aeeab59..8a234bfa4a 100644 --- a/app/Helpers/Report/ReportQueryInterface.php +++ b/app/Helpers/Report/ReportQueryInterface.php @@ -55,6 +55,19 @@ interface ReportQueryInterface */ public function incomeInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false); + /** + * This method works the same way as ReportQueryInterface::incomeInPeriod does, but instead of returning results + * will simply list the transaction journals only. This should allow any follow up counting to be accurate with + * regards to tags. It will only get the incomes to the specified accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Collection + */ + public function incomeInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts); + /** * Covers tags as well. * diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index a89efd2dd5..9bbfbfa727 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -241,8 +241,8 @@ class ReportController extends Controller $expenseTopLength = 8; // get report stuff! - $accounts = $this->helper->getAccountReportForList($start, $end, $list); -// $incomes = $this->helper->getIncomeReportForList($start, $end, $list); + $accounts = $this->helper->getAccountReportForList($start, $end, $list); + $incomes = $this->helper->getIncomeReportForList($start, $end, $list); // $expenses = $this->helper->getExpenseReportForList($start, $end, $list); // $budgets = $this->helper->getBudgetReportForList($start, $end, $list); // $categories = $this->helper->getCategoryReportForList($start, $end, $list);