mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-11 01:19:31 +00:00
Auto commit for release 'develop' on 2024-04-21
This commit is contained in:
@@ -73,7 +73,7 @@ class UpdateController extends Controller
|
||||
$data = $request->getAll();
|
||||
|
||||
// Fixes 8750.
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
foreach ($transactions as $index => $info) {
|
||||
unset($data['transactions'][$index]['type']);
|
||||
}
|
||||
|
||||
@@ -106,8 +106,8 @@ class StoreRequest extends FormRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$validTriggers = $this->getTriggers();
|
||||
$validActions = array_keys(config('firefly.rule-actions'));
|
||||
$validTriggers = $this->getTriggers();
|
||||
$validActions = array_keys(config('firefly.rule-actions'));
|
||||
|
||||
// some triggers and actions require text:
|
||||
$contextTriggers = implode(',', $this->getTriggersWithContext());
|
||||
@@ -119,11 +119,11 @@ class StoreRequest extends FormRequest
|
||||
'rule_group_id' => 'belongsToUser:rule_groups|required_without:rule_group_title',
|
||||
'rule_group_title' => 'nullable|min:1|max:255|required_without:rule_group_id|belongsToUser:rule_groups,title',
|
||||
'trigger' => 'required|in:store-journal,update-journal',
|
||||
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
|
||||
'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue|max:1024',
|
||||
'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
|
||||
'triggers.*.value' => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024',
|
||||
'triggers.*.stop_processing' => [new IsBoolean()],
|
||||
'triggers.*.active' => [new IsBoolean()],
|
||||
'actions.*.type' => 'required|in:' . implode(',', $validActions),
|
||||
'actions.*.type' => 'required|in:'.implode(',', $validActions),
|
||||
'actions.*.value' => [sprintf('required_if:actions.*.type,%s', $contextActions), new IsValidActionExpression(), 'ruleActionValue'],
|
||||
'actions.*.stop_processing' => [new IsBoolean()],
|
||||
'actions.*.active' => [new IsBoolean()],
|
||||
@@ -182,10 +182,10 @@ class StoreRequest extends FormRequest
|
||||
*/
|
||||
protected function atLeastOneActiveTrigger(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$data = $validator->getData();
|
||||
|
||||
/** @var null|array|int|string $triggers */
|
||||
$triggers = $data['triggers'] ?? [];
|
||||
$triggers = $data['triggers'] ?? [];
|
||||
// need at least one trigger
|
||||
if (!is_countable($triggers) || 0 === count($triggers)) {
|
||||
return;
|
||||
@@ -211,10 +211,10 @@ class StoreRequest extends FormRequest
|
||||
*/
|
||||
protected function atLeastOneActiveAction(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$data = $validator->getData();
|
||||
|
||||
/** @var null|array|int|string $actions */
|
||||
$actions = $data['actions'] ?? [];
|
||||
$actions = $data['actions'] ?? [];
|
||||
// need at least one trigger
|
||||
if (!is_countable($actions) || 0 === count($actions)) {
|
||||
return;
|
||||
|
||||
@@ -47,7 +47,7 @@ class UpdateRequest extends FormRequest
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
$fields = [
|
||||
$fields = [
|
||||
'title' => ['title', 'convertString'],
|
||||
'description' => ['description', 'stringWithNewlines'],
|
||||
'rule_group_id' => ['rule_group_id', 'convertInteger'],
|
||||
@@ -122,11 +122,11 @@ class UpdateRequest extends FormRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$validTriggers = $this->getTriggers();
|
||||
$validActions = array_keys(config('firefly.rule-actions'));
|
||||
$validTriggers = $this->getTriggers();
|
||||
$validActions = array_keys(config('firefly.rule-actions'));
|
||||
|
||||
/** @var Rule $rule */
|
||||
$rule = $this->route()->parameter('rule');
|
||||
$rule = $this->route()->parameter('rule');
|
||||
|
||||
// some triggers and actions require text:
|
||||
$contextTriggers = implode(',', $this->getTriggersWithContext());
|
||||
@@ -138,11 +138,11 @@ class UpdateRequest extends FormRequest
|
||||
'rule_group_id' => 'belongsToUser:rule_groups',
|
||||
'rule_group_title' => 'nullable|min:1|max:255|belongsToUser:rule_groups,title',
|
||||
'trigger' => 'in:store-journal,update-journal',
|
||||
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
|
||||
'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue|max:1024',
|
||||
'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
|
||||
'triggers.*.value' => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024',
|
||||
'triggers.*.stop_processing' => [new IsBoolean()],
|
||||
'triggers.*.active' => [new IsBoolean()],
|
||||
'actions.*.type' => 'required|in:' . implode(',', $validActions),
|
||||
'actions.*.type' => 'required|in:'.implode(',', $validActions),
|
||||
'actions.*.value' => [sprintf('required_if:actions.*.type,%s', $contextActions), new IsValidActionExpression(), 'ruleActionValue'],
|
||||
'actions.*.stop_processing' => [new IsBoolean()],
|
||||
'actions.*.active' => [new IsBoolean()],
|
||||
|
||||
@@ -122,7 +122,7 @@ class Controller extends BaseController
|
||||
$obj = null;
|
||||
}
|
||||
}
|
||||
if(null !== $date && 'end' === $field) {
|
||||
if (null !== $date && 'end' === $field) {
|
||||
$obj->endOfDay();
|
||||
}
|
||||
$bag->set($field, $obj);
|
||||
|
||||
@@ -25,7 +25,6 @@ namespace FireflyIII\Api\V2\Controllers\Model\Account;
|
||||
|
||||
use FireflyIII\Api\V2\Controllers\Controller;
|
||||
use FireflyIII\Api\V2\Request\Model\Account\IndexRequest;
|
||||
use FireflyIII\Api\V2\Request\Model\Transaction\InfiniteListRequest;
|
||||
use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Transformers\V2\AccountTransformer;
|
||||
@@ -77,6 +76,7 @@ class IndexController extends Controller
|
||||
|
||||
return response()
|
||||
->json($this->jsonApiList('accounts', $paginator, $transformer))
|
||||
->header('Content-Type', self::CONTENT_TYPE);
|
||||
->header('Content-Type', self::CONTENT_TYPE)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace FireflyIII\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Vite;
|
||||
use Barryvdh\Debugbar\Facades\Debugbar;
|
||||
|
||||
/**
|
||||
* Class SecureHeaders
|
||||
@@ -41,16 +42,16 @@ class SecureHeaders
|
||||
public function handle(Request $request, \Closure $next)
|
||||
{
|
||||
// generate and share nonce.
|
||||
$nonce = base64_encode(random_bytes(16));
|
||||
$nonce = base64_encode(random_bytes(16));
|
||||
Vite::useCspNonce($nonce);
|
||||
if(class_exists('Barryvdh\Debugbar\Facades\Debugbar')) {
|
||||
\Barryvdh\Debugbar\Facades\Debugbar::getJavascriptRenderer()->setCspNonce($nonce);
|
||||
if (class_exists('Barryvdh\Debugbar\Facades\Debugbar')) {
|
||||
Debugbar::getJavascriptRenderer()->setCspNonce($nonce);
|
||||
}
|
||||
app('view')->share('JS_NONCE', $nonce);
|
||||
|
||||
$response = $next($request);
|
||||
$trackingScriptSrc = $this->getTrackingScriptSource();
|
||||
$csp = [
|
||||
$response = $next($request);
|
||||
$trackingScriptSrc = $this->getTrackingScriptSource();
|
||||
$csp = [
|
||||
"default-src 'none'",
|
||||
"object-src 'none'",
|
||||
sprintf("script-src 'unsafe-eval' 'strict-dynamic' 'nonce-%1s'", $nonce),
|
||||
@@ -77,11 +78,10 @@ class SecureHeaders
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
$route = $request->route();
|
||||
$customUrl = '';
|
||||
$authGuard = (string) config('firefly.authentication_guard');
|
||||
$logoutUrl = (string) config('firefly.custom_logout_url');
|
||||
$route = $request->route();
|
||||
$customUrl = '';
|
||||
$authGuard = (string) config('firefly.authentication_guard');
|
||||
$logoutUrl = (string) config('firefly.custom_logout_url');
|
||||
if ('remote_user_guard' === $authGuard && '' !== $logoutUrl) {
|
||||
$customUrl = $logoutUrl;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ class SecureHeaders
|
||||
$csp[] = sprintf("form-action 'self' %s", $customUrl);
|
||||
}
|
||||
|
||||
$featurePolicies = [
|
||||
$featurePolicies = [
|
||||
"geolocation 'none'",
|
||||
"midi 'none'",
|
||||
// "notifications 'none'",
|
||||
|
||||
@@ -296,13 +296,14 @@ class UserGroupRepository implements UserGroupRepositoryInterface
|
||||
$this->user->save();
|
||||
}
|
||||
|
||||
#[\Override] public function getMembershipsFromGroupId(int $groupId): Collection
|
||||
#[\Override]
|
||||
public function getMembershipsFromGroupId(int $groupId): Collection
|
||||
{
|
||||
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
|
||||
}
|
||||
|
||||
|
||||
#[\Override] public function getById(int $id): ?UserGroup
|
||||
#[\Override]
|
||||
public function getById(int $id): ?UserGroup
|
||||
{
|
||||
return UserGroup::find($id);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,8 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
$q1->where('account_meta.name', '=', 'account_number');
|
||||
$q1->where('account_meta.data', '=', $json);
|
||||
}
|
||||
);
|
||||
)
|
||||
;
|
||||
|
||||
if (0 !== count($types)) {
|
||||
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
@@ -91,7 +92,7 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
|
||||
public function findByName(string $name, array $types): ?Account
|
||||
{
|
||||
$query = $this->userGroup->accounts();
|
||||
$query = $this->userGroup->accounts();
|
||||
|
||||
if (0 !== count($types)) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
@@ -115,8 +116,8 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
|
||||
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
||||
{
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
|
||||
// return null if not in this list.
|
||||
if (!in_array($type, $list, true)) {
|
||||
@@ -241,9 +242,9 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
|
||||
public function getAccountsByType(array $types, ?array $sort = []): Collection
|
||||
{
|
||||
$sortable = ['name', 'active']; // TODO yes this is a duplicate array.
|
||||
$res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types);
|
||||
$query = $this->userGroup->accounts();
|
||||
$sortable = ['name', 'active']; // TODO yes this is a duplicate array.
|
||||
$res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types);
|
||||
$query = $this->userGroup->accounts();
|
||||
if (0 !== count($types)) {
|
||||
$query->accountTypeIn($types);
|
||||
}
|
||||
@@ -276,11 +277,12 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
{
|
||||
// search by group, not by user
|
||||
$dbQuery = $this->userGroup->accounts()
|
||||
->where('active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType']);
|
||||
->where('active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType'])
|
||||
;
|
||||
if ('' !== $query) {
|
||||
// split query on spaces just in case:
|
||||
$parts = explode(' ', $query);
|
||||
@@ -306,29 +308,33 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $service->update($account, $data);
|
||||
}
|
||||
|
||||
#[\Override] public function getMetaValues(Collection $accounts, array $fields): Collection
|
||||
#[\Override]
|
||||
public function getMetaValues(Collection $accounts, array $fields): Collection
|
||||
{
|
||||
$query = AccountMeta::whereIn('account_id', $accounts->pluck('id')->toArray());
|
||||
if (count($fields) > 0) {
|
||||
$query->whereIn('name', $fields);
|
||||
}
|
||||
|
||||
return $query->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']);
|
||||
}
|
||||
|
||||
#[\Override] public function getAccountTypes(Collection $accounts): Collection
|
||||
#[\Override]
|
||||
public function getAccountTypes(Collection $accounts): Collection
|
||||
{
|
||||
return AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
|
||||
->whereIn('accounts.id', $accounts->pluck('id')->toArray())
|
||||
->get(['accounts.id', 'account_types.type']);
|
||||
|
||||
->whereIn('accounts.id', $accounts->pluck('id')->toArray())
|
||||
->get(['accounts.id', 'account_types.type'])
|
||||
;
|
||||
}
|
||||
|
||||
#[\Override] public function getLastActivity(Collection $accounts): array
|
||||
#[\Override]
|
||||
public function getLastActivity(Collection $accounts): array
|
||||
{
|
||||
return Transaction::whereIn('account_id', $accounts->pluck('id')->toArray())
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
|
||||
->groupBy('transactions.account_id')
|
||||
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) as date_max')])->toArray() // @phpstan-ignore-line
|
||||
;
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
|
||||
->groupBy('transactions.account_id')
|
||||
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) as date_max')])->toArray() // @phpstan-ignore-line
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ trait ValidatesUserGroupTrait
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$groupId = 0;
|
||||
$user = auth()->user();
|
||||
$groupId = 0;
|
||||
if (!$request->has('user_group_id')) {
|
||||
$groupId = $user->user_group_id;
|
||||
Log::debug(sprintf('validateUserGroup: no user group submitted, use default group #%d.', $groupId));
|
||||
@@ -61,8 +61,9 @@ trait ValidatesUserGroupTrait
|
||||
$groupId = (int) $request->get('user_group_id');
|
||||
Log::debug(sprintf('validateUserGroup: user group submitted, search for memberships in group #%d.', $groupId));
|
||||
}
|
||||
|
||||
/** @var UserGroupRepositoryInterface $repository */
|
||||
$repository = app(UserGroupRepositoryInterface::class);
|
||||
$repository = app(UserGroupRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
$memberships = $repository->getMembershipsFromGroupId($groupId);
|
||||
|
||||
@@ -73,14 +74,14 @@ trait ValidatesUserGroupTrait
|
||||
}
|
||||
|
||||
// need to get the group from the membership:
|
||||
$group = $repository->getById($groupId);
|
||||
$group = $repository->getById($groupId);
|
||||
if (null === $group) {
|
||||
Log::debug(sprintf('validateUserGroup: group #%d does not exist.', $groupId));
|
||||
|
||||
throw new AuthorizationException((string) trans('validation.belongs_user_or_user_group'));
|
||||
}
|
||||
Log::debug(sprintf('validateUserGroup: validate access of user to group #%d ("%s").', $groupId, $group->title));
|
||||
$roles = property_exists($this, 'acceptedRoles') ? $this->acceptedRoles : [];
|
||||
$roles = property_exists($this, 'acceptedRoles') ? $this->acceptedRoles : [];
|
||||
if (0 === count($roles)) {
|
||||
Log::debug('validateUserGroup: no roles defined, so no access.');
|
||||
|
||||
|
||||
@@ -464,7 +464,7 @@ class Navigation
|
||||
$increment = 'addDay';
|
||||
$format = $this->preferredCarbonFormat($start, $end);
|
||||
$displayFormat = (string)trans('config.month_and_day_js', [], $locale);
|
||||
$diff = $start->diffInMonths($end, true);
|
||||
$diff = $start->diffInMonths($end, true);
|
||||
// increment by month (for year)
|
||||
if ($diff >= 1.0001) {
|
||||
$increment = 'addMonth';
|
||||
@@ -495,7 +495,7 @@ class Navigation
|
||||
public function preferredCarbonFormat(Carbon $start, Carbon $end): string
|
||||
{
|
||||
$format = 'Y-m-d';
|
||||
$diff = $start->diffInMonths($end, true);
|
||||
$diff = $start->diffInMonths($end, true);
|
||||
Log::debug(sprintf('preferredCarbonFormat(%s, %s) = %f', $start->format('Y-m-d'), $end->format('Y-m-d'), $diff));
|
||||
if ($diff >= 1.001) {
|
||||
Log::debug(sprintf('Return Y-m because %s', $diff));
|
||||
|
||||
@@ -71,7 +71,7 @@ class RuleTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/rules/' . $rule->id,
|
||||
'uri' => '/rules/'.$rule->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -108,12 +108,12 @@ class RuleTransformer extends AbstractTransformer
|
||||
if ('user_action' === $ruleTrigger->trigger_type) {
|
||||
continue;
|
||||
}
|
||||
$triggerType = (string) $ruleTrigger->trigger_type;
|
||||
$triggerType = (string) $ruleTrigger->trigger_type;
|
||||
$triggerValue = (string)$ruleTrigger->trigger_value;
|
||||
$prohibited = false;
|
||||
|
||||
if(str_starts_with($triggerType, '-')) {
|
||||
$prohibited = true;
|
||||
if (str_starts_with($triggerType, '-')) {
|
||||
$prohibited = true;
|
||||
$triggerType = substr($triggerType, 1);
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ class RuleTransformer extends AbstractTransformer
|
||||
$triggerValue = 'true';
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
$result[] = [
|
||||
'id' => (string)$ruleTrigger->id,
|
||||
'created_at' => $ruleTrigger->created_at->toAtomString(),
|
||||
'updated_at' => $ruleTrigger->updated_at->toAtomString(),
|
||||
|
||||
@@ -80,8 +80,6 @@ class AccountTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
return $this->sortAccounts($objects);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function getDate(): Carbon
|
||||
@@ -99,21 +97,21 @@ class AccountTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(Account $account): array
|
||||
{
|
||||
$id = $account->id;
|
||||
$id = $account->id;
|
||||
|
||||
// various meta
|
||||
$accountRole = $this->accountMeta[$id]['account_role'] ?? null;
|
||||
$accountType = $this->accountTypes[$id];
|
||||
$order = $account->order;
|
||||
$accountRole = $this->accountMeta[$id]['account_role'] ?? null;
|
||||
$accountType = $this->accountTypes[$id];
|
||||
$order = $account->order;
|
||||
|
||||
// no currency? use default
|
||||
$currency = $this->default;
|
||||
$currency = $this->default;
|
||||
if (array_key_exists($id, $this->accountMeta) && 0 !== (int) ($this->accountMeta[$id]['currency_id'] ?? 0)) {
|
||||
$currency = $this->currencies[(int) $this->accountMeta[$id]['currency_id']];
|
||||
}
|
||||
// amounts and calculation.
|
||||
$balance = $this->balances[$id]['balance'] ?? null;
|
||||
$nativeBalance = $this->convertedBalances[$id]['native_balance'] ?? null;
|
||||
$balance = $this->balances[$id]['balance'] ?? null;
|
||||
$nativeBalance = $this->convertedBalances[$id]['native_balance'] ?? null;
|
||||
|
||||
// no order for some accounts:
|
||||
if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) {
|
||||
@@ -121,33 +119,32 @@ class AccountTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
// balance difference
|
||||
$diffStart = null;
|
||||
$diffEnd = null;
|
||||
$balanceDiff = null;
|
||||
$diffStart = null;
|
||||
$diffEnd = null;
|
||||
$balanceDiff = null;
|
||||
$nativeBalanceDiff = null;
|
||||
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
||||
$diffStart = $this->parameters->get('start')->toAtomString();
|
||||
$diffEnd = $this->parameters->get('end')->toAtomString();
|
||||
$balanceDiff = $this->balanceDifferences[$id]['balance'] ?? null;
|
||||
$diffStart = $this->parameters->get('start')->toAtomString();
|
||||
$diffEnd = $this->parameters->get('end')->toAtomString();
|
||||
$balanceDiff = $this->balanceDifferences[$id]['balance'] ?? null;
|
||||
$nativeBalanceDiff = $this->balanceDifferences[$id]['native_balance'] ?? null;
|
||||
}
|
||||
|
||||
|
||||
return [
|
||||
'id' => (string) $account->id,
|
||||
'created_at' => $account->created_at->toAtomString(),
|
||||
'updated_at' => $account->updated_at->toAtomString(),
|
||||
'active' => $account->active,
|
||||
'order' => $order,
|
||||
'name' => $account->name,
|
||||
'iban' => '' === (string) $account->iban ? null : $account->iban,
|
||||
'account_number' => $this->accountMeta[$id]['account_number'] ?? null,
|
||||
'type' => strtolower($accountType),
|
||||
'account_role' => $accountRole,
|
||||
'currency_id' => (string) $currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => $currency->decimal_places,
|
||||
'id' => (string) $account->id,
|
||||
'created_at' => $account->created_at->toAtomString(),
|
||||
'updated_at' => $account->updated_at->toAtomString(),
|
||||
'active' => $account->active,
|
||||
'order' => $order,
|
||||
'name' => $account->name,
|
||||
'iban' => '' === (string) $account->iban ? null : $account->iban,
|
||||
'account_number' => $this->accountMeta[$id]['account_number'] ?? null,
|
||||
'type' => strtolower($accountType),
|
||||
'account_role' => $accountRole,
|
||||
'currency_id' => (string) $currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => $currency->decimal_places,
|
||||
|
||||
'native_currency_id' => (string) $this->default->id,
|
||||
'native_currency_code' => $this->default->code,
|
||||
@@ -188,7 +185,7 @@ class AccountTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/accounts/' . $account->id,
|
||||
'uri' => '/accounts/'.$account->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -206,19 +203,19 @@ class AccountTransformer extends AbstractTransformer
|
||||
private function getDefaultCurrency(): void
|
||||
{
|
||||
$this->default = app('amount')->getDefaultCurrency();
|
||||
|
||||
}
|
||||
|
||||
private function collectAccountMetaData(Collection $accounts): void
|
||||
{
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$metaFields = $accountRepository->getMetaValues($accounts, ['currency_id', 'account_role', 'account_number']);
|
||||
$currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
|
||||
|
||||
$currencies = $repository->getByIds($currencyIds);
|
||||
$currencies = $repository->getByIds($currencyIds);
|
||||
foreach ($currencies as $currency) {
|
||||
$id = $currency->id;
|
||||
$this->currencies[$id] = $currency;
|
||||
@@ -265,14 +262,12 @@ class AccountTransformer extends AbstractTransformer
|
||||
* @var string $direction
|
||||
*/
|
||||
foreach ($sort as $column => $direction) {
|
||||
|
||||
// account_number + iban
|
||||
if ('iban' === $column) {
|
||||
$accounts = $this->sortByIban($accounts, $direction);
|
||||
}
|
||||
if ('balance' === $column) {
|
||||
$accounts = $this->sortByBalance($accounts, $direction);
|
||||
|
||||
}
|
||||
if ('last_activity' === $column) {
|
||||
$accounts = $this->sortByLastActivity($accounts, $direction);
|
||||
@@ -281,12 +276,14 @@ class AccountTransformer extends AbstractTransformer
|
||||
$accounts = $this->sortByBalanceDifference($accounts, $direction);
|
||||
}
|
||||
}
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
private function sortByIban(Collection $accounts, string $direction): Collection
|
||||
{
|
||||
$meta = $this->accountMeta;
|
||||
|
||||
return $accounts->sort(function (Account $left, Account $right) use ($meta, $direction) {
|
||||
$leftIban = trim(sprintf('%s%s', $left->iban, $meta[$left->id]['account_number'] ?? ''));
|
||||
$rightIban = trim(sprintf('%s%s', $right->iban, $meta[$right->id]['account_number'] ?? ''));
|
||||
@@ -301,6 +298,7 @@ class AccountTransformer extends AbstractTransformer
|
||||
private function sortByBalance(Collection $accounts, string $direction): Collection
|
||||
{
|
||||
$balances = $this->convertedBalances;
|
||||
|
||||
return $accounts->sort(function (Account $left, Account $right) use ($balances, $direction) {
|
||||
$leftBalance = (float) ($balances[$left->id]['native_balance'] ?? 0);
|
||||
$rightBalance = (float) ($balances[$right->id]['native_balance'] ?? 0);
|
||||
@@ -315,6 +313,7 @@ class AccountTransformer extends AbstractTransformer
|
||||
private function sortByLastActivity(Collection $accounts, string $direction): Collection
|
||||
{
|
||||
$dates = $this->lastActivity;
|
||||
|
||||
return $accounts->sort(function (Account $left, Account $right) use ($dates, $direction) {
|
||||
$leftDate = $dates[$left->id] ?? Carbon::create(1900, 1, 1, 0, 0, 0);
|
||||
$rightDate = $dates[$right->id] ?? Carbon::create(1900, 1, 1, 0, 0, 0);
|
||||
@@ -332,12 +331,14 @@ class AccountTransformer extends AbstractTransformer
|
||||
// yes the b is usually used for boolean by idiots but here it's for balance.
|
||||
$bStart = [];
|
||||
$bEnd = [];
|
||||
|
||||
try {
|
||||
$bStart = app('steam')->balancesByAccountsConverted($accounts, $start);
|
||||
$bEnd = app('steam')->balancesByAccountsConverted($accounts, $end);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
$id = $account->id;
|
||||
@@ -348,11 +349,12 @@ class AccountTransformer extends AbstractTransformer
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function sortByBalanceDifference(Collection $accounts, string $direction): Collection {
|
||||
private function sortByBalanceDifference(Collection $accounts, string $direction): Collection
|
||||
{
|
||||
$balances = $this->balanceDifferences;
|
||||
|
||||
return $accounts->sort(function (Account $left, Account $right) use ($balances, $direction) {
|
||||
$leftBalance = (float) ($balances[$left->id]['native_balance'] ?? 0);
|
||||
$rightBalance = (float) ($balances[$right->id]['native_balance'] ?? 0);
|
||||
|
||||
Reference in New Issue
Block a user