diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index b3cdda48b9..8011f089af 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -684,4 +684,52 @@ class ReportHelper implements ReportHelperInterface return $balance; } + + /** + * This method generates a full report for the given period on all + * the users bills and their payments. + * + * Excludes bills which have not had a payment on the mentioned accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return BillCollection + */ + public function getBillReportForList(Carbon $start, Carbon $end, Collection $accounts) + { + /** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $bills = $repository->getBillsForAccounts($accounts); + $collection = new BillCollection; + + /** @var Bill $bill */ + foreach ($bills as $bill) { + $billLine = new BillLine; + $billLine->setBill($bill); + $billLine->setActive(intval($bill->active) == 1); + $billLine->setMin($bill->amount_min); + $billLine->setMax($bill->amount_max); + + // is hit in period? + bcscale(2); + $set = $repository->getJournalsInRange($bill, $start, $end); + if ($set->count() == 0) { + $billLine->setHit(false); + } else { + $billLine->setHit(true); + $amount = '0'; + foreach ($set as $entry) { + $amount = bcadd($amount, $entry->amount); + } + $billLine->setAmount($amount); + } + + $collection->addBill($billLine); + + } + + return $collection; + } } diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index 12de8abbec..34e8aa88f8 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -55,6 +55,20 @@ interface ReportHelperInterface */ public function getBillReport(Carbon $start, Carbon $end); + /** + * This method generates a full report for the given period on all + * the users bills and their payments. + * + * Excludes bills which have not had a payment on the mentioned accounts. + * + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return BillCollection + */ + public function getBillReportForList(Carbon $start, Carbon $end, Collection $accounts); + /** * @param Carbon $start * @param Carbon $end @@ -65,8 +79,8 @@ interface ReportHelperInterface public function getBalanceReport(Carbon $start, Carbon $end, $shared); /** - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * @param Collection $accounts * * @return Balance diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 864b6beb27..1210e5d45d 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -247,7 +247,7 @@ class ReportController extends Controller $budgets = $this->helper->getBudgetReportForList($start, $end, $list); $categories = $this->helper->getCategoryReportForList($start, $end, $list); $balance = $this->helper->getBalanceReportForList($start, $end, $list); - // $bills = $this->helper->getBillReportForList($start, $end); + $bills = $this->helper->getBillReportForList($start, $end, $list); // continue! return view( diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 3bc0326ffc..686267e980 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -5,6 +5,7 @@ namespace FireflyIII\Repositories\Bill; use Auth; use Carbon\Carbon; use DB; +use FireflyIII\Models\Account; use FireflyIII\Models\Bill; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; @@ -108,6 +109,46 @@ class BillRepository implements BillRepositoryInterface return $set; } + /** + * @param Collection $accounts + * + * @return Collection + */ + public function getBillsForAccounts(Collection $accounts) + { + /** @var Collection $set */ + $set = Auth::user()->bills()->orderBy('name', 'ASC')->get(); + + $ids = []; + /** @var Account $account */ + foreach ($accounts as $account) { + $ids[] = $account->id; + } + + $set = $set->filter( + function (Bill $bill) use ($ids) { + // get transaction journals from or to any of the mentioned accounts. + // if zero, return null. + $journals = $bill->transactionjournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->whereIn('transactions.account_id', $ids)->count(); + + return ($journals > 0); + + } + ); + + $set = $set->sortBy( + function (Bill $bill) { + + $int = $bill->active == 1 ? 0 : 1; + + return $int . strtolower($bill->name); + } + ); + + return $set; + } + /** * @param Bill $bill * diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index fe36b810a5..a886536cd4 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -77,6 +77,15 @@ interface BillRepositoryInterface */ public function getBills(); + /** + * Gets the bills which have some kind of relevance to the accounts mentioned. + * + * @param Collection $accounts + * + * @return Collection + */ + public function getBillsForAccounts(Collection $accounts); + /** * @param Bill $bill *