Fixed missing chart.

This commit is contained in:
James Cole
2016-12-11 11:15:19 +01:00
parent 5c02eaa66c
commit 43f59a1135
6 changed files with 106 additions and 67 deletions

View File

@@ -55,6 +55,51 @@ class AccountController extends Controller
$this->generator = app(AccountChartGeneratorInterface::class);
}
/**
* @param Account $account
* @param string $date
*
* @return \Illuminate\Http\JsonResponse
* @throws FireflyException
*/
public function all(Account $account)
{
$cache = new CacheProperties();
$cache->addProperty('account-all-chart');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
}
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$start = $repository->oldestJournalDate($account);
$end = new Carbon;
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$labels = [];
$chartData = [];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$labels[] = $current->formatLocalized($format);
$chartData[] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
$cache->store($data);
return Response::json($data);
}
/**
* Shows the balances for all the user's expense accounts.
*
@@ -242,6 +287,58 @@ class AccountController extends Controller
}
/**
* @param Account $account
* @param string $date
*
* @return \Illuminate\Http\JsonResponse
* @throws FireflyException
*/
public function period(Account $account, string $date)
{
try {
$start = new Carbon($date);
} catch (Exception $e) {
Log::error($e->getMessage());
throw new FireflyException('"' . e($date) . '" does not seem to be a valid date. Should be in the format YYYY-MM-DD');
}
$range = Preferences::get('viewRange', '1M')->data;
$end = Navigation::endOfPeriod($start, $range);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('frontpage');
$cache->addProperty('specificPeriod');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
}
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$labels = [];
$chartData = [];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$labels[] = $current->formatLocalized($format);
$chartData[] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
$cache->store($data);
return Response::json($data);
}
/**
* Shows the balances for a given set of dates and accounts.
*
@@ -353,58 +450,6 @@ class AccountController extends Controller
return Response::json($data);
}
/**
* @param Account $account
* @param string $date
*
* @return \Illuminate\Http\JsonResponse
* @throws FireflyException
*/
public function period(Account $account, string $date)
{
try {
$start = new Carbon($date);
} catch (Exception $e) {
Log::error($e->getMessage());
throw new FireflyException('"' . e($date) . '" does not seem to be a valid date. Should be in the format YYYY-MM-DD');
}
$range = Preferences::get('viewRange', '1M')->data;
$end = Navigation::endOfPeriod($start, $range);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('frontpage');
$cache->addProperty('specificPeriod');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
}
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$labels = [];
$chartData = [];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$labels[] = $current->formatLocalized($format);
$chartData[] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
$cache->store($data);
return Response::json($data);
}
/**
* @param Collection $accounts
* @param Carbon $start