firefly-iii/app/controllers/ChartController.php

217 lines
7.0 KiB
PHP
Raw Normal View History

<?php
2014-07-22 23:57:51 -05:00
use Firefly\Exception\FireflyException;
use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;
2014-07-22 23:57:51 -05:00
use Firefly\Helper\Toolkit\ToolkitInterface as tk;
2014-07-09 05:56:06 -05:00
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
2014-07-09 05:56:06 -05:00
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
2014-07-15 15:16:29 -05:00
/**
* Class ChartController
*/
class ChartController extends BaseController
{
2014-07-15 15:16:29 -05:00
protected $_accounts;
protected $_journals;
protected $_tk;
protected $_preferences;
protected $_budgets;
2014-07-15 15:16:29 -05:00
/**
* @param ARI $accounts
* @param TJRI $journals
*/
public function __construct(ARI $accounts, TJRI $journals, PHI $preferences, tk $toolkit, BRI $budgets)
{
2014-07-15 15:16:29 -05:00
$this->_accounts = $accounts;
$this->_journals = $journals;
$this->_preferences = $preferences;
$this->_tk = $toolkit;
$this->_budgets = $budgets;
}
/**
2014-07-15 15:16:29 -05:00
* @param null $accountId
*
* @return \Illuminate\Http\JsonResponse
*/
2014-07-15 15:16:29 -05:00
public function homeAccount($accountId = null)
{
list($start, $end) = $this->_tk->getDateRangeDates();
$current = clone $start;
2014-07-14 23:58:08 -05:00
$return = [];
$account = null;
$today = new Carbon\Carbon;
2014-07-15 15:16:29 -05:00
if (!is_null($accountId)) {
/** @var \Account $account */
2014-07-15 15:16:29 -05:00
$account = $this->_accounts->find($accountId);
}
if (is_null($account)) {
$pref = $this->_preferences->get('frontpageAccounts', []);
if ($pref->data == []) {
$accounts = $this->_accounts->getActiveDefault();
} else {
$accounts = $this->_accounts->getByIds($pref->data);
}
2014-07-15 15:16:29 -05:00
foreach ($accounts as $account) {
$return[] = ['name' => $account->name, 'id' => 'acc-' . $account->id, 'data' => []];
}
while ($current <= $end) {
// loop accounts:
2014-07-14 23:58:08 -05:00
foreach ($accounts as $index => $account) {
if ($current > $today) {
$return[$index]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
} else {
$return[$index]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];
}
}
$current->addDay();
}
} else {
$return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []];
while ($current <= $end) {
if ($current > $today) {
$return[0]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
} else {
$return[0]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];
}
2014-07-14 23:58:08 -05:00
$current->addDay();
}
2014-07-14 23:58:08 -05:00
}
return Response::json($return);
2014-07-09 05:56:06 -05:00
}
/**
* Return some beneficiary info for an account and a date.
*
* @param $name
* @param $day
* @param $month
* @param $year
2014-07-09 05:56:06 -05:00
*/
public function homeAccountInfo($name, $day, $month, $year)
2014-07-09 05:56:06 -05:00
{
$account = $this->_accounts->findByName($name);
$result = [];
$sum = 0;
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);
}
}
}
2014-07-09 05:56:06 -05:00
}
return View::make('charts.info')->with('rows', $result)->with('sum', $sum);
2014-07-09 05:56:06 -05:00
}
2014-07-20 14:56:20 -05:00
public function homeCategories()
{
list($start, $end) = $this->_tk->getDateRangeDates();
$account = null;
$result = [];
// grab all transaction journals in this period:
2014-07-20 14:56:20 -05:00
$journals = $this->_journals->getByDateRange($start, $end);
2014-07-09 05:56:06 -05:00
$result = [];
foreach ($journals as $journal) {
// has to be one:
2014-07-09 05:56:06 -05:00
if (!isset($journal->transactions[0])) {
throw new FireflyException('Journal #' . $journal->id . ' has ' . count($journal->transactions)
. ' transactions!');
}
$transaction = $journal->transactions[0];
$amount = floatval($transaction->amount);
2014-07-09 05:56:06 -05:00
// 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;
2014-07-09 05:56:06 -05:00
}
unset($journal, $transaction, $budget, $amount);
2014-07-09 05:56:06 -05:00
// sort
arsort($result);
$chartData = [
];
2014-07-20 14:56:20 -05:00
foreach ($result as $name => $value) {
$chartData[] = [$name, $value];
2014-07-09 05:56:06 -05:00
}
return Response::json($chartData);
}
public function homeBudgets()
{
// grab all budgets in the time period, like the index does:
// get the budgets for this period:
$data = [];
list($start, $end) = $this->_tk->getDateRangeDates();
$budgets = $this->_budgets->getWithRepetitionsInPeriod($start, \Session::get('range'));
$repeatFreq = Config::get('firefly.range_to_repeat_freq.'.Session::get('range'));
$dateFormats = Config::get('firefly.date_formats_by_period.' . $repeatFreq);
if (is_null($dateFormats)) {
throw new FireflyException('No date formats for ' . \Session::get('range'));
}
$limitInPeriod = 'Envelope for ' . $start->format($dateFormats['display_date']);
$spentInPeriod = 'Spent in ' . $start->format($dateFormats['display_date']);
$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:
$data['series'][0]['data'][] = floatval($rep->amount);
$data['series'][1]['data'][] = $rep->spent;
}
}
}
return Response::json($data);
echo '<pre>';
print_r($data);
}
}