Cleaning up the chart controller.

This commit is contained in:
James Cole
2014-09-09 07:10:16 +02:00
parent 5ca466a826
commit 9965297f36
4 changed files with 225 additions and 125 deletions

View File

@@ -26,6 +26,9 @@ $(function () {
str += '<span style="color:' + colour + '">' + point.series.name + '</span>: € ' + Highcharts.numberFormat(point.y, 2) + '<br />'; str += '<span style="color:' + colour + '">' + point.series.name + '</span>: € ' + Highcharts.numberFormat(point.y, 2) + '<br />';
} }
if (x == 1) { if (x == 1) {
str += '<span style="color:' + colour + '">' + point.series.name + '</span>: € ' + Highcharts.numberFormat(point.y, 2) + '<br />';
}
if (x == 2) {
str += '<span style="color:' + colour + '">' + point.series.name + '</span>: ' + Highcharts.numberFormat(point.y, 1) + '%<br />'; str += '<span style="color:' + colour + '">' + point.series.name + '</span>: ' + Highcharts.numberFormat(point.y, 1) + '%<br />';
} }
} }

View File

@@ -25,35 +25,28 @@ class ChartController extends BaseController
} }
/** /**
* This method takes a budget, all limits and all their repetitions and displays three numbers per repetition:
* the amount of money in the repetition (represented as "an envelope"), the amount spent and the spent percentage.
* *
* @param Budget $budget
*
* @return \Illuminate\Http\JsonResponse
*/ */
public function budgetDefault(\Budget $budget) public function budgetDefault(\Budget $budget)
{ {
$expense = []; $expense = [];
$left = []; $left = [];
$envelope = [];
// get all limit repetitions for this budget. // get all limit repetitions for this budget.
/** @var \Limit $limit */ /** @var \Limit $limit */
foreach ($budget->limits as $limit) { foreach ($budget->limits as $limit) {
/** @var \LimitRepetition $rep */ /** @var \LimitRepetition $rep */
foreach ($limit->limitrepetitions as $rep) { foreach ($limit->limitrepetitions as $rep) {
$spentInRep = \Transaction:: // get the amount of money spent in this period on this budget.
leftJoin( $spentInRep = $rep->amount - $rep->left();
'transaction_journals', 'transaction_journals.id', '=', $pct = round((floatval($spentInRep) / floatval($limit->amount)) * 100, 2);
'transactions.transaction_journal_id'
)
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id',
'=',
'transaction_journals.id'
)->where('component_transaction_journal.component_id', '=', $budget->id)->where(
'transaction_journals.date', '>=', $rep->startdate->format('Y-m-d')
)->where('transaction_journals.date', '<=', $rep->enddate->format('Y-m-d'))->where(
'amount', '>', 0
)->sum('amount');
$pct = round(($spentInRep / $limit->amount) * 100, 2);
$name = $rep->periodShow(); $name = $rep->periodShow();
$envelope[] = [$name, floatval($limit->amount)];
$expense[] = [$name, floatval($spentInRep)]; $expense[] = [$name, floatval($spentInRep)];
$left[] = [$name, $pct]; $left[] = [$name, $pct];
} }
@@ -63,6 +56,12 @@ class ChartController extends BaseController
'chart_title' => 'Overview for budget ' . $budget->name, 'chart_title' => 'Overview for budget ' . $budget->name,
'subtitle' => 'All envelopes', 'subtitle' => 'All envelopes',
'series' => [ 'series' => [
[
'type' => 'line',
'yAxis' => 1,
'name' => 'Amount in envelope',
'data' => $envelope
],
[ [
'type' => 'column', 'type' => 'column',
'name' => 'Expenses in envelope', 'name' => 'Expenses in envelope',
@@ -75,6 +74,7 @@ class ChartController extends BaseController
'data' => $left 'data' => $left
] ]
] ]
]; ];
@@ -82,7 +82,12 @@ class ChartController extends BaseController
} }
/** /**
* This method takes a single limit repetition (so a single "envelope") and displays the amount of money spent
* per day and subsequently how much money is left.
*
* @param LimitRepetition $rep * @param LimitRepetition $rep
*
* @return \Illuminate\Http\JsonResponse
*/ */
public function budgetLimit(\LimitRepetition $rep) public function budgetLimit(\LimitRepetition $rep)
{ {
@@ -92,14 +97,7 @@ class ChartController extends BaseController
$leftInLimit = []; $leftInLimit = [];
$currentLeftInLimit = floatval($rep->limit->amount); $currentLeftInLimit = floatval($rep->limit->amount);
while ($current <= $rep->enddate) { while ($current <= $rep->enddate) {
$spent = \Transaction:: $spent = $this->_chart->spentOnDay($budget, $current);
leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
'transaction_journals.id'
)->where('component_transaction_journal.component_id', '=', $budget->id)->where(
'transaction_journals.date', $current->format('Y-m-d')
)->where('amount', '>', 0)->sum('amount');
$spent = floatval($spent) == 0 ? null : floatval($spent); $spent = floatval($spent) == 0 ? null : floatval($spent);
$entry = [$current->timestamp * 1000, $spent]; $entry = [$current->timestamp * 1000, $spent];
$expense[] = $entry; $expense[] = $entry;
@@ -132,49 +130,40 @@ class ChartController extends BaseController
} }
/** /**
* This method takes a budget and gets all transactions in it which haven't got an envelope (limit).
* *
* Usually this means that very old and unorganized or very NEW transactions get displayed; there was never an
* envelope or it hasn't been created (yet).
*
*
* @param Budget $budget
*
* @return \Illuminate\Http\JsonResponse
*/ */
public function budgetNoLimits(\Budget $budget) public function budgetNoLimits(\Budget $budget)
{ {
$inRepetitions = []; /*
foreach ($budget->limits as $limit) { * We can go about this two ways. Either we find all transactions which definitely are IN an envelope
foreach ($limit->limitrepetitions as $repetition) { * and exclude them or we search for transactions outside of the range of any of the envelopes we have.
$set = $budget->transactionjournals()->leftJoin( *
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id' * Since either is shitty we go with the first one because it's easier to build.
)->where('transaction_types.type', 'Withdrawal')->where( */
'date', '>=', $repetition->startdate->format('Y-m-d') $inRepetitions = $this->_chart->allJournalsInBudgetEnvelope($budget);
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->get(
['transaction_journals.id']
);
foreach ($set as $item) {
$inRepetitions[] = $item->id;
}
}
} /*
* With this set of id's, we can search for all journals NOT in that set.
$query = $budget->transactionjournals()->whereNotIn( * BUT they have to be in the budget (duh).
'transaction_journals.id', $inRepetitions */
)->orderBy('date', 'DESC')->orderBy( $set = $this->_chart->journalsNotInSet($budget, $inRepetitions);
'transaction_journals.id', 'DESC' /*
); * Next step: get all transactions for those journals.
*/
$transactions = $this->_chart->transactionsByJournals($set);
$result = $query->get(['transaction_journals.id']); /*
$set = []; * this set builds the chart:
foreach ($result as $entry) { */
$set[] = $entry->id;
}
// all transactions for these journals, grouped by date and SUM
$transactions = \Transaction::whereIn('transaction_journal_id', $set)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)
->groupBy('transaction_journals.date')->where('amount', '>', 0)->get(
['transaction_journals.date', DB::Raw('SUM(`amount`) as `aggregate`')]
);
// this set builds the chart:
$expense = []; $expense = [];
foreach ($transactions as $t) { foreach ($transactions as $t) {
@@ -186,19 +175,20 @@ class ChartController extends BaseController
'subtitle' => 'Not organized by an envelope', 'subtitle' => 'Not organized by an envelope',
'series' => [ 'series' => [
[ [
'type' => 'spline', 'type' => 'column',
'name' => 'Expenses per day', 'name' => 'Expenses per day',
'data' => $expense 'data' => $expense
] ]
] ]
]; ];
return Response::json($return); return Response::json($return);
} }
/** /**
* @param Budget $budget
* *
* @return \Illuminate\Http\JsonResponse
*/ */
public function budgetSession(\Budget $budget) public function budgetSession(\Budget $budget)
{ {
@@ -207,19 +197,9 @@ class ChartController extends BaseController
$current = clone Session::get('start'); $current = clone Session::get('start');
$end = clone Session::get('end'); $end = clone Session::get('end');
while ($current <= $end) { while ($current <= $end) {
$spent = \Transaction:: $spent = $this->_chart->spentOnDay($budget, $current);
leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
'transaction_journals.id'
)->where('component_transaction_journal.component_id', '=', $budget->id)->where(
'transaction_journals.date', $current->format('Y-m-d')
)->where('amount', '>', 0)->sum('amount');
$spent = floatval($spent) == 0 ? null : floatval($spent); $spent = floatval($spent) == 0 ? null : floatval($spent);
if (!is_null($spent)) {
$expense[] = [$current->timestamp * 1000, $spent]; $expense[] = [$current->timestamp * 1000, $spent];
}
$current->addDay(); $current->addDay();
} }
@@ -252,8 +232,7 @@ class ChartController extends BaseController
} }
); );
} }
) )->get();
->get();
$currentLeftInLimit = floatval($limit->amount); $currentLeftInLimit = floatval($limit->amount);
/** @var \LimitRepetition $repetition */ /** @var \LimitRepetition $repetition */
foreach ($reps as $repetition) { foreach ($reps as $repetition) {
@@ -269,6 +248,7 @@ class ChartController extends BaseController
while ($current <= $repetition->enddate) { while ($current <= $repetition->enddate) {
if ($current >= Session::get('start') && $current <= Session::get('end')) { if ($current >= Session::get('start') && $current <= Session::get('end')) {
// spent on limit: // spent on limit:
$spentSoFar = \Transaction:: $spentSoFar = \Transaction::
leftJoin( leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transaction_journals', 'transaction_journals.id', '=',

View File

@@ -7,6 +7,7 @@ use Firefly\Exception\FireflyException;
/** /**
* Class Chart * Class Chart
*
* @package Firefly\Helper\Controllers * @package Firefly\Helper\Controllers
*/ */
class Chart implements ChartInterface class Chart implements ChartInterface
@@ -400,4 +401,86 @@ class Chart implements ChartInterface
} }
/**
* @param \Budget $budget
* @param Carbon $date
*
* @return float|null
*/
public function spentOnDay(\Budget $budget, Carbon $date)
{
return floatval(
\Transaction::
leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
'transaction_journals.id'
)->where('component_transaction_journal.component_id', '=', $budget->id)->where(
'transaction_journals.date', $date->format('Y-m-d')
)->where('amount', '>', 0)->sum('amount')
);
}
/**
* @param \Budget $budget
*
* @return int[]
*/
public function allJournalsInBudgetEnvelope(\Budget $budget)
{
$inRepetitions = [];
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $repetition) {
$set = $budget
->transactionjournals()
->transactionTypes(['Withdrawal'])
->after($repetition->startdate)
->before($repetition->enddate)
->get(['transaction_journals.id']);
foreach ($set as $item) {
$inRepetitions[] = $item->id;
}
}
}
return $inRepetitions;
}
/**
* @param \Budget $budget
* @param array $ids
*
* @return mixed|void
*/
public function journalsNotInSet(\Budget $budget, array $ids)
{
$query = $budget->transactionjournals()
->whereNotIn('transaction_journals.id', $ids)
->orderBy('date', 'DESC')
->orderBy('transaction_journals.id', 'DESC');
$result = $query->get(['transaction_journals.id']);
$set = [];
foreach ($result as $entry) {
$set[] = $entry->id;
}
return $set;
}
/**
* @param array $set
*
* @return mixed
*/
public function transactionsByJournals(array $set)
{
$transactions = \Transaction::whereIn('transaction_journal_id', $set)
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->groupBy('transaction_journals.date')
->where('amount', '>', 0)->get(['transaction_journals.date', \DB::Raw('SUM(`amount`) as `aggregate`')]);
return $transactions;
}
} }

View File

@@ -54,4 +54,38 @@ interface ChartInterface
* @return mixed * @return mixed
*/ */
public function categoryShowChart(\Category $category, $range, Carbon $start, Carbon $end); public function categoryShowChart(\Category $category, $range, Carbon $start, Carbon $end);
/**
* @param \Budget $budget
* @param Carbon $date
*
* @return float|null
*/
public function spentOnDay(\Budget $budget, Carbon $date);
/**
* @param \Budget $budget
*
* @return int[]
*/
public function allJournalsInBudgetEnvelope(\Budget $budget);
/**
* @param \Budget $budget
* @param array $ids
*
* @return mixed
*/
public function journalsNotInSet(\Budget $budget, array $ids);
/**
* @param array $set
*
* @return mixed
*/
public function transactionsByJournals(array $set);
} }