First attempt at account specific bill report.

This commit is contained in:
James Cole 2015-12-12 10:33:19 +01:00
parent afdae8bc1e
commit 9284eb3fe9
5 changed files with 115 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -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(

View File

@ -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
*

View File

@ -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
*