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\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,

View File

@ -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
*

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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'));
}
}