From 09bc50dd4d7d8010d9e06c7437e91790b6e971ee Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 30 Aug 2019 08:09:39 +0200 Subject: [PATCH] Move method to correct repository. --- app/Helpers/Report/BudgetReportHelper.php | 12 +- .../Controllers/Chart/BudgetController.php | 6 +- .../Budget/BudgetLimitRepositoryInterface.php | 10 +- app/Repositories/Budget/BudgetRepository.php | 2 - .../Budget/BudgetRepositoryInterface.php | 9 - .../Budget/OperationsRepository.php | 6 +- app/Support/Http/Controllers/AugumentData.php | 170 +++++++++--------- 7 files changed, 101 insertions(+), 114 deletions(-) diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index d01e6b1003..dd85ad8a74 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -25,6 +25,7 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Support\Collection; use Log; @@ -36,17 +37,18 @@ use Log; */ class BudgetReportHelper implements BudgetReportHelperInterface { + /** @var BudgetLimitRepositoryInterface */ + private $blRepository; /** @var BudgetRepositoryInterface The budget repository interface. */ private $repository; /** * BudgetReportHelper constructor. - * - * @param BudgetRepositoryInterface $repository */ - public function __construct(BudgetRepositoryInterface $repository) + public function __construct() { - $this->repository = $repository; + $this->repository = app(BudgetRepositoryInterface::class); + $this->blRepository = app(BudgetLimitRepositoryInterface::class); if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); @@ -82,7 +84,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface 'rows' => [], ]; // get multi currency expenses first: - $budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end); + $budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end); $expenses = $this->repository->spentInPeriodMc(new Collection([$budget]), $accounts, $start, $end); $defaultCurrency = app('amount')->getDefaultCurrencyByUser($budget->user); Log::debug(sprintf('Default currency for getBudgetReport is %s', $defaultCurrency->code)); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 8db63d7a9f..7000f133bd 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -30,6 +30,7 @@ use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\TransactionType; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\CacheProperties; @@ -51,6 +52,8 @@ class BudgetController extends Controller protected $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ protected $repository; + /** @var BudgetLimitRepositoryInterface */ + private $blRepository; /** * BudgetController constructor. @@ -66,6 +69,7 @@ class BudgetController extends Controller $this->generator = app(GeneratorInterface::class); $this->repository = app(BudgetRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); + $this->blRepository = app(BudgetLimitRepositoryInterface::class); return $next($request); } @@ -351,7 +355,7 @@ class BudgetController extends Controller /** @var Budget $budget */ foreach ($budgets as $budget) { // get relevant repetitions: - $limits = $this->repository->getBudgetLimits($budget, $start, $end); + $limits = $this->blRepository->getBudgetLimits($budget, $start, $end); $expenses = $this->getExpensesForBudget($limits, $budget, $start, $end); foreach ($expenses as $name => $row) { diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php index d8f2c6c747..92cdd67e79 100644 --- a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php @@ -61,11 +61,6 @@ interface BudgetLimitRepositoryInterface */ public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection; - /** - * @param User $user - */ - public function setUser(User $user): void; - /** * @param Budget $budget * @param Carbon $start @@ -75,4 +70,9 @@ interface BudgetLimitRepositoryInterface */ public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection; + /** + * @param User $user + */ + public function setUser(User $user): void; + } \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 71eddaf57b..5828d259e4 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -36,7 +36,6 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\User; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Log; use Navigation; @@ -207,7 +206,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - /** * This method is being used to generate the budget overview in the year/multi-year report. Its used * in both the year/multi-year budget overview AND in the accompanying chart. diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 21aa495982..dc23f42d0e 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -88,15 +88,6 @@ interface BudgetRepositoryInterface */ public function getActiveBudgets(): Collection; - /** - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection; - /** * @param Collection $budgets * @param Collection $accounts diff --git a/app/Repositories/Budget/OperationsRepository.php b/app/Repositories/Budget/OperationsRepository.php index b07f9d64f4..639f38e2ee 100644 --- a/app/Repositories/Budget/OperationsRepository.php +++ b/app/Repositories/Budget/OperationsRepository.php @@ -176,9 +176,9 @@ class OperationsRepository implements OperationsRepositoryInterface */ private function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection { - /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); + /** @var BudgetLimitRepositoryInterface $blRepository */ + $blRepository = app(BudgetLimitRepositoryInterface::class); - return $repository->getBudgetLimits($budget, $start, $end); + return $blRepository->getBudgetLimits($budget, $start, $end); } } \ No newline at end of file diff --git a/app/Support/Http/Controllers/AugumentData.php b/app/Support/Http/Controllers/AugumentData.php index bc78e95987..9cf29a83fd 100644 --- a/app/Support/Http/Controllers/AugumentData.php +++ b/app/Support/Http/Controllers/AugumentData.php @@ -31,6 +31,7 @@ use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; @@ -75,8 +76,8 @@ trait AugumentData * * @param Collection $assets * @param Collection $opposing - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array * @@ -133,8 +134,8 @@ trait AugumentData * * @param Collection $assets * @param Collection $opposing - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -212,9 +213,9 @@ trait AugumentData * Returns the budget limits belonging to the given budget and valid on the given day. * * @param Collection $budgetLimits - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end * * @return Collection */ @@ -295,8 +296,8 @@ trait AugumentData */ protected function getBudgetedInPeriod(Budget $budget, Carbon $start, Carbon $end): array // get data + augment with info { - /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); + /** @var BudgetLimitRepositoryInterface $blRepository */ + $blRepository = app(BudgetLimitRepositoryInterface::class); $key = app('navigation')->preferredCarbonFormat($start, $end); $range = app('navigation')->preferredRangeFormat($start, $end); @@ -307,7 +308,7 @@ trait AugumentData $currentStart = app('navigation')->startOfPeriod($current, $range); /** @var Carbon $currentEnd */ $currentEnd = app('navigation')->endOfPeriod($current, $range); - $budgetLimits = $repository->getBudgetLimits($budget, $currentStart, $currentEnd); + $budgetLimits = $blRepository->getBudgetLimits($budget, $currentStart, $currentEnd); $index = $currentStart->format($key); $budgeted[$index] = $budgetLimits->sum('amount'); $currentEnd->addDay(); @@ -345,9 +346,9 @@ trait AugumentData * Get the expenses for a budget in a date range. * * @param Collection $limits - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end * * @return array * @@ -383,64 +384,6 @@ trait AugumentData return $return; } - /** - * - * Returns an array with the following values: - * 0 => - * 'name' => name of budget + repetition - * 'left' => left in budget repetition (always zero) - * 'overspent' => spent more than budget repetition? (always zero) - * 'spent' => actually spent in period for budget - * 1 => (etc) - * - * @param Budget $budget - * @param Collection $limits - * - * @return array - * - */ - protected function spentInPeriodMulti(Budget $budget, Collection $limits): array // get data + augment with info - { - /** @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 = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date); - $expenses = app('steam')->positive($expenses); - - if ($limits->count() > 1) { - $name = $budget->name . ' ' . trans( - 'firefly.between_dates', - [ - 'start' => $budgetLimit->start_date->formatLocalized($format), - 'end' => $budgetLimit->end_date->formatLocalized($format), - ] - ); - } - $amount = $budgetLimit->amount; - $leftInLimit = bcsub($amount, $expenses); - $hasOverspent = bccomp($leftInLimit, '0') === -1; - $left = $hasOverspent ? '0' : bcsub($amount, $expenses); - $spent = $hasOverspent ? $amount : $expenses; - $overspent = $hasOverspent ? app('steam')->positive($leftInLimit) : '0'; - - $return[$name] = [ - 'left' => $left, - 'overspent' => $overspent, - 'spent' => $spent, - ]; - } - - return $return; - } - /** * Gets all budget limits for a budget. * @@ -452,12 +395,12 @@ trait AugumentData */ protected function getLimits(Budget $budget, Carbon $start, Carbon $end): Collection // get data + augment with info { - /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); - /** @var OperationsRepositoryInterface $opsRepository */ $opsRepository = app(OperationsRepositoryInterface::class); + /** @var BudgetLimitRepositoryInterface $blRepository */ + $blRepository = app(BudgetLimitRepositoryInterface::class); + // properties for cache $cache = new CacheProperties; $cache->addProperty($start); @@ -469,7 +412,7 @@ trait AugumentData return $cache->get(); // @codeCoverageIgnore } - $set = $repository->getBudgetLimits($budget, $start, $end); + $set = $blRepository->getBudgetLimits($budget, $start, $end); $limits = new Collection(); /** @var BudgetLimit $entry */ @@ -553,7 +496,6 @@ trait AugumentData return $grouped; } - /** * Group transactions by tag. * @@ -569,7 +511,7 @@ trait AugumentData foreach ($array as $journal) { $tags = $journal['tags'] ?? []; /** - * @var int $id + * @var int $id * @var array $tag */ foreach ($tags as $id => $tag) { @@ -581,15 +523,13 @@ trait AugumentData return $grouped; } - - /** * Spent by budget. * * @param Collection $assets * @param Collection $opposing - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array * @@ -640,15 +580,13 @@ trait AugumentData return $sum; } - - /** * Spent by category. * * @param Collection $assets * @param Collection $opposing - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array * @@ -699,15 +637,13 @@ trait AugumentData return $sum; } - - /** * Spent in a period. * * @param Collection $assets * @param Collection $opposing - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -747,7 +683,63 @@ trait AugumentData return $sum; } + /** + * + * Returns an array with the following values: + * 0 => + * 'name' => name of budget + repetition + * 'left' => left in budget repetition (always zero) + * 'overspent' => spent more than budget repetition? (always zero) + * 'spent' => actually spent in period for budget + * 1 => (etc) + * + * @param Budget $budget + * @param Collection $limits + * + * @return array + * + */ + protected function spentInPeriodMulti(Budget $budget, Collection $limits): array // get data + augment with info + { + /** @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 = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date); + $expenses = app('steam')->positive($expenses); + + if ($limits->count() > 1) { + $name = $budget->name . ' ' . trans( + 'firefly.between_dates', + [ + 'start' => $budgetLimit->start_date->formatLocalized($format), + 'end' => $budgetLimit->end_date->formatLocalized($format), + ] + ); + } + $amount = $budgetLimit->amount; + $leftInLimit = bcsub($amount, $expenses); + $hasOverspent = bccomp($leftInLimit, '0') === -1; + $left = $hasOverspent ? '0' : bcsub($amount, $expenses); + $spent = $hasOverspent ? $amount : $expenses; + $overspent = $hasOverspent ? app('steam')->positive($leftInLimit) : '0'; + + $return[$name] = [ + 'left' => $left, + 'overspent' => $overspent, + 'spent' => $spent, + ]; + } + + return $return; + } /** * Returns an array with the following values: