Skip to content

Firefly III: a personal finances manager

Move method to correct repository.

James Cole7685e2007f67
diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php
index eac5923d7a..ba96bcf6b8 100644
--- a/app/Api/V1/Controllers/BudgetController.php
+++ b/app/Api/V1/Controllers/BudgetController.php
@@ -29,6 +29,7 @@ use FireflyIII\Api\V1\Requests\BudgetRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Budget;
+use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\BudgetLimitTransformer;
@@ -51,6 +52,8 @@ use League\Fractal\Serializer\JsonApiSerializer;
class BudgetController extends Controller
{
use TransactionFilter;
+ /** @var BudgetLimitRepositoryInterface */
+ private $blRepository;
/** @var BudgetRepositoryInterface The budget repository */
private $repository;
@@ -67,9 +70,10 @@ class BudgetController extends Controller
/** @var User $admin */
$admin = auth()->user();
- /** @var BudgetRepositoryInterface repository */
- $this->repository = app(BudgetRepositoryInterface::class);
+ $this->repository = app(BudgetRepositoryInterface::class);
+ $this->blRepository = app(BudgetLimitRepositoryInterface::class);
$this->repository->setUser($admin);
+ $this->blRepository->setUser($admin);
return $next($request);
}
@@ -78,8 +82,9 @@ class BudgetController extends Controller
/**
* Display a listing of the resource.
+ *
* @param Request $request
- * @param Budget $budget
+ * @param Budget $budget
*
* @return JsonResponse
* @codeCoverageIgnore
@@ -90,7 +95,7 @@ class BudgetController extends Controller
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$this->parameters->set('budget_id', $budget->id);
- $collection = $this->repository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));
+ $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));
$count = $collection->count();
$budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
$paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page'));
@@ -167,7 +172,7 @@ class BudgetController extends Controller
* Show a budget.
*
* @param Request $request
- * @param Budget $budget
+ * @param Budget $budget
*
* @return JsonResponse
* @codeCoverageIgnore
@@ -219,7 +224,8 @@ class BudgetController extends Controller
* Store a newly created resource in storage.
*
* @param BudgetLimitRequest $request
- * @param Budget $budget
+ * @param Budget $budget
+ *
* @return JsonResponse
* @throws Exception
*/
@@ -246,7 +252,7 @@ class BudgetController extends Controller
*
* @param Request $request
*
- * @param Budget $budget
+ * @param Budget $budget
*
* @return JsonResponse
* @codeCoverageIgnore
@@ -311,7 +317,7 @@ class BudgetController extends Controller
* Update a budget.
*
* @param BudgetRequest $request
- * @param Budget $budget
+ * @param Budget $budget
*
* @return JsonResponse
*/
diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php
index 6edbb5c093..5c5142aaaa 100644
--- a/app/Api/V1/Controllers/BudgetLimitController.php
+++ b/app/Api/V1/Controllers/BudgetLimitController.php
@@ -117,7 +117,7 @@ class BudgetLimitController extends Controller
$collection = $this->blRepository->getAllBudgetLimits($this->parameters->get('start'), $this->parameters->get('end'));
}
if (null !== $budget) {
- $collection = $this->repository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));
+ $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));
}
$count = $collection->count();
diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php
index 1fa232db85..a9b3a4f4df 100644
--- a/app/Repositories/Budget/BudgetLimitRepository.php
+++ b/app/Repositories/Budget/BudgetLimitRepository.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use Exception;
+use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User;
@@ -157,6 +158,71 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
);
}
+
+ /**
+ * @param Budget $budget
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return Collection
+ *
+ */
+ public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
+ {
+ if (null === $end && null === $start) {
+ return $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
+ }
+ if (null === $end xor null === $start) {
+ $query = $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC');
+ // one of the two is null
+ if (null !== $end) {
+ // end date must be before $end.
+ $query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
+ }
+ if (null !== $start) {
+ // start date must be after $start.
+ $query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
+ }
+ $set = $query->get(['budget_limits.*']);
+
+ return $set;
+ }
+
+ // when both dates are set:
+ $set = $budget->budgetlimits()
+ ->where(
+ static function (Builder $q5) use ($start, $end) {
+ $q5->where(
+ static function (Builder $q1) use ($start, $end) {
+ // budget limit ends within period
+ $q1->where(
+ static function (Builder $q2) use ($start, $end) {
+ $q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
+ $q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
+ }
+ )
+ // budget limit start within period
+ ->orWhere(
+ static function (Builder $q3) use ($start, $end) {
+ $q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
+ $q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
+ }
+ );
+ }
+ )
+ ->orWhere(
+ static function (Builder $q4) use ($start, $end) {
+ // or start is before start AND end is after end.
+ $q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
+ $q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
+ }
+ );
+ }
+ )->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
+
+ return $set;
+ }
+
/**
* @param User $user
*/
diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php
index d7308d702d..d8f2c6c747 100644
--- a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
+use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User;
@@ -64,4 +65,14 @@ interface BudgetLimitRepositoryInterface
* @param User $user
*/
public function setUser(User $user): void;
+
+ /**
+ * @param Budget $budget
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return Collection
+ */
+ public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection;
+
}
\ No newline at end of file
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index a9b5ffbdd8..71eddaf57b 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -208,71 +208,6 @@ class BudgetRepository implements BudgetRepositoryInterface
}
-
- /**
- * @param Budget $budget
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return Collection
- *
- */
- public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
- {
- if (null === $end && null === $start) {
- return $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
- }
- if (null === $end xor null === $start) {
- $query = $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC');
- // one of the two is null
- if (null !== $end) {
- // end date must be before $end.
- $query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
- }
- if (null !== $start) {
- // start date must be after $start.
- $query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
- }
- $set = $query->get(['budget_limits.*']);
-
- return $set;
- }
-
- // when both dates are set:
- $set = $budget->budgetlimits()
- ->where(
- function (Builder $q5) use ($start, $end) {
- $q5->where(
- function (Builder $q1) use ($start, $end) {
- // budget limit ends within period
- $q1->where(
- function (Builder $q2) use ($start, $end) {
- $q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
- $q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
- }
- )
- // budget limit start within period
- ->orWhere(
- function (Builder $q3) use ($start, $end) {
- $q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
- $q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
- }
- );
- }
- )
- ->orWhere(
- function (Builder $q4) use ($start, $end) {
- // or start is before start AND end is after end.
- $q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
- $q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
- }
- );
- }
- )->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
-
- 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 4798e14179..21aa495982 100644
--- a/app/Repositories/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php
@@ -104,6 +104,7 @@ interface BudgetRepositoryInterface
* @param Carbon $end
*
* @return array
+ * @deprecated
*/
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array;