First render of new budget charts.

This commit is contained in:
James Cole 2016-04-24 20:23:17 +02:00
parent 2cfbfd8649
commit 32c8ddbe1b
6 changed files with 105 additions and 6 deletions

View File

@ -19,6 +19,7 @@ use Illuminate\Support\Collection;
*/
interface BudgetChartGeneratorInterface
{
/**
* @param Collection $entries
*
@ -40,6 +41,14 @@ interface BudgetChartGeneratorInterface
*/
public function multiYear(Collection $entries): array;
/**
* @param Collection $entries
* @param string $viewRange
*
* @return array
*/
public function period(Collection $entries, string $viewRange) : array;
/**
* @param Collection $budgets
* @param Collection $entries

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Generator\Chart\Budget;
use Config;
use Illuminate\Support\Collection;
use Navigation;
use Preferences;
/**
@ -126,6 +127,42 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
}
/**
* @param Collection $entries
* @param string $viewRange
*
* @return array
*/
public function period(Collection $entries, string $viewRange) : array
{
$data = [
'labels' => [],
'datasets' => [
0 => [
'label' => trans('firefly.budgeted'),
'data' => [],
],
1 => [
'label' => trans('firefly.spent'),
'data' => [],
],
],
'count' => 2,
];
foreach ($entries as $entry) {
$label = Navigation::periodShow($entry['date'], $viewRange);
$data['labels'][] = $label;
// data set 0 is budgeted
// data set 1 is spent:
$data['datasets'][0]['data'][] = $entry['budgeted'];
$data['datasets'][1]['data'][] = round(($entry['spent'] * -1), 2);
}
return $data;
}
/**
* @param Collection $budgets
* @param Collection $entries

View File

@ -240,6 +240,59 @@ class BudgetController extends Controller
}
/**
* @param Budget $budget
* @param string $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*/
public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($reportType);
$cache->addProperty($accounts);
$cache->addProperty('budget');
$cache->addProperty('period');
if ($cache->has()) {
//return Response::json($cache->get());
}
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;
$set = new Collection;
while ($current < $end) {
$currentStart = clone $current;
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
// get all budget limits and their repetitions.
$reps = $repository->getAllBudgetLimitRepetitions($currentStart, $currentEnd);
$budgeted = $reps->sum('amount');
$perBudget = $repository->spentPerBudgetPerAccount(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
$spent = $perBudget->sum('spent');
$entry = [
'date' => clone $currentStart,
'budgeted' => $budgeted,
'spent' => $spent,
];
$set->push($entry);
$currentEnd->addDay();
$current = clone $currentEnd;
}
$data = $this->generator->period($set, $viewRange);
$cache->store($data);
return Response::json($data);
}
/**
*
* @param BudgetRepositoryInterface $repository

View File

@ -201,6 +201,7 @@ Route::group(
// this chart is used in reports:
Route::get('/chart/budget/multi-year/{reportType}/{start_date}/{end_date}/{accountList}/{budgetList}', ['uses' => 'Chart\BudgetController@multiYear']);
Route::get('/chart/budget/period/{budget}/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\BudgetController@period']);
Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'Chart\BudgetController@budgetLimit']);
Route::get('/chart/budget/{budget}', ['uses' => 'Chart\BudgetController@budget']);

View File

@ -56,18 +56,18 @@ class Navigation
}
/**
* @param \Carbon\Carbon $theCurrentEnd
* @param \Carbon\Carbon $end
* @param $repeatFreq
*
* @return \Carbon\Carbon
* @throws FireflyException
*/
public function endOfPeriod(Carbon $theCurrentEnd, string $repeatFreq): Carbon
public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon
{
$currentEnd = clone $theCurrentEnd;
$currentEnd = clone $end;
$functionMap = [
'1D' => 'addDay', 'daily' => 'addDay',
'1D' => 'endOfDay', 'daily' => 'endOfDay',
'1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek',
'1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth',
'3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',

View File

@ -22,9 +22,8 @@ function drawChart() {
// in a loop
$.each($('.budget_year_chart'), function (i, v) {
var holder = $(v);
var id = holder.id;
var id = holder.attr('id');
var budgetId = holder.data('budget');
console.log('now at ' + id);
columnChart('chart/budget/period/' + budgetId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, id);
});