Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add new chart, replace old code.
James Cole6143c0f14f49
diff --git a/app/Api/V1/Controllers/Chart/BudgetController.php b/app/Api/V1/Controllers/Chart/BudgetController.phpindex bad1304a1d..bbd5fd968e 100644--- a/app/Api/V1/Controllers/Chart/BudgetController.php+++ b/app/Api/V1/Controllers/Chart/BudgetController.php@@ -44,6 +44,7 @@ use Illuminate\Http\Request;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;use Override;+use function _PHPStan_02959ca10\Symfony\Component\String\b;/*** Class BudgetController@@ -81,11 +82,9 @@ final class BudgetController extends Controller}/**- * TODO see autocomplete/accountcontroller- ** @throws FireflyException*/- public function overview(DateRangeRequest $request): JsonResponse+ public function overviewWithBudgetLimits(DateRangeRequest $request): JsonResponse{/** @var Carbon $start */$start = $request->attributes->get('start');@@ -97,6 +96,29 @@ final class BudgetController extends Controller$budgets = $this->repository->getActiveBudgets();$data = [];+ /** @var Budget $budget */+ foreach ($budgets as $budget) {+ // could return multiple arrays, so merge.+ $data = array_merge($data, $this->processBudgetAndLimits($budget, $start, $end));+ }++ return response()->json($this->clean($data));+ }++ /**+ * @throws FireflyException+ */+ public function overview(DateRangeRequest $request): JsonResponse+ {+ /** @var Carbon $start */+ $start = $request->attributes->get('start');++ /** @var Carbon $end */+ $end = $request->attributes->get('end');++ // code from FrontpageChartGenerator, but not in separate class+ $budgets = $this->repository->getActiveBudgets();+ $data = [];/** @var Budget $budget */foreach ($budgets as $budget) {// could return multiple arrays, so merge.@@ -232,22 +254,156 @@ final class BudgetController extends Controller];$return[] = $current;}-return $return;}- // /**- // * When no budget limits are present, the expenses of the whole period are collected and grouped.- // * This is grouped per currency. Because there is no limit set, "left to spend" and "overspent" are empty.- // *- // * @throws FireflyException- // */- // private function noBudgetLimits(Budget $budget, Carbon $start, Carbon $end): array- // {- // $spent = $this->opsRepository->listExpenses($start, $end, null, new Collection()->push($budget));- //- // return $this->processExpenses($budget->id, $spent, $start, $end);- // }+ /**+ * @throws FireflyException+ */+ private function processBudgetAndLimits(Budget $budget, Carbon $start, Carbon $end): array+ {+ // prep function+ $converter = new ExchangeRateConverter();+ $currencies = [$this->primaryCurrency->id => $this->primaryCurrency];+ $rows= [];+ // get all limits:+ $limits = $this->blRepository->getBudgetLimits($budget, $start, $end);+ $spentAll = $this->opsRepository->listExpenses($start, $end, null, new Collection()->push($budget));+ $allExpenses = $this->processExpenses($budget->id, $spentAll, $start, $end);+ /** @var BudgetLimit $limit */+ foreach ($limits as $limit) {+ $spentLimit = $this->opsRepository->listExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget));+ $limitExpenses = $this->processExpenses($budget->id, $spentLimit, $start, $end);++ foreach ($limitExpenses as $currencyId => $row) {+ // also process the data in $allExpenses.+ if(array_key_exists($currencyId, $allExpenses)) {+ $allExpenses[$currencyId]['spent'] = bcadd($allExpenses[$currencyId]['spent'], bcmul($row['spent'], '-1'));+ }++ // process entry for single limit:+ // primary currency entries+ $row['pc_budgeted'] = '0';+ $row['pc_spent'] = '0';+ $row['pc_left'] = '0';+ $row['pc_overspent'] = '0';++ $row['budgeted'] = $limit->amount;+ $row['start'] = $limit->start_date;+ $row['end'] = $limit->end_date;+ $row['left'] = bcsub((string)$row['budgeted'], bcmul((string)$row['spent'], '-1'));+ $row['overspent'] = bcmul($row['left'], '-1');+ $row['left'] = 1 === bccomp($row['left'], '0') ? $row['left'] : '0';+ $row['overspent'] = 1 === bccomp($row['overspent'], '0') ? $row['overspent'] : '0';++ // convert data if necessary.+ if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id) {+ $currencies[$currencyId] ??= Amount::getTransactionCurrencyById($currencyId);+ $row['pc_budgeted'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['budgeted']);+ $row['pc_spent'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['spent']);+ $row['pc_left'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['left']);+ $row['pc_overspent'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['overspent']);+ }+ if ($this->convertToPrimary && $currencyId === $this->primaryCurrency->id) {+ $row['pc_budgeted'] = $row['budgeted'];+ $row['pc_spent'] = $row['spent'];+ $row['pc_left'] = $row['left'];+ $row['pc_overspent'] = $row['overspent'];+ }+ $rows[] = $row;+ }+ }+ // still need to process the "main" spentAll to see if anything is left.++ // is always an array+ $return = [];+ foreach ($rows as $row) {++ $current = [+ 'label' => sprintf('%s (%s - %s)', $budget->name, $row['start']->isoFormat($this->monthAndDayFormat), $row['end']->isoFormat($this->monthAndDayFormat)),+ 'currency_id' => (string) $row['currency_id'],+ 'currency_name' => $row['currency_name'],+ 'currency_code' => $row['currency_code'],+ 'currency_decimal_places' => $row['currency_decimal_places'],++ 'primary_currency_id' => (string) $this->primaryCurrency->id,+ 'primary_currency_name' => $this->primaryCurrency->name,+ 'primary_currency_code' => $this->primaryCurrency->code,+ 'primary_currency_symbol' => $this->primaryCurrency->symbol,+ 'primary_currency_decimal_places' => $this->primaryCurrency->decimal_places,++ 'period' => null,+ 'date' => $row['start'],+ 'start_date' => $row['start'],+ 'end_date' => $row['end'],+ 'yAxisID' => 0,+ 'type' => 'bar',+ 'entries' => ['budgeted' => $row['budgeted'], 'spent' => $row['spent'], 'left' => $row['left'], 'overspent' => $row['overspent']],+ 'pc_entries' => [+ 'budgeted' => $row['pc_budgeted'],+ 'spent' => $row['pc_spent'],+ 'left' => $row['pc_left'],+ 'overspent' => $row['pc_overspent'],+ ],+ ];+ $return[] = $current;+ }+ /// process main "left over" array.+ foreach($allExpenses as $currencyId => $row) {+ if(bccomp($row['spent'], '0') !== 0) {+ // primary currency entries+ $row['pc_budgeted'] = '0';+ $row['pc_spent'] = '0';+ $row['pc_left'] = '0';+ $row['pc_overspent'] = '0';++ // convert data if necessary.+ if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id) {+ $currencies[$currencyId] ??= Amount::getTransactionCurrencyById($currencyId);+ $row['pc_budgeted'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['budgeted']);+ $row['pc_spent'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['spent']);+ $row['pc_left'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['left']);+ $row['pc_overspent'] = $converter->convert($currencies[$currencyId], $this->primaryCurrency, $start, $row['overspent']);+ }+ if ($this->convertToPrimary && $currencyId === $this->primaryCurrency->id) {+ $row['pc_budgeted'] = $row['budgeted'];+ $row['pc_spent'] = $row['spent'];+ $row['pc_left'] = $row['left'];+ $row['pc_overspent'] = $row['overspent'];+ }++ $current = [+ 'label' => sprintf('%s %s', $budget->name, trans('firefly.spent_outside_chart')),+ 'currency_id' => (string) $row['currency_id'],+ 'currency_name' => $row['currency_name'],+ 'currency_code' => $row['currency_code'],+ 'currency_decimal_places' => $row['currency_decimal_places'],++ 'primary_currency_id' => (string) $this->primaryCurrency->id,+ 'primary_currency_name' => $this->primaryCurrency->name,+ 'primary_currency_code' => $this->primaryCurrency->code,+ 'primary_currency_symbol' => $this->primaryCurrency->symbol,+ 'primary_currency_decimal_places' => $this->primaryCurrency->decimal_places,++ 'period' => null,+ 'date' => $row['start'],+ 'start_date' => $row['start'],+ 'end_date' => $row['end'],+ 'yAxisID' => 0,+ 'type' => 'bar',+ 'entries' => ['budgeted' => $row['budgeted'], 'spent' => $row['spent'], 'left' => $row['left'], 'overspent' => $row['overspent']],+ 'pc_entries' => [+ 'budgeted' => $row['pc_budgeted'],+ 'spent' => $row['pc_spent'],+ 'left' => $row['pc_left'],+ 'overspent' => $row['pc_overspent'],+ ],+ ];+ $return[] = $current;+ }+ }+ return $return;+ }/*** Shared between the "noBudgetLimits" function and "processLimit". Will take a single set of expenses and returndiff --git a/app/Api/V1/Controllers/Controller.php b/app/Api/V1/Controllers/Controller.phpindex 65791080e2..29ce282429 100644--- a/app/Api/V1/Controllers/Controller.php+++ b/app/Api/V1/Controllers/Controller.php@@ -62,6 +62,7 @@ abstract class Controller extends BaseControlleruse DispatchesJobs;use ValidatesRequests;use ValidatesUserGroupTrait;+ protected string $monthAndDayFormat;protected const string CONTENT_TYPE = 'application/vnd.api+json';protected const string JSON_CONTENT_TYPE = 'application/json';@@ -87,6 +88,8 @@ abstract class Controller extends BaseController$language = Steam::getLanguage();$this->convertToPrimary = Amount::convertToPrimary();$this->primaryCurrency = Amount::getPrimaryCurrency();+ $locale = Steam::getLocale();+ $this->monthAndDayFormat = (string) trans('config.month_and_day_js', [], $locale);app()->setLocale($language);}diff --git a/public/v1/js/ff/index.js b/public/v1/js/ff/index.jsindex 7b00a4bb1b..480aeab0be 100644--- a/public/v1/js/ff/index.js+++ b/public/v1/js/ff/index.js@@ -34,7 +34,7 @@ function drawChart() {if (billCount > 0) {multiCurrencyPieChart('chart/bill/frontpage', 'bills-chart');}- stackedColumnChart('chart/budget/frontpage', 'budgets-two-chart');+ //stackedColumnChart('chart/budget/frontpage', 'budgets-two-chart');columnChart('chart/category/frontpage', 'categories-chart');columnChart(accountExpenseUrl, 'expense-accounts-chart');columnChart(accountRevenueUrl, 'revenue-accounts-chart');diff --git a/resources/assets/v3/js/pages/dashboard/dashboard.js b/resources/assets/v3/js/pages/dashboard/dashboard.jsindex 00979124db..31c9d0fadf 100644--- a/resources/assets/v3/js/pages/dashboard/dashboard.js+++ b/resources/assets/v3/js/pages/dashboard/dashboard.js@@ -50,15 +50,15 @@ let index = function () {true,);- // drawMultiCurrencyChart(- // "stacked-column",- // "api/v1/chart/budget/overview?start=" +- // format(start, "yyyy-LL-dd") +- // "&end=" +- // format(end, "yyyy-LL-dd"),- // "budgets-chart",- // value, false- // );+ drawMultiCurrencyChart(+ "stacked-column",+ "api/v1/chart/budget/overview-with-limits?start=" ++ format(start, "yyyy-LL-dd") ++ "&end=" ++ format(end, "yyyy-LL-dd"),+ "budgets-chart",+ value, false+ );});},loadPiggyBanks() {diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.phpindex c2c1250f4c..bbc599dec7 100644--- a/resources/lang/en_US/firefly.php+++ b/resources/lang/en_US/firefly.php@@ -1827,6 +1827,7 @@ return ['spent_this_period' => 'Spent on this budget','spent_this_period_per_day' => 'Spent on this budget per day (:days day(s))','spent_in_budget_limit_outside_period' => 'Spent on this budget, but NOT in this period',+ 'spent_outside_chart' => '(outside of pre-set budget amounts)','spent_in_budget_limit_outside_period_per_day' => 'Spent on this budget, but NOT in this period per day (:days day(s))','left_in_budget_limit_overview' => 'Left in this budget','left_in_budget_limit_per_day' => 'Left in this budget per day (:days day(s))',diff --git a/resources/views/index.blade.php b/resources/views/index.blade.phpindex 5c9f7e0ea9..117661b6be 100644--- a/resources/views/index.blade.php+++ b/resources/views/index.blade.php@@ -32,14 +32,14 @@</div></div><div class="card-body">- <!--<div><canvas id="budgets-chart" class="wide-chart" height="400" width="100%"></canvas></div>- -->+ <!--<div><canvas id="budgets-two-chart" class="wide-chart" height="400" width="100%"></canvas></div>+ --></div><div class="card-footer text-end"><a href="{{ route('budgets.index') }}" class="btn btn-primary btn-sm">diff --git a/routes/api.php b/routes/api.phpindex b9a68ad06b..54f7094e16 100644--- a/routes/api.php+++ b/routes/api.php@@ -143,6 +143,7 @@ Route::group(],static function (): void {Route::get('overview', ['uses' => 'BudgetController@overview', 'as' => 'overview']);+ Route::get('overview-with-limits', ['uses' => 'BudgetController@overviewWithBudgetLimits', 'as' => 'overview-with-limits']);});