mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 14:26:58 +00:00
Cleaned up a lot of source, finished the ChartController. [skip ci]
This commit is contained in:
@@ -9,50 +9,13 @@
|
||||
namespace Firefly\Helper\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Firefly\Exception\FireflyException;
|
||||
|
||||
class Chart implements ChartInterface
|
||||
{
|
||||
|
||||
public function account(\Account $account)
|
||||
public function account(\Account $account, Carbon $start, Carbon $end)
|
||||
{
|
||||
$data = [
|
||||
'chart_title' => $account->name,
|
||||
'subtitle' => '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>',
|
||||
'series' => [$this->_account($account)]
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function accounts()
|
||||
{
|
||||
$data = [
|
||||
'chart_title' => 'All accounts',
|
||||
'subtitle' => '<a href="' . route('accounts.index') . '">View more</a>',
|
||||
'series' => []
|
||||
];
|
||||
/** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */
|
||||
$prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
|
||||
$pref = $prefs->get('frontpageAccounts', []);
|
||||
|
||||
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */
|
||||
$acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
||||
|
||||
if ($pref->data == []) {
|
||||
$accounts = $acct->getActiveDefault();
|
||||
} else {
|
||||
$accounts = $acct->getByIds($pref->data);
|
||||
}
|
||||
foreach($accounts as $account) {
|
||||
$data['series'][] = $this->_account($account);
|
||||
}
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
protected function _account(\Account $account)
|
||||
{
|
||||
$start = \Session::get('start');
|
||||
$end = \Session::get('end');
|
||||
$current = clone $start;
|
||||
$today = new Carbon;
|
||||
$return = ['name' => $account->name, 'id' => $account->id, 'data' => []];
|
||||
@@ -65,7 +28,189 @@ class Chart implements ChartInterface
|
||||
|
||||
$current->addDay();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function accountDailySummary(\Account $account, Carbon $date)
|
||||
{
|
||||
$result = [
|
||||
'rows' => [],
|
||||
'sum' => 0
|
||||
];
|
||||
if ($account) {
|
||||
// get journals in range:
|
||||
$journals = \Auth::user()->transactionjournals()->with(
|
||||
[
|
||||
'transactions',
|
||||
'transactions.account',
|
||||
'transactioncurrency',
|
||||
'transactiontype'
|
||||
]
|
||||
)
|
||||
->distinct()
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.date', $date->format('Y-m-d'))
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(['transaction_journals.*']);
|
||||
|
||||
// loop all journals:
|
||||
foreach ($journals as $journal) {
|
||||
foreach ($journal->transactions as $transaction) {
|
||||
$name = $transaction->account->name;
|
||||
if ($transaction->account->id != $account->id) {
|
||||
$result['rows'][$name] = isset($result[$name]) ? $result[$name] + floatval($transaction->amount)
|
||||
: floatval($transaction->amount);
|
||||
$result['sum'] += floatval($transaction->amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function budgets(Carbon $start)
|
||||
{
|
||||
// grab all budgets in the time period, like the index does:
|
||||
// get the budgets for this period:
|
||||
|
||||
$data = [];
|
||||
|
||||
$budgets = \Auth::user()->budgets()->with(
|
||||
['limits' => function ($q) {
|
||||
$q->orderBy('limits.startdate', 'ASC');
|
||||
}, 'limits.limitrepetitions' => function ($q) use ($start) {
|
||||
$q->orderBy('limit_repetitions.startdate', 'ASC');
|
||||
$q->where('startdate', $start->format('Y-m-d'));
|
||||
}]
|
||||
)->orderBy('name', 'ASC')->get();
|
||||
|
||||
foreach ($budgets as $budget) {
|
||||
$budget->count = 0;
|
||||
foreach ($budget->limits as $limit) {
|
||||
/** @var $rep \LimitRepetition */
|
||||
foreach ($limit->limitrepetitions as $rep) {
|
||||
$rep->left = $rep->left();
|
||||
// overspent:
|
||||
if ($rep->left < 0) {
|
||||
$rep->spent = ($rep->left * -1) + $rep->amount;
|
||||
$rep->overspent = $rep->left * -1;
|
||||
$total = $rep->spent + $rep->overspent;
|
||||
$rep->spent_pct = round(($rep->spent / $total) * 100);
|
||||
$rep->overspent_pct = 100 - $rep->spent_pct;
|
||||
} else {
|
||||
$rep->spent = $rep->amount - $rep->left;
|
||||
$rep->spent_pct = round(($rep->spent / $rep->amount) * 100);
|
||||
$rep->left_pct = 100 - $rep->spent_pct;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
$budget->count += count($limit->limitrepetitions);
|
||||
}
|
||||
}
|
||||
|
||||
$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 $data;
|
||||
}
|
||||
|
||||
public function categories(Carbon $start, Carbon $end)
|
||||
{
|
||||
|
||||
$result = [];
|
||||
// grab all transaction journals in this period:
|
||||
$journals = \TransactionJournal::
|
||||
with(
|
||||
['components', 'transactions' => function ($q) {
|
||||
$q->where('amount', '>', 0);
|
||||
}]
|
||||
)
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->where('transaction_types.type', 'Withdrawal')
|
||||
->after($start)->before($end)
|
||||
->where('completed', 1)
|
||||
->get(['transaction_journals.*']);
|
||||
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:
|
||||
$category = $journal->categories()->first();
|
||||
$categoryName = is_null($category) ? '(no category)' : $category->name;
|
||||
|
||||
$result[$categoryName] = isset($result[$categoryName]) ? $result[$categoryName] + floatval($amount)
|
||||
: $amount;
|
||||
|
||||
}
|
||||
unset($journal, $transaction, $category, $amount);
|
||||
|
||||
// sort
|
||||
arsort($result);
|
||||
$chartData = [
|
||||
];
|
||||
foreach ($result as $name => $value) {
|
||||
$chartData[] = [$name, $value];
|
||||
}
|
||||
|
||||
|
||||
return $chartData;
|
||||
}
|
||||
|
||||
public function accountXX(\Account $account)
|
||||
{
|
||||
$data = [
|
||||
'chart_title' => $account->name,
|
||||
'subtitle' => '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>',
|
||||
'series' => [$this->_account($account)]
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user