Move method to correct repository.

This commit is contained in:
James Cole
2019-08-30 09:13:10 +02:00
parent 5973b94677
commit 134d1b2746
6 changed files with 78 additions and 89 deletions

View File

@@ -27,6 +27,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -40,6 +41,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
{ {
/** @var BudgetLimitRepositoryInterface */ /** @var BudgetLimitRepositoryInterface */
private $blRepository; private $blRepository;
/** @var NoBudgetRepositoryInterface */
private $noBudgetRepository;
/** @var OperationsRepositoryInterface */ /** @var OperationsRepositoryInterface */
private $opsRepository; private $opsRepository;
/** @var BudgetRepositoryInterface The budget repository interface. */ /** @var BudgetRepositoryInterface The budget repository interface. */
@@ -53,6 +56,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
$this->repository = app(BudgetRepositoryInterface::class); $this->repository = app(BudgetRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class); $this->blRepository = app(BudgetLimitRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noBudgetRepository = app(NoBudgetRepositoryInterface::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)));
} }
@@ -148,7 +152,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
} }
$array['budgets'][] = $entry; $array['budgets'][] = $entry;
} }
$noBudget = $this->repository->spentInPeriodWoBudgetMc($accounts, $start, $end); $noBudget = $this->noBudgetRepository->spentInPeriodWoBudgetMc($accounts, $start, $end);
$noBudgetEntry = [ $noBudgetEntry = [
'budget_id' => null, 'budget_id' => null,
'budget_name' => null, 'budget_name' => null,

View File

@@ -276,79 +276,6 @@ class BudgetRepository implements BudgetRepositoryInterface
$this->user = $user; $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 * @param array $data
* *

View File

@@ -125,15 +125,6 @@ interface BudgetRepositoryInterface
*/ */
public function setUser(User $user); 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 Collection $accounts
* @param Carbon $start * @param Carbon $start

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; 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 Collection $accounts
* @param Carbon $start * @param Carbon $start

View File

@@ -47,4 +47,14 @@ interface NoBudgetRepositoryInterface
* @deprecated * @deprecated
*/ */
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array; 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;
} }

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\AvailableBudget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -35,6 +36,8 @@ use Log;
*/ */
class AvailableBudgetTransformer extends AbstractTransformer class AvailableBudgetTransformer extends AbstractTransformer
{ {
/** @var NoBudgetRepositoryInterface */
private $noBudgetRepository;
/** @var OperationsRepositoryInterface */ /** @var OperationsRepositoryInterface */
private $opsRepository; private $opsRepository;
/** @var BudgetRepositoryInterface */ /** @var BudgetRepositoryInterface */
@@ -49,6 +52,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
{ {
$this->repository = app(BudgetRepositoryInterface::class); $this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noBudgetRepository = app(NoBudgetRepositoryInterface::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)));
} }
@@ -114,7 +118,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
*/ */
private function spentOutsideBudgets(): array 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'));
} }
} }