mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Code for #1040
This commit is contained in:
@@ -76,21 +76,46 @@ class AccountController extends Controller
|
|||||||
$repository = app(AccountRepositoryInterface::class);
|
$repository = app(AccountRepositoryInterface::class);
|
||||||
$start = $repository->oldestJournalDate($account);
|
$start = $repository->oldestJournalDate($account);
|
||||||
$end = new Carbon;
|
$end = new Carbon;
|
||||||
$format = (string)trans('config.month_and_day');
|
|
||||||
$range = Steam::balanceInRange($account, $start, $end);
|
|
||||||
$current = clone $start;
|
|
||||||
$previous = array_values($range)[0];
|
|
||||||
$chartData = [];
|
|
||||||
|
|
||||||
while ($end >= $current) {
|
// depending on diff, do something with range of chart.
|
||||||
$theDate = $current->format('Y-m-d');
|
$step = '1D';
|
||||||
$balance = $range[$theDate] ?? $previous;
|
$months = $start->diffInMonths($end);
|
||||||
$label = $current->formatLocalized($format);
|
if ($months > 3) {
|
||||||
$chartData[$label] = $balance;
|
$step = '1W';
|
||||||
$previous = $balance;
|
}
|
||||||
$current->addDay();
|
if ($months > 24) {
|
||||||
|
$step = '1M';
|
||||||
|
}
|
||||||
|
if ($months > 100) {
|
||||||
|
$step = '1Y';
|
||||||
|
}
|
||||||
|
$chartData = [];
|
||||||
|
$current = clone $start;
|
||||||
|
switch ($step) {
|
||||||
|
case '1D':
|
||||||
|
$format = (string)trans('config.month_and_day');
|
||||||
|
$range = Steam::balanceInRange($account, $start, $end);
|
||||||
|
$previous = array_values($range)[0];
|
||||||
|
while ($end >= $current) {
|
||||||
|
$theDate = $current->format('Y-m-d');
|
||||||
|
$balance = $range[$theDate] ?? $previous;
|
||||||
|
$label = $current->formatLocalized($format);
|
||||||
|
$chartData[$label] = floatval($balance);
|
||||||
|
$previous = $balance;
|
||||||
|
$current->addDay();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '1W':
|
||||||
|
case '1M':
|
||||||
|
case '1Y':
|
||||||
|
while ($end >= $current) {
|
||||||
|
$balance = floatval(Steam::balance($account, $current));
|
||||||
|
$label = app('navigation')->periodShow($current, $step);
|
||||||
|
$chartData[$label] = $balance;
|
||||||
|
$current = app('navigation')->addPeriod($current, $step, 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->generator->singleSet($account->name, $chartData);
|
$data = $this->generator->singleSet($account->name, $chartData);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
|
@@ -37,7 +37,6 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|||||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Preferences;
|
|
||||||
use Response;
|
use Response;
|
||||||
use Steam;
|
use Steam;
|
||||||
|
|
||||||
@@ -78,37 +77,43 @@ class BudgetController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function budget(Budget $budget)
|
public function budget(Budget $budget)
|
||||||
{
|
{
|
||||||
$first = $this->repository->firstUseDate($budget);
|
$start = $this->repository->firstUseDate($budget);
|
||||||
$range = Preferences::get('viewRange', '1M')->data;
|
$end = session('end', new Carbon);
|
||||||
$currentStart = app('navigation')->startOfPeriod($first, $range);
|
$cache = new CacheProperties();
|
||||||
$last = session('end', new Carbon);
|
$cache->addProperty($start);
|
||||||
$cache = new CacheProperties();
|
$cache->addProperty($end);
|
||||||
$cache->addProperty($first);
|
|
||||||
$cache->addProperty($last);
|
|
||||||
$cache->addProperty('chart.budget.budget');
|
$cache->addProperty('chart.budget.budget');
|
||||||
|
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$final = clone $last;
|
// depending on diff, do something with range of chart.
|
||||||
$final->addYears(2);
|
$step = '1D';
|
||||||
|
$months = $start->diffInMonths($end);
|
||||||
|
if ($months > 3) {
|
||||||
|
$step = '1W';
|
||||||
|
}
|
||||||
|
if ($months > 24) {
|
||||||
|
$step = '1M';
|
||||||
|
}
|
||||||
|
if ($months > 60) {
|
||||||
|
$step = '1Y';
|
||||||
|
}
|
||||||
$budgetCollection = new Collection([$budget]);
|
$budgetCollection = new Collection([$budget]);
|
||||||
$last = app('navigation')->endOfX($last, $range, $final); // not to overshoot.
|
$chartData = [];
|
||||||
$entries = [];
|
$current = clone $start;
|
||||||
while ($currentStart < $last) {
|
$current = app('navigation')->startOfPeriod($current, $step);
|
||||||
// periodspecific dates:
|
|
||||||
$currentEnd = app('navigation')->endOfPeriod($currentStart, $range);
|
while ($end >= $current) {
|
||||||
// sub another day because reasons.
|
$currentEnd = app('navigation')->endOfPeriod($current, $step);
|
||||||
$currentEnd->subDay();
|
$spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $current, $currentEnd);
|
||||||
$spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd);
|
$label = app('navigation')->periodShow($current, $step);
|
||||||
$format = app('navigation')->periodShow($currentStart, $range);
|
$chartData[$label] = floatval(bcmul($spent,'-1'));
|
||||||
$entries[$format] = bcmul($spent, '-1');
|
$current = app('navigation')->addPeriod($current, $step, 1);
|
||||||
$currentStart = clone $currentEnd;
|
|
||||||
$currentStart->addDays(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->generator->singleSet(strval(trans('firefly.spent')), $entries);
|
$data = $this->generator->singleSet(strval(trans('firefly.spent')), $chartData);
|
||||||
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user