diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php index 9b408c8917..6edbb5c093 100644 --- a/app/Api/V1/Controllers/BudgetLimitController.php +++ b/app/Api/V1/Controllers/BudgetLimitController.php @@ -114,7 +114,7 @@ class BudgetLimitController extends Controller $collection = new Collection; if (null === $budget) { - $collection = $this->repository->getAllBudgetLimits($this->parameters->get('start'), $this->parameters->get('end')); + $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')); diff --git a/app/Api/V1/Controllers/CurrencyController.php b/app/Api/V1/Controllers/CurrencyController.php index 5e7f61b8a1..2e0a2a0a45 100644 --- a/app/Api/V1/Controllers/CurrencyController.php +++ b/app/Api/V1/Controllers/CurrencyController.php @@ -36,6 +36,7 @@ use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; @@ -56,7 +57,6 @@ use FireflyIII\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Support\Collection; use League\Fractal\Manager; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Resource\Collection as FractalCollection; @@ -77,6 +77,7 @@ class CurrencyController extends Controller /** * CurrencyRepository constructor. + * * @codeCoverageIgnore */ public function __construct() @@ -100,7 +101,7 @@ class CurrencyController extends Controller /** * Display a list of accounts. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -158,7 +159,7 @@ class CurrencyController extends Controller /** * Display a listing of the resource. * - * @param Request $request + * @param Request $request * * @param TransactionCurrency $currency * @@ -207,7 +208,7 @@ class CurrencyController extends Controller /** * List all bills * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -222,7 +223,7 @@ class CurrencyController extends Controller /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; - $unfiltered = $repository->getBills(); + $unfiltered = $repository->getBills(); // filter and paginate list: $collection = $unfiltered->filter( @@ -253,7 +254,7 @@ class CurrencyController extends Controller /** * List all budget limits * - * @param Request $request + * @param Request $request * * @param TransactionCurrency $currency * @@ -262,12 +263,13 @@ class CurrencyController extends Controller */ public function budgetLimits(Request $request, TransactionCurrency $currency): JsonResponse { - /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); + /** @var BudgetLimitRepositoryInterface $blRepository */ + $blRepository = app(BudgetLimitRepositoryInterface::class); + $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; - $collection = $repository->getAllBudgetLimitsByCurrency($currency, $this->parameters->get('start'), $this->parameters->get('end')); + $collection = $blRepository->getAllBudgetLimitsByCurrency($currency, $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')); @@ -288,7 +290,7 @@ class CurrencyController extends Controller /** * Show a list of known exchange rates * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -350,7 +352,7 @@ class CurrencyController extends Controller /** * Disable a currency. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -383,7 +385,7 @@ class CurrencyController extends Controller /** * Enable a currency. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -447,7 +449,7 @@ class CurrencyController extends Controller /** * Make the currency a default currency. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -479,7 +481,7 @@ class CurrencyController extends Controller /** * List all recurring transactions. * - * @param Request $request + * @param Request $request * * @param TransactionCurrency $currency * @@ -539,7 +541,7 @@ class CurrencyController extends Controller /** * List all of them. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -593,7 +595,7 @@ class CurrencyController extends Controller /** * Show a currency. * - * @param Request $request + * @param Request $request * @param TransactionCurrency $currency * * @return JsonResponse @@ -654,7 +656,7 @@ class CurrencyController extends Controller /** * Show all transactions. * - * @param Request $request + * @param Request $request * * @param TransactionCurrency $currency * @@ -712,7 +714,7 @@ class CurrencyController extends Controller /** * Update a currency. * - * @param CurrencyRequest $request + * @param CurrencyRequest $request * @param TransactionCurrency $currency * * @return JsonResponse diff --git a/app/Http/Controllers/Chart/BudgetReportController.php b/app/Http/Controllers/Chart/BudgetReportController.php index 641a98a138..1f3a2537f4 100644 --- a/app/Http/Controllers/Chart/BudgetReportController.php +++ b/app/Http/Controllers/Chart/BudgetReportController.php @@ -27,6 +27,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Helpers\Chart\MetaPieChartInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Budget; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Http\Controllers\AugumentData; @@ -43,6 +44,8 @@ use Illuminate\Support\Collection; class BudgetReportController extends Controller { use AugumentData, TransactionCalculation; + /** @var BudgetLimitRepositoryInterface */ + private $blRepository; /** @var BudgetRepositoryInterface The budget repository */ private $budgetRepository; /** @var GeneratorInterface Chart generation methods. */ @@ -50,6 +53,7 @@ class BudgetReportController extends Controller /** * BudgetReportController constructor. + * * @codeCoverageIgnore */ public function __construct() @@ -59,6 +63,7 @@ class BudgetReportController extends Controller function ($request, $next) { $this->generator = app(GeneratorInterface::class); $this->budgetRepository = app(BudgetRepositoryInterface::class); + $this->blRepository = app(BudgetLimitRepositoryInterface::class); return $next($request); } @@ -176,7 +181,7 @@ class BudgetReportController extends Controller 'entries' => [], ]; } - $allBudgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end); + $allBudgetLimits = $this->blRepository->getAllBudgetLimits($start, $end); $sumOfExpenses = []; $leftOfLimits = []; while ($currentStart < $end) { diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php index 01ab99ca3c..1fa232db85 100644 --- a/app/Repositories/Budget/BudgetLimitRepository.php +++ b/app/Repositories/Budget/BudgetLimitRepository.php @@ -24,9 +24,13 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; use Exception; use FireflyIII\Models\BudgetLimit; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Collection; use Log; /** @@ -63,6 +67,96 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface } } + /** + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + * + */ + public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection + { + // both are NULL: + if (null === $start && null === $end) { + $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') + ->with(['budget']) + ->where('budgets.user_id', $this->user->id) + ->whereNull('budgets.deleted_at') + ->get(['budget_limits.*']); + + return $set; + } + // one of the two is NULL. + if (null === $start xor null === $end) { + $query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') + ->with(['budget']) + ->whereNull('budgets.deleted_at') + ->where('budgets.user_id', $this->user->id); + 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; + } + // neither are NULL: + $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') + ->with(['budget']) + ->where('budgets.user_id', $this->user->id) + ->whereNull('budgets.deleted_at') + ->where( + static function (Builder $q5) use ($start, $end) { + $q5->where( + static function (Builder $q1) use ($start, $end) { + $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')); + } + ) + ->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')); + } + ); + } + )->get(['budget_limits.*']); + + return $set; + } + + + /** + * @param TransactionCurrency $currency + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection + { + return $this->getAllBudgetLimits($start, $end)->filter( + static function (BudgetLimit $budgetLimit) use ($currency) { + return $budgetLimit->transaction_currency_id === $currency->id; + } + ); + } + /** * @param User $user */ diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php index 88a90122ef..d7308d702d 100644 --- a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php @@ -23,8 +23,11 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; use FireflyIII\Models\BudgetLimit; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; +use Illuminate\Support\Collection; /** * Interface BudgetLimitRepositoryInterface @@ -38,6 +41,25 @@ interface BudgetLimitRepositoryInterface */ public function destroyBudgetLimit(BudgetLimit $budgetLimit): void; + /** + * TODO this method is not multi-currency aware. + * + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection; + + /** + * @param TransactionCurrency $currency + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection; + /** * @param User $user */ diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 45ef4bcaf5..f9a27ece6a 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -27,7 +27,6 @@ use Exception; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Helpers\Collector\GroupCollectorInterface; -use FireflyIII\Models\AccountType; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; @@ -35,7 +34,6 @@ use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\User; use Illuminate\Database\Eloquent\Builder; @@ -209,95 +207,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - /** - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - * - */ - public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection - { - // both are NULL: - if (null === $start && null === $end) { - $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') - ->with(['budget']) - ->where('budgets.user_id', $this->user->id) - ->whereNull('budgets.deleted_at') - ->get(['budget_limits.*']); - - return $set; - } - // one of the two is NULL. - if (null === $start xor null === $end) { - $query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') - ->with(['budget']) - ->whereNull('budgets.deleted_at') - ->where('budgets.user_id', $this->user->id); - 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; - } - // neither are NULL: - $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') - ->with(['budget']) - ->where('budgets.user_id', $this->user->id) - ->whereNull('budgets.deleted_at') - ->where( - function (Builder $q5) use ($start, $end) { - $q5->where( - function (Builder $q1) use ($start, $end) { - $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')); - } - ) - ->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')); - } - ); - } - )->get(['budget_limits.*']); - - return $set; - } - - /** - * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection - { - return $this->getAllBudgetLimits($start, $end)->filter( - function (BudgetLimit $budgetLimit) use ($currency) { - return $budgetLimit->transaction_currency_id === $currency->id; - } - ); - } - /** * @param TransactionCurrency $currency * @param Carbon $start diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 6487888765..6eebf69039 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -88,23 +88,6 @@ interface BudgetRepositoryInterface */ public function getActiveBudgets(): Collection; - /** - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection; - - /** - * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection; - /** * @param TransactionCurrency $currency * @param Carbon $start