Cleaned up a lot of source, finished the ChartController. [skip ci]

This commit is contained in:
James Cole
2014-07-29 19:37:52 +02:00
parent 9054b20700
commit 9af2b47463
20 changed files with 450 additions and 347 deletions

View File

@@ -1,6 +1,7 @@
<?php
use Firefly\Helper\Controllers\ChartInterface;
use Firefly\Storage\Account\AccountRepositoryInterface;
/**
* Class ChartController
@@ -9,29 +10,58 @@ class ChartController extends BaseController
{
protected $_chart;
protected $_accounts;
/**
* @param ChartInterface $chart
*/
public function __construct(ChartInterface $chart)
public function __construct(ChartInterface $chart, AccountRepositoryInterface $accounts)
{
$this->_chart = $chart;
$this->_accounts = $accounts;
}
/**
* @param Account $account
*
* @return mixed
*/
public function homeAccount(Account $account = null)
{
// get preferences and accounts (if necessary):
$data = [];
$start = Session::get('start');
$end = Session::get('end');
if (is_null($account)) {
// get, depending on preferences:
/** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */
$prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
$pref = $prefs->get('frontpageAccounts', []);
if (!is_null($account)) {
$data = $this->_chart->account($account);
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */
$acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts = $acct->getByIds($pref->data);
} else {
$data = $this->_chart->accounts();
$accounts = [$account];
}
// loop and get array data.
$url = count($accounts) == 1
? '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>'
:
'<a href="' . route('accounts.index') . '">View more</a>';
$data = [
'chart_title' => count($accounts) == 1 ? $accounts[0]->name : 'All accounts',
'subtitle' => $url,
'series' => []
];
foreach ($accounts as $account) {
$data['series'][] = $this->_chart->account($account, $start, $end);
}
return Response::json($data);
}
@@ -47,120 +77,38 @@ class ChartController extends BaseController
*/
public function homeAccountInfo($name, $day, $month, $year)
{
$account = $this->_accounts->findByName($name);
$result = [];
$sum = 0;
$date = Carbon::createFromDate($year, $month, $day);
if ($account) {
$date = \Carbon\Carbon::createFromDate($year, $month, $day);
$journals = $this->_journals->getByAccountAndDate($account, $date);
// loop all journals:
foreach ($journals as $journal) {
foreach ($journal->transactions as $transaction) {
$name = $transaction->account->name;
if ($transaction->account->id != $account->id) {
$result[$name] = isset($result[$name]) ? $result[$name] + floatval($transaction->amount)
: floatval($transaction->amount);
$sum += floatval($transaction->amount);
}
}
}
}
$result = $this->_chart->accountDailySummary($account, $date);
return View::make('charts.info')->with('rows', $result)->with('sum', $sum);
return View::make('charts.info')->with('rows', $result['rows'])->with('sum', $result['sum']);
} else {
return View::make('error')->with('message', 'No account!');
}
}
/**
* @return \Illuminate\Http\JsonResponse
* @throws Firefly\Exception\FireflyException
*/
public function homeCategories()
{
list($start, $end) = $this->_tk->getDateRangeDates();
$result = [];
// grab all transaction journals in this period:
$journals = $this->_journals->getByDateRange($start, $end);
foreach ($journals as $journal) {
// has to be one:
if (!isset($journal->transactions[0])) {
throw new FireflyException('Journal #' . $journal->id . ' has ' . count($journal->transactions)
. ' transactions!');
}
$transaction = $journal->transactions[0];
$amount = floatval($transaction->amount);
// get budget from journal:
$budget = $journal->categories()->first();
$budgetName = is_null($budget) ? '(no category)' : $budget->name;
$result[$budgetName] = isset($result[$budgetName]) ? $result[$budgetName] + floatval($amount) : $amount;
}
unset($journal, $transaction, $budget, $amount);
// sort
arsort($result);
$chartData = [
];
foreach ($result as $name => $value) {
$chartData[] = [$name, $value];
}
return Response::json($chartData);
}
/**
* @return \Illuminate\Http\JsonResponse
* @throws Firefly\Exception\FireflyException
*/
public function homeBudgets()
{
// grab all budgets in the time period, like the index does:
// get the budgets for this period:
$data = [];
$start = \Session::get('start');
list($start) = $this->_tk->getDateRangeDates();
$budgets = $this->_budgets->getWithRepetitionsInPeriod($start, \Session::get('range'));
return Response::json($this->_chart->budgets($start));
}
$repeatFreq = Config::get('firefly.range_to_repeat_freq.' . Session::get('range'));
/**
* @return \Illuminate\Http\JsonResponse
*/
public function homeCategories()
{
$start = Session::get('start');
$end = Session::get('end');
return Response::json($this->_chart->categories($start, $end));
$limitInPeriod = 'Envelope for XXX';
$spentInPeriod = 'Spent in XXX';
$data['series'] = [
[
'name' => $limitInPeriod,
'data' => []
],
[
'name' => $spentInPeriod,
'data' => []
],
];
foreach ($budgets as $budget) {
if ($budget->count > 0) {
$data['labels'][] = wordwrap($budget->name, 12, "<br>");
}
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
//0: envelope for period:
$amount = floatval($rep->amount);
$spent = $rep->spent;
$color = $spent > $amount ? '#FF0000' : null;
$data['series'][0]['data'][] = $amount;
$data['series'][1]['data'][] = ['y' => $rep->spent, 'color' => $color];
}
}
}
return Response::json($data);
}
}