2014-07-06 08:18:11 -05:00
|
|
|
<?php
|
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;
|
2014-07-08 00:51:25 -05:00
|
|
|
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-06 08:18:11 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
/**
|
|
|
|
* Class ChartController
|
|
|
|
*/
|
2014-07-06 08:18:11 -05:00
|
|
|
class ChartController extends BaseController
|
|
|
|
{
|
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
protected $_accounts;
|
|
|
|
protected $_journals;
|
2014-07-17 13:52:54 -05:00
|
|
|
protected $_preferences;
|
2014-07-06 14:07:52 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
/**
|
|
|
|
* @param ARI $accounts
|
|
|
|
* @param TJRI $journals
|
|
|
|
*/
|
2014-07-17 13:52:54 -05:00
|
|
|
public function __construct(ARI $accounts, TJRI $journals, PHI $preferences)
|
2014-07-06 08:18:11 -05:00
|
|
|
{
|
2014-07-15 15:16:29 -05:00
|
|
|
$this->_accounts = $accounts;
|
|
|
|
$this->_journals = $journals;
|
2014-07-17 13:52:54 -05:00
|
|
|
$this->_preferences = $preferences;
|
2014-07-06 08:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-15 15:16:29 -05:00
|
|
|
* @param null $accountId
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2014-07-06 08:18:11 -05:00
|
|
|
*/
|
2014-07-15 15:16:29 -05:00
|
|
|
public function homeAccount($accountId = null)
|
2014-07-06 08:18:11 -05:00
|
|
|
{
|
2014-07-09 05:56:06 -05:00
|
|
|
list($start, $end) = tk::getDateRange();
|
2014-07-08 00:51:25 -05:00
|
|
|
$current = clone $start;
|
2014-07-14 23:58:08 -05:00
|
|
|
$return = [];
|
2014-07-15 10:09:59 -05:00
|
|
|
$account = null;
|
2014-07-17 13:52:54 -05:00
|
|
|
$today = new Carbon\Carbon;
|
2014-07-15 10:09:59 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
if (!is_null($accountId)) {
|
2014-07-17 13:52:54 -05:00
|
|
|
/** @var \Account $account */
|
2014-07-15 15:16:29 -05:00
|
|
|
$account = $this->_accounts->find($accountId);
|
2014-07-15 10:09:59 -05:00
|
|
|
}
|
2014-07-08 00:51:25 -05:00
|
|
|
|
2014-07-06 08:18:11 -05:00
|
|
|
if (is_null($account)) {
|
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
$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) {
|
2014-07-17 13:52:54 -05:00
|
|
|
$return[] = ['name' => $account->name, 'id' => 'acc-' . $account->id, 'data' => []];
|
|
|
|
|
2014-07-06 08:18:11 -05:00
|
|
|
}
|
2014-07-08 00:51:25 -05:00
|
|
|
while ($current <= $end) {
|
2014-07-06 08:18:11 -05:00
|
|
|
// loop accounts:
|
2014-07-14 23:58:08 -05:00
|
|
|
foreach ($accounts as $index => $account) {
|
2014-07-17 13:52:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
if ($current > $today) {
|
|
|
|
$return[$index]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
|
|
|
|
} else {
|
|
|
|
$return[$index]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];
|
|
|
|
}
|
2014-07-06 08:18:11 -05:00
|
|
|
}
|
|
|
|
$current->addDay();
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-17 13:52:54 -05:00
|
|
|
$return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []];
|
2014-07-14 23:58:08 -05:00
|
|
|
|
2014-07-08 00:51:25 -05:00
|
|
|
while ($current <= $end) {
|
2014-07-17 13:52:54 -05:00
|
|
|
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
|
|
|
|
2014-07-06 08:18:11 -05:00
|
|
|
$current->addDay();
|
|
|
|
}
|
2014-07-14 23:58:08 -05:00
|
|
|
}
|
2014-07-17 13:52:54 -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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-17 13:52:54 -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
|
|
|
*/
|
2014-07-17 13:52:54 -05:00
|
|
|
public function homeAccountInfo($name, $day, $month, $year)
|
2014-07-09 05:56:06 -05:00
|
|
|
{
|
2014-07-17 13:52:54 -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
|
|
|
}
|
2014-07-17 13:52:54 -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();
|
2014-07-17 13:52:54 -05:00
|
|
|
$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
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
$result = [];
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
// has to be one:
|
2014-07-09 05:56:06 -05:00
|
|
|
|
2014-07-17 13:52:54 -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
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
// get budget from journal:
|
|
|
|
$budget = $journal->categories()->first();
|
|
|
|
$budgetName = is_null($budget) ? '(no category)' : $budget->name;
|
2014-07-06 08:18:11 -05:00
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
$result[$budgetName] = isset($result[$budgetName]) ? $result[$budgetName] + floatval($amount) : $amount;
|
2014-07-09 05:56:06 -05:00
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
}
|
|
|
|
unset($journal, $transaction, $budget, $amount);
|
2014-07-09 05:56:06 -05:00
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
// sort
|
|
|
|
arsort($result);
|
|
|
|
$chartData = [
|
|
|
|
];
|
2014-07-20 14:56:20 -05:00
|
|
|
foreach ($result as $name => $value) {
|
2014-07-17 13:52:54 -05:00
|
|
|
$chartData[] = [$name, $value];
|
2014-07-09 05:56:06 -05:00
|
|
|
}
|
2014-07-17 13:52:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
return Response::json($chartData);
|
2014-07-06 08:18:11 -05:00
|
|
|
|
|
|
|
}
|
2014-07-17 13:52:54 -05:00
|
|
|
}
|