From a6b1fcb6096532e8c3291a68e6ac0c1b44992bed Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 29 Aug 2019 21:42:55 +0200 Subject: [PATCH] Refactor references to budget repository. --- .../Controllers/Budget/AmountController.php | 2 +- .../Controllers/Budget/IndexController.php | 9 +- .../Controllers/Chart/BudgetController.php | 32 ++++--- app/Http/Controllers/Json/BoxController.php | 15 ++- app/Repositories/Budget/BudgetRepository.php | 70 -------------- .../Budget/BudgetRepositoryInterface.php | 24 ----- .../Budget/OperationsRepository.php | 96 +++++++++++++++++++ .../Budget/OperationsRepositoryInterface.php | 27 ++++++ app/Support/Http/Controllers/AugumentData.php | 17 +++- 9 files changed, 173 insertions(+), 119 deletions(-) diff --git a/app/Http/Controllers/Budget/AmountController.php b/app/Http/Controllers/Budget/AmountController.php index 4f9c419157..25245a6393 100644 --- a/app/Http/Controllers/Budget/AmountController.php +++ b/app/Http/Controllers/Budget/AmountController.php @@ -98,7 +98,7 @@ class AmountController extends Controller $budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount); // calculate what the user has spent in current period. - $spent = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); + $spent = $this->opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); // given the new budget, this is what they have left (and left per day?) $left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true); diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.php index 77dcb37d1d..8b34f2743d 100644 --- a/app/Http/Controllers/Budget/IndexController.php +++ b/app/Http/Controllers/Budget/IndexController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\Budget; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\Http\Controllers\DateCalculation; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -40,11 +41,14 @@ class IndexController extends Controller { use DateCalculation; + /** @var OperationsRepositoryInterface */ + private $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ private $repository; /** * IndexController constructor. + * * @codeCoverageIgnore */ public function __construct() @@ -57,7 +61,8 @@ class IndexController extends Controller function ($request, $next) { app('view')->share('title', (string)trans('firefly.budgets')); app('view')->share('mainTitleIcon', 'fa-tasks'); - $this->repository = app(BudgetRepositoryInterface::class); + $this->repository = app(BudgetRepositoryInterface::class); + $this->opsRepository = app(OperationsRepositoryInterface::class); $this->repository->cleanupBudgets(); return $next($request); @@ -110,7 +115,7 @@ class IndexController extends Controller $inactive = $this->repository->getInactiveBudgets(); // collect budget info to fill bars and so on. - $budgetInformation = $this->repository->collectBudgetInformation($collection, $start, $end); + $budgetInformation = $this->opsRepository->collectBudgetInformation($collection, $start, $end); // to display available budget: $available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 19cbbb363f..8db63d7a9f 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -31,6 +31,7 @@ use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Http\Controllers\AugumentData; use FireflyIII\Support\Http\Controllers\DateCalculation; @@ -46,12 +47,14 @@ class BudgetController extends Controller use DateCalculation, AugumentData; /** @var GeneratorInterface Chart generation methods. */ protected $generator; - + /** @var OperationsRepositoryInterface */ + protected $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ protected $repository; /** * BudgetController constructor. + * * @codeCoverageIgnore */ public function __construct() @@ -60,8 +63,9 @@ class BudgetController extends Controller $this->middleware( function ($request, $next) { - $this->generator = app(GeneratorInterface::class); - $this->repository = app(BudgetRepositoryInterface::class); + $this->generator = app(GeneratorInterface::class); + $this->repository = app(BudgetRepositoryInterface::class); + $this->opsRepository = app(OperationsRepositoryInterface::class); return $next($request); } @@ -105,7 +109,7 @@ class BudgetController extends Controller if ('1Y' === $step) { $currentEnd->subDay(); // @codeCoverageIgnore } - $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $current, $currentEnd); + $spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $current, $currentEnd); $label = app('navigation')->periodShow($current, $step); $chartData[$label] = (float)bcmul($spent, '-1'); $current = clone $currentEnd; @@ -125,7 +129,7 @@ class BudgetController extends Controller * * TODO this chart is not multi-currency aware. * - * @param Budget $budget + * @param Budget $budget * @param BudgetLimit $budgetLimit * * @return JsonResponse @@ -155,7 +159,7 @@ class BudgetController extends Controller $amount = $budgetLimit->amount; $budgetCollection = new Collection([$budget]); while ($start <= $end) { - $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $start, $start); + $spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $start, $start); $amount = bcadd($amount, $spent); $format = $start->formatLocalized((string)trans('config.month_and_day')); $entries[$format] = $amount; @@ -174,7 +178,7 @@ class BudgetController extends Controller * * TODO this chart is not multi-currency aware. * - * @param Budget $budget + * @param Budget $budget * @param BudgetLimit|null $budgetLimit * * @return JsonResponse @@ -223,7 +227,7 @@ class BudgetController extends Controller * * TODO this chart is not multi-currency aware. * - * @param Budget $budget + * @param Budget $budget * @param BudgetLimit|null $budgetLimit * * @return JsonResponse @@ -271,7 +275,7 @@ class BudgetController extends Controller * * TODO this chart is not multi-currency aware. * - * @param Budget $budget + * @param Budget $budget * @param BudgetLimit|null $budgetLimit * * @return JsonResponse @@ -377,9 +381,9 @@ class BudgetController extends Controller * * TODO this chart is not multi-currency aware. * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end * @param Collection $accounts * * @return JsonResponse @@ -426,8 +430,8 @@ class BudgetController extends Controller * TODO this chart is not multi-currency aware. * * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return JsonResponse */ diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.php index 137a3ae21a..c341d149bf 100644 --- a/app/Http/Controllers/Json/BoxController.php +++ b/app/Http/Controllers/Json/BoxController.php @@ -33,6 +33,7 @@ use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Http\Controllers\RequestInformation; @@ -49,12 +50,16 @@ class BoxController extends Controller /** * How much money user has available. * - * @param BudgetRepositoryInterface $repository - * * @return JsonResponse */ - public function available(BudgetRepositoryInterface $repository): JsonResponse + public function available(): JsonResponse { + /** @var BudgetRepositoryInterface $repository */ + $repository = app(BudgetRepositoryInterface::class); + /** @var OperationsRepositoryInterface $opsRepository */ + $opsRepository = app(OperationsRepositoryInterface::class); + + /** @var Carbon $start */ $start = session('start', Carbon::now()->startOfMonth()); /** @var Carbon $end */ @@ -74,7 +79,7 @@ class BoxController extends Controller // get spent amount: $budgets = $repository->getActiveBudgets(); - $budgetInformation = $repository->collectBudgetInformation($budgets, $start, $end); + $budgetInformation = $opsRepository->collectBudgetInformation($budgets, $start, $end); $spent = (string)array_sum(array_column($budgetInformation, 'spent')); $left = bcadd($available, $spent); $days = $today->diffInDays($end) + 1; @@ -121,7 +126,7 @@ class BoxController extends Controller $cache->addProperty($end); $cache->addProperty('box-balance'); if ($cache->has()) { - return response()->json($cache->get()); // @codeCoverageIgnore + return response()->json($cache->get()); // @codeCoverageIgnore } // prep some arrays: $incomes = []; diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 69c0cd22a1..39dab19811 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -96,52 +96,6 @@ class BudgetRepository implements BudgetRepositoryInterface return true; } - /** - * This method collects various info on budgets, used on the budget page and on the index. - * - * @param Collection $budgets - * @param Carbon $start - * @param Carbon $end - * - * @return array - * - */ - public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array - { - // get account information - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); - $defaultCurrency = app('amount')->getDefaultCurrency(); - $return = []; - /** @var Budget $budget */ - foreach ($budgets as $budget) { - $budgetId = $budget->id; - $return[$budgetId] = [ - 'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end), - 'budgeted' => '0', - ]; - $budgetLimits = $this->getBudgetLimits($budget, $start, $end); - $otherLimits = new Collection; - - // get all the budget limits relevant between start and end and examine them: - /** @var BudgetLimit $limit */ - foreach ($budgetLimits as $limit) { - if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) - ) { - $return[$budgetId]['currentLimit'] = $limit; - $return[$budgetId]['budgeted'] = round($limit->amount, $defaultCurrency->decimal_places); - continue; - } - // otherwise it's just one of the many relevant repetitions: - $otherLimits->push($limit); - } - $return[$budgetId]['otherLimits'] = $otherLimits; - } - - return $return; - } - /** * @param Budget $budget * @@ -705,30 +659,6 @@ class BudgetRepository implements BudgetRepositoryInterface $this->user = $user; } - /** - * @param Collection $budgets - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string - { - /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); - - $collector->setUser($this->user); - $collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation(); - - if ($accounts->count() > 0) { - $collector->setAccounts($accounts); - } - - return $collector->getSum(); - - } - /** * @param Collection $budgets * @param Collection $accounts diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 535e766a9f..e1f6583c03 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -41,17 +41,6 @@ interface BudgetRepositoryInterface */ public function cleanupBudgets(): bool; - /** - * This method collects various info on budgets, used on the budget page and on the index. - * - * @param Collection $budgets - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array; - /** * @param Budget $budget * @@ -245,19 +234,6 @@ interface BudgetRepositoryInterface */ public function setUser(User $user); - /** - * TODO this method is not multi-currency aware. - * - * @param Collection $budgets - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string; - - /** * Return multi-currency spent information. * diff --git a/app/Repositories/Budget/OperationsRepository.php b/app/Repositories/Budget/OperationsRepository.php index 82aa1e455e..b07f9d64f4 100644 --- a/app/Repositories/Budget/OperationsRepository.php +++ b/app/Repositories/Budget/OperationsRepository.php @@ -23,8 +23,14 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; +use FireflyIII\Helpers\Collector\GroupCollectorInterface; +use FireflyIII\Models\AccountType; use FireflyIII\Models\Budget; +use FireflyIII\Models\BudgetLimit; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\User; +use Illuminate\Support\Collection; use Log; /** @@ -54,6 +60,7 @@ class OperationsRepository implements OperationsRepositoryInterface * @param Budget $budget * * @return string + * @deprecated */ public function budgetedPerDay(Budget $budget): string { @@ -78,6 +85,53 @@ class OperationsRepository implements OperationsRepositoryInterface return $avg; } + /** + * This method collects various info on budgets, used on the budget page and on the index. + * + * @param Collection $budgets + * @param Carbon $start + * @param Carbon $end + * + * @return array + * @deprecated + * + */ + public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array + { + // get account information + /** @var AccountRepositoryInterface $accountRepository */ + $accountRepository = app(AccountRepositoryInterface::class); + $accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); + $defaultCurrency = app('amount')->getDefaultCurrency(); + $return = []; + /** @var Budget $budget */ + foreach ($budgets as $budget) { + $budgetId = $budget->id; + $return[$budgetId] = [ + 'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end), + 'budgeted' => '0', + ]; + $budgetLimits = $this->getBudgetLimits($budget, $start, $end); + $otherLimits = new Collection; + + // get all the budget limits relevant between start and end and examine them: + /** @var BudgetLimit $limit */ + foreach ($budgetLimits as $limit) { + if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) + ) { + $return[$budgetId]['currentLimit'] = $limit; + $return[$budgetId]['budgeted'] = round($limit->amount, $defaultCurrency->decimal_places); + continue; + } + // otherwise it's just one of the many relevant repetitions: + $otherLimits->push($limit); + } + $return[$budgetId]['otherLimits'] = $otherLimits; + } + + return $return; + } + /** * @param User $user */ @@ -85,4 +139,46 @@ class OperationsRepository implements OperationsRepositoryInterface { $this->user = $user; } + + /** + * @param Collection $budgets + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + * @deprecated + */ + public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string + { + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + + $collector->setUser($this->user); + $collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation(); + + if ($accounts->count() > 0) { + $collector->setAccounts($accounts); + } + + return $collector->getSum(); + } + + /** + * For now, simply refer to whichever repository holds this function. + * TODO might be done better in the future. + * + * @param Budget $budget + * @param Carbon|null $start + * @param Carbon|null $end + * + * @return Collection + */ + private function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection + { + /** @var BudgetRepositoryInterface $repository */ + $repository = app(BudgetRepositoryInterface::class); + + return $repository->getBudgetLimits($budget, $start, $end); + } } \ No newline at end of file diff --git a/app/Repositories/Budget/OperationsRepositoryInterface.php b/app/Repositories/Budget/OperationsRepositoryInterface.php index 35dbcf7bc9..4a6d40075c 100644 --- a/app/Repositories/Budget/OperationsRepositoryInterface.php +++ b/app/Repositories/Budget/OperationsRepositoryInterface.php @@ -23,8 +23,10 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; use FireflyIII\Models\Budget; use FireflyIII\User; +use Illuminate\Support\Collection; /** * Interface OperationsRepositoryInterface @@ -46,4 +48,29 @@ interface OperationsRepositoryInterface * @deprecated */ public function budgetedPerDay(Budget $budget): string; + + /** + * This method collects various info on budgets, used on the budget page and on the index. + * + * @param Collection $budgets + * @param Carbon $start + * @param Carbon $end + * + * @return array + * @deprecated + */ + public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array; + + /** + * @param Collection $budgets + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + * @deprecated + */ + public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string; + + } \ No newline at end of file diff --git a/app/Support/Http/Controllers/AugumentData.php b/app/Support/Http/Controllers/AugumentData.php index 085f0d2f85..bc78e95987 100644 --- a/app/Support/Http/Controllers/AugumentData.php +++ b/app/Support/Http/Controllers/AugumentData.php @@ -32,6 +32,7 @@ use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; @@ -356,9 +357,12 @@ trait AugumentData /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class); + /** @var OperationsRepositoryInterface $opsRepository */ + $opsRepository = app(OperationsRepositoryInterface::class); + $return = []; if (0 === $limits->count()) { - $spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); + $spent = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); if (0 !== bccomp($spent, '0')) { $return[$budget->name]['spent'] = bcmul($spent, '-1'); $return[$budget->name]['left'] = 0; @@ -400,12 +404,15 @@ trait AugumentData /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class); + /** @var OperationsRepositoryInterface $opsRepository */ + $opsRepository = app(OperationsRepositoryInterface::class); + $return = []; $format = (string)trans('config.month_and_day'); $name = $budget->name; /** @var BudgetLimit $budgetLimit */ foreach ($limits as $budgetLimit) { - $expenses = $repository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date); + $expenses = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date); $expenses = app('steam')->positive($expenses); if ($limits->count() > 1) { @@ -447,6 +454,10 @@ trait AugumentData { /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class); + + /** @var OperationsRepositoryInterface $opsRepository */ + $opsRepository = app(OperationsRepositoryInterface::class); + // properties for cache $cache = new CacheProperties; $cache->addProperty($start); @@ -463,7 +474,7 @@ trait AugumentData /** @var BudgetLimit $entry */ foreach ($set as $entry) { - $entry->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date); + $entry->spent = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date); $limits->push($entry); } $cache->store($limits);