Clean up balance methods.

This commit is contained in:
James Cole
2024-12-22 20:32:58 +01:00
parent a0e92b6969
commit d90ac519f7
93 changed files with 1233 additions and 1853 deletions

View File

@@ -37,7 +37,7 @@ trait UserGroupDetectable
public function detectUserGroup(): ?UserGroup
{
/** @var User $user */
$user = auth()->user();
$user = auth()->user();
app('log')->debug('Now in detectUserGroup()');
/** @var null|UserGroup $userGroup */
@@ -49,7 +49,7 @@ trait UserGroupDetectable
app('log')->debug(sprintf('Request class has no user_group_id parameter, grab default from user (group #%d).', $user->user_group_id));
$userGroupId = (int) $user->user_group_id;
}
$userGroup = UserGroup::find($userGroupId);
$userGroup = UserGroup::find($userGroupId);
if (null === $userGroup) {
app('log')->error(sprintf('Request class has user_group_id (#%d), but group does not exist.', $userGroupId));

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\JsonApi\Enrichments;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ObjectGroup;
@@ -36,6 +37,8 @@ use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Override;
use stdClass;
/**
* Class AccountEnrichment
@@ -44,16 +47,16 @@ use Illuminate\Support\Facades\Log;
*/
class AccountEnrichment implements EnrichmentInterface
{
private array $balances;
private Collection $collection;
private array $currencies;
private array $balances;
private Collection $collection;
private array $currencies;
private CurrencyRepositoryInterface $currencyRepository;
private TransactionCurrency $default;
private ?Carbon $end;
private array $grouped;
private array $objectGroups;
private TransactionCurrency $default;
private ?Carbon $end;
private array $grouped;
private array $objectGroups;
private AccountRepositoryInterface $repository;
private ?Carbon $start;
private ?Carbon $start;
public function __construct()
{
@@ -63,7 +66,7 @@ class AccountEnrichment implements EnrichmentInterface
$this->end = null;
}
#[\Override]
#[Override]
public function enrichSingle(Model $model): Account
{
Log::debug(__METHOD__);
@@ -73,7 +76,7 @@ class AccountEnrichment implements EnrichmentInterface
return $collection->first();
}
#[\Override]
#[Override]
/**
* Do the actual enrichment.
*/
@@ -144,7 +147,7 @@ class AccountEnrichment implements EnrichmentInterface
$metaFields = $this->repository->getMetaValues($this->collection, ['is_multi_currency', 'currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']);
$currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
$currencies = [];
$currencies = [];
foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) {
$id = $currency->id;
$currencies[$id] = $currency;
@@ -174,8 +177,8 @@ class AccountEnrichment implements EnrichmentInterface
$default = $this->default;
// get start and end, so the balance difference can be generated.
$start = null;
$end = null;
$start = null;
$end = null;
if (null !== $this->start) {
$start = Balance::getAccountBalances($this->collection, $this->start);
}
@@ -197,7 +200,7 @@ class AccountEnrichment implements EnrichmentInterface
'balance_difference' => null,
];
if (array_key_exists($account->id, $balances)) {
$set = [];
$set = [];
foreach ($balances[$account->id] as $currencyId => $entry) {
$left = $start[$account->id][$currencyId]['balance'] ?? null;
$right = $end[$account->id][$currencyId]['balance'] ?? null;
@@ -239,12 +242,11 @@ class AccountEnrichment implements EnrichmentInterface
private function getObjectGroups(): void
{
$set = \DB::table('object_groupables')
->where('object_groupable_type', Account::class)
->whereIn('object_groupable_id', $this->collection->pluck('id')->toArray())
->distinct()
->get(['object_groupables.object_groupable_id', 'object_groupables.object_group_id'])
;
$set = DB::table('object_groupables')
->where('object_groupable_type', Account::class)
->whereIn('object_groupable_id', $this->collection->pluck('id')->toArray())
->distinct()
->get(['object_groupables.object_groupable_id', 'object_groupables.object_group_id']);
// get the groups:
$groupIds = $set->pluck('object_group_id')->toArray();
$groups = ObjectGroup::whereIn('id', $groupIds)->get();
@@ -254,7 +256,7 @@ class AccountEnrichment implements EnrichmentInterface
$this->objectGroups[$group->id] = $group;
}
/** @var \stdClass $entry */
/** @var stdClass $entry */
foreach ($set as $entry) {
$this->grouped[(int) $entry->object_groupable_id] = (int) $entry->object_group_id;
}

View File

@@ -77,7 +77,7 @@ trait ExpandsQuery
$config = config('api.valid_api_filters')[$class];
$parsed = [];
foreach ($filters->all() as $filter) {
$key = $filter->key();
$key = $filter->key();
if (!in_array($key, $config, true)) {
continue;
}

View File

@@ -39,8 +39,8 @@ trait FiltersPagination
$pagination['number'] = min(65536, max($pagination['number'], 1));
// clean up page size
$pagination['size'] = (int) ($pagination['size'] ?? $this->getPageSize());
$pagination['size'] = min(1337, max($pagination['size'], 1));
$pagination['size'] = (int) ($pagination['size'] ?? $this->getPageSize());
$pagination['size'] = min(1337, max($pagination['size'], 1));
return $pagination;
}