Skip to content

Firefly III: a personal finances manager

Move method to correct repository.

James Cole134d1b274664
diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php
index ac25f37caf..25f981f051 100644
--- a/app/Helpers/Report/BudgetReportHelper.php
+++ b/app/Helpers/Report/BudgetReportHelper.php
@@ -27,6 +27,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -40,6 +41,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
{
/** @var BudgetLimitRepositoryInterface */
private $blRepository;
+ /** @var NoBudgetRepositoryInterface */
+ private $noBudgetRepository;
/** @var OperationsRepositoryInterface */
private $opsRepository;
/** @var BudgetRepositoryInterface The budget repository interface. */
@@ -50,9 +53,10 @@ class BudgetReportHelper implements BudgetReportHelperInterface
*/
public function __construct()
{
- $this->repository = app(BudgetRepositoryInterface::class);
- $this->blRepository = app(BudgetLimitRepositoryInterface::class);
- $this->opsRepository = app(OperationsRepositoryInterface::class);
+ $this->repository = app(BudgetRepositoryInterface::class);
+ $this->blRepository = app(BudgetLimitRepositoryInterface::class);
+ $this->opsRepository = app(OperationsRepositoryInterface::class);
+ $this->noBudgetRepository = app(NoBudgetRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
@@ -148,7 +152,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
}
$array['budgets'][] = $entry;
}
- $noBudget = $this->repository->spentInPeriodWoBudgetMc($accounts, $start, $end);
+ $noBudget = $this->noBudgetRepository->spentInPeriodWoBudgetMc($accounts, $start, $end);
$noBudgetEntry = [
'budget_id' => null,
'budget_name' => null,
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index febb7ce2b2..0265d6b8fa 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -276,79 +276,6 @@ class BudgetRepository implements BudgetRepositoryInterface
$this->user = $user;
}
- /**
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return string
- */
- public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string
- {
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- $collector->setUser($this->user);
- $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])
- ->withoutBudget();
-
- if ($accounts->count() > 0) {
- $collector->setAccounts($accounts);
- }
-
- return $collector->getSum();
- }
-
- /**
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return array
- */
- public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array
- {
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
-
- $collector->setUser($this->user);
- $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
-
- if ($accounts->count() > 0) {
- $collector->setAccounts($accounts);
- }
- $journals = $collector->getExtractedJournals();
- $return = [];
- $total = [];
- $currencies = [];
- /** @var array $journal */
- foreach ($journals as $journal) {
- $code = $journal['currency_code'];
- if (!isset($currencies[$code])) {
- $currencies[$code] = [
- 'id' => $journal['currency_id'],
- 'name' => $journal['currency_name'],
- 'symbol' => $journal['currency_symbol'],
- 'decimal_places' => $journal['currency_decimal_places'],
- ];
- }
- $total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
- }
- foreach ($total as $code => $spent) {
- /** @var TransactionCurrency $currency */
- $currency = $currencies[$code];
- $return[] = [
- 'currency_id' => $currency['id'],
- 'currency_code' => $code,
- 'currency_name' => $currency['name'],
- 'currency_symbol' => $currency['symbol'],
- 'currency_decimal_places' => $currency['decimal_places'],
- 'amount' => $spent,
- ];
- }
-
- return $return;
- }
-
/**
* @param array $data
*
diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php
index 1d5334ecfa..2264ffd8e3 100644
--- a/app/Repositories/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php
@@ -125,15 +125,6 @@ interface BudgetRepositoryInterface
*/
public function setUser(User $user);
- /**
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return string
- */
- public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string;
-
/**
* @param Collection $accounts
* @param Carbon $start
diff --git a/app/Repositories/Budget/NoBudgetRepository.php b/app/Repositories/Budget/NoBudgetRepository.php
index 5dff50a5e2..64cfea57b4 100644
--- a/app/Repositories/Budget/NoBudgetRepository.php
+++ b/app/Repositories/Budget/NoBudgetRepository.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
+use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
@@ -51,6 +52,58 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
}
}
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array
+ {
+ /** @var GroupCollectorInterface $collector */
+ $collector = app(GroupCollectorInterface::class);
+
+ $collector->setUser($this->user);
+ $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
+
+ if ($accounts->count() > 0) {
+ $collector->setAccounts($accounts);
+ }
+ $journals = $collector->getExtractedJournals();
+ $return = [];
+ $total = [];
+ $currencies = [];
+ /** @var array $journal */
+ foreach ($journals as $journal) {
+ $code = $journal['currency_code'];
+ if (!isset($currencies[$code])) {
+ $currencies[$code] = [
+ 'id' => $journal['currency_id'],
+ 'name' => $journal['currency_name'],
+ 'symbol' => $journal['currency_symbol'],
+ 'decimal_places' => $journal['currency_decimal_places'],
+ ];
+ }
+ $total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
+ }
+ foreach ($total as $code => $spent) {
+ /** @var TransactionCurrency $currency */
+ $currency = $currencies[$code];
+ $return[] = [
+ 'currency_id' => $currency['id'],
+ 'currency_code' => $code,
+ 'currency_name' => $currency['name'],
+ 'currency_symbol' => $currency['symbol'],
+ 'currency_decimal_places' => $currency['decimal_places'],
+ 'amount' => $spent,
+ ];
+ }
+
+ return $return;
+ }
+
/**
* @param Collection $accounts
* @param Carbon $start
diff --git a/app/Repositories/Budget/NoBudgetRepositoryInterface.php b/app/Repositories/Budget/NoBudgetRepositoryInterface.php
index ed83ae10ac..39a07c1b35 100644
--- a/app/Repositories/Budget/NoBudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/NoBudgetRepositoryInterface.php
@@ -47,4 +47,14 @@ interface NoBudgetRepositoryInterface
* @deprecated
*/
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ * @deprecated
+ */
+ public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array;
}
\ No newline at end of file
diff --git a/app/Transformers/AvailableBudgetTransformer.php b/app/Transformers/AvailableBudgetTransformer.php
index 5bd93f1f48..ca3ccd2f1d 100644
--- a/app/Transformers/AvailableBudgetTransformer.php
+++ b/app/Transformers/AvailableBudgetTransformer.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -35,6 +36,8 @@ use Log;
*/
class AvailableBudgetTransformer extends AbstractTransformer
{
+ /** @var NoBudgetRepositoryInterface */
+ private $noBudgetRepository;
/** @var OperationsRepositoryInterface */
private $opsRepository;
/** @var BudgetRepositoryInterface */
@@ -47,8 +50,9 @@ class AvailableBudgetTransformer extends AbstractTransformer
*/
public function __construct()
{
- $this->repository = app(BudgetRepositoryInterface::class);
- $this->opsRepository = app(OperationsRepositoryInterface::class);
+ $this->repository = app(BudgetRepositoryInterface::class);
+ $this->opsRepository = app(OperationsRepositoryInterface::class);
+ $this->noBudgetRepository = app(NoBudgetRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
@@ -114,7 +118,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
*/
private function spentOutsideBudgets(): array
{
- return $this->repository->spentInPeriodWoBudgetMc(new Collection, $this->parameters->get('start'), $this->parameters->get('end'));
+ return $this->noBudgetRepository->spentInPeriodWoBudgetMc(new Collection, $this->parameters->get('start'), $this->parameters->get('end'));
}
}