Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Code for #1040
James Colec2a425121da0
diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.phpindex 81402b25d9..46e332fc87 100644--- a/app/Http/Controllers/Chart/AccountController.php+++ b/app/Http/Controllers/Chart/AccountController.php@@ -76,21 +76,46 @@ class AccountController extends Controller$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];- $chartData = [];- while ($end >= $current) {- $theDate = $current->format('Y-m-d');- $balance = $range[$theDate] ?? $previous;- $label = $current->formatLocalized($format);- $chartData[$label] = $balance;- $previous = $balance;- $current->addDay();+ // depending on diff, do something with range of chart.+ $step = '1D';+ $months = $start->diffInMonths($end);+ if ($months > 3) {+ $step = '1W';+ }+ 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);$cache->store($data);diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.phpindex 932e30d797..7852fe15d9 100644--- a/app/Http/Controllers/Chart/BudgetController.php+++ b/app/Http/Controllers/Chart/BudgetController.php@@ -37,7 +37,6 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;use FireflyIII\Repositories\Category\CategoryRepositoryInterface;use FireflyIII\Support\CacheProperties;use Illuminate\Support\Collection;-use Preferences;use Response;use Steam;@@ -78,37 +77,43 @@ class BudgetController extends Controller*/public function budget(Budget $budget){- $first = $this->repository->firstUseDate($budget);- $range = Preferences::get('viewRange', '1M')->data;- $currentStart = app('navigation')->startOfPeriod($first, $range);- $last = session('end', new Carbon);- $cache = new CacheProperties();- $cache->addProperty($first);- $cache->addProperty($last);+ $start = $this->repository->firstUseDate($budget);+ $end = session('end', new Carbon);+ $cache = new CacheProperties();+ $cache->addProperty($start);+ $cache->addProperty($end);$cache->addProperty('chart.budget.budget');if ($cache->has()) {return Response::json($cache->get()); // @codeCoverageIgnore}- $final = clone $last;- $final->addYears(2);+ // depending on diff, do something with range of chart.+ $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]);- $last = app('navigation')->endOfX($last, $range, $final); // not to overshoot.- $entries = [];- while ($currentStart < $last) {- // periodspecific dates:- $currentEnd = app('navigation')->endOfPeriod($currentStart, $range);- // sub another day because reasons.- $currentEnd->subDay();- $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd);- $format = app('navigation')->periodShow($currentStart, $range);- $entries[$format] = bcmul($spent, '-1');- $currentStart = clone $currentEnd;- $currentStart->addDays(2);- }-- $data = $this->generator->singleSet(strval(trans('firefly.spent')), $entries);+ $chartData = [];+ $current = clone $start;+ $current = app('navigation')->startOfPeriod($current, $step);++ while ($end >= $current) {+ $currentEnd = app('navigation')->endOfPeriod($current, $step);+ $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $current, $currentEnd);+ $label = app('navigation')->periodShow($current, $step);+ $chartData[$label] = floatval(bcmul($spent,'-1'));+ $current = app('navigation')->addPeriod($current, $step, 1);+ }++ $data = $this->generator->singleSet(strval(trans('firefly.spent')), $chartData);$cache->store($data);