Move method to correct repository.

This commit is contained in:
James Cole
2019-08-30 08:09:39 +02:00
parent 7685e2007f
commit 09bc50dd4d
7 changed files with 101 additions and 114 deletions

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Helpers\Report;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -36,17 +37,18 @@ use Log;
*/ */
class BudgetReportHelper implements BudgetReportHelperInterface class BudgetReportHelper implements BudgetReportHelperInterface
{ {
/** @var BudgetLimitRepositoryInterface */
private $blRepository;
/** @var BudgetRepositoryInterface The budget repository interface. */ /** @var BudgetRepositoryInterface The budget repository interface. */
private $repository; private $repository;
/** /**
* BudgetReportHelper constructor. * 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')) { if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
@@ -82,7 +84,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
'rows' => [], 'rows' => [],
]; ];
// get multi currency expenses first: // 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); $expenses = $this->repository->spentInPeriodMc(new Collection([$budget]), $accounts, $start, $end);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($budget->user); $defaultCurrency = app('amount')->getDefaultCurrencyByUser($budget->user);
Log::debug(sprintf('Default currency for getBudgetReport is %s', $defaultCurrency->code)); Log::debug(sprintf('Default currency for getBudgetReport is %s', $defaultCurrency->code));

View File

@@ -30,6 +30,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
@@ -51,6 +52,8 @@ class BudgetController extends Controller
protected $opsRepository; protected $opsRepository;
/** @var BudgetRepositoryInterface The budget repository */ /** @var BudgetRepositoryInterface The budget repository */
protected $repository; protected $repository;
/** @var BudgetLimitRepositoryInterface */
private $blRepository;
/** /**
* BudgetController constructor. * BudgetController constructor.
@@ -66,6 +69,7 @@ class BudgetController extends Controller
$this->generator = app(GeneratorInterface::class); $this->generator = app(GeneratorInterface::class);
$this->repository = app(BudgetRepositoryInterface::class); $this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
return $next($request); return $next($request);
} }
@@ -351,7 +355,7 @@ class BudgetController extends Controller
/** @var Budget $budget */ /** @var Budget $budget */
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
// get relevant repetitions: // get relevant repetitions:
$limits = $this->repository->getBudgetLimits($budget, $start, $end); $limits = $this->blRepository->getBudgetLimits($budget, $start, $end);
$expenses = $this->getExpensesForBudget($limits, $budget, $start, $end); $expenses = $this->getExpensesForBudget($limits, $budget, $start, $end);
foreach ($expenses as $name => $row) { foreach ($expenses as $name => $row) {

View File

@@ -61,11 +61,6 @@ interface BudgetLimitRepositoryInterface
*/ */
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection; 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 Budget $budget
* @param Carbon $start * @param Carbon $start
@@ -75,4 +70,9 @@ interface BudgetLimitRepositoryInterface
*/ */
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection; public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection;
/**
* @param User $user
*/
public function setUser(User $user): void;
} }

View File

@@ -36,7 +36,6 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
use Navigation; use Navigation;
@@ -207,7 +206,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set; return $set;
} }
/** /**
* This method is being used to generate the budget overview in the year/multi-year report. Its used * 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. * in both the year/multi-year budget overview AND in the accompanying chart.

View File

@@ -88,15 +88,6 @@ interface BudgetRepositoryInterface
*/ */
public function getActiveBudgets(): Collection; 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 $budgets
* @param Collection $accounts * @param Collection $accounts

View File

@@ -176,9 +176,9 @@ class OperationsRepository implements OperationsRepositoryInterface
*/ */
private function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection private function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
{ {
/** @var BudgetRepositoryInterface $repository */ /** @var BudgetLimitRepositoryInterface $blRepository */
$repository = app(BudgetRepositoryInterface::class); $blRepository = app(BudgetLimitRepositoryInterface::class);
return $repository->getBudgetLimits($budget, $start, $end); return $blRepository->getBudgetLimits($budget, $start, $end);
} }
} }

View File

@@ -31,6 +31,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
@@ -295,8 +296,8 @@ trait AugumentData
*/ */
protected function getBudgetedInPeriod(Budget $budget, Carbon $start, Carbon $end): array // get data + augment with info protected function getBudgetedInPeriod(Budget $budget, Carbon $start, Carbon $end): array // get data + augment with info
{ {
/** @var BudgetRepositoryInterface $repository */ /** @var BudgetLimitRepositoryInterface $blRepository */
$repository = app(BudgetRepositoryInterface::class); $blRepository = app(BudgetLimitRepositoryInterface::class);
$key = app('navigation')->preferredCarbonFormat($start, $end); $key = app('navigation')->preferredCarbonFormat($start, $end);
$range = app('navigation')->preferredRangeFormat($start, $end); $range = app('navigation')->preferredRangeFormat($start, $end);
@@ -307,7 +308,7 @@ trait AugumentData
$currentStart = app('navigation')->startOfPeriod($current, $range); $currentStart = app('navigation')->startOfPeriod($current, $range);
/** @var Carbon $currentEnd */ /** @var Carbon $currentEnd */
$currentEnd = app('navigation')->endOfPeriod($current, $range); $currentEnd = app('navigation')->endOfPeriod($current, $range);
$budgetLimits = $repository->getBudgetLimits($budget, $currentStart, $currentEnd); $budgetLimits = $blRepository->getBudgetLimits($budget, $currentStart, $currentEnd);
$index = $currentStart->format($key); $index = $currentStart->format($key);
$budgeted[$index] = $budgetLimits->sum('amount'); $budgeted[$index] = $budgetLimits->sum('amount');
$currentEnd->addDay(); $currentEnd->addDay();
@@ -383,64 +384,6 @@ trait AugumentData
return $return; 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. * 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 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 */ /** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class); $opsRepository = app(OperationsRepositoryInterface::class);
/** @var BudgetLimitRepositoryInterface $blRepository */
$blRepository = app(BudgetLimitRepositoryInterface::class);
// properties for cache // properties for cache
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
@@ -469,7 +412,7 @@ trait AugumentData
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
$set = $repository->getBudgetLimits($budget, $start, $end); $set = $blRepository->getBudgetLimits($budget, $start, $end);
$limits = new Collection(); $limits = new Collection();
/** @var BudgetLimit $entry */ /** @var BudgetLimit $entry */
@@ -553,7 +496,6 @@ trait AugumentData
return $grouped; return $grouped;
} }
/** /**
* Group transactions by tag. * Group transactions by tag.
* *
@@ -581,8 +523,6 @@ trait AugumentData
return $grouped; return $grouped;
} }
/** /**
* Spent by budget. * Spent by budget.
* *
@@ -640,8 +580,6 @@ trait AugumentData
return $sum; return $sum;
} }
/** /**
* Spent by category. * Spent by category.
* *
@@ -699,8 +637,6 @@ trait AugumentData
return $sum; return $sum;
} }
/** /**
* Spent in a period. * Spent in a period.
* *
@@ -747,7 +683,63 @@ trait AugumentData
return $sum; 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: * Returns an array with the following values: