mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fixed a chart.
This commit is contained in:
parent
fc44a52ba5
commit
9ab3679d49
@ -46,7 +46,7 @@ class ChartController extends BaseController
|
||||
/** @var \LimitRepetition $rep */
|
||||
foreach ($limit->limitrepetitions as $rep) {
|
||||
// get the amount of money spent in this period on this budget.
|
||||
$spentInRep = $rep->amount - $rep->left();
|
||||
$spentInRep = $rep->amount - $rep->leftInRepetition();
|
||||
$pct = round((floatval($spentInRep) / floatval($limit->amount)) * 100, 2);
|
||||
$name = $rep->periodShow();
|
||||
$envelope[] = [$name, floatval($limit->amount)];
|
||||
@ -372,9 +372,113 @@ class ChartController extends BaseController
|
||||
*/
|
||||
public function homeBudgets()
|
||||
{
|
||||
$start = \Session::get('start');
|
||||
$start = Session::get('start');
|
||||
$end = Session::get('end');
|
||||
$data = [
|
||||
'labels' => [],
|
||||
'series' => [
|
||||
[
|
||||
'name' => 'Limit',
|
||||
'data' => []
|
||||
],
|
||||
[
|
||||
'name' => 'Spent',
|
||||
'data' => []
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
/*
|
||||
* Get all budgets.
|
||||
*/
|
||||
$budgets = \Auth::user()->budgets()->orderBy('name', 'ASC')->get();
|
||||
$budgetIds = [];
|
||||
/** @var \Budget $budget */
|
||||
foreach ($budgets as $budget) {
|
||||
$budgetIds[] = $budget->id;
|
||||
/*
|
||||
* Does the budget have a limit starting on $start?
|
||||
*/
|
||||
$rep = \LimitRepetition::
|
||||
leftJoin('limits', 'limit_repetitions.limit_id', '=', 'limits.id')->leftJoin(
|
||||
'components', 'limits.component_id', '=', 'components.id'
|
||||
)->where('limit_repetitions.startdate', $start->format('Y-m-d'))->where(
|
||||
'components.id', $budget->id
|
||||
)->first(['limit_repetitions.*']);
|
||||
$limit = is_null($rep) ? 0.0 : floatval($rep->amount);
|
||||
$id = is_null($rep) ? null : $rep->id;
|
||||
$parameter = is_null($rep) ? 'useSession=true' : '';
|
||||
|
||||
/*
|
||||
* Date range to check for expenses made?
|
||||
*/
|
||||
if (is_null($rep)) {
|
||||
// use the session start and end for our search query
|
||||
$expenseStart = Session::get('start');
|
||||
$expenseEnd = Session::get('end');
|
||||
|
||||
} else {
|
||||
// use the limit's start and end for our search query
|
||||
$expenseStart = $rep->startdate;
|
||||
$expenseEnd = $rep->enddate;
|
||||
}
|
||||
/*
|
||||
* How much have we spent on this budget?
|
||||
*/
|
||||
$expenses = $budget->transactionjournals()->before($expenseEnd)->after($expenseStart)->lessThan(0)->sum(
|
||||
'amount'
|
||||
);
|
||||
$expenses = floatval($expenses) * -1;
|
||||
|
||||
/*
|
||||
* Append to chart:
|
||||
*/
|
||||
if ($limit > 0 || $expenses > 0) {
|
||||
$data['labels'][] = $budget->name;
|
||||
$data['series'][0]['data'][] = [
|
||||
'y' => $limit,
|
||||
'url' => route('budgets.show', [$budget->id, $id]) . '?' . $parameter
|
||||
];
|
||||
$data['series'][1]['data'][] = [
|
||||
'y' => $expenses,
|
||||
'url' => route('budgets.show', [$budget->id, $id]) . '?' . $parameter
|
||||
];
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Add expenses that have no budget:
|
||||
*/
|
||||
$set = \Auth::user()->transactionjournals()->whereNotIn(
|
||||
'transaction_journals.id', function ($query) use ($start, $end) {
|
||||
$query->select('transaction_journals.id')->from('transaction_journals')
|
||||
->leftJoin(
|
||||
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
|
||||
'transaction_journals.id'
|
||||
)
|
||||
->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->where('components.class', 'Budget');
|
||||
}
|
||||
)->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->sum('amount');
|
||||
/*
|
||||
* This can be debugged by using get(['transaction_journals.*','transactions.amount']);
|
||||
*
|
||||
*
|
||||
*/
|
||||
$data['labels'][] = 'No budget';
|
||||
$data['series'][0]['data'][] = [
|
||||
'y' => 0,
|
||||
'url' => null
|
||||
];
|
||||
$data['series'][1]['data'][] = [
|
||||
'y' => floatval($set) * -1,
|
||||
'url' => null
|
||||
];
|
||||
|
||||
return Response::json($data);
|
||||
|
||||
return Response::json($this->_chart->budgets($start));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,117 +102,6 @@ class Chart implements ChartInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
*
|
||||
* @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();
|
||||
$limitInPeriod = '';
|
||||
$spentInPeriod = '';
|
||||
|
||||
/** @var \Budget $budget */
|
||||
foreach ($budgets as $budget) {
|
||||
$budget->count = 0;
|
||||
foreach ($budget->limits as $limit) {
|
||||
/** @var $rep \LimitRepetition */
|
||||
foreach ($limit->limitrepetitions as $index => $rep) {
|
||||
if ($index == 0) {
|
||||
$limitInPeriod = 'Envelope for ' . $rep->periodShow();
|
||||
$spentInPeriod = 'Spent in ' . $rep->periodShow();
|
||||
}
|
||||
$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);
|
||||
}
|
||||
if ($budget->count == 0) {
|
||||
// get expenses in period until today, starting at $start.
|
||||
$end = \Session::get('end');
|
||||
$expenses = $budget->transactionjournals()->after($start)->before($end)
|
||||
->transactionTypes(
|
||||
['Withdrawal']
|
||||
)->get();
|
||||
$budget->spentInPeriod = 0;
|
||||
/** @var \TransactionJournal $expense */
|
||||
foreach ($expenses as $expense) {
|
||||
$transaction = $expense->transactions[1];
|
||||
if (!is_null($transaction)) {
|
||||
$budget->spentInPeriod += floatval($transaction->amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$data['series'] = [
|
||||
[
|
||||
'name' => $limitInPeriod,
|
||||
'data' => []
|
||||
],
|
||||
[
|
||||
'name' => $spentInPeriod,
|
||||
'data' => []
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
foreach ($budgets as $budget) {
|
||||
if ($budget->count > 0) {
|
||||
$data['labels'][] = $budget->name;
|
||||
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'][] = ['y' => $amount, 'id' => 'amount-' . $rep->id];
|
||||
$data['series'][1]['data'][] = ['y' => $rep->spent, 'color' => $color,
|
||||
'id' => 'spent-' . $rep->id];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// add for "empty" budget:
|
||||
if ($budget->spentInPeriod > 0) {
|
||||
$data['labels'][] = $budget->name;
|
||||
$data['series'][0]['data'][] = ['y' => null, 'id' => 'amount-norep-' . $budget->id];
|
||||
$data['series'][1]['data'][] = ['y' => $budget->spentInPeriod,
|
||||
'id' => 'spent-norep-' . $budget->id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
|
@ -30,13 +30,6 @@ interface ChartInterface
|
||||
*/
|
||||
public function categories(Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function budgets(Carbon $start);
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
* @param Carbon $date
|
||||
|
Loading…
Reference in New Issue
Block a user