mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expand test coverage and improve transaction management code.
This commit is contained in:
@@ -36,7 +36,7 @@ class AccountList implements BinderInterface
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
* @param Route $route
|
||||
*
|
||||
* @return Collection
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
@@ -44,25 +44,29 @@ class AccountList implements BinderInterface
|
||||
*/
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
Log::debug(sprintf('Now in AccountList::routeBinder("%s")', $value));
|
||||
if (auth()->check()) {
|
||||
Log::debug('User is logged in.');
|
||||
$collection = new Collection;
|
||||
if ('allAssetAccounts' === $value) {
|
||||
/** @var \Illuminate\Support\Collection $collection */
|
||||
/** @var Collection $collection */
|
||||
$collection = auth()->user()->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->where('account_types.type', AccountType::ASSET)
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->get(['accounts.*']);
|
||||
Log::debug(sprintf('Collection length is %d', $collection->count()));
|
||||
}
|
||||
if ('allAssetAccounts' !== $value) {
|
||||
$incoming = array_map('\intval', explode(',', $value));
|
||||
$list = array_merge(array_unique($incoming), [0]);
|
||||
/** @var \Illuminate\Support\Collection $collection */
|
||||
/** @var Collection $collection */
|
||||
$collection = auth()->user()->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('accounts.id', $list)
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->get(['accounts.*']);
|
||||
Log::debug(sprintf('Collection length is %d', $collection->count()));
|
||||
}
|
||||
|
||||
if ($collection->count() > 0) {
|
||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Support\Binder;
|
||||
use FireflyIII\Models\Budget;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@@ -34,7 +35,7 @@ class BudgetList implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
* @param Route $route
|
||||
*
|
||||
* @return Collection
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
@@ -42,27 +43,36 @@ class BudgetList implements BinderInterface
|
||||
*/
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
Log::debug(sprintf('Now in BudgetList::routeBinder("%s")', $value));
|
||||
if (auth()->check()) {
|
||||
$list = array_unique(array_map('\intval', explode(',', $value)));
|
||||
Log::debug('List is now', $list);
|
||||
if (0 === count($list)) {
|
||||
Log::warning('List count is zero, return 404.');
|
||||
throw new NotFoundHttpException; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/** @var \Illuminate\Support\Collection $collection */
|
||||
/** @var Collection $collection */
|
||||
$collection = auth()->user()->budgets()
|
||||
->where('active', 1)
|
||||
->whereIn('id', $list)
|
||||
->get();
|
||||
Log::debug(sprintf('Found %d active budgets', $collection->count()), $list);
|
||||
|
||||
// add empty budget if applicable.
|
||||
if (in_array(0, $list, true)) {
|
||||
Log::debug('Add empty budget because $list contains 0.');
|
||||
$collection->push(new Budget);
|
||||
}
|
||||
|
||||
if ($collection->count() > 0) {
|
||||
Log::debug(sprintf('List length is > 0 (%d), so return it.', $collection->count()));
|
||||
|
||||
return $collection;
|
||||
}
|
||||
Log::debug('List length is zero, fall back to 404.');
|
||||
}
|
||||
Log::debug('Final fallback to 404.');
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
}
|
||||
|
@@ -102,10 +102,7 @@ class ExpandedForm
|
||||
{
|
||||
// make repositories
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$accountList = $repository->getActiveAccountsByType(
|
||||
[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]
|
||||
);
|
||||
@@ -113,12 +110,15 @@ class ExpandedForm
|
||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
||||
$grouped = [];
|
||||
// group accounts:
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
|
||||
$currency = $currencyRepos->findNull($currencyId);
|
||||
$role = $repository->getMetaValue($account, 'account_role');
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
|
||||
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
|
||||
$role = $repository->getMetaValue($account, 'account_role');
|
||||
|
||||
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'no_account_type'; // @codeCoverageIgnore
|
||||
}
|
||||
@@ -126,15 +126,11 @@ class ExpandedForm
|
||||
if (in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (null === $currency) {
|
||||
$currency = $defaultCurrency;
|
||||
}
|
||||
|
||||
$key = (string)trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
|
||||
}
|
||||
|
||||
|
||||
return $this->select($name, $grouped, $value, $options);
|
||||
}
|
||||
|
||||
@@ -152,8 +148,6 @@ class ExpandedForm
|
||||
// make repositories
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
$accountList = $repository->getActiveAccountsByType(
|
||||
[
|
||||
@@ -176,10 +170,9 @@ class ExpandedForm
|
||||
// group accounts:
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
|
||||
$currency = $currencyRepos->findNull($currencyId);
|
||||
$role = (string)$repository->getMetaValue($account, 'account_role');
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$role = (string)$repository->getMetaValue($account, 'account_role');
|
||||
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'no_account_type'; // @codeCoverageIgnore
|
||||
}
|
||||
@@ -217,8 +210,6 @@ class ExpandedForm
|
||||
// make repositories
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
$accountList = $repository->getActiveAccountsByType(
|
||||
[
|
||||
@@ -241,10 +232,9 @@ class ExpandedForm
|
||||
// group accounts:
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
|
||||
$currency = $currencyRepos->findNull($currencyId);
|
||||
$role = (string)$repository->getMetaValue($account, 'account_role');
|
||||
$balance = app('steam')->balance($account, new Carbon);
|
||||
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$role = (string)$repository->getMetaValue($account, 'account_role');
|
||||
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'no_account_type'; // @codeCoverageIgnore
|
||||
}
|
||||
@@ -257,10 +247,6 @@ class ExpandedForm
|
||||
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (null === $currency) {
|
||||
$currency = $defaultCurrency;
|
||||
}
|
||||
|
||||
$key = (string)trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
|
||||
}
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -81,10 +80,7 @@ trait PeriodOverview
|
||||
protected function getAccountPeriodOverview(Account $account, Carbon $start, Carbon $end): array
|
||||
{
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
||||
}
|
||||
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
|
||||
|
||||
// properties for cache
|
||||
$cache = new CacheProperties;
|
||||
@@ -159,10 +155,7 @@ trait PeriodOverview
|
||||
protected function getCategoryPeriodOverview(Category $category, Carbon $start, Carbon $end): array
|
||||
{
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
||||
}
|
||||
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
|
||||
|
||||
// properties for entries with their amounts.
|
||||
$cache = new CacheProperties();
|
||||
@@ -173,7 +166,7 @@ trait PeriodOverview
|
||||
$cache->addProperty($category->id);
|
||||
|
||||
if ($cache->has()) {
|
||||
//return $cache->get(); // @codeCoverageIgnore
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
/** @var array $dates */
|
||||
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
||||
@@ -240,9 +233,7 @@ trait PeriodOverview
|
||||
{
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
||||
}
|
||||
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
|
||||
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
@@ -250,7 +241,7 @@ trait PeriodOverview
|
||||
$cache->addProperty('no-budget-period-entries');
|
||||
|
||||
if ($cache->has()) {
|
||||
//return $cache->get(); // @codeCoverageIgnore
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/** @var array $dates */
|
||||
@@ -284,6 +275,8 @@ trait PeriodOverview
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO fix date.
|
||||
*
|
||||
* Show period overview for no category view.
|
||||
*
|
||||
* @param Carbon $theDate
|
||||
@@ -309,7 +302,7 @@ trait PeriodOverview
|
||||
$cache->addProperty('no-category-period-entries');
|
||||
|
||||
if ($cache->has()) {
|
||||
//return $cache->get(); // @codeCoverageIgnore
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
||||
@@ -379,11 +372,7 @@ trait PeriodOverview
|
||||
/** @var Carbon $end */
|
||||
$start = clone $date;
|
||||
$end = $repository->firstUseDate($tag) ?? new Carbon;
|
||||
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
||||
}
|
||||
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
|
||||
|
||||
// properties for entries with their amounts.
|
||||
$cache = new CacheProperties;
|
||||
@@ -431,63 +420,60 @@ trait PeriodOverview
|
||||
* @param string $transactionType
|
||||
* @param Carbon $endDate
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*/
|
||||
protected function getTransactionPeriodOverview(string $transactionType, Carbon $endDate): Collection
|
||||
protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array
|
||||
{
|
||||
die('not yet complete');
|
||||
/** @var JournalRepositoryInterface $repository */
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
$endJournal = $repository->firstNull();
|
||||
$end = null === $endJournal ? new Carbon : $endJournal->date;
|
||||
$start = clone $endDate;
|
||||
$types = config('firefly.transactionTypesByType.' . $transactionType);
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
$types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType));
|
||||
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
||||
// properties for cache
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty('transactions-period-entries');
|
||||
$cache->addProperty($transactionType);
|
||||
if ($cache->has()) {
|
||||
// return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/** @var array $dates */
|
||||
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
||||
$entries = new Collection;
|
||||
$entries = [];
|
||||
|
||||
// collect all journals in this period (regardless of type)
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setTypes($types)->setRange($start, $end);
|
||||
$genericSet = $collector->getExtractedJournals();
|
||||
|
||||
foreach ($dates as $currentDate) {
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setTypes($types)->setRange($currentDate['start'], $currentDate['end']);
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$amounts = $this->getJournalsSum($journals);
|
||||
|
||||
$spent = [];
|
||||
$earned = [];
|
||||
$transferred = [];
|
||||
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
||||
|
||||
// set to correct array
|
||||
if ('expenses' === $transactionType || 'withdrawal' === $transactionType) {
|
||||
$spent = $amounts;
|
||||
$spent = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
|
||||
}
|
||||
if ('revenue' === $transactionType || 'deposit' === $transactionType) {
|
||||
$earned = $amounts;
|
||||
$earned = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
|
||||
}
|
||||
if ('transfer' === $transactionType || 'transfers' === $transactionType) {
|
||||
$transferred = $amounts;
|
||||
$transferred = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
|
||||
}
|
||||
|
||||
|
||||
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
||||
$entries->push(
|
||||
$entries[] =
|
||||
[
|
||||
'transactions' => $amounts['count'],
|
||||
'title' => $title,
|
||||
'spent' => $spent,
|
||||
'earned' => $earned,
|
||||
'transferred' => $transferred,
|
||||
'route' => route(
|
||||
'transactions.index', [$transactionType, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]
|
||||
),
|
||||
]
|
||||
);
|
||||
'title' => $title,
|
||||
'route' =>
|
||||
route('transactions.index', [$transactionType, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
||||
'total_transactions' => count($spent) + count($earned) + count($transferred),
|
||||
'spent' => $this->groupByCurrency($spent),
|
||||
'earned' => $this->groupByCurrency($earned),
|
||||
'transferred' => $this->groupByCurrency($transferred),
|
||||
];
|
||||
}
|
||||
|
||||
return $entries;
|
||||
|
@@ -178,7 +178,7 @@ trait RequestInformation
|
||||
// both must be array and either must be > 0
|
||||
if (count($intro) > 0 || count($specialIntro) > 0) {
|
||||
$shownDemo = app('preferences')->get($key, false)->data;
|
||||
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %s', $key, var_export($shownDemo, true)));
|
||||
//Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %s', $key, var_export($shownDemo, true)));
|
||||
}
|
||||
if (!is_bool($shownDemo)) {
|
||||
$shownDemo = true;
|
||||
|
@@ -50,7 +50,7 @@ trait UserNavigation
|
||||
*/
|
||||
protected function getPreviousUri(string $identifier): string
|
||||
{
|
||||
Log::debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
|
||||
// Log::debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
|
||||
// "forbidden" words for specific identifiers:
|
||||
// if these are in the previous URI, don't refer back there.
|
||||
$array = [
|
||||
@@ -67,24 +67,24 @@ trait UserNavigation
|
||||
'transactions.mass-delete.uri' => '/transactions/show/',
|
||||
];
|
||||
$forbidden = $array[$identifier] ?? '/show/';
|
||||
Log::debug(sprintf('The forbidden word for %s is "%s"', $identifier, $forbidden));
|
||||
//Log::debug(sprintf('The forbidden word for %s is "%s"', $identifier, $forbidden));
|
||||
|
||||
$uri = (string)session($identifier);
|
||||
Log::debug(sprintf('The URI is %s', $uri));
|
||||
//Log::debug(sprintf('The URI is %s', $uri));
|
||||
if (
|
||||
!(false === strpos($identifier, 'delete'))
|
||||
&& !(false === strpos($uri, $forbidden))) {
|
||||
$uri = $this->redirectUri;
|
||||
Log::debug(sprintf('URI is now %s (identifier contains "delete")', $uri));
|
||||
//Log::debug(sprintf('URI is now %s (identifier contains "delete")', $uri));
|
||||
}
|
||||
if (!(false === strpos($uri, 'jscript'))) {
|
||||
$uri = $this->redirectUri; // @codeCoverageIgnore
|
||||
Log::debug(sprintf('URI is now %s (uri contains jscript)', $uri));
|
||||
//Log::debug(sprintf('URI is now %s (uri contains jscript)', $uri));
|
||||
}
|
||||
|
||||
// more debug notes:
|
||||
Log::debug(sprintf('strpos($identifier, "delete"): %s', var_export(strpos($identifier, 'delete'), true)));
|
||||
Log::debug(sprintf('strpos($uri, $forbidden): %s', var_export(strpos($uri, $forbidden), true)));
|
||||
//Log::debug(sprintf('strpos($identifier, "delete"): %s', var_export(strpos($identifier, 'delete'), true)));
|
||||
//Log::debug(sprintf('strpos($uri, $forbidden): %s', var_export(strpos($uri, $forbidden), true)));
|
||||
|
||||
return $uri;
|
||||
}
|
||||
@@ -154,10 +154,10 @@ trait UserNavigation
|
||||
if (null === $errors || (null !== $errors && 0 === $errors->count())) {
|
||||
$url = app('url')->previous();
|
||||
session()->put($identifier, $url);
|
||||
Log::debug(sprintf('Will put previous URI in cache under key %s: %s', $identifier, $url));
|
||||
//Log::debug(sprintf('Will put previous URI in cache under key %s: %s', $identifier, $url));
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('The users session contains errors somehow so we will not remember the URI!: %s', var_export($errors, true)));
|
||||
//Log::debug(sprintf('The users session contains errors somehow so we will not remember the URI!: %s', var_export($errors, true)));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user