Refactor methods in category repositories.

This commit is contained in:
James Cole 2019-08-27 12:11:46 +02:00
parent e5269bb312
commit 0daed6529d
4 changed files with 113 additions and 92 deletions

View File

@ -41,12 +41,10 @@ class CategoryController extends Controller
{
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var OperationsRepositoryInterface */
private $opsRepository;
/** @var NoCategoryRepositoryInterface */
private $noCatRepository;
/** @var OperationsRepositoryInterface */
private $opsRepository;
/**
* AccountController constructor.
@ -62,7 +60,7 @@ class CategoryController extends Controller
$user = auth()->user();
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
$this->categoryRepository->setUser($user);
$this->opsRepository->setUser($user);
$this->noCatRepository->setUser($user);
@ -123,7 +121,8 @@ class CategoryController extends Controller
}
// earned with no category:
$noCategory = $this->noCatRepository->earnedInPeriodPcWoCategory(new Collection, $start, $end);
$noCategory = $this->noCatRepository->sumIncome($start, $end);
foreach ($noCategory as $currencyId => $income) {
$categoryName = (string)trans('firefly.no_category');
// find or make set for currency:
@ -131,7 +130,7 @@ class CategoryController extends Controller
$decimalPlaces = $income['currency_decimal_places'];
if (!isset($tempData[$key])) {
$tempData[$key] = [
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $income['currency_symbol']]),
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $income['currency_name']]),
'currency_id' => $income['currency_id'],
'currency_code' => $income['currency_code'],
'currency_symbol' => $income['currency_symbol'],
@ -141,10 +140,9 @@ class CategoryController extends Controller
'entries' => [],
];
}
$amount = round($income['earned'], $decimalPlaces);
$categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount;
$tempData[$key]['entries'][$categoryName]
= $amount;
$amount = round($income['sum'], $decimalPlaces);
$categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount;
$tempData[$key]['entries'][$categoryName] = $amount;
}

View File

@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
use Navigation;
/**
*
@ -51,62 +52,9 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* A very cryptic method name that means:
*
* Get me the amount earned in this period, grouped per currency, where no category was set.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function earnedInPeriodPcWoCategory(Collection $accounts, Carbon $start, Carbon $end): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->withoutCategory();
if ($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
$journals = $collector->getExtractedJournals();
$return = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
if (!isset($return[$currencyId])) {
$return[$currencyId] = [
'earned' => '0',
'currency_id' => $currencyId,
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
}
$return[$currencyId]['earned'] = bcadd($return[$currencyId]['earned'], $journal['amount']);
}
return $return;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@ -140,11 +88,10 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $result;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@ -173,7 +120,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
if (!isset($result['entries'][$date])) {
$result['entries'][$date] = '0';
}
$result['entries'][$date] = bcadd($result['entries'][$date], bcmul($journal['amount'],'-1'));
$result['entries'][$date] = bcadd($result['entries'][$date], bcmul($journal['amount'], '-1'));
}
Log::debug('Done looping transactions..');
Log::debug('Finished periodIncomeNoCategory()');
@ -181,6 +128,13 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $result;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* A very cryptic method name that means:
@ -188,8 +142,8 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
* Get me the amount spent in this period, grouped per currency, where no category was set.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@ -225,4 +179,41 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $return;
}
/**
* Sum of income journals in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->withoutCategory();
if (null !== $accounts && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}
return $array;
}
}

View File

@ -35,26 +35,6 @@ use Illuminate\Support\Collection;
*/
interface NoCategoryRepositoryInterface
{
/**
* @param User $user
*/
public function setUser(User $user): void;
/**
* A very cryptic method name that means:
*
* Get me the amount earned in this period, grouped per currency, where no category was set.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
*/
public function earnedInPeriodPcWoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* TODO not multi-currency
*
@ -67,6 +47,29 @@ interface NoCategoryRepositoryInterface
*/
public function periodExpensesNoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have no category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
//public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* Sum of withdrawal journals in period without a category, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
//public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* TODO not multi-currency
@ -80,6 +83,11 @@ interface NoCategoryRepositoryInterface
*/
public function periodIncomeNoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param User $user
*/
public function setUser(User $user): void;
/**
* A very cryptic method name that means:
*
@ -95,5 +103,16 @@ interface NoCategoryRepositoryInterface
*/
public function spentInPeriodPcWoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* Sum of income journals in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
}

View File

@ -372,4 +372,17 @@ class OperationsRepository implements OperationsRepositoryInterface
return $return;
}
/**
* Returns a list of all the categories belonging to a user.
*
* @return Collection
*/
private function getCategories(): Collection
{
/** @var Collection $set */
$set = $this->user->categories()->get();
return $set;
}
}