Reinstated a chart.

This commit is contained in:
James Cole 2016-04-25 09:57:39 +02:00
parent 2cd593157f
commit 51d97cdca5
3 changed files with 54 additions and 3 deletions

View File

@ -22,10 +22,11 @@ interface BudgetChartGeneratorInterface
/**
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
public function budgetLimit(Collection $entries): array;
public function budgetLimit(Collection $entries, string $dateFormat): array;
/**
* @param Collection $entries

View File

@ -18,13 +18,14 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
/**
*
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
public function budgetLimit(Collection $entries): array
public function budgetLimit(Collection $entries, string $dateFormat = 'monthAndDay'): array
{
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
$format = Config::get('firefly.monthAndDay.' . $language);
$format = Config::get('firefly.' . $dateFormat . '.' . $language);
$data = [
'labels' => [],

View File

@ -37,6 +37,55 @@ class BudgetController extends Controller
$this->generator = app('FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface');
}
/**
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function budget(BudgetRepositoryInterface $repository, Budget $budget)
{
// dates and times
$first = $repository->getFirstBudgetLimitDate($budget);
$range = Preferences::get('viewRange', '1M')->data;
$last = session('end', new Carbon);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($first);
$cache->addProperty($last);
$cache->addProperty('budget');
if ($cache->has()) {
return Response::json($cache->get());
}
$final = clone $last;
$final->addYears(2);
$last = Navigation::endOfX($last, $range, $final);
$entries = new Collection;
// get all expenses:
$spentArray = $repository->spentPerDay($budget, $first, $last);
while ($first < $last) {
// periodspecific dates:
$currentStart = Navigation::startOfPeriod($first, $range);
$currentEnd = Navigation::endOfPeriod($first, $range);
$spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray);
$entry = [$first, ($spent * -1)];
$entries->push($entry);
$first = Navigation::addPeriod($first, $range, 0);
}
$data = $this->generator->budgetLimit($entries, 'month');
$cache->store($data);
return Response::json($data);
}
/**
* Shows the amount left in a specific budget limit.
*