mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 20:22:07 +00:00
Refactor methods in category repositories.
This commit is contained in:
@@ -41,12 +41,10 @@ class CategoryController extends Controller
|
|||||||
{
|
{
|
||||||
/** @var CategoryRepositoryInterface */
|
/** @var CategoryRepositoryInterface */
|
||||||
private $categoryRepository;
|
private $categoryRepository;
|
||||||
|
|
||||||
/** @var OperationsRepositoryInterface */
|
|
||||||
private $opsRepository;
|
|
||||||
|
|
||||||
/** @var NoCategoryRepositoryInterface */
|
/** @var NoCategoryRepositoryInterface */
|
||||||
private $noCatRepository;
|
private $noCatRepository;
|
||||||
|
/** @var OperationsRepositoryInterface */
|
||||||
|
private $opsRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AccountController constructor.
|
* AccountController constructor.
|
||||||
@@ -62,7 +60,7 @@ class CategoryController extends Controller
|
|||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$this->categoryRepository = app(CategoryRepositoryInterface::class);
|
$this->categoryRepository = app(CategoryRepositoryInterface::class);
|
||||||
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
||||||
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
|
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
|
||||||
$this->categoryRepository->setUser($user);
|
$this->categoryRepository->setUser($user);
|
||||||
$this->opsRepository->setUser($user);
|
$this->opsRepository->setUser($user);
|
||||||
$this->noCatRepository->setUser($user);
|
$this->noCatRepository->setUser($user);
|
||||||
@@ -123,7 +121,8 @@ class CategoryController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// earned with no category:
|
// earned with no category:
|
||||||
$noCategory = $this->noCatRepository->earnedInPeriodPcWoCategory(new Collection, $start, $end);
|
$noCategory = $this->noCatRepository->sumIncome($start, $end);
|
||||||
|
|
||||||
foreach ($noCategory as $currencyId => $income) {
|
foreach ($noCategory as $currencyId => $income) {
|
||||||
$categoryName = (string)trans('firefly.no_category');
|
$categoryName = (string)trans('firefly.no_category');
|
||||||
// find or make set for currency:
|
// find or make set for currency:
|
||||||
@@ -131,7 +130,7 @@ class CategoryController extends Controller
|
|||||||
$decimalPlaces = $income['currency_decimal_places'];
|
$decimalPlaces = $income['currency_decimal_places'];
|
||||||
if (!isset($tempData[$key])) {
|
if (!isset($tempData[$key])) {
|
||||||
$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_id' => $income['currency_id'],
|
||||||
'currency_code' => $income['currency_code'],
|
'currency_code' => $income['currency_code'],
|
||||||
'currency_symbol' => $income['currency_symbol'],
|
'currency_symbol' => $income['currency_symbol'],
|
||||||
@@ -141,10 +140,9 @@ class CategoryController extends Controller
|
|||||||
'entries' => [],
|
'entries' => [],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$amount = round($income['earned'], $decimalPlaces);
|
$amount = round($income['sum'], $decimalPlaces);
|
||||||
$categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount;
|
$categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount;
|
||||||
$tempData[$key]['entries'][$categoryName]
|
$tempData[$key]['entries'][$categoryName] = $amount;
|
||||||
= $amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionType;
|
|||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
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 Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @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
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -140,11 +88,10 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -173,7 +120,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
|
|||||||
if (!isset($result['entries'][$date])) {
|
if (!isset($result['entries'][$date])) {
|
||||||
$result['entries'][$date] = '0';
|
$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('Done looping transactions..');
|
||||||
Log::debug('Finished periodIncomeNoCategory()');
|
Log::debug('Finished periodIncomeNoCategory()');
|
||||||
@@ -181,6 +128,13 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A very cryptic method name that means:
|
* 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.
|
* Get me the amount spent in this period, grouped per currency, where no category was set.
|
||||||
*
|
*
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -225,4 +179,41 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
|
|||||||
return $return;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -35,26 +35,6 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface NoCategoryRepositoryInterface
|
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
|
* TODO not multi-currency
|
||||||
*
|
*
|
||||||
@@ -67,6 +47,29 @@ interface NoCategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function periodExpensesNoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
|
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
|
* TODO not multi-currency
|
||||||
@@ -80,6 +83,11 @@ interface NoCategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function periodIncomeNoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
|
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:
|
* A very cryptic method name that means:
|
||||||
*
|
*
|
||||||
@@ -95,5 +103,16 @@ interface NoCategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentInPeriodPcWoCategory(Collection $accounts, Carbon $start, Carbon $end): array;
|
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;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -372,4 +372,17 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
|
|
||||||
return $return;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user