Clean up balance methods.

This commit is contained in:
James Cole 2024-12-22 20:32:58 +01:00
parent a0e92b6969
commit d90ac519f7
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
93 changed files with 1233 additions and 1853 deletions

View File

@ -116,13 +116,13 @@ class AccountController extends Controller
];
// TODO this code is also present in the V2 chart account controller so this method is due to be deprecated.
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end);
$range = app('steam')->finalAccountBalanceInRange($account, $start, clone $end);
// 2022-10-11 this method no longer converts to float.
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$balance = array_key_exists($format, $range) ? $range[$format]['balance'] : $previous;
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;

View File

@ -118,22 +118,21 @@ class AccountController extends Controller
'native_entries' => [],
];
$currentStart = clone $params['start'];
$range = app('steam')->balanceInRange($account, $params['start'], clone $params['end'], $currency);
$rangeConverted = app('steam')->balanceInRangeConverted($account, $params['start'], clone $params['end'], $this->default);
$range = app('steam')->finalAccountBalanceInRange($account, $params['start'], clone $params['end'], $currency);
$previous = array_values($range)[0];
$previousConverted = array_values($rangeConverted)[0];
$previous = array_values($range)[0]['balance'];
$previousNative = array_values($range)[0]['native_balance'];
while ($currentStart <= $params['end']) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$balanceConverted = array_key_exists($format, $rangeConverted) ? $rangeConverted[$format] : $previousConverted;
$balance = array_key_exists($format, $range) ? $range[$format]['balance'] : $previous;
$balanceNative = array_key_exists($format, $range) ? $range[$format]['balance_native'] : $previousNative;
$previous = $balance;
$previousConverted = $balanceConverted;
$previousNative = $balanceNative;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
$currentSet['native_entries'][$label] = $balanceNative;
}
$this->chartData->add($currentSet);
}

View File

@ -95,7 +95,7 @@ class NetWorth implements NetWorthInterface
'native_currency_decimal_places' => $default->decimal_places,
],
];
$balances = app('steam')->balancesByAccountsConverted($accounts, $date);
$balances = app('steam')->finalAccountsBalance($accounts, $date);
/** @var Account $account */
foreach ($accounts as $account) {
@ -187,10 +187,10 @@ class NetWorth implements NetWorthInterface
*/
$accounts = $this->getAccounts();
$return = [];
$balances = app('steam')->balancesByAccounts($accounts, $date);
$balances = app('steam')->finalAccountsBalance($accounts, $date);
foreach ($accounts as $account) {
$currency = $this->getRepository()->getAccountCurrency($account);
$balance = $balances[$account->id] ?? '0';
$balance = $balances[$account->id]['balance'] ?? '0';
// always subtract virtual balance.
$virtualBalance = $account->virtual_balance;

View File

@ -89,8 +89,8 @@ class IndexController extends Controller
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = app('steam')->balancesByAccounts($accounts, $start);
$endBalances = app('steam')->balancesByAccounts($accounts, $end);
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
$activities = app('steam')->getLastActivities($ids);
$accounts->each(
@ -149,8 +149,8 @@ class IndexController extends Controller
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = app('steam')->balancesByAccounts($accounts, $start);
$endBalances = app('steam')->balancesByAccounts($accounts, $end);
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
$activities = app('steam')->getLastActivities($ids);
$accounts->each(

View File

@ -106,28 +106,27 @@ class AccountController extends Controller
$accountNames = $this->extractNames($accounts);
// grab all balances
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int) $accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int) $currencyId;
foreach ($expenses as $currencyCode => $endAmount) {
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string) ($startBalances[$accountId][$currencyId] ?? '0');
$startAmount = (string) ($startBalances[$accountId][$currencyCode] ?? '0');
$diff = bcsub((string) $endAmount, $startAmount);
$currencies[$currencyId] ??= $this->currencyRepository->find($currencyId);
$currencies[$currencyCode] ??= $this->currencyRepository->findByCode($currencyCode);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
'diff_float' => (float) $diff, // intentional float
'currency_id' => $currencyId,
'currency_id' => $currencies[$currencyCode]->id,
];
}
}
@ -437,11 +436,11 @@ class AccountController extends Controller
if ('1D' === $step) {
// per day the entire period, balance for every day.
$format = (string) trans('config.month_and_day_js', [], $locale);
$range = app('steam')->balanceInRange($account, $start, $end, $currency);
$range = app('steam')->finalAccountBalanceInRange($account, $start, $end);
$previous = array_values($range)[0];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$balance = $range[$theDate]['balance'] ?? $previous;
$label = $current->isoFormat($format);
$entries[$label] = (float) $balance;
$previous = $balance;
@ -507,28 +506,27 @@ class AccountController extends Controller
$accountNames = $this->extractNames($accounts);
// grab all balances
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int) $accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int) $currencyId;
foreach ($expenses as $currencyCode => $endAmount) {
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string) ($startBalances[$accountId][$currencyId] ?? '0');
$startAmount = (string) ($startBalances[$accountId][$currencyCode] ?? '0');
$diff = bcsub((string) $endAmount, $startAmount);
$currencies[$currencyId] ??= $this->currencyRepository->find($currencyId);
$currencies[$currencyCode] ??= $this->currencyRepository->findByCode($currencyCode);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
'diff_float' => (float) $diff, // intentional float
'currency_id' => $currencyId,
'currency_id' => $currencies[$currencyCode]->id,
];
}
}

View File

@ -47,8 +47,8 @@ class AccountTasker implements AccountTaskerInterface
{
$yesterday = clone $start;
$yesterday->subDay();
$startSet = app('steam')->balancesByAccounts($accounts, $yesterday);
$endSet = app('steam')->balancesByAccounts($accounts, $end);
$startSet = app('steam')->finalAccountsBalance($accounts, $yesterday);
$endSet = app('steam')->finalAccountsBalance($accounts, $end);
app('log')->debug('Start of accountreport');
/** @var AccountRepositoryInterface $repository */
@ -86,8 +86,8 @@ class AccountTasker implements AccountTaskerInterface
// get first journal date:
$first = $repository->oldestJournal($account);
$entry['start_balance'] = $startSet[$account->id] ?? '0';
$entry['end_balance'] = $endSet[$account->id] ?? '0';
$entry['start_balance'] = $startSet[$account->id]['balance'] ?? '0';
$entry['end_balance'] = $endSet[$account->id]['balance'] ?? '0';
// first journal exists, and is on start, then this is the actual opening balance:
if (null !== $first && $first->date->isSameDay($start) && TransactionType::OPENING_BALANCE === $first->transactionType->type) {

View File

@ -28,6 +28,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Support\Collection;
use NumberFormatter;
/**
* Class Amount.
@ -59,10 +60,10 @@ class Amount
$rounded = app('steam')->bcround($amount, $decimalPlaces);
$coloured ??= true;
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = (string) $fmt->format((float) $rounded); // intentional float
if (true === $coloured) {
@ -185,10 +186,10 @@ class Amount
$info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space');
$info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space');
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$info['mon_decimal_point'] = $fmt->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
$info['mon_thousands_sep'] = $fmt->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
$info['mon_decimal_point'] = $fmt->getSymbol(NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
$info['mon_thousands_sep'] = $fmt->getSymbol(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
return $info;
}
@ -263,11 +264,11 @@ class Amount
}
// default is amount before currency
$format = $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE;
$format = $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE;
if ($csPrecedes) {
// alternative is currency before amount
$format = $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE;
$format = $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE;
}
return $format;

View File

@ -139,14 +139,14 @@ class RemoteUserGuard implements Guard
/**
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function id(): null|int|string
public function id(): null | int | string
{
app('log')->debug(sprintf('Now at %s', __METHOD__));
return $this->user?->id;
}
public function setUser(null|Authenticatable|User $user): void // @phpstan-ignore-line
public function setUser(null | Authenticatable | User $user): void // @phpstan-ignore-line
{
app('log')->debug(sprintf('Now at %s', __METHOD__));
if ($user instanceof User) {

View File

@ -30,13 +30,15 @@ use FireflyIII\Models\Role;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Override;
use Str;
/**
* Class RemoteUserProvider
*/
class RemoteUserProvider implements UserProvider
{
#[\Override]
#[Override]
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false): void
{
app('log')->debug(sprintf('Now at %s', __METHOD__));
@ -72,7 +74,7 @@ class RemoteUserProvider implements UserProvider
'blocked' => false,
'blocked_code' => null,
'email' => $identifier,
'password' => bcrypt(\Str::random(64)),
'password' => bcrypt(Str::random(64)),
]
);
// if this is the first user, give them admin as well.

View File

@ -54,8 +54,7 @@ class Balance
->orderBy('transaction_journals.order', 'asc')
->orderBy('transaction_journals.description', 'desc')
->orderBy('transactions.amount', 'desc')
->where('transaction_journals.date', '<=', $date)
;
->where('transaction_journals.date', '<=', $date);
$result = $query->get(['transactions.account_id', 'transactions.transaction_currency_id', 'transactions.balance_after']);
foreach ($result as $entry) {

View File

@ -46,8 +46,7 @@ class AccountList implements BinderInterface
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereIn('account_types.type', [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE])
->orderBy('accounts.name', 'ASC')
->get(['accounts.*'])
;
->get(['accounts.*']);
}
if ('allAssetAccounts' !== $value) {
$incoming = array_map('\intval', explode(',', $value));
@ -58,8 +57,7 @@ class AccountList implements BinderInterface
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereIn('accounts.id', $list)
->orderBy('accounts.name', 'ASC')
->get(['accounts.*'])
;
->get(['accounts.*']);
}
if ($collection->count() > 0) {

View File

@ -43,8 +43,7 @@ class BudgetList implements BinderInterface
return auth()->user()->budgets()->where('active', true)
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')
->get()
;
->get();
}
$list = array_unique(array_map('\intval', explode(',', $value)));
@ -59,8 +58,7 @@ class BudgetList implements BinderInterface
$collection = auth()->user()->budgets()
->where('active', true)
->whereIn('id', $list)
->get()
;
->get();
// add empty budget if applicable.
if (in_array(0, $list, true)) {

View File

@ -42,8 +42,7 @@ class CategoryList implements BinderInterface
if ('allCategories' === $value) {
return auth()->user()->categories()
->orderBy('name', 'ASC')
->get()
;
->get();
}
$list = array_unique(array_map('\intval', explode(',', $value)));
@ -54,8 +53,7 @@ class CategoryList implements BinderInterface
/** @var Collection $collection */
$collection = auth()->user()->categories()
->whereIn('id', $list)
->get()
;
->get();
// add empty category if applicable.
if (in_array(0, $list, true)) {

View File

@ -68,7 +68,7 @@ class Date implements BinderInterface
try {
$result = new Carbon($value);
} catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line
} catch (InvalidDateException | InvalidFormatException $e) { // @phpstan-ignore-line
$message = sprintf('Could not parse date "%s" for user #%d: %s', $value, auth()->user()->id, $e->getMessage());
app('log')->error($message);

View File

@ -44,8 +44,7 @@ class TagList implements BinderInterface
if ('allTags' === $value) {
return auth()->user()->tags()
->orderBy('tag', 'ASC')
->get()
;
->get();
}
$list = array_unique(array_map('\strtolower', explode(',', $value)));
app('log')->debug('List of tags is', $list);

View File

@ -43,8 +43,7 @@ class UserGroupAccount implements BinderInterface
$user = auth()->user();
$account = Account::where('id', (int) $value)
->where('user_group_id', $user->user_group_id)
->first()
;
->first();
if (null !== $account) {
return $account;
}

View File

@ -43,8 +43,7 @@ class UserGroupBill implements BinderInterface
$user = auth()->user();
$currency = Bill::where('id', (int) $value)
->where('user_group_id', $user->user_group_id)
->first()
;
->first();
if (null !== $currency) {
return $currency;
}

View File

@ -40,8 +40,7 @@ class UserGroupExchangeRate implements BinderInterface
$user = auth()->user();
$rate = CurrencyExchangeRate::where('id', (int) $value)
->where('user_group_id', $user->user_group_id)
->first()
;
->first();
if (null !== $rate) {
return $rate;
}

View File

@ -40,8 +40,7 @@ class UserGroupTransaction implements BinderInterface
$user = auth()->user();
$group = TransactionGroup::where('id', (int) $value)
->where('user_group_id', $user->user_group_id)
->first()
;
->first();
if (null !== $group) {
return $group;
}

View File

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use Cache;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class CacheProperties.
@ -55,7 +57,7 @@ class CacheProperties
*/
public function get()
{
return \Cache::get($this->hash);
return Cache::get($this->hash);
}
public function getHash(): string
@ -70,7 +72,7 @@ class CacheProperties
}
$this->hash();
return \Cache::has($this->hash);
return Cache::has($this->hash);
}
private function hash(): void
@ -79,7 +81,7 @@ class CacheProperties
foreach ($this->properties as $property) {
try {
$content .= json_encode($property, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
} catch (JsonException $e) {
// @ignoreException
$content .= hash('sha256', (string) time());
}
@ -92,6 +94,6 @@ class CacheProperties
*/
public function store($data): void
{
\Cache::forever($this->hash, $data);
Cache::forever($this->hash, $data);
}
}

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Support\Calendar;
use Carbon\Carbon;
use FireflyIII\Exceptions\IntervalException;
use SplObjectStorage;
/**
* Class Calculator
@ -33,7 +34,7 @@ use FireflyIII\Exceptions\IntervalException;
class Calculator
{
public const int DEFAULT_INTERVAL = 1;
private static ?\SplObjectStorage $intervalMap = null;
private static ?SplObjectStorage $intervalMap = null;
private static array $intervals = [];
/**
@ -65,14 +66,14 @@ class Calculator
/**
* @SuppressWarnings(PHPMD.MissingImport)
*/
private static function loadIntervalMap(): \SplObjectStorage
private static function loadIntervalMap(): SplObjectStorage
{
if (null !== self::$intervalMap) {
return self::$intervalMap;
}
self::$intervalMap = new \SplObjectStorage();
self::$intervalMap = new SplObjectStorage();
foreach (Periodicity::cases() as $interval) {
$periodicityClass = __NAMESPACE__."\\Periodicity\\{$interval->name}";
$periodicityClass = __NAMESPACE__ . "\\Periodicity\\{$interval->name}";
self::$intervals[] = $interval->name;
self::$intervalMap->attach($interval, new $periodicityClass());
}

View File

@ -27,6 +27,7 @@ use Eloquent;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Form\FormSupport;
use Illuminate\Support\Collection;
use Throwable;
/**
* Class ExpandedForm.
@ -56,7 +57,7 @@ class ExpandedForm
// }
try {
$html = view('form.amount-no-currency', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render amountNoCurrency(): %s', $e->getMessage()));
$html = 'Could not render amountNoCurrency.';
@ -91,7 +92,7 @@ class ExpandedForm
try {
$html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render checkbox(): %s', $e->getMessage()));
$html = 'Could not render checkbox.';
@ -116,7 +117,7 @@ class ExpandedForm
try {
$html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render date(): %s', $e->getMessage()));
$html = 'Could not render date.';
@ -138,7 +139,7 @@ class ExpandedForm
try {
$html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render file(): %s', $e->getMessage()));
$html = 'Could not render file.';
@ -164,7 +165,7 @@ class ExpandedForm
try {
$html = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render integer(): %s', $e->getMessage()));
$html = 'Could not render integer.';
@ -189,7 +190,7 @@ class ExpandedForm
try {
$html = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render location(): %s', $e->getMessage()));
$html = 'Could not render location.';
@ -205,7 +206,7 @@ class ExpandedForm
$selectList[0] = '(none)';
$fields = ['title', 'name', 'description'];
/** @var \Eloquent $entry */
/** @var Eloquent $entry */
foreach ($set as $entry) {
// All Eloquent models have an ID
$entryId = $entry->id; // @phpstan-ignore-line
@ -242,7 +243,7 @@ class ExpandedForm
try {
$html = view('form.object_group', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render objectGroup(): %s', $e->getMessage()));
$html = 'Could not render objectGroup.';
@ -259,7 +260,7 @@ class ExpandedForm
{
try {
$html = view('form.options', compact('type', 'name'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render select(): %s', $e->getMessage()));
$html = 'Could not render optionsList.';
@ -280,7 +281,7 @@ class ExpandedForm
try {
$html = view('form.password', compact('classes', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render password(): %s', $e->getMessage()));
$html = 'Could not render password.';
@ -301,7 +302,7 @@ class ExpandedForm
try {
$html = view('form.password', compact('classes', 'value', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render passwordWithValue(): %s', $e->getMessage()));
$html = 'Could not render passwordWithValue.';
@ -329,7 +330,7 @@ class ExpandedForm
try {
$html = view('form.percentage', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render percentage(): %s', $e->getMessage()));
$html = 'Could not render percentage.';
@ -352,7 +353,7 @@ class ExpandedForm
try {
$html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render staticText(): %s', $e->getMessage()));
$html = 'Could not render staticText.';
@ -376,7 +377,7 @@ class ExpandedForm
try {
$html = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render text(): %s', $e->getMessage()));
$html = 'Could not render text.';
@ -405,7 +406,7 @@ class ExpandedForm
try {
$html = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render textarea(): %s', $e->getMessage()));
$html = 'Could not render textarea.';

View File

@ -736,8 +736,7 @@ class ExportDataGenerator
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setRange($this->start, $this->end)->withAccountInformation()->withCategoryInformation()->withBillInformation()
->withBudgetInformation()->withTagInformation()->withNotes()
;
->withBudgetInformation()->withTagInformation()->withNotes();
if (0 !== $this->accounts->count()) {
$collector->setAccounts($this->accounts);
}

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use Cache;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration;
use Illuminate\Contracts\Encryption\DecryptException;
@ -37,9 +39,9 @@ class FireflyConfig
{
public function delete(string $name): void
{
$fullName = 'ff-config-'.$name;
if (\Cache::has($fullName)) {
\Cache::forget($fullName);
$fullName = 'ff-config-' . $name;
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
Configuration::where('name', $name)->forceDelete();
}
@ -79,20 +81,20 @@ class FireflyConfig
*/
public function get(string $name, $default = null): ?Configuration
{
$fullName = 'ff-config-'.$name;
if (\Cache::has($fullName)) {
return \Cache::get($fullName);
$fullName = 'ff-config-' . $name;
if (Cache::has($fullName)) {
return Cache::get($fullName);
}
try {
/** @var null|Configuration $config */
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
} catch (\Exception|QueryException $e) {
} catch (Exception | QueryException $e) {
throw new FireflyException(sprintf('Could not poll the database: %s', $e->getMessage()), 0, $e);
}
if (null !== $config) {
\Cache::forever($fullName, $config);
Cache::forever($fullName, $config);
return $config;
}
@ -122,13 +124,13 @@ class FireflyConfig
$item->name = $name;
$item->data = $value;
$item->save();
\Cache::forget('ff-config-'.$name);
Cache::forget('ff-config-' . $name);
return $item;
}
$config->data = $value;
$config->save();
\Cache::forget('ff-config-'.$name);
Cache::forget('ff-config-' . $name);
return $config;
}

View File

@ -29,6 +29,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Throwable;
/**
* Class AccountForm
@ -125,7 +126,7 @@ class AccountForm
try {
$html = view('form.assetAccountCheckList', compact('classes', 'selected', 'name', 'label', 'options', 'grouped'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage()));
$html = 'Could not render assetAccountCheckList.';

View File

@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Throwable;
/**
* Class CurrencyForm
@ -68,7 +69,7 @@ class CurrencyForm
if (!is_array($preFilled)) {
$preFilled = [];
}
$key = 'amount_currency_id_'.$name;
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = array_key_exists($key, $preFilled) ? (int) $preFilled[$key] : $defaultCurrency->id;
app('log')->debug(sprintf('Sent currency ID is %d', $sentCurrencyId));
@ -89,8 +90,8 @@ class CurrencyForm
}
try {
$html = view('form.'.$view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render currencyField(): %s', $e->getMessage()));
$html = 'Could not render currencyField.';
@ -137,7 +138,7 @@ class CurrencyForm
if (!is_array($preFilled)) {
$preFilled = [];
}
$key = 'amount_currency_id_'.$name;
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = array_key_exists($key, $preFilled) ? (int) $preFilled[$key] : $defaultCurrency->id;
app('log')->debug(sprintf('Sent currency ID is %d', $sentCurrencyId));
@ -158,8 +159,8 @@ class CurrencyForm
}
try {
$html = view('form.'.$view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render currencyField(): %s', $e->getMessage()));
$html = 'Could not render currencyField.';
@ -185,7 +186,7 @@ class CurrencyForm
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name.' ('.$currency->symbol.')';
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
return $this->select($name, $array, $value, $options);
@ -209,7 +210,7 @@ class CurrencyForm
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name.' ('.$currency->symbol.')';
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
return $this->select($name, $array, $value, $options);

View File

@ -28,6 +28,7 @@ use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\MessageBag;
use Throwable;
/**
* Trait FormSupport
@ -46,7 +47,7 @@ trait FormSupport
try {
$html = view('form.multi-select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render multi-select(): %s', $e->getMessage()));
$html = 'Could not render multi-select.';
}
@ -54,28 +55,6 @@ trait FormSupport
return $html;
}
/**
* @param mixed $selected
*/
public function select(string $name, ?array $list = null, $selected = null, ?array $options = null): string
{
$list ??= [];
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
unset($options['autocomplete'], $options['placeholder']);
try {
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
} catch (\Throwable $e) {
app('log')->debug(sprintf('Could not render select(): %s', $e->getMessage()));
$html = 'Could not render select.';
}
return $html;
}
protected function label(string $name, ?array $options = null): string
{
$options ??= [];
@ -84,7 +63,7 @@ trait FormSupport
}
$name = str_replace('[]', '', $name);
return (string) trans('form.'.$name);
return (string) trans('form.' . $name);
}
/**
@ -95,7 +74,7 @@ trait FormSupport
$options ??= [];
$name = str_replace('[]', '', $name);
$options['class'] = 'form-control';
$options['id'] = 'ffInput_'.$name;
$options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off';
$options['placeholder'] = ucfirst($label);
@ -139,6 +118,28 @@ trait FormSupport
return $value;
}
/**
* @param mixed $selected
*/
public function select(string $name, ?array $list = null, $selected = null, ?array $options = null): string
{
$list ??= [];
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
unset($options['autocomplete'], $options['placeholder']);
try {
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
} catch (Throwable $e) {
app('log')->debug(sprintf('Could not render select(): %s', $e->getMessage()));
$html = 'Could not render select.';
}
return $html;
}
protected function getAccountRepository(): AccountRepositoryInterface
{
return app(AccountRepositoryInterface::class);

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Http\Api;
use Carbon\Carbon;
use DateTimeInterface;
use FireflyIII\Models\TransactionCurrency;
/**
@ -66,7 +67,7 @@ trait ConvertsExchangeRates
return $set;
}
foreach ($set['entries'] as $date => $entry) {
$carbon = Carbon::createFromFormat(\DateTimeInterface::ATOM, $date);
$carbon = Carbon::createFromFormat(DateTimeInterface::ATOM, $date);
$rate = $this->getRate($currency, $native, $carbon);
$rate = '0' === $rate ? '1' : $rate;
app('log')->debug(sprintf('bcmul("%s", "%s")', (string) $entry, $rate));

View File

@ -172,8 +172,7 @@ class ExchangeRateConverter
->where('to_currency_id', $to)
->where('date', '<=', $date)
->orderBy('date', 'DESC')
->first()
;
->first();
++$this->queryCount;
$rate = (string) $result?->rate;
@ -278,8 +277,7 @@ class ExchangeRateConverter
->where('to_currency_id', $to->id)
->where('date', '<=', $end->format('Y-m-d'))
->where('date', '>=', $start->format('Y-m-d'))
->orderBy('date', 'DESC')->get()
;
->orderBy('date', 'DESC')->get();
++$this->queryCount;
if (0 === $set->count()) {
Log::debug('No prepared rates found in this period, use the fallback');

View File

@ -38,7 +38,7 @@ trait BasicDataSupport
*/
protected function isInArray(array $array, int $entryId)
{
return $array[$entryId] ?? '0';
return $array[$entryId]['balance'] ?? '0';
}
/**
@ -46,6 +46,6 @@ trait BasicDataSupport
*/
protected function isInArrayDate(array $array, int $entryId): ?Carbon
{
return $array[$entryId] ?? null;
return $array[$entryId]['balance'] ?? null;
}
}

View File

@ -30,6 +30,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Facades\Steam;
use Illuminate\Support\Collection;
/**
@ -50,7 +51,7 @@ trait ChartGeneration
$cache->addProperty($end);
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
$convertToNative =app('preferences')->get('convert_to_native', false)->data;
$convertToNative = app('preferences')->get('convert_to_native', false)->data;
if ($cache->has()) {
// return $cache->get();
}
@ -74,7 +75,7 @@ trait ChartGeneration
$currency = $default;
}
// if the user prefers the native currency, overrule the currency of the account.
if($currency->id !== $default->id && $convertToNative) {
if ($currency->id !== $default->id && $convertToNative) {
$currency = $default;
}
@ -85,7 +86,7 @@ trait ChartGeneration
];
$currentStart = clone $start;
$range = $convertToNative ? app('steam')->balanceInRangeNative($account, $start, clone $end) : app('steam')->balanceInRange($account, $start, clone $end);
$range = Steam::finalAccountBalanceInRange($account, $start, clone $end);
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
@ -93,7 +94,7 @@ trait ChartGeneration
$balance = $range[$format] ?? $previous;
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['entries'][$label] = $balance['balance']; // TODO or native_balance
}
$chartData[] = $currentSet;
}

View File

@ -64,7 +64,7 @@ trait GetConfigurationData
$currentStep = $options;
// get the text:
$currentStep['intro'] = (string) trans('intro.'.$route.'_'.$key);
$currentStep['intro'] = (string) trans('intro.' . $route . '_' . $key);
// save in array:
$steps[] = $currentStep;
@ -186,13 +186,13 @@ trait GetConfigurationData
// user is on page with specific instructions:
if ('' !== $specificPage) {
$routeKey = str_replace('.', '_', $route);
$elements = config(sprintf('intro.%s', $routeKey.'_'.$specificPage));
$elements = config(sprintf('intro.%s', $routeKey . '_' . $specificPage));
if (is_array($elements) && count($elements) > 0) {
foreach ($elements as $key => $options) {
$currentStep = $options;
// get the text:
$currentStep['intro'] = (string) trans('intro.'.$route.'_'.$specificPage.'_'.$key);
$currentStep['intro'] = (string) trans('intro.' . $route . '_' . $specificPage . '_' . $key);
// save in array:
$steps[] = $currentStep;

View File

@ -32,6 +32,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Throwable;
/**
* Trait ModelInformation
@ -55,7 +56,7 @@ trait ModelInformation
'count' => 1,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Throwable was thrown in getActionsForBill(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
$result = 'Could not render view. See log files.';
@ -142,7 +143,7 @@ trait ModelInformation
'triggers' => $triggers,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getTriggersForBill(): %s', $e->getMessage()));
app('log')->debug($e->getTraceAsString());
@ -258,7 +259,7 @@ trait ModelInformation
'triggers' => $triggers,
];
$string = view('rules.partials.trigger', $renderInfo)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getTriggersForJournal(): %s', $e->getMessage()));
app('log')->debug($e->getTraceAsString());

View File

@ -37,6 +37,7 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\Search\OperatorQuerySearch;
use Throwable;
/**
* Trait RenderPartialViews
@ -68,7 +69,7 @@ trait RenderPartialViews
try {
$view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
@ -91,7 +92,7 @@ trait RenderPartialViews
try {
$result = view('reports.options.budget', compact('budgets'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';
@ -123,7 +124,7 @@ trait RenderPartialViews
try {
$view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
@ -150,7 +151,7 @@ trait RenderPartialViews
try {
$view = view('popup.report.category-entry', compact('journals', 'category'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
@ -173,7 +174,7 @@ trait RenderPartialViews
try {
$result = view('reports.options.category', compact('categories'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Cannot render reports.options.category: %s', $e->getMessage()));
$result = 'Could not render view.';
@ -215,7 +216,7 @@ trait RenderPartialViews
try {
$result = view('reports.options.double', compact('set'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';
@ -248,7 +249,7 @@ trait RenderPartialViews
try {
$view = view('popup.report.expense-entry', compact('journals', 'account'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
@ -284,7 +285,7 @@ trait RenderPartialViews
'count' => $count,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getCurrentActions(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
@ -339,7 +340,7 @@ trait RenderPartialViews
'triggers' => $triggers,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getCurrentTriggers(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
@ -375,7 +376,7 @@ trait RenderPartialViews
try {
$view = view('popup.report.income-entry', compact('journals', 'account'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
@ -394,7 +395,7 @@ trait RenderPartialViews
{
try {
$result = view('reports.options.no-options')->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage()));
$result = 'Could not render view.';
@ -417,7 +418,7 @@ trait RenderPartialViews
try {
$result = view('reports.options.tag', compact('tags'))->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';

View File

@ -30,6 +30,7 @@ use FireflyIII\Http\Requests\RuleFormRequest;
use FireflyIII\Http\Requests\TestRuleFormRequest;
use FireflyIII\Support\Binder\AccountList;
use FireflyIII\User;
use Hash;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Validator;
@ -169,7 +170,7 @@ trait RequestInformation
*/
final protected function validatePassword(User $user, string $current, string $new): bool // get request info
{
if (!\Hash::check($current, $user->password)) {
if (!Hash::check($current, $user->password)) {
throw new ValidationException((string) trans('firefly.invalid_current_password'));
}

View File

@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Support\Search\OperatorQuerySearch;
use Illuminate\Http\Request;
use Throwable;
/**
* Trait RuleManagement
@ -54,7 +55,7 @@ trait RuleManagement
'count' => $index + 1,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->error(sprintf('Throwable was thrown in getPreviousActions(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
@ -99,7 +100,7 @@ trait RuleManagement
'triggers' => $triggers,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
@ -145,7 +146,7 @@ trait RuleManagement
'triggers' => $triggers,
]
)->render();
} catch (\Throwable $e) {
} catch (Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());

View File

@ -46,8 +46,7 @@ trait TransactionCalculation
$collector->setAccounts($total)
->setRange($start, $end)
->withAccountInformation()
->setTypes([TransactionType::WITHDRAWAL])
;
->setTypes([TransactionType::WITHDRAWAL]);
return $collector->getExtractedJournals();
}
@ -61,8 +60,7 @@ trait TransactionCalculation
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setTags($tags)->withAccountInformation()
;
->setTags($tags)->withAccountInformation();
return $collector->getExtractedJournals();
}
@ -75,8 +73,7 @@ trait TransactionCalculation
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setBudgets($budgets)->withAccountInformation()
;
->setBudgets($budgets)->withAccountInformation();
return $collector->getExtractedJournals();
}
@ -93,8 +90,7 @@ trait TransactionCalculation
->setRange($start, $end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setCategories($categories)
->withAccountInformation()
;
->withAccountInformation();
return $collector->getExtractedJournals();
}
@ -107,8 +103,7 @@ trait TransactionCalculation
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setCategories($categories)->withAccountInformation()
;
->setCategories($categories)->withAccountInformation();
return $collector->getExtractedJournals();
}
@ -135,8 +130,7 @@ trait TransactionCalculation
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setTags($tags)->withAccountInformation()
;
->setTags($tags)->withAccountInformation();
return $collector->getExtractedJournals();
}

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
@ -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.
*/
@ -239,12 +242,11 @@ class AccountEnrichment implements EnrichmentInterface
private function getObjectGroups(): void
{
$set = \DB::table('object_groupables')
$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(['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

@ -78,8 +78,7 @@ class AccountBalanceCalculator
->orderBy('transaction_journals.order', 'desc')
->orderBy('transaction_journals.id', 'asc')
->orderBy('transaction_journals.description', 'asc')
->orderBy('transactions.amount', 'asc')
;
->orderBy('transactions.amount', 'asc');
if ($accounts->count() > 0) {
$query->whereIn('transactions.account_id', $accounts->pluck('id')->toArray());
}
@ -137,8 +136,7 @@ class AccountBalanceCalculator
->orderBy('transaction_journals.id', 'DESC')
->orderBy('transaction_journals.description', 'DESC')
->orderBy('transactions.amount', 'DESC')
->where('transactions.account_id', $accountId)
;
->where('transactions.account_id', $accountId);
$notBefore->startOfDay();
$query->where('transaction_journals.date', '<', $notBefore);

View File

@ -39,7 +39,7 @@ trait ReturnsIntegerIdTrait
protected function id(): Attribute
{
return Attribute::make(
get: static fn ($value) => (int) $value,
get: static fn($value) => (int) $value,
);
}
}

View File

@ -37,14 +37,14 @@ trait ReturnsIntegerUserIdTrait
protected function userGroupId(): Attribute
{
return Attribute::make(
get: static fn ($value) => (int) $value,
get: static fn($value) => (int) $value,
);
}
protected function userId(): Attribute
{
return Attribute::make(
get: static fn ($value) => (int) $value,
get: static fn($value) => (int) $value,
);
}
}

View File

@ -30,6 +30,7 @@ use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Support\Calendar\Calculator;
use FireflyIII\Support\Calendar\Periodicity;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* Class Navigation.
@ -93,7 +94,7 @@ class Navigation
return $this->calculator->nextDateByInterval($epoch, $periodicity, $skipInterval);
} catch (IntervalException $exception) {
Log::warning($exception->getMessage(), ['exception' => $exception]);
} catch (\Throwable $exception) { // @phpstan-ignore-line
} catch (Throwable $exception) { // @phpstan-ignore-line
Log::error($exception->getMessage(), ['exception' => $exception]);
}

View File

@ -24,10 +24,12 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use ArrayObject;
/**
* Class NullArrayObject
*/
class NullArrayObject extends \ArrayObject
class NullArrayObject extends ArrayObject
{
/** @var null|mixed */
public $default;

View File

@ -52,11 +52,10 @@ class Preferences
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->get()
;
->get();
}
public function get(string $name, null|array|bool|int|string $default = null): ?Preference
public function get(string $name, null | array | bool | int | string $default = null): ?Preference
{
/** @var null|User $user */
$user = auth()->user();
@ -70,7 +69,7 @@ class Preferences
return $this->getForUser($user, $name, $default);
}
public function getForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
public function getForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
{
// don't care about user group ID, except for some specific preferences.
$userGroupId = $this->getUserGroupId($user, $name);
@ -122,11 +121,11 @@ class Preferences
Cache::put($key, '', 5);
}
public function setForUser(User $user, string $name, null|array|bool|int|string $value): Preference
public function setForUser(User $user, string $name, null | array | bool | int | string $value): Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
$groupId = $this->getUserGroupId($user, $name);
$groupId = 0 === (int)$groupId ? null : (int) $groupId;
$groupId = 0 === (int) $groupId ? null : (int) $groupId;
Cache::forget($fullName);
@ -179,8 +178,7 @@ class Preferences
$q->orWhere('user_group_id', $user->user_group_id);
})
->whereIn('name', $list)
->get(['id', 'name', 'data'])
;
->get(['id', 'name', 'data']);
/** @var Preference $preference */
foreach ($preferences as $preference) {
@ -218,7 +216,7 @@ class Preferences
return $result;
}
public function getEncryptedForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
public function getEncryptedForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
{
$result = $this->getForUser($user, $name, $default);
if ('' === $result->data) {
@ -239,7 +237,7 @@ class Preferences
return $result;
}
public function getFresh(string $name, null|array|bool|int|string $default = null): ?Preference
public function getFresh(string $name, null | array | bool | int | string $default = null): ?Preference
{
/** @var null|User $user */
$user = auth()->user();
@ -277,20 +275,7 @@ class Preferences
Session::forget('first');
}
public function setEncrypted(string $name, mixed $value): Preference
{
try {
$encrypted = encrypt($value);
} catch (EncryptException $e) {
Log::error(sprintf('Could not encrypt preference "%s": %s', $name, $e->getMessage()));
throw new FireflyException(sprintf('Could not encrypt preference "%s". Cowardly refuse to continue.', $name));
}
return $this->set($name, $encrypted);
}
public function set(string $name, null|array|bool|int|string $value): Preference
public function set(string $name, null | array | bool | int | string $value): Preference
{
/** @var null|User $user */
$user = auth()->user();
@ -305,4 +290,17 @@ class Preferences
return $this->setForUser($user, $name, $value);
}
public function setEncrypted(string $name, mixed $value): Preference
{
try {
$encrypted = encrypt($value);
} catch (EncryptException $e) {
Log::error(sprintf('Could not encrypt preference "%s": %s', $name, $e->getMessage()));
throw new FireflyException(sprintf('Could not encrypt preference "%s". Cowardly refuse to continue.', $name));
}
return $this->set($name, $encrypted);
}
}

View File

@ -54,7 +54,7 @@ trait UserGroupTrait
/**
* @throws FireflyException
*/
public function setUser(null|Authenticatable|User $user): void
public function setUser(null | Authenticatable | User $user): void
{
if ($user instanceof User) {
$this->user = $user;
@ -72,8 +72,7 @@ trait UserGroupTrait
{
$memberships = GroupMembership::where('user_id', $this->user->id)
->where('user_group_id', $userGroupId)
->count()
;
->count();
if (0 === $memberships) {
throw new FireflyException(sprintf('User #%d has no access to administration #%d', $this->user->id, $userGroupId));
}

View File

@ -27,4 +27,6 @@ namespace FireflyIII\Support\Request;
/**
* Trait ConvertAPIDataTypes
*/
trait ConvertAPIDataTypes {}
trait ConvertAPIDataTypes
{
}

View File

@ -96,24 +96,6 @@ trait ConvertsDataTypes
return Steam::filterSpaces($string);
}
public function convertIban(string $field): string
{
return Steam::filterSpaces($this->convertString($field));
}
/**
* Return string value.
*/
public function convertString(string $field, string $default = ''): string
{
$entry = $this->get($field);
if (!is_scalar($entry)) {
return $default;
}
return (string) $this->clearString((string) $entry);
}
public function clearString(?string $string): ?string
{
$string = $this->clearStringKeepNewlines($string);
@ -147,12 +129,22 @@ trait ConvertsDataTypes
return trim((string) $string);
}
/**
* Return integer value.
*/
public function convertInteger(string $field): int
public function convertIban(string $field): string
{
return (int) $this->get($field);
return Steam::filterSpaces($this->convertString($field));
}
/**
* Return string value.
*/
public function convertString(string $field, string $default = ''): string
{
$entry = $this->get($field);
if (!is_scalar($entry)) {
return $default;
}
return (string) $this->clearString((string) $entry);
}
/**
@ -161,6 +153,14 @@ trait ConvertsDataTypes
*/
abstract public function get(string $key, mixed $default = null): mixed;
/**
* Return integer value.
*/
public function convertInteger(string $field): int
{
return (int) $this->get($field);
}
/**
* TODO duplicate, see SelectTransactionsRequest
*

View File

@ -63,8 +63,7 @@ class AccountSearch implements GenericSearchInterface
$searchQuery = $this->user->accounts()
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->whereIn('account_types.type', $this->types)
;
->whereIn('account_types.type', $this->types);
$like = sprintf('%%%s%%', $this->query);
$originalQuery = $this->query;
@ -135,7 +134,7 @@ class AccountSearch implements GenericSearchInterface
$this->types = $types;
}
public function setUser(null|Authenticatable|User $user): void
public function setUser(null | Authenticatable | User $user): void
{
if ($user instanceof User) {
$this->user = $user;

View File

@ -57,6 +57,8 @@ use Gdbots\QueryParser\Node\Word;
use Gdbots\QueryParser\QueryParser;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use LogicException;
use TypeError;
/**
* Class OperatorQuerySearch
@ -149,7 +151,7 @@ class OperatorQuerySearch implements SearchInterface
try {
$query1 = $parser->parse($query);
} catch (\LogicException|\TypeError $e) {
} catch (LogicException | TypeError $e) {
app('log')->error($e->getMessage());
app('log')->error(sprintf('Could not parse search: "%s".', $query));

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
@ -32,7 +31,6 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use stdClass;
@ -44,468 +42,72 @@ use ValueError;
*/
class Steam
{
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @deprecated
*/
public function balanceInRangeConverted(Account $account, Carbon $start, Carbon $end, TransactionCurrency $native): array
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
// return null if not in this list.
if (!in_array($type, $list, true)) {
return null;
}
$result = $account->accountMeta->where('name', 'currency_id')->first();
if (null === $result) {
return null;
}
return TransactionCurrency::find((int) $result->data);
}
private function sumTransactions(array $transactions, string $key): string
{
$sum = '0';
/** @var array $transaction */
foreach ($transactions as $transaction) {
$value = (string) ($transaction[$key] ?? '0');
$value = '' === $value ? '0' : $value;
$sum = bcadd($sum, $value);
}
return $sum;
}
public function finalAccountBalanceInRange(Account $account, Carbon $start, Carbon $end): array
{
// expand period.
$start->subDay()->startOfDay();
$end->addDay()->endOfDay();
// set up cache
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range-converted');
$cache->addProperty($native->id);
$cache->addProperty('final-balance-in-range');
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
//return $cache->get();
}
Log::debug(sprintf('balanceInRangeConverted for account #%d to %s', $account->id, $native->code));
$start->subDay();
$end->addDay();
$balances = [];
$formatted = $start->format('Y-m-d');
$currencies = [];
$startBalance = $this->balanceConverted($account, $start, $native); // already converted to native amount
$balances[$formatted] = $startBalance;
Log::debug(sprintf('Start balance on %s is %s', $formatted, $startBalance));
Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__));
$converter = new ExchangeRateConverter();
// not sure why this is happening:
$start->addDay();
// grab all transactions between start and end:
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 23:59:59'))
->orderBy('transaction_journals.date', 'ASC')
->whereNull('transaction_journals.deleted_at')
->get(
[
'transaction_journals.date',
'transactions.transaction_currency_id',
'transactions.amount',
'transactions.foreign_currency_id',
'transactions.foreign_amount',
]
)->toArray();
// loop the set and convert if necessary:
$currentBalance = $startBalance;
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$day = false;
try {
$day = Carbon::parse($transaction['date'], config('app.timezone'));
} catch (InvalidFormatException $e) {
Log::error(sprintf('Could not parse date "%s" in %s: %s', $transaction['date'], __METHOD__, $e->getMessage()));
}
if (false === $day) {
$day = today(config('app.timezone'));
}
$format = $day->format('Y-m-d');
// if the transaction is in the expected currency, change nothing.
if ((int) $transaction['transaction_currency_id'] === $native->id) {
// change the current balance, set it to today, continue the loop.
$currentBalance = bcadd($currentBalance, $transaction['amount']);
$balances[$format] = $currentBalance;
Log::debug(sprintf('%s: transaction in %s, new balance is %s.', $format, $native->code, $currentBalance));
continue;
}
// if foreign currency is in the expected currency, do nothing:
if ((int) $transaction['foreign_currency_id'] === $native->id) {
$currentBalance = bcadd($currentBalance, $transaction['foreign_amount']);
$balances[$format] = $currentBalance;
Log::debug(sprintf('%s: transaction in %s (foreign), new balance is %s.', $format, $native->code, $currentBalance));
continue;
}
// otherwise, convert 'amount' to the necessary currency:
$currencyId = (int) $transaction['transaction_currency_id'];
$currency = $currencies[$currencyId] ?? TransactionCurrency::find($currencyId);
$currencies[$currencyId] = $currency;
$rate = $converter->getCurrencyRate($currency, $native, $day);
$convertedAmount = bcmul($transaction['amount'], $rate);
$currentBalance = bcadd($currentBalance, $convertedAmount);
$balances[$format] = $currentBalance;
Log::debug(sprintf(
'%s: transaction in %s(!). Conversion rate is %s. %s %s = %s %s',
$format,
$currency->code,
$rate,
$currency->code,
$transaction['amount'],
$native->code,
$convertedAmount
));
}
$cache->store($balances);
$converter->summarize();
return $balances;
}
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @deprecated
* selection of transactions
* 1: all normal transactions. No foreign currency info. In $currency. Need conversion.
* 2: all normal transactions. No foreign currency info. In $native. Need NO conversion.
* 3: all normal transactions. No foreign currency info. In neither currency. Need conversion.
* Then, select everything with foreign currency info:
* 4. All transactions with foreign currency info in $native. Normal currency value is ignored. Do not need
* conversion.
* 5. All transactions with foreign currency info NOT in $native, but currency info in $currency. Need conversion.
* 6. All transactions with foreign currency info NOT in $native, and currency info NOT in $currency. Need
* conversion.
*
* Gets balance at the end of current month by default. Returns the balance converted
* to the indicated currency ($native).
*
*/
private function balanceConverted(Account $account, Carbon $date, TransactionCurrency $native): string
{
Log::debug(sprintf('Now in balanceConverted (%s) for account #%d, converting to %s', $date->format('Y-m-d'), $account->id, $native->code));
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-converted');
$cache->addProperty($date);
$cache->addProperty($native->id);
if ($cache->has()) {
Log::debug('Cached!');
// return $cache->get();
}
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account);
$currency = null === $currency ? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup) : $currency;
if ($native->id === $currency->id) {
Log::debug('No conversion necessary!');
return $this->balance($account, $date);
}
$new = [];
$existing = [];
$new[] = $account->transactions() // 1
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', $currency->id)
->whereNull('transactions.foreign_currency_id')
->get(['transaction_journals.date', 'transactions.amount'])->toArray();
Log::debug(sprintf('%d transaction(s) in set #1', count($new[0])));
$existing[] = $account->transactions() // 2
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', $native->id)
->whereNull('transactions.foreign_currency_id')
->get(['transactions.amount'])->toArray();
Log::debug(sprintf('%d transaction(s) in set #2', count($existing[0])));
$new[] = $account->transactions() // 3
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', '!=', $currency->id)
->where('transactions.transaction_currency_id', '!=', $native->id)
->whereNull('transactions.foreign_currency_id')
->get(['transaction_journals.date', 'transactions.amount'])->toArray();
Log::debug(sprintf('%d transactions in set #3', count($new[1])));
$existing[] = $account->transactions() // 4
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.foreign_currency_id', $native->id)
->whereNotNull('transactions.foreign_amount')
->get(['transactions.foreign_amount'])->toArray();
Log::debug(sprintf('%d transactions in set #4', count($existing[1])));
$new[] = $account->transactions()// 5
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', $currency->id)
->where('transactions.foreign_currency_id', '!=', $native->id)
->whereNotNull('transactions.foreign_amount')
->get(['transaction_journals.date', 'transactions.amount'])->toArray();
Log::debug(sprintf('%d transactions in set #5', count($new[2])));
$new[] = $account->transactions()// 6
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', '!=', $currency->id)
->where('transactions.foreign_currency_id', '!=', $native->id)
->whereNotNull('transactions.foreign_amount')
->get(['transaction_journals.date', 'transactions.amount'])->toArray();
Log::debug(sprintf('%d transactions in set #6', count($new[3])));
// process both sets of transactions. Of course, no need to convert set "existing".
$balance = $this->sumTransactions($existing[0], 'amount');
$balance = bcadd($balance, $this->sumTransactions($existing[1], 'foreign_amount'));
Log::debug(sprintf('Balance from set #2 and #4 is %f', $balance));
// need to convert the others. All sets use the "amount" value as their base (that's easy)
// but we need to convert each transaction separately because the date difference may
// incur huge currency changes.
Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__));
$start = clone $date;
$end = clone $date;
$converter = new ExchangeRateConverter();
foreach ($new as $set) {
foreach ($set as $transaction) {
$currentDate = false;
try {
$currentDate = Carbon::parse($transaction['date'], config('app.timezone'));
} catch (InvalidFormatException $e) {
Log::error(sprintf('Could not parse date "%s" in %s', $transaction['date'], __METHOD__));
}
if (false === $currentDate) {
$currentDate = today(config('app.timezone'));
}
if ($currentDate->lte($start)) {
$start = clone $currentDate;
}
}
}
unset($currentDate);
$converter->prepare($currency, $native, $start, $end);
foreach ($new as $set) {
foreach ($set as $transaction) {
$currentDate = false;
try {
$currentDate = Carbon::parse($transaction['date'], config('app.timezone'));
} catch (InvalidFormatException $e) {
Log::error(sprintf('Could not parse date "%s" in %s', $transaction['date'], __METHOD__));
}
if (false === $currentDate) {
$currentDate = today(config('app.timezone'));
}
$rate = $converter->getCurrencyRate($currency, $native, $currentDate);
$convertedAmount = bcmul($transaction['amount'], $rate);
$balance = bcadd($balance, $convertedAmount);
}
}
// add virtual balance (also needs conversion)
$virtual = null === $account->virtual_balance ? '0' : $account->virtual_balance;
$virtual = $converter->convert($currency, $native, $account->created_at, $virtual);
$balance = bcadd($balance, $virtual);
$converter->summarize();
$cache->store($balance);
$converter->summarize();
return $balance;
}
//
/**
* Gets balance at the end of current month by default
*
* @throws FireflyException
* @deprecated
*/
private function balance(Account $account, Carbon $date, ?TransactionCurrency $currency = null): string
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
// abuse chart properties:
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance');
$cache->addProperty($date);
$cache->addProperty(null !== $currency ? $currency->id : 0);
if ($cache->has()) {
return $cache->get();
}
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
if (null === $currency) {
$currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// first part: get all balances in own currency:
$transactions = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', $currency->id)
->get(['transactions.amount'])->toArray();
$nativeBalance = $this->sumTransactions($transactions, 'amount');
// get all balances in foreign currency:
$transactions = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.foreign_currency_id', $currency->id)
->where('transactions.transaction_currency_id', '!=', $currency->id)
->get(['transactions.foreign_amount'])->toArray();
$foreignBalance = $this->sumTransactions($transactions, 'foreign_amount');
$balance = bcadd($nativeBalance, $foreignBalance);
$virtual = null === $account->virtual_balance ? '0' : $account->virtual_balance;
$balance = bcadd($balance, $virtual);
$cache->store($balance);
return $balance;
}
/**
* @throws FireflyException
* @deprecated
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function balanceInRangeNative(Account $account, Carbon $start, Carbon $end): array
{
$native = app('amount')->getDefaultCurrency();
Log::debug(sprintf('balanceInRangeNative for account #%d, to %s', $account->id, $native->code));
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($account->user);
$currency = $repository->getAccountCurrency($account) ?? $native;
if ($native->id === $currency->id) {
Log::debug('No need to get native balance, account prefers this currency.');
return $this->balanceInRange($account, $start, $end, $native);
}
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range-native');
$cache->addProperty($native->id);
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
$value = $cache->get();
Log::debug('Return cached values');
//return $value;
}
$start->subDay();
$end->addDay();
$balances = [];
$formatted = $start->format('Y-m-d');
$startBalance = $this->balanceNative($account, $start); // already converted to native amount
$startBalance = $this->finalAccountBalance($account, $start);
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$currency = $this->getAccountCurrency($account) ?? $defaultCurrency;
$currencies = [
$currency->id => $currency,
$defaultCurrency->id => $defaultCurrency,
];
$startBalance[$defaultCurrency->code] ??= '0';
$startBalance[$currency->code] ??= '0';
$balances[$formatted] = $startBalance;
Log::debug(sprintf('Start balance on %s is %s', $formatted, $startBalance));
// not sure why this is happening:
$start->addDay();
// grab all transactions between start and end:
// sums up the balance changes per day, for foreign, native and normal amounts.
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 23:59:59'))
->orderBy('transaction_journals.date', 'ASC')
->whereNull('transaction_journals.deleted_at')
->get(
[
'transaction_journals.date',
'transactions.transaction_currency_id',
'transactions.amount',
'transactions.native_amount',
'transactions.foreign_currency_id',
'transactions.foreign_amount',
]
)->toArray();
// loop the set
$currentBalance = $startBalance;
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$day = false;
try {
$day = Carbon::parse($transaction['date'], config('app.timezone'));
} catch (InvalidFormatException $e) {
Log::error(sprintf('Could not parse date "%s" in %s: %s', $transaction['date'], __METHOD__, $e->getMessage()));
}
if (false === $day) {
$day = today(config('app.timezone'));
}
$format = $day->format('Y-m-d');
// first, check the native amount. If not NULL, add it, and continue.
if (null !== $transaction['native_amount']) {
$currentBalance = bcadd($currentBalance, $transaction['native_amount']);
$balances[$format] = $currentBalance;
Log::debug(sprintf('%s: transaction in %s (native), new balance is %s.', $format, $native->code, $currentBalance));
continue;
}
// if the foreign amount is in the native currency, add it and continue.
if ((int) $transaction['foreign_currency_id'] === $native->id) {
$currentBalance = bcadd($currentBalance, $transaction['foreign_amount']);
$balances[$format] = $currentBalance;
Log::debug(sprintf('%s: transaction in %s (foreign), new balance is %s.', $format, $native->code, $currentBalance));
continue;
}
// anything else is added as is. Warning in logs.
Log::warning(sprintf('Account "%s" (#%d) has transactions that are not converted in the native currency. Please run "php artisan firefly-iii:recalculate-native-amounts"', $account->name, $account->id));
$currentBalance = bcadd($currentBalance, $transaction['amount']);
$balances[$format] = $currentBalance;
Log::debug(sprintf('%s: transaction BAD currency, new balance is %s.', $format, $currentBalance));
}
$cache->store($balances);
return $balances;
}
/**
* Gets the balance for the given account during the whole range, using this format:.
*
* [yyyy-mm-dd] => 123,2
*
* @throws FireflyException
* @deprecated
*/
public function balanceInRange(Account $account, Carbon $start, Carbon $end, ?TransactionCurrency $currency = null): array
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range');
$cache->addProperty(null !== $currency ? $currency->id : 0);
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
return $cache->get();
}
$start->subDay();
$end->addDay();
$balances = [];
$formatted = $start->format('Y-m-d');
$startBalance = $this->balance($account, $start, $currency);
$balances[$formatted] = $startBalance;
if (null === $currency) {
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($account->user);
$currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
$currencyId = $currency->id;
$start->addDay();
// query!
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 23:59:59'))
->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
->groupBy('transaction_journals.date')
->groupBy('transactions.transaction_currency_id')
->groupBy('transactions.foreign_currency_id')
@ -518,6 +120,7 @@ class Steam
DB::raw('SUM(transactions.amount) AS modified'),
'transactions.foreign_currency_id',
DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
DB::raw('SUM(transactions.native_amount) AS modified_native'),
]
);
@ -525,235 +128,43 @@ class Steam
/** @var Transaction $entry */
foreach ($set as $entry) {
// normal amount and foreign amount
// normal, native and foreign amount
$carbon = new Carbon($entry->date, $entry->date_tz);
$modified = (string) (null === $entry->modified ? '0' : $entry->modified);
$foreignModified = (string) (null === $entry->modified_foreign ? '0' : $entry->modified_foreign);
$amount = '0';
if ($currencyId === (int) $entry->transaction_currency_id || 0 === $currencyId) {
// use normal amount:
$amount = $modified;
}
if ($currencyId === (int) $entry->foreign_currency_id) {
// use foreign amount:
$amount = $foreignModified;
}
// Log::debug(sprintf('Trying to add %s and %s.', var_export($currentBalance, true), var_export($amount, true)));
$currentBalance = bcadd($currentBalance, $amount);
$carbon = new Carbon($entry->date, config('app.timezone'));
$date = $carbon->format('Y-m-d');
$balances[$date] = $currentBalance;
$nativeModified = (string) (null === $entry->modified_native ? '0' : $entry->modified_native);
// add "modified" to amount if the currency id matches the account currency id.
if ($entry->transaction_currency_id === $currency->id) {
$currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
$currentBalance[$currency->code] = bcadd($currentBalance[$currency->code], $modified);
}
// always add the native balance, even if it ends up at zero.
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
// add modified foreign to the array
if (null !== $entry->foreign_currency_id) {
$foreignId = $entry->foreign_currency_id;
$currencies[$foreignId] ??= TransactionCurrency::find($foreignId);
$foreignCurrency = $currencies[$foreignId];
$currentBalance[$foreignCurrency->code] ??= '0';
$currentBalance[$foreignCurrency->code] = bcadd($currentBalance[$foreignCurrency->code], $foreignModified);
}
$balances[$carbon->format('Y-m-d')] = $currentBalance;
}
$cache->store($balances);
return $balances;
}
/**
* Balance of an (asset) account in the user's native currency.
* Is calculated by summing up three numbers.
*
* - Transactions in foreign amount that happen to be in the native currency.
* - The rest of the transactions in the native currency.
* - Where both are zero or NULL, the normal amount converted (and stored!)
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @deprecated
*/
private function balanceNative(Account $account, Carbon $date): string
{
$native = app('amount')->getDefaultCurrency();
Log::debug(sprintf('Now in balanceNative (%s) for account #%d, converting to %s', $date->format('Y-m-d'), $account->id, $native->code));
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-native');
$cache->addProperty($date);
$cache->addProperty($native->id);
if ($cache->has()) {
$value = $cache->get();
Log::debug(sprintf('Return cached value %s', $value));
return $value;
}
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account);
$currency = null === $currency ? $native : $currency;
if ($native->id === $currency->id) {
Log::debug('No conversion necessary!');
return $this->balance($account, $date);
}
$balance = '0';
// transactions in foreign amount that happen to be in the native currency:
$set = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.foreign_currency_id', $native->id)
->get(['transactions.foreign_amount'])->toArray();
$balance = bcadd($this->sumTransactions($set, 'foreign_amount'), $balance);
Log::debug(sprintf('The balance is now %s', $balance));
// transactions in the native amount.
$set = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->whereNull('transactions.foreign_currency_id')
->whereNotNull('transactions.native_amount')
->get(['transactions.native_amount'])->toArray();
$balance = bcadd($this->sumTransactions($set, 'native_amount'), $balance);
Log::debug(sprintf('The balance is now %s', $balance));
// transactions in the normal amount with no native amount set.
$set = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->whereNull('transactions.foreign_currency_id')
->whereNull('transactions.native_amount')
->get(['transactions.amount'])->toArray();
$balance = bcadd($this->sumTransactions($set, 'amount'), $balance);
Log::debug(sprintf('The balance is now %s', $balance));
// add virtual balance (also needs conversion)
$virtualNative = null === $account->native_virtual_balance ? '0' : $account->native_virtual_balance;
$final = bcadd($virtualNative, $balance);
Log::debug(sprintf('Final balance is %s', $final));
$cache->store($final);
return $final;
}
/**
* This method always ignores the virtual balance.
*
* @throws FireflyException
* @deprecated
*/
public function balancesByAccounts(Collection $accounts, Carbon $date): array
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties();
$cache->addProperty($ids);
$cache->addProperty('balances');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
}
// need to do this per account.
$result = [];
/** @var Account $account */
public function finalAccountsBalance(Collection $accounts, Carbon $date): array {
$balances = [];
foreach ($accounts as $account) {
$result[$account->id] = $this->balance($account, $date);
$balances[$account->id] = $this->finalAccountBalance($account, $date);
}
$cache->store($result);
return $result;
}
/**
* This method always ignores the virtual balance.
*
* @throws FireflyException
* @deprecated
*/
public function balancesByAccountsConverted(Collection $accounts, Carbon $date): array
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties();
$cache->addProperty($ids);
$cache->addProperty('balances-converted');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
}
// need to do this per account.
$result = [];
/** @var Account $account */
foreach ($accounts as $account) {
$default = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$result[$account->id]
= [
'balance' => $this->balance($account, $date),
'native_balance' => $this->balanceConverted($account, $date, $default),
];
}
$cache->store($result);
return $result;
}
/**
* Same as above, but also groups per currency.
*
* @deprecated
*/
public function balancesPerCurrencyByAccounts(Collection $accounts, Carbon $date): array
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties();
$cache->addProperty($ids);
$cache->addProperty('balances-per-currency');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
}
// need to do this per account.
$result = [];
/** @var Account $account */
foreach ($accounts as $account) {
$result[$account->id] = $this->balancePerCurrency($account, $date);
}
$cache->store($result);
return $result;
}
/**
* @param Account $account
* @param Carbon $date
*
* @return array
* @deprecated
*/
private function balancePerCurrency(Account $account, Carbon $date): array
{
// Log::warning(sprintf('Deprecated method %s, do not use.', __METHOD__));
// abuse chart properties:
$cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('balance-per-currency');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
}
$query = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->groupBy('transactions.transaction_currency_id');
$balances = $query->get(['transactions.transaction_currency_id', DB::raw('SUM(transactions.amount) as sum_for_currency')]); // @phpstan-ignore-line
$return = [];
/** @var stdClass $entry */
foreach ($balances as $entry) {
$return[(int) $entry->transaction_currency_id] = (string) $entry->sum_for_currency;
}
$cache->store($return);
return $return;
return $balances;
}
/**
@ -899,36 +310,6 @@ class Steam
return array_merge($return, $others);
}
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
// return null if not in this list.
if (!in_array($type, $list, true)) {
return null;
}
$result = $account->accountMeta->where('name', 'currency_id')->first();
if (null === $result) {
return null;
}
return TransactionCurrency::find((int) $result->data);
}
private function sumTransactions(array $transactions, string $key): string
{
$sum = '0';
/** @var array $transaction */
foreach ($transactions as $transaction) {
$value = (string) ($transaction[$key] ?? '0');
$value = '' === $value ? '0' : $value;
$sum = bcadd($sum, $value);
}
return $sum;
}
private function groupAndSumTransactions(array $array, string $group, string $field): array
{
$return = [];

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Support\System;
use Artisan;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\DecryptException;
use Laravel\Passport\Console\KeysCommand;
@ -65,7 +67,7 @@ class OAuthKeys
try {
$privateKey = (string) app('fireflyconfig')->get(self::PRIVATE_KEY)?->data;
$publicKey = (string) app('fireflyconfig')->get(self::PUBLIC_KEY)?->data;
} catch (ContainerExceptionInterface|FireflyException|NotFoundExceptionInterface $e) {
} catch (ContainerExceptionInterface | FireflyException | NotFoundExceptionInterface $e) {
app('log')->error(sprintf('Could not validate keysInDatabase(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
}
@ -87,16 +89,16 @@ class OAuthKeys
public static function generateKeys(): void
{
\Artisan::registerCommand(new KeysCommand());
\Artisan::call('firefly-iii:laravel-passport-keys');
Artisan::registerCommand(new KeysCommand());
Artisan::call('firefly-iii:laravel-passport-keys');
}
public static function storeKeysInDB(): void
{
$private = storage_path('oauth-private.key');
$public = storage_path('oauth-public.key');
app('fireflyconfig')->set(self::PRIVATE_KEY, \Crypt::encrypt(file_get_contents($private)));
app('fireflyconfig')->set(self::PUBLIC_KEY, \Crypt::encrypt(file_get_contents($public)));
app('fireflyconfig')->set(self::PRIVATE_KEY, Crypt::encrypt(file_get_contents($private)));
app('fireflyconfig')->set(self::PUBLIC_KEY, Crypt::encrypt(file_get_contents($public)));
}
/**
@ -108,8 +110,8 @@ class OAuthKeys
$publicKey = (string) app('fireflyconfig')->get(self::PUBLIC_KEY)?->data;
try {
$privateContent = \Crypt::decrypt($privateKey);
$publicContent = \Crypt::decrypt($publicKey);
$privateContent = Crypt::decrypt($privateKey);
$publicContent = Crypt::decrypt($publicKey);
} catch (DecryptException $e) {
app('log')->error('Could not decrypt pub/private keypair.');
app('log')->error($e->getMessage());

View File

@ -82,7 +82,7 @@ class General extends AbstractExtension
}
if ('native_balance' === $key) {
// balance in native currency.
if($convertToNative) {
if ($convertToNative) {
$strings[] = app('amount')->formatAnything($native, $balance, false);
}
continue;
@ -282,7 +282,7 @@ class General extends AbstractExtension
$args = func_get_args();
$route = $args[0]; // name of the route.
if (\Route::getCurrentRoute()->getName() === $route) {
if (Route::getCurrentRoute()->getName() === $route) {
return 'active';
}
@ -302,7 +302,7 @@ class General extends AbstractExtension
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = \Route::getCurrentRoute()->getName() ?? '';
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'active';
}
@ -326,7 +326,7 @@ class General extends AbstractExtension
if ($objectType === $activeObjectType
&& false !== stripos(
\Route::getCurrentRoute()->getName(),
Route::getCurrentRoute()->getName(),
$route
)) {
return 'active';
@ -349,7 +349,7 @@ class General extends AbstractExtension
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = \Route::getCurrentRoute()->getName() ?? '';
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'menu-open';
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Config;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@ -63,7 +64,7 @@ class Rule extends AbstractExtension
$possibleTriggers = [];
foreach ($ruleTriggers as $key) {
if ('user_action' !== $key) {
$possibleTriggers[$key] = (string) trans('firefly.rule_trigger_'.$key.'_choice');
$possibleTriggers[$key] = (string) trans('firefly.rule_trigger_' . $key . '_choice');
}
}
unset($ruleTriggers);
@ -80,10 +81,10 @@ class Rule extends AbstractExtension
'allRuleActions',
static function () {
// array of valid values for actions
$ruleActions = array_keys(\Config::get('firefly.rule-actions'));
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
$possibleActions = [];
foreach ($ruleActions as $key) {
$possibleActions[$key] = (string) trans('firefly.rule_action_'.$key.'_choice');
$possibleActions[$key] = (string) trans('firefly.rule_action_' . $key . '_choice');
}
unset($ruleActions);
asort($possibleActions);

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
@ -223,12 +224,11 @@ class TransactionGroupTwig extends AbstractExtension
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = \DB::table('journal_meta')
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count()
;
->count();
return 1 === $count;
}
@ -241,12 +241,11 @@ class TransactionGroupTwig extends AbstractExtension
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
/** @var null|TransactionJournalMeta $entry */
$entry = \DB::table('journal_meta')
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first()
;
->first();
if (null === $entry) {
return today(config('app.timezone'));
}
@ -262,12 +261,11 @@ class TransactionGroupTwig extends AbstractExtension
'journalGetMetaField',
static function (int $journalId, string $metaField) {
/** @var null|TransactionJournalMeta $entry */
$entry = \DB::table('journal_meta')
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first()
;
->first();
if (null === $entry) {
return '';
}

View File

@ -115,7 +115,7 @@ class AccountTransformer extends AbstractTransformer
private function getMetaBalances(Collection $accounts): void
{
try {
$this->convertedBalances = app('steam')->balancesByAccountsConverted($accounts, $this->getDate());
$this->convertedBalances = app('steam')->finalAccountsBalance($accounts, $this->getDate());
} catch (FireflyException $e) {
Log::error($e->getMessage());
}
@ -172,14 +172,15 @@ class AccountTransformer extends AbstractTransformer
private function getBalanceDifference(Collection $accounts, Carbon $start, Carbon $end): void
{
throw new FireflyException('Used deprecated method, rethink this.');
// collect balances, start and end for both native and converted.
// 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);
$bStart = app('steam')->finalAccountsBalance($accounts, $start);
$bEnd = app('steam')->finalAccountsBalance($accounts, $end);
} catch (FireflyException $e) {
Log::error($e->getMessage());
}