mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Collect account balances, optimized.
This commit is contained in:
@@ -157,6 +157,10 @@ class DebugController extends Controller
|
|||||||
return view('debug', compact('table', 'now', 'logContent'));
|
return view('debug', compact('table', 'now', 'logContent'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function apiTest() {
|
||||||
|
return view('test.api-test');
|
||||||
|
}
|
||||||
|
|
||||||
private function generateTable(): string
|
private function generateTable(): string
|
||||||
{
|
{
|
||||||
// system information:
|
// system information:
|
||||||
|
@@ -51,8 +51,8 @@ use Override;
|
|||||||
*/
|
*/
|
||||||
class AccountEnrichment implements EnrichmentInterface
|
class AccountEnrichment implements EnrichmentInterface
|
||||||
{
|
{
|
||||||
private array $accountIds;
|
private array $ids;
|
||||||
private array $accountTypeIds;
|
private array $accountTypeIds;
|
||||||
private array $accountTypes;
|
private array $accountTypes;
|
||||||
private Collection $collection;
|
private Collection $collection;
|
||||||
private array $currencies;
|
private array $currencies;
|
||||||
@@ -66,14 +66,15 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
private array $lastActivities;
|
private array $lastActivities;
|
||||||
private ?Carbon $date = null;
|
private ?Carbon $date = null;
|
||||||
private bool $convertToPrimary = false;
|
private bool $convertToPrimary = false;
|
||||||
|
private array $balances = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO The account enricher must do conversion from and to the primary currency.
|
* TODO The account enricher must do conversion from and to the primary currency.
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->accountIds = [];
|
$this->ids = [];
|
||||||
$this->openingBalances = [];
|
$this->openingBalances = [];
|
||||||
$this->currencies = [];
|
$this->currencies = [];
|
||||||
$this->accountTypeIds = [];
|
$this->accountTypeIds = [];
|
||||||
$this->accountTypes = [];
|
$this->accountTypes = [];
|
||||||
@@ -105,7 +106,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
// prep local fields
|
// prep local fields
|
||||||
$this->collection = $collection;
|
$this->collection = $collection;
|
||||||
$this->collectAccountIds();
|
$this->collectIds();
|
||||||
$this->getAccountTypes();
|
$this->getAccountTypes();
|
||||||
$this->collectMetaData();
|
$this->collectMetaData();
|
||||||
$this->collectNotes();
|
$this->collectNotes();
|
||||||
@@ -118,14 +119,14 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
return $this->collection;
|
return $this->collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectAccountIds(): void
|
private function collectIds(): void
|
||||||
{
|
{
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
foreach ($this->collection as $account) {
|
foreach ($this->collection as $account) {
|
||||||
$this->accountIds[] = (int) $account->id;
|
$this->ids[] = (int) $account->id;
|
||||||
$this->accountTypeIds[] = (int) $account->account_type_id;
|
$this->accountTypeIds[] = (int) $account->account_type_id;
|
||||||
}
|
}
|
||||||
$this->accountIds = array_unique($this->accountIds);
|
$this->ids = array_unique($this->ids);
|
||||||
$this->accountTypeIds = array_unique($this->accountTypeIds);
|
$this->accountTypeIds = array_unique($this->accountTypeIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +143,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
private function collectMetaData(): void
|
private function collectMetaData(): void
|
||||||
{
|
{
|
||||||
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
|
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
|
||||||
->whereIn('account_id', $this->accountIds)
|
->whereIn('account_id', $this->ids)
|
||||||
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray()
|
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray()
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -167,7 +168,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
private function collectNotes(): void
|
private function collectNotes(): void
|
||||||
{
|
{
|
||||||
$notes = Note::query()->whereIn('noteable_id', $this->accountIds)
|
$notes = Note::query()->whereIn('noteable_id', $this->ids)
|
||||||
->whereNotNull('notes.text')
|
->whereNotNull('notes.text')
|
||||||
->where('notes.text', '!=', '')
|
->where('notes.text', '!=', '')
|
||||||
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
|
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
|
||||||
@@ -180,7 +181,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
private function collectLocations(): void
|
private function collectLocations(): void
|
||||||
{
|
{
|
||||||
$locations = Location::query()->whereIn('locatable_id', $this->accountIds)
|
$locations = Location::query()->whereIn('locatable_id', $this->ids)
|
||||||
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray()
|
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray()
|
||||||
;
|
;
|
||||||
foreach ($locations as $location) {
|
foreach ($locations as $location) {
|
||||||
@@ -234,13 +235,10 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
private function appendCollectedData(): void
|
private function appendCollectedData(): void
|
||||||
{
|
{
|
||||||
$notes = $this->notes;
|
$this->collection = $this->collection->map(function (Account $item) {
|
||||||
$openingBalances = $this->openingBalances;
|
$id = (int) $item->id;
|
||||||
$locations = $this->locations;
|
|
||||||
$lastActivities = $this->lastActivities;
|
|
||||||
$this->collection = $this->collection->map(function (Account $item) use ($notes, $openingBalances, $locations, $lastActivities) {
|
|
||||||
$item->full_account_type = $this->accountTypes[(int) $item->account_type_id] ?? null;
|
$item->full_account_type = $this->accountTypes[(int) $item->account_type_id] ?? null;
|
||||||
$accountMeta = [
|
$meta = [
|
||||||
'currency' => null,
|
'currency' => null,
|
||||||
'location' => [
|
'location' => [
|
||||||
'latitude' => null,
|
'latitude' => null,
|
||||||
@@ -248,45 +246,50 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
'zoom_level' => null,
|
'zoom_level' => null,
|
||||||
],
|
],
|
||||||
'opening_balance_date' => null,
|
'opening_balance_date' => null,
|
||||||
|
'opening_balance_amount' => null,
|
||||||
|
'account_number'=> null,
|
||||||
|
'notes' => $notes[$id] ?? null,
|
||||||
|
'last_activity' => $this->lastActivities[$id] ?? null,
|
||||||
];
|
];
|
||||||
if (array_key_exists((int) $item->id, $this->meta)) {
|
|
||||||
foreach ($this->meta[(int) $item->id] as $name => $value) {
|
// if location, add location:
|
||||||
$accountMeta[$name] = $value;
|
if (array_key_exists($id, $this->locations)) {
|
||||||
|
$meta['location'] = $this->locations[$id];
|
||||||
|
}
|
||||||
|
if (array_key_exists($id, $this->meta)) {
|
||||||
|
foreach ($this->meta[$id] as $name => $value) {
|
||||||
|
$meta[$name] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// also add currency, if present.
|
// also add currency, if present.
|
||||||
if (array_key_exists('currency_id', $accountMeta)) {
|
if (array_key_exists('currency_id', $meta)) {
|
||||||
$currencyId = (int) $accountMeta['currency_id'];
|
$currencyId = (int) $meta['currency_id'];
|
||||||
$accountMeta['currency'] = $this->currencies[$currencyId];
|
$meta['currency'] = $this->currencies[$currencyId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// if notes, add notes.
|
if (array_key_exists($id, $this->openingBalances)) {
|
||||||
if (array_key_exists($item->id, $notes)) {
|
$meta['opening_balance_date'] = $this->openingBalances[$id]['date'];
|
||||||
$accountMeta['notes'] = $notes[$item->id];
|
$meta['opening_balance_amount'] = $this->openingBalances[$id]['amount'];
|
||||||
}
|
|
||||||
// if opening balance, add opening balance
|
|
||||||
if (array_key_exists($item->id, $openingBalances)) {
|
|
||||||
$accountMeta['opening_balance_date'] = $openingBalances[$item->id]['date'];
|
|
||||||
$accountMeta['opening_balance_amount'] = $openingBalances[$item->id]['amount'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add balances
|
// add balances
|
||||||
// get currencies:
|
// get currencies:
|
||||||
$currency = $this->primaryCurrency; // assume primary currency
|
$currency = $this->primaryCurrency; // assume primary currency
|
||||||
if (null !== $accountMeta['currency']) {
|
if (null !== $meta['currency']) {
|
||||||
$currency = $accountMeta['currency'];
|
$currency = $meta['currency'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the current balance:
|
// get the current balance:
|
||||||
$date = $this->getDate();
|
$date = $this->getDate();
|
||||||
$finalBalance = Steam::finalAccountBalance($item, $date, $this->primaryCurrency, $this->convertToPrimary);
|
//$finalBalance = Steam::finalAccountBalance($item, $date, $this->primaryCurrency, $this->convertToPrimary);
|
||||||
|
$finalBalance = $this->balances[$id];
|
||||||
Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance);
|
Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance);
|
||||||
|
|
||||||
// collect current balances:
|
// collect current balances:
|
||||||
$currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places);
|
$currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places);
|
||||||
$openingBalance = Steam::bcround($accountMeta['opening_balance_amount'] ?? '0', $currency->decimal_places);
|
$openingBalance = Steam::bcround($meta['opening_balance_amount'] ?? '0', $currency->decimal_places);
|
||||||
$virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places);
|
$virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places);
|
||||||
$debtAmount = $accountMeta['current_debt'] ?? null;
|
$debtAmount = $meta['current_debt'] ?? null;
|
||||||
|
|
||||||
// set some pc_ default values to NULL:
|
// set some pc_ default values to NULL:
|
||||||
$pcCurrentBalance = null;
|
$pcCurrentBalance = null;
|
||||||
@@ -311,11 +314,11 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set opening balance(s) to NULL if the date is null
|
// set opening balance(s) to NULL if the date is null
|
||||||
if (null === $accountMeta['opening_balance_date']) {
|
if (null === $meta['opening_balance_date']) {
|
||||||
$openingBalance = null;
|
$openingBalance = null;
|
||||||
$pcOpeningBalance = null;
|
$pcOpeningBalance = null;
|
||||||
}
|
}
|
||||||
$accountMeta['balances'] = [
|
$meta['balances'] = [
|
||||||
'current_balance' => $currentBalance,
|
'current_balance' => $currentBalance,
|
||||||
'pc_current_balance' => $pcCurrentBalance,
|
'pc_current_balance' => $pcCurrentBalance,
|
||||||
'opening_balance' => $openingBalance,
|
'opening_balance' => $openingBalance,
|
||||||
@@ -326,16 +329,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
'pc_debt_amount' => $pcDebtAmount,
|
'pc_debt_amount' => $pcDebtAmount,
|
||||||
];
|
];
|
||||||
// end add balances
|
// end add balances
|
||||||
|
$item->meta = $meta;
|
||||||
|
|
||||||
// if location, add location:
|
|
||||||
if (array_key_exists($item->id, $locations)) {
|
|
||||||
$accountMeta['location'] = $locations[$item->id];
|
|
||||||
}
|
|
||||||
if (array_key_exists($item->id, $lastActivities)) {
|
|
||||||
$accountMeta['last_activity'] = $lastActivities[$item->id];
|
|
||||||
}
|
|
||||||
$item->meta = $accountMeta;
|
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
});
|
});
|
||||||
@@ -343,10 +337,12 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
private function collectLastActivities(): void
|
private function collectLastActivities(): void
|
||||||
{
|
{
|
||||||
$this->lastActivities = Steam::getLastActivities($this->accountIds);
|
$this->lastActivities = Steam::getLastActivities($this->ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectBalances(): void {}
|
private function collectBalances(): void {
|
||||||
|
$this->balances = Steam::finalAccountsBalanceOptimized($this->collection, $this->getDate(), $this->primaryCurrency, $this->convertToPrimary);
|
||||||
|
}
|
||||||
|
|
||||||
public function setDate(?Carbon $date): void
|
public function setDate(?Carbon $date): void
|
||||||
{
|
{
|
||||||
|
@@ -24,8 +24,10 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Support;
|
namespace FireflyIII\Support;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\AccountMeta;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Support\Facades\Amount;
|
use FireflyIII\Support\Facades\Amount;
|
||||||
@@ -34,11 +36,9 @@ use Illuminate\Support\Collection;
|
|||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Exception;
|
|
||||||
use ValueError;
|
use ValueError;
|
||||||
|
|
||||||
use function Safe\preg_replace;
|
|
||||||
use function Safe\parse_url;
|
use function Safe\parse_url;
|
||||||
|
use function Safe\preg_replace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Steam.
|
* Class Steam.
|
||||||
@@ -64,10 +64,10 @@ class Steam
|
|||||||
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
|
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
|
||||||
if (str_contains($number, '.')) {
|
if (str_contains($number, '.')) {
|
||||||
if ('-' !== $number[0]) {
|
if ('-' !== $number[0]) {
|
||||||
return bcadd($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bcsub($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $number;
|
return $number;
|
||||||
@@ -203,7 +203,7 @@ class Steam
|
|||||||
Log::debug(sprintf('finalAccountBalanceInRange(#%d, %s, %s)', $account->id, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('finalAccountBalanceInRange(#%d, %s, %s)', $account->id, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
||||||
|
|
||||||
// set up cache
|
// set up cache
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
$cache->addProperty('final-balance-in-range');
|
$cache->addProperty('final-balance-in-range');
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
@@ -213,21 +213,21 @@ class Steam
|
|||||||
return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$balances = [];
|
$balances = [];
|
||||||
$formatted = $start->format('Y-m-d');
|
$formatted = $start->format('Y-m-d');
|
||||||
/*
|
/*
|
||||||
* To make sure the start balance is correct, we need to get the balance at the exact end of the previous day.
|
* To make sure the start balance is correct, we need to get the balance at the exact end of the previous day.
|
||||||
* Since we just did "startOfDay" we can do subDay()->endOfDay() to get the correct moment.
|
* Since we just did "startOfDay" we can do subDay()->endOfDay() to get the correct moment.
|
||||||
* THAT will be the start balance.
|
* THAT will be the start balance.
|
||||||
*/
|
*/
|
||||||
$request = clone $start;
|
$request = clone $start;
|
||||||
$request->subDay()->endOfDay();
|
$request->subDay()->endOfDay();
|
||||||
Log::debug(sprintf('finalAccountBalanceInRange: Call finalAccountBalance with date/time "%s"', $request->toIso8601String()));
|
Log::debug(sprintf('finalAccountBalanceInRange: Call finalAccountBalance with date/time "%s"', $request->toIso8601String()));
|
||||||
$startBalance = $this->finalAccountBalance($account, $request);
|
$startBalance = $this->finalAccountBalance($account, $request);
|
||||||
$primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
|
$primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
|
||||||
$accountCurrency = $this->getAccountCurrency($account);
|
$accountCurrency = $this->getAccountCurrency($account);
|
||||||
$hasCurrency = $accountCurrency instanceof TransactionCurrency;
|
$hasCurrency = $accountCurrency instanceof TransactionCurrency;
|
||||||
$currency = $accountCurrency ?? $primaryCurrency;
|
$currency = $accountCurrency ?? $primaryCurrency;
|
||||||
Log::debug(sprintf('Currency is %s', $currency->code));
|
Log::debug(sprintf('Currency is %s', $currency->code));
|
||||||
|
|
||||||
|
|
||||||
@@ -240,7 +240,7 @@ class Steam
|
|||||||
Log::debug(sprintf('Also set start balance in %s', $primaryCurrency->code));
|
Log::debug(sprintf('Also set start balance in %s', $primaryCurrency->code));
|
||||||
$startBalance[$primaryCurrency->code] ??= '0';
|
$startBalance[$primaryCurrency->code] ??= '0';
|
||||||
}
|
}
|
||||||
$currencies = [
|
$currencies = [
|
||||||
$currency->id => $currency,
|
$currency->id => $currency,
|
||||||
$primaryCurrency->id => $primaryCurrency,
|
$primaryCurrency->id => $primaryCurrency,
|
||||||
];
|
];
|
||||||
@@ -250,66 +250,65 @@ class Steam
|
|||||||
|
|
||||||
// sums up the balance changes per day.
|
// sums up the balance changes per day.
|
||||||
Log::debug(sprintf('Date >= %s and <= %s', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('Date >= %s and <= %s', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
||||||
$set = $account->transactions()
|
$set = $account->transactions()
|
||||||
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
|
||||||
->groupBy('transaction_journals.date')
|
->groupBy('transaction_journals.date')
|
||||||
->groupBy('transactions.transaction_currency_id')
|
->groupBy('transactions.transaction_currency_id')
|
||||||
->orderBy('transaction_journals.date', 'ASC')
|
->orderBy('transaction_journals.date', 'ASC')
|
||||||
->whereNull('transaction_journals.deleted_at')
|
->whereNull('transaction_journals.deleted_at')
|
||||||
->get(
|
->get(
|
||||||
[ // @phpstan-ignore-line
|
[ // @phpstan-ignore-line
|
||||||
'transaction_journals.date',
|
'transaction_journals.date',
|
||||||
'transactions.transaction_currency_id',
|
'transactions.transaction_currency_id',
|
||||||
DB::raw('SUM(transactions.amount) AS sum_of_day'),
|
DB::raw('SUM(transactions.amount) AS sum_of_day'),
|
||||||
]
|
]
|
||||||
)
|
);
|
||||||
;
|
|
||||||
|
|
||||||
$currentBalance = $startBalance;
|
$currentBalance = $startBalance;
|
||||||
$converter = new ExchangeRateConverter();
|
$converter = new ExchangeRateConverter();
|
||||||
|
|
||||||
|
|
||||||
/** @var Transaction $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
// get date object
|
// get date object
|
||||||
$carbon = new Carbon($entry->date, $entry->date_tz);
|
$carbon = new Carbon($entry->date, $entry->date_tz);
|
||||||
$carbonKey = $carbon->format('Y-m-d');
|
$carbonKey = $carbon->format('Y-m-d');
|
||||||
// make sure sum is a string:
|
// make sure sum is a string:
|
||||||
$sumOfDay = (string) ($entry->sum_of_day ?? '0');
|
$sumOfDay = (string)($entry->sum_of_day ?? '0');
|
||||||
// #10426 make sure sum is not in scientific notation.
|
// #10426 make sure sum is not in scientific notation.
|
||||||
$sumOfDay = $this->floatalize($sumOfDay);
|
$sumOfDay = $this->floatalize($sumOfDay);
|
||||||
|
|
||||||
// find currency of this entry, does not have to exist.
|
// find currency of this entry, does not have to exist.
|
||||||
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
|
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
|
||||||
|
|
||||||
// make sure this $entry has its own $entryCurrency
|
// make sure this $entry has its own $entryCurrency
|
||||||
/** @var TransactionCurrency $entryCurrency */
|
/** @var TransactionCurrency $entryCurrency */
|
||||||
$entryCurrency = $currencies[$entry->transaction_currency_id];
|
$entryCurrency = $currencies[$entry->transaction_currency_id];
|
||||||
|
|
||||||
Log::debug(sprintf('Processing transaction(s) on moment %s', $carbon->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('Processing transaction(s) on moment %s', $carbon->format('Y-m-d H:i:s')));
|
||||||
|
|
||||||
// add amount to current balance in currency code.
|
// add amount to current balance in currency code.
|
||||||
$currentBalance[$entryCurrency->code] ??= '0';
|
$currentBalance[$entryCurrency->code] ??= '0';
|
||||||
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, (string) $currentBalance[$entryCurrency->code]);
|
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, (string)$currentBalance[$entryCurrency->code]);
|
||||||
|
|
||||||
// if not requested to convert to primary currency, add the amount to "balance", do nothing else.
|
// if not requested to convert to primary currency, add the amount to "balance", do nothing else.
|
||||||
if (!$convertToPrimary) {
|
if (!$convertToPrimary) {
|
||||||
$currentBalance['balance'] = bcadd((string) $currentBalance['balance'], $sumOfDay);
|
$currentBalance['balance'] = bcadd((string)$currentBalance['balance'], $sumOfDay);
|
||||||
}
|
}
|
||||||
// if convert to primary currency add the converted amount to "pc_balance".
|
// if convert to primary currency add the converted amount to "pc_balance".
|
||||||
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
||||||
if ($convertToPrimary) {
|
if ($convertToPrimary) {
|
||||||
$pcSumOfDay = $converter->convert($entryCurrency, $primaryCurrency, $carbon, $sumOfDay);
|
$pcSumOfDay = $converter->convert($entryCurrency, $primaryCurrency, $carbon, $sumOfDay);
|
||||||
$currentBalance['pc_balance'] = bcadd((string) ($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay);
|
$currentBalance['pc_balance'] = bcadd((string)($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay);
|
||||||
// if it's the same currency as the entry, also add to balance (see other code).
|
// if it's the same currency as the entry, also add to balance (see other code).
|
||||||
if ($currency->id === $entryCurrency->id) {
|
if ($currency->id === $entryCurrency->id) {
|
||||||
$currentBalance['balance'] = bcadd((string) $currentBalance['balance'], $sumOfDay);
|
$currentBalance['balance'] = bcadd((string)$currentBalance['balance'], $sumOfDay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// add to final array.
|
// add to final array.
|
||||||
$balances[$carbonKey] = $currentBalance;
|
$balances[$carbonKey] = $currentBalance;
|
||||||
Log::debug(sprintf('Updated entry [%s]', $carbonKey), $currentBalance);
|
Log::debug(sprintf('Updated entry [%s]', $carbonKey), $currentBalance);
|
||||||
}
|
}
|
||||||
$cache->store($balances);
|
$cache->store($balances);
|
||||||
@@ -318,6 +317,70 @@ class Steam
|
|||||||
return $balances;
|
return $balances;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function finalAccountsBalanceOptimized(Collection $accounts, Carbon $date, ?TransactionCurrency $primary = null, ?bool $convertToPrimary = null): array
|
||||||
|
{
|
||||||
|
$result = [];
|
||||||
|
$convertToPrimary = $convertToPrimary ?? Amount::convertToPrimary();
|
||||||
|
$primary = $primary ?? Amount::getPrimaryCurrency();
|
||||||
|
$currencies = $this->getCurrencies($accounts);
|
||||||
|
|
||||||
|
// balance(s) in all currencies for ALL accounts.
|
||||||
|
$array = Transaction::whereIn('account_id', $accounts->pluck('id')->toArray())
|
||||||
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||||
|
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
|
||||||
|
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
|
||||||
|
->get(['transactions.account_id', 'transaction_currencies.code', 'transactions.amount'])->toArray();
|
||||||
|
|
||||||
|
/** @var Account $account */
|
||||||
|
foreach ($accounts as $account) {
|
||||||
|
// filter array back to this account:
|
||||||
|
$filtered = array_filter($array, function ($item) use ($account) {
|
||||||
|
return (int)$item['account_id'] === $account->id;
|
||||||
|
});
|
||||||
|
$currency = $currencies[$account->id];
|
||||||
|
// this array is PER account, so we wait a bit before we change code here.
|
||||||
|
$return = [
|
||||||
|
'pc_balance' => '0',
|
||||||
|
'balance' => '0', // this key is overwritten right away, but I must remember it is always created.
|
||||||
|
];
|
||||||
|
|
||||||
|
// balance(s) in all currencies.
|
||||||
|
$others = $this->groupAndSumTransactions($filtered, 'code', 'amount');
|
||||||
|
// Log::debug('All balances are (joined)', $others);
|
||||||
|
// if there is no request to convert, take this as "balance" and "pc_balance".
|
||||||
|
$return['balance'] = $others[$currency->code] ?? '0';
|
||||||
|
if (!$convertToPrimary) {
|
||||||
|
unset($return['pc_balance']);
|
||||||
|
// Log::debug(sprintf('Set balance to %s, unset pc_balance', $return['balance']));
|
||||||
|
}
|
||||||
|
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
||||||
|
if ($convertToPrimary) {
|
||||||
|
$return['pc_balance'] = $this->convertAllBalances($others, $primary, $date); // todo sum all and convert.
|
||||||
|
// Log::debug(sprintf('Set pc_balance to %s', $return['pc_balance']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// either way, the balance is always combined with the virtual balance:
|
||||||
|
$virtualBalance = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
|
||||||
|
|
||||||
|
if ($convertToPrimary) {
|
||||||
|
// the primary currency balance is combined with a converted virtual_balance:
|
||||||
|
$converter = new ExchangeRateConverter();
|
||||||
|
$pcVirtualBalance = $converter->convert($currency, $primary, $date, $virtualBalance);
|
||||||
|
$return['pc_balance'] = bcadd($pcVirtualBalance, $return['pc_balance']);
|
||||||
|
// Log::debug(sprintf('Primary virtual balance makes the primary total %s', $return['pc_balance']));
|
||||||
|
}
|
||||||
|
if (!$convertToPrimary) {
|
||||||
|
// if not, also increase the balance + primary balance for consistency.
|
||||||
|
$return['balance'] = bcadd($return['balance'], $virtualBalance);
|
||||||
|
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
|
||||||
|
}
|
||||||
|
$final = array_merge($return, $others);
|
||||||
|
$result[$account->id] = $final;
|
||||||
|
// Log::debug('Final balance is', $final);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns smaller than or equal to, so be careful with END OF DAY.
|
* Returns smaller than or equal to, so be careful with END OF DAY.
|
||||||
*
|
*
|
||||||
@@ -334,13 +397,13 @@ class Steam
|
|||||||
public function finalAccountBalance(Account $account, Carbon $date, ?TransactionCurrency $primary = null, ?bool $convertToPrimary = null): array
|
public function finalAccountBalance(Account $account, Carbon $date, ?TransactionCurrency $primary = null, ?bool $convertToPrimary = null): array
|
||||||
{
|
{
|
||||||
|
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
$cache->addProperty($date);
|
$cache->addProperty($date);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
||||||
|
|
||||||
return $cache->get();
|
//return $cache->get();
|
||||||
}
|
}
|
||||||
// Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
// Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
||||||
if (null === $convertToPrimary) {
|
if (null === $convertToPrimary) {
|
||||||
@@ -350,7 +413,7 @@ class Steam
|
|||||||
$primary = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
|
$primary = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
|
||||||
}
|
}
|
||||||
// account balance thing.
|
// account balance thing.
|
||||||
$currencyPresent = isset($account->meta) && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
|
$currencyPresent = isset($account->meta) && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
|
||||||
if ($currencyPresent) {
|
if ($currencyPresent) {
|
||||||
$accountCurrency = $account->meta['currency'];
|
$accountCurrency = $account->meta['currency'];
|
||||||
}
|
}
|
||||||
@@ -358,20 +421,19 @@ class Steam
|
|||||||
|
|
||||||
$accountCurrency = $this->getAccountCurrency($account);
|
$accountCurrency = $this->getAccountCurrency($account);
|
||||||
}
|
}
|
||||||
$hasCurrency = null !== $accountCurrency;
|
$hasCurrency = null !== $accountCurrency;
|
||||||
$currency = $hasCurrency ? $accountCurrency : $primary;
|
$currency = $hasCurrency ? $accountCurrency : $primary;
|
||||||
$return = [
|
$return = [
|
||||||
'pc_balance' => '0',
|
'pc_balance' => '0',
|
||||||
'balance' => '0', // this key is overwritten right away, but I must remember it is always created.
|
'balance' => '0', // this key is overwritten right away, but I must remember it is always created.
|
||||||
];
|
];
|
||||||
// balance(s) in all currencies.
|
// balance(s) in all currencies.
|
||||||
$array = $account->transactions()
|
$array = $account->transactions()
|
||||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||||
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
|
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
|
||||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
|
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
|
||||||
->get(['transaction_currencies.code', 'transactions.amount'])->toArray()
|
->get(['transaction_currencies.code', 'transactions.amount'])->toArray();
|
||||||
;
|
$others = $this->groupAndSumTransactions($array, 'code', 'amount');
|
||||||
$others = $this->groupAndSumTransactions($array, 'code', 'amount');
|
|
||||||
// Log::debug('All balances are (joined)', $others);
|
// Log::debug('All balances are (joined)', $others);
|
||||||
// if there is no request to convert, take this as "balance" and "pc_balance".
|
// if there is no request to convert, take this as "balance" and "pc_balance".
|
||||||
$return['balance'] = $others[$currency->code] ?? '0';
|
$return['balance'] = $others[$currency->code] ?? '0';
|
||||||
@@ -386,7 +448,7 @@ class Steam
|
|||||||
}
|
}
|
||||||
|
|
||||||
// either way, the balance is always combined with the virtual balance:
|
// either way, the balance is always combined with the virtual balance:
|
||||||
$virtualBalance = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
|
$virtualBalance = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
|
||||||
|
|
||||||
if ($convertToPrimary) {
|
if ($convertToPrimary) {
|
||||||
// the primary currency balance is combined with a converted virtual_balance:
|
// the primary currency balance is combined with a converted virtual_balance:
|
||||||
@@ -400,7 +462,7 @@ class Steam
|
|||||||
$return['balance'] = bcadd($return['balance'], $virtualBalance);
|
$return['balance'] = bcadd($return['balance'], $virtualBalance);
|
||||||
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
|
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
|
||||||
}
|
}
|
||||||
$final = array_merge($return, $others);
|
$final = array_merge($return, $others);
|
||||||
// Log::debug('Final balance is', $final);
|
// Log::debug('Final balance is', $final);
|
||||||
$cache->store($final);
|
$cache->store($final);
|
||||||
|
|
||||||
@@ -409,8 +471,8 @@ class Steam
|
|||||||
|
|
||||||
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
||||||
{
|
{
|
||||||
$type = $account->accountType->type;
|
$type = $account->accountType->type;
|
||||||
$list = config('firefly.valid_currency_account_types');
|
$list = config('firefly.valid_currency_account_types');
|
||||||
|
|
||||||
// return null if not in this list.
|
// return null if not in this list.
|
||||||
if (!in_array($type, $list, true)) {
|
if (!in_array($type, $list, true)) {
|
||||||
@@ -421,7 +483,7 @@ class Steam
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return TransactionCurrency::find((int) $result->data);
|
return TransactionCurrency::find((int)$result->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function groupAndSumTransactions(array $array, string $group, string $field): array
|
private function groupAndSumTransactions(array $array, string $group, string $field): array
|
||||||
@@ -430,7 +492,7 @@ class Steam
|
|||||||
|
|
||||||
foreach ($array as $item) {
|
foreach ($array as $item) {
|
||||||
$groupKey = $item[$group] ?? 'unknown';
|
$groupKey = $item[$group] ?? 'unknown';
|
||||||
$return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string) $item[$field]);
|
$return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string)$item[$field]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
@@ -445,9 +507,9 @@ class Steam
|
|||||||
if (null === $currency) {
|
if (null === $currency) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$current = $converter->convert($currency, $primary, $date, $amount);
|
$current = $converter->convert($currency, $primary, $date, $amount);
|
||||||
Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $primary->code, $current));
|
Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $primary->code, $current));
|
||||||
$total = bcadd($current, $total);
|
$total = bcadd($current, $total);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $total;
|
return $total;
|
||||||
@@ -478,28 +540,28 @@ class Steam
|
|||||||
$hostName = $ipAddress;
|
$hostName = $ipAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('' !== (string) $hostName && $hostName !== $ipAddress) {
|
if ('' !== (string)$hostName && $hostName !== $ipAddress) {
|
||||||
$host = $hostName;
|
$host = $hostName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) $host;
|
return (string)$host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLastActivities(array $accounts): array
|
public function getLastActivities(array $accounts): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|
||||||
$set = auth()->user()->transactions()
|
$set = auth()->user()->transactions()
|
||||||
->whereIn('transactions.account_id', $accounts)
|
->whereIn('transactions.account_id', $accounts)
|
||||||
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
||||||
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
||||||
;
|
;
|
||||||
|
|
||||||
/** @var Transaction $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
$date = new Carbon($entry->max_date, config('app.timezone'));
|
$date = new Carbon($entry->max_date, config('app.timezone'));
|
||||||
$date->setTimezone(config('app.timezone'));
|
$date->setTimezone(config('app.timezone'));
|
||||||
$list[(int) $entry->account_id] = $date;
|
$list[(int)$entry->account_id] = $date;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $list;
|
return $list;
|
||||||
@@ -517,7 +579,7 @@ class Steam
|
|||||||
if ('equal' === $locale) {
|
if ('equal' === $locale) {
|
||||||
$locale = $this->getLanguage();
|
$locale = $this->getLanguage();
|
||||||
}
|
}
|
||||||
$locale = (string) $locale;
|
$locale = (string)$locale;
|
||||||
|
|
||||||
// Check for Windows to replace the locale correctly.
|
// Check for Windows to replace the locale correctly.
|
||||||
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
|
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
|
||||||
@@ -572,9 +634,9 @@ class Steam
|
|||||||
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
|
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
|
||||||
{
|
{
|
||||||
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
|
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
|
||||||
$returnUrl = $safeUrl;
|
$returnUrl = $safeUrl;
|
||||||
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
||||||
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
||||||
|
|
||||||
if (null !== $unknownHost && $unknownHost === $safeHost) {
|
if (null !== $unknownHost && $unknownHost === $safeHost) {
|
||||||
$returnUrl = $unknownUrl;
|
$returnUrl = $unknownUrl;
|
||||||
@@ -611,26 +673,26 @@ class Steam
|
|||||||
*/
|
*/
|
||||||
public function floatalize(string $value): string
|
public function floatalize(string $value): string
|
||||||
{
|
{
|
||||||
$value = strtoupper($value);
|
$value = strtoupper($value);
|
||||||
if (!str_contains($value, 'E')) {
|
if (!str_contains($value, 'E')) {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Floatalizing %s', $value));
|
Log::debug(sprintf('Floatalizing %s', $value));
|
||||||
|
|
||||||
$number = substr($value, 0, (int) strpos($value, 'E'));
|
$number = substr($value, 0, (int)strpos($value, 'E'));
|
||||||
if (str_contains($number, '.')) {
|
if (str_contains($number, '.')) {
|
||||||
$post = strlen(substr($number, (int) strpos($number, '.') + 1));
|
$post = strlen(substr($number, (int)strpos($number, '.') + 1));
|
||||||
$mantis = substr($value, (int) strpos($value, 'E') + 1);
|
$mantis = substr($value, (int)strpos($value, 'E') + 1);
|
||||||
if ($mantis < 0) {
|
if ($mantis < 0) {
|
||||||
$post += abs((int) $mantis);
|
$post += abs((int)$mantis);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO careless float could break financial math.
|
// TODO careless float could break financial math.
|
||||||
return number_format((float) $value, $post, '.', '');
|
return number_format((float)$value, $post, '.', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO careless float could break financial math.
|
// TODO careless float could break financial math.
|
||||||
return number_format((float) $value, 0, '.', '');
|
return number_format((float)$value, 0, '.', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function opposite(?string $amount = null): ?string
|
public function opposite(?string $amount = null): ?string
|
||||||
@@ -650,24 +712,24 @@ class Steam
|
|||||||
// has a K in it, remove the K and multiply by 1024.
|
// has a K in it, remove the K and multiply by 1024.
|
||||||
$bytes = bcmul(rtrim($string, 'k'), '1024');
|
$bytes = bcmul(rtrim($string, 'k'), '1024');
|
||||||
|
|
||||||
return (int) $bytes;
|
return (int)$bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false !== stripos($string, 'm')) {
|
if (false !== stripos($string, 'm')) {
|
||||||
// has a M in it, remove the M and multiply by 1048576.
|
// has a M in it, remove the M and multiply by 1048576.
|
||||||
$bytes = bcmul(rtrim($string, 'm'), '1048576');
|
$bytes = bcmul(rtrim($string, 'm'), '1048576');
|
||||||
|
|
||||||
return (int) $bytes;
|
return (int)$bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false !== stripos($string, 'g')) {
|
if (false !== stripos($string, 'g')) {
|
||||||
// has a G in it, remove the G and multiply by (1024)^3.
|
// has a G in it, remove the G and multiply by (1024)^3.
|
||||||
$bytes = bcmul(rtrim($string, 'g'), '1073741824');
|
$bytes = bcmul(rtrim($string, 'g'), '1073741824');
|
||||||
|
|
||||||
return (int) $bytes;
|
return (int)$bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int) $string;
|
return (int)$string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function positive(string $amount): string
|
public function positive(string $amount): string
|
||||||
@@ -689,4 +751,42 @@ class Steam
|
|||||||
|
|
||||||
return $amount;
|
return $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCurrencies(Collection $accounts): array
|
||||||
|
{
|
||||||
|
$currencies = [];
|
||||||
|
$accountCurrencies = [];
|
||||||
|
$accountPreferences = [];
|
||||||
|
$primary = Amount::getPrimaryCurrency();
|
||||||
|
|
||||||
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
|
$result = AccountMeta::whereIn('account_id', $ids)->where('name', 'currency_id')->get();
|
||||||
|
/** @var AccountMeta $item */
|
||||||
|
foreach ($result as $item) {
|
||||||
|
$accountPreferences[(int)$item->account_id] = (int)$item->data;
|
||||||
|
}
|
||||||
|
// collect those currencies.
|
||||||
|
$set = TransactionCurrency::whereIn('id', $accountPreferences)->get();
|
||||||
|
foreach ($set as $item) {
|
||||||
|
$currencies[$item->id] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Account $account */
|
||||||
|
foreach ($accounts as $account) {
|
||||||
|
$accountId = $account->id;
|
||||||
|
$currencyPresent = isset($account->meta) && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
|
||||||
|
if ($currencyPresent) {
|
||||||
|
$currencyId = $account->meta['currency']->id;
|
||||||
|
$currencies[$currencyId] ??= $account->meta['currency'];
|
||||||
|
$accountCurrencies[$accountId] = $account->meta['currency'];
|
||||||
|
}
|
||||||
|
if (!$currencyPresent && !array_key_exists($account->id, $accountPreferences)) {
|
||||||
|
$accountCurrencies[$accountId] = $primary;
|
||||||
|
}
|
||||||
|
if (!$currencyPresent && array_key_exists($account->id, $accountPreferences)) {
|
||||||
|
$accountCurrencies[$account->id] = $currencies[$accountPreferences[$account->id]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $accountCurrencies;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -143,7 +143,7 @@ class AccountTransformer extends AbstractTransformer
|
|||||||
'notes' => $account->meta['notes'] ?? null,
|
'notes' => $account->meta['notes'] ?? null,
|
||||||
'monthly_payment_date' => $monthlyPaymentDate,
|
'monthly_payment_date' => $monthlyPaymentDate,
|
||||||
'credit_card_type' => $creditCardType,
|
'credit_card_type' => $creditCardType,
|
||||||
'account_number' => $account->meta['account_number'] ?? null,
|
'account_number' => $account->meta['account_number'],
|
||||||
'iban' => '' === $account->iban ? null : $account->iban,
|
'iban' => '' === $account->iban ? null : $account->iban,
|
||||||
'bic' => $account->meta['BIC'] ?? null,
|
'bic' => $account->meta['BIC'] ?? null,
|
||||||
'opening_balance_date' => $openingBalanceDate,
|
'opening_balance_date' => $openingBalanceDate,
|
||||||
@@ -155,7 +155,7 @@ class AccountTransformer extends AbstractTransformer
|
|||||||
'longitude' => $longitude,
|
'longitude' => $longitude,
|
||||||
'latitude' => $latitude,
|
'latitude' => $latitude,
|
||||||
'zoom_level' => $zoomLevel,
|
'zoom_level' => $zoomLevel,
|
||||||
'last_activity' => array_key_exists('last_activity', $account->meta) ? $account->meta['last_activity']->toAtomString() : null,
|
'last_activity' => $account->meta['last_activity']?->toAtomString(),
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
|
33
resources/views/test/api-test.twig
Normal file
33
resources/views/test/api-test.twig
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Firefly III API test</title>
|
||||||
|
<base href="{{ route('index', null, true) }}/">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="csrf-token" content="{{ csrf_token() }}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
Hi there,
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
This page is created to do some basic API testing. It's not very exciting, is it?
|
||||||
|
</p>
|
||||||
|
<script src="v1/js/app.js?v={{ FF_VERSION }}" type="text/javascript" nonce="{{ JS_NONCE }}"></script>
|
||||||
|
<script type="text/javascript" nonce="{{ JS_NONCE }}">
|
||||||
|
$(function () {
|
||||||
|
"use strict";
|
||||||
|
console.log('Hello from the API test page!');
|
||||||
|
$.ajax({
|
||||||
|
url: 'api/v1/accounts?size=50&date=2025-08-06',
|
||||||
|
type: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@@ -1,2 +0,0 @@
|
|||||||
list-length: {{ listLength }}
|
|
||||||
user-email: {{ Auth.user.email }}
|
|
@@ -120,6 +120,7 @@ Route::group(
|
|||||||
Route::get('flush', ['uses' => 'DebugController@flush', 'as' => 'flush']);
|
Route::get('flush', ['uses' => 'DebugController@flush', 'as' => 'flush']);
|
||||||
Route::get('routes', ['uses' => 'DebugController@routes', 'as' => 'routes']);
|
Route::get('routes', ['uses' => 'DebugController@routes', 'as' => 'routes']);
|
||||||
Route::get('debug', 'DebugController@index')->name('debug');
|
Route::get('debug', 'DebugController@index')->name('debug');
|
||||||
|
Route::get('debug/api-test', 'DebugController@apiTest')->name('api-test');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user