firefly-iii/app/controllers/ChartController.php

175 lines
5.5 KiB
PHP
Raw Normal View History

<?php
use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;
use Firefly\Helper\Toolkit\Toolkit as tk;
2014-07-09 05:56:06 -05:00
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
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 $_preferences;
2014-07-15 15:16:29 -05:00
/**
* @param ARI $accounts
* @param TJRI $journals
*/
public function __construct(ARI $accounts, TJRI $journals, PHI $preferences)
{
2014-07-15 15:16:29 -05:00
$this->_accounts = $accounts;
$this->_journals = $journals;
$this->_preferences = $preferences;
}
/**
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)
{
2014-07-09 05:56:06 -05:00
list($start, $end) = tk::getDateRange();
$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' => []];
2014-07-14 23:58:08 -05:00
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
}
// // add an error bar as experiment:
// foreach($return as $index => $serie) {
// $err = [
// 'type' => 'errorbar',
// 'name' => $serie['name'].' pred',
// 'linkedTo' => $serie['id'],
// 'data' => []
// ];
// foreach($serie['data'] as $entry) {
// $err['data'][] = [$entry[0],10,300];
// }
// $return[] = $err;
// }
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()
{
2014-07-09 05:56:06 -05:00
list($start, $end) = tk::getDateRange();
$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);
}
}