diff --git a/app/Api/V1/Controllers/SummaryController.php b/app/Api/V1/Controllers/SummaryController.php index 1fcb4fa713..2cdc8aa86f 100644 --- a/app/Api/V1/Controllers/SummaryController.php +++ b/app/Api/V1/Controllers/SummaryController.php @@ -36,6 +36,7 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\User; @@ -47,6 +48,8 @@ use Illuminate\Support\Collection; */ class SummaryController extends Controller { + /** @var AvailableBudgetRepositoryInterface */ + private $abRepository; /** @var AccountRepositoryInterface */ private $accountRepository; /** @var BillRepositoryInterface */ @@ -58,6 +61,7 @@ class SummaryController extends Controller /** * SummaryController constructor. + * * @codeCoverageIgnore */ public function __construct() @@ -71,11 +75,13 @@ class SummaryController extends Controller $this->billRepository = app(BillRepositoryInterface::class); $this->budgetRepository = app(BudgetRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class); + $this->abRepository = app(AvailableBudgetRepositoryInterface::class); $this->billRepository->setUser($user); $this->currencyRepos->setUser($user); $this->budgetRepository->setUser($user); $this->accountRepository->setUser($user); + $this->abRepository->setUser($user); return $next($request); @@ -140,6 +146,25 @@ class SummaryController extends Controller return $result; } + /** + * This method will scroll through the results of the spentInPeriodMc() array and return the correct info. + * + * @param array $spentInfo + * @param TransactionCurrency $currency + * + * @return float + */ + private function findInSpentArray(array $spentInfo, TransactionCurrency $currency): float + { + foreach ($spentInfo as $array) { + if ($array['currency_id'] === $currency->id) { + return $array['amount']; + } + } + + return 0.0; // @codeCoverageIgnore + } + /** * @param Carbon $start * @param Carbon $end @@ -316,7 +341,7 @@ class SummaryController extends Controller { $return = []; $today = new Carbon; - $available = $this->budgetRepository->getAvailableBudgetWithCurrency($start, $end); + $available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end); $budgets = $this->budgetRepository->getActiveBudgets(); $spentInfo = $this->budgetRepository->spentInPeriodMc($budgets, new Collection, $start, $end); foreach ($available as $currencyId => $amount) { @@ -350,25 +375,6 @@ class SummaryController extends Controller return $return; } - /** - * This method will scroll through the results of the spentInPeriodMc() array and return the correct info. - * - * @param array $spentInfo - * @param TransactionCurrency $currency - * - * @return float - */ - private function findInSpentArray(array $spentInfo, TransactionCurrency $currency): float - { - foreach ($spentInfo as $array) { - if ($array['currency_id'] === $currency->id) { - return $array['amount']; - } - } - - return 0.0; // @codeCoverageIgnore - } - /** * @param Carbon $start * @param Carbon $end diff --git a/app/Http/Controllers/Budget/AmountController.php b/app/Http/Controllers/Budget/AmountController.php index 25245a6393..174ec61e28 100644 --- a/app/Http/Controllers/Budget/AmountController.php +++ b/app/Http/Controllers/Budget/AmountController.php @@ -28,6 +28,7 @@ use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Models\Budget; +use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\Http\Controllers\DateCalculation; @@ -43,6 +44,8 @@ class AmountController extends Controller { use DateCalculation; + /** @var AvailableBudgetRepositoryInterface */ + private $abRepository; /** @var OperationsRepositoryInterface */ private $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ @@ -63,6 +66,7 @@ class AmountController extends Controller app('view')->share('mainTitleIcon', 'fa-tasks'); $this->repository = app(BudgetRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); + $this->abRepository = app(AvailableBudgetRepositoryInterface::class); return $next($request); } @@ -179,7 +183,7 @@ class AmountController extends Controller public function updateIncome(Request $request, Carbon $start, Carbon $end) { $defaultCurrency = app('amount')->getDefaultCurrency(); - $available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end); + $available = $this->abRepository->getAvailableBudget($defaultCurrency, $start, $end); $available = round($available, $defaultCurrency->decimal_places); $page = (int)$request->get('page'); diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.php index 8b34f2743d..c536e3109a 100644 --- a/app/Http/Controllers/Budget/IndexController.php +++ b/app/Http/Controllers/Budget/IndexController.php @@ -26,6 +26,7 @@ namespace FireflyIII\Http\Controllers\Budget; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; +use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\Http\Controllers\DateCalculation; @@ -45,6 +46,8 @@ class IndexController extends Controller private $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ private $repository; + /** @var AvailableBudgetRepositoryInterface */ + private $abRepository; /** * IndexController constructor. @@ -63,6 +66,7 @@ class IndexController extends Controller app('view')->share('mainTitleIcon', 'fa-tasks'); $this->repository = app(BudgetRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); + $this->abRepository = app(AvailableBudgetRepositoryInterface::class); $this->repository->cleanupBudgets(); return $next($request); @@ -118,7 +122,7 @@ class IndexController extends Controller $budgetInformation = $this->opsRepository->collectBudgetInformation($collection, $start, $end); // to display available budget: - $available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end); + $available = $this->abRepository->getAvailableBudget($defaultCurrency, $start, $end); $spent = array_sum(array_column($budgetInformation, 'spent')); $budgeted = array_sum(array_column($budgetInformation, 'budgeted')); diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.php index c341d149bf..050b25f2e4 100644 --- a/app/Http/Controllers/Json/BoxController.php +++ b/app/Http/Controllers/Json/BoxController.php @@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; @@ -59,6 +60,9 @@ class BoxController extends Controller /** @var OperationsRepositoryInterface $opsRepository */ $opsRepository = app(OperationsRepositoryInterface::class); + /** @var AvailableBudgetRepositoryInterface $abRepository */ + $abRepository = app(AvailableBudgetRepositoryInterface::class); + /** @var Carbon $start */ $start = session('start', Carbon::now()->startOfMonth()); @@ -75,7 +79,7 @@ class BoxController extends Controller } // get available amount $currency = app('amount')->getDefaultCurrency(); - $available = $repository->getAvailableBudget($currency, $start, $end); + $available = $abRepository->getAvailableBudget($currency, $start, $end); // get spent amount: $budgets = $repository->getActiveBudgets(); diff --git a/app/Repositories/Budget/AvailableBudgetRepository.php b/app/Repositories/Budget/AvailableBudgetRepository.php index a0b3759614..e641c79080 100644 --- a/app/Repositories/Budget/AvailableBudgetRepository.php +++ b/app/Repositories/Budget/AvailableBudgetRepository.php @@ -23,8 +23,10 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; use Exception; use FireflyIII\Models\AvailableBudget; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; use Log; @@ -60,6 +62,48 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface } } + /** + * @param TransactionCurrency $currency + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string + { + $amount = '0'; + $availableBudget = $this->user->availableBudgets() + ->where('transaction_currency_id', $currency->id) + ->where('start_date', $start->format('Y-m-d 00:00:00')) + ->where('end_date', $end->format('Y-m-d 00:00:00'))->first(); + if (null !== $availableBudget) { + $amount = (string)$availableBudget->amount; + } + + return $amount; + } + + + /** + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array + { + $return = []; + $availableBudgets = $this->user->availableBudgets() + ->where('start_date', $start->format('Y-m-d 00:00:00')) + ->where('end_date', $end->format('Y-m-d 00:00:00'))->get(); + /** @var AvailableBudget $availableBudget */ + foreach ($availableBudgets as $availableBudget) { + $return[$availableBudget->transaction_currency_id] = $availableBudget->amount; + } + + return $return; + } + /** * @param User $user */ diff --git a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php index 81ab533bd2..db9d5f6f10 100644 --- a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php +++ b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; +use Carbon\Carbon; use FireflyIII\Models\AvailableBudget; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; /** @@ -36,8 +38,26 @@ interface AvailableBudgetRepositoryInterface */ public function destroyAvailableBudget(AvailableBudget $availableBudget): void; + /** + * @param TransactionCurrency $currency + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string; + + /** + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array; + /** * @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 f9a27ece6a..546fe0a463 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -207,48 +207,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - /** - * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string - { - $amount = '0'; - $availableBudget = $this->user->availableBudgets() - ->where('transaction_currency_id', $currency->id) - ->where('start_date', $start->format('Y-m-d 00:00:00')) - ->where('end_date', $end->format('Y-m-d 00:00:00'))->first(); - if (null !== $availableBudget) { - $amount = (string)$availableBudget->amount; - } - - return $amount; - } - - - /** - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array - { - $return = []; - $availableBudgets = $this->user->availableBudgets() - ->where('start_date', $start->format('Y-m-d 00:00:00')) - ->where('end_date', $end->format('Y-m-d 00:00:00'))->get(); - /** @var AvailableBudget $availableBudget */ - foreach ($availableBudgets as $availableBudget) { - $return[$availableBudget->transaction_currency_id] = $availableBudget->amount; - } - - return $return; - } - /** * Returns all available budget objects. * diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 6eebf69039..65cb6fd699 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -89,27 +89,6 @@ interface BudgetRepositoryInterface public function getActiveBudgets(): Collection; /** - * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string; - - /** - * TODO only used in API - * - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array; - - /** - * TODO only used in API - * * Returns all available budget objects. * * @param TransactionCurrency $currency diff --git a/app/Repositories/Budget/OperationsRepositoryInterface.php b/app/Repositories/Budget/OperationsRepositoryInterface.php index 4a6d40075c..f4e4fc2356 100644 --- a/app/Repositories/Budget/OperationsRepositoryInterface.php +++ b/app/Repositories/Budget/OperationsRepositoryInterface.php @@ -33,11 +33,6 @@ use Illuminate\Support\Collection; */ interface OperationsRepositoryInterface { - /** - * @param User $user - */ - public function setUser(User $user): void; - /** * A method that returns the amount of money budgeted per day for this budget, * on average. @@ -61,6 +56,11 @@ interface OperationsRepositoryInterface */ public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array; + /** + * @param User $user + */ + public function setUser(User $user): void; + /** * @param Collection $budgets * @param Collection $accounts