Skip to content

Firefly III: a personal finances manager

Refactor methods in category repositories.

James Cole0daed6529db8
diff --git a/app/Api/V1/Controllers/Chart/CategoryController.php b/app/Api/V1/Controllers/Chart/CategoryController.php
index 63b0b17975..069fa3cb92 100644
--- a/app/Api/V1/Controllers/Chart/CategoryController.php
+++ b/app/Api/V1/Controllers/Chart/CategoryController.php
@@ -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;
}
diff --git a/app/Repositories/Category/NoCategoryRepository.php b/app/Repositories/Category/NoCategoryRepository.php
index 287999e356..0ab5a02ba2 100644
--- a/app/Repositories/Category/NoCategoryRepository.php
+++ b/app/Repositories/Category/NoCategoryRepository.php
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
+use Navigation;
/**
*
@@ -50,63 +51,10 @@ 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;
+ }
}
\ No newline at end of file
diff --git a/app/Repositories/Category/NoCategoryRepositoryInterface.php b/app/Repositories/Category/NoCategoryRepositoryInterface.php
index 630523a3fb..eee7a07eb6 100644
--- a/app/Repositories/Category/NoCategoryRepositoryInterface.php
+++ b/app/Repositories/Category/NoCategoryRepositoryInterface.php
@@ -36,14 +36,7 @@ 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.
+ * TODO not multi-currency
*
* @param Collection $accounts
* @param Carbon $start
@@ -52,21 +45,31 @@ interface NoCategoryRepositoryInterface
* @return array
* @deprecated
*/
- public function earnedInPeriodPcWoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
-
+ public function periodExpensesNoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
/**
- * TODO not multi-currency
+ * 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 Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection|null $accounts
*
* @return array
- * @deprecated
*/
- public function periodExpensesNoCategory(Collection $accounts, Carbon $start, Carbon $end): 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;
+
}
\ No newline at end of file
diff --git a/app/Repositories/Category/OperationsRepository.php b/app/Repositories/Category/OperationsRepository.php
index 0cf5527738..233d42d71b 100644
--- a/app/Repositories/Category/OperationsRepository.php
+++ b/app/Repositories/Category/OperationsRepository.php
@@ -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;
+ }
}
\ No newline at end of file