Files
firefly-iii/app/controllers/ReportController.php

133 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2014-11-02 18:46:01 +01:00
use Carbon\Carbon;
2014-12-13 22:54:52 +01:00
use FireflyIII\Database\Account\Account as AccountRepository;
use FireflyIII\Database\TransactionJournal\TransactionJournal as TransactionJournalRepository;
use FireflyIII\Report\ReportInterface as Report;
2014-08-10 15:01:46 +02:00
/**
2014-12-14 20:40:02 +01:00
*
2014-08-10 15:01:46 +02:00
* Class ReportController
*/
class ReportController extends BaseController
{
2014-12-07 15:37:53 +01:00
/** @var AccountRepository */
protected $_accounts;
/** @var TransactionJournalRepository */
protected $_journals;
2014-12-13 22:54:52 +01:00
/** @var Report */
2014-12-07 15:37:53 +01:00
protected $_repository;
/**
* @param AccountRepository $accounts
* @param TransactionJournalRepository $journals
2014-12-13 22:54:52 +01:00
* @param Report $repository
2014-12-07 15:37:53 +01:00
*/
2014-12-13 22:54:52 +01:00
public function __construct(AccountRepository $accounts, TransactionJournalRepository $journals, Report $repository)
2014-12-07 15:37:53 +01:00
{
$this->_accounts = $accounts;
$this->_journals = $journals;
$this->_repository = $repository;
}
2014-08-10 15:01:46 +02:00
/**
*
*/
public function index()
{
2014-12-07 15:37:53 +01:00
$start = $this->_journals->firstDate();
2014-12-13 22:54:52 +01:00
$months = $this->_repository->listOfMonths(clone $start);
$years = $this->_repository->listOfYears(clone $start);
2014-12-07 15:37:53 +01:00
$title = 'Reports';
$mainTitleIcon = 'fa-line-chart';
2014-11-26 17:20:05 +01:00
2014-12-07 15:37:53 +01:00
return View::make('reports.index', compact('years', 'months', 'title', 'mainTitleIcon'));
2014-11-26 17:20:05 +01:00
}
2014-12-06 17:53:25 +01:00
/**
* @param $year
* @param $month
*
* @return \Illuminate\View\View
*/
2014-11-26 17:20:05 +01:00
public function unbalanced($year, $month)
{
try {
2014-12-13 21:59:02 +01:00
new Carbon($year . '-' . $month . '-01');
2014-11-26 17:20:05 +01:00
} catch (Exception $e) {
App::abort(500);
}
$start = new Carbon($year . '-' . $month . '-01');
$end = clone $start;
$title = 'Reports';
$subTitle = 'Unbalanced transactions in ' . $start->format('F Y');
$mainTitleIcon = 'fa-line-chart';
$subTitleIcon = 'fa-bar-chart';
$end->endOfMonth();
2014-12-13 22:54:52 +01:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $journalRepository */
$journalRepository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-12-14 20:40:02 +01:00
$journals = $journalRepository->getInDateRange($start, $end);
2014-11-02 18:46:01 +01:00
$withdrawals = $journals->filter(
2014-11-26 17:20:05 +01:00
function (TransactionJournal $journal) {
2014-12-14 20:40:02 +01:00
$relations = $journal->transactiongroups()->where('relation', 'balance')->count();
$budgets = $journal->budgets()->count();
$type = $journal->transactionType->type;
if ($type == 'Withdrawal' && $budgets == 0 && $relations == 0) {
return $journal;
2014-11-26 17:20:05 +01:00
}
2014-12-06 17:34:39 +01:00
return null;
2014-11-26 17:20:05 +01:00
}
);
2014-11-28 14:41:58 +01:00
$deposits = $journals->filter(
function (TransactionJournal $journal) {
2014-12-14 20:40:02 +01:00
$relations = $journal->transactiongroups()->where('relation', 'balance')->count();
$budgets = $journal->budgets()->count();
$type = $journal->transactionType->type;
if ($type == 'Deposit' && $budgets == 0 && $relations == 0) {
return $journal;
2014-11-28 14:41:58 +01:00
}
2014-12-06 17:34:39 +01:00
return null;
2014-11-28 14:41:58 +01:00
}
);
2014-12-04 20:38:45 +01:00
2014-12-01 05:57:03 +01:00
$journals = $withdrawals->merge($deposits);
return View::make('reports.unbalanced', compact('start', 'end', 'title', 'subTitle', 'subTitleIcon', 'mainTitleIcon', 'journals'));
2014-11-02 18:46:01 +01:00
}
/**
* @param $year
2014-11-26 17:20:05 +01:00
*
* @return $this
2014-11-02 18:46:01 +01:00
*/
public function year($year)
{
try {
2014-12-07 15:37:53 +01:00
new Carbon('01-01-' . $year);
2014-11-02 18:46:01 +01:00
} catch (Exception $e) {
App::abort(500);
}
$date = new Carbon('01-01-' . $year);
$end = clone $date;
$end->endOfYear();
2014-12-07 15:37:53 +01:00
$title = 'Reports';
$subTitle = $year;
$subTitleIcon = 'fa-bar-chart';
$mainTitleIcon = 'fa-line-chart';
2014-12-13 22:54:52 +01:00
$balances = $this->_repository->yearBalanceReport($date);
$groupedIncomes = $this->_repository->revenueGroupedByAccount($date, $end, 15);
$groupedExpenses = $this->_repository->expensesGroupedByAccount($date, $end, 15);
2014-12-07 15:37:53 +01:00
return View::make(
'reports.year', compact('date', 'groupedIncomes', 'groupedExpenses', 'year', 'balances', 'title', 'subTitle', 'subTitleIcon', 'mainTitleIcon')
);
}
}