Frontpage seems to be multi currency aware.

This commit is contained in:
James Cole
2024-12-24 16:56:31 +01:00
parent 7e2e49e129
commit 7b3a5c1afd
14 changed files with 211 additions and 108 deletions

View File

@@ -83,6 +83,7 @@ class AccountController extends Controller
*/
public function expenseAccounts(): JsonResponse
{
Log::debug('RevenueAccounts');
/** @var Carbon $start */
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
@@ -95,7 +96,7 @@ class AccountController extends Controller
$cache->addProperty($convertToNative);
$cache->addProperty('chart.account.expense-accounts');
if ($cache->has()) {
// return response()->json($cache->get());
// return response()->json($cache->get());
}
$start->subDay();
@@ -113,7 +114,6 @@ class AccountController extends Controller
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
// loop the accounts, then check for balance and currency info.
foreach($accounts as $account) {
Log::debug(sprintf('Now in account #%d ("%s")', $account->id, $account->name));
@@ -518,11 +518,13 @@ class AccountController extends Controller
/** @var Carbon $end */
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
$cache = new CacheProperties();
$convertToNative = app('preferences')->get('convert_to_native', false)->data;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($convertToNative);
$cache->addProperty('chart.account.revenue-accounts');
if ($cache->has()) {
return response()->json($cache->get());
// return response()->json($cache->get());
}
$start->subDay();
@@ -530,9 +532,10 @@ class AccountController extends Controller
$currencies = [];
$chartData = [];
$tempData = [];
$default = Amount::getDefaultCurrency();
// grab all accounts and names
$accounts = $this->accountRepository->getAccountsByType([AccountType::REVENUE]);
$accounts = $this->accountRepository->getAccountsByType([AccountTypeEnum::REVENUE->value]);
$accountNames = $this->extractNames($accounts);
// grab all balances
@@ -540,33 +543,49 @@ class AccountController extends Controller
$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 $currencyCode => $endAmount) {
if (3 !== strlen($currencyCode)) {
// loop the accounts, then check for balance and currency info.
foreach($accounts as $account) {
Log::debug(sprintf('Now in account #%d ("%s")', $account->id, $account->name));
$expenses = $endBalances[$account->id] ?? false;
if(false === $expenses) {
Log::error(sprintf('Found no end balance for account #%d',$account->id));
continue;
}
/**
* @var string $key
* @var string $endBalance
*/
foreach ($expenses as $key => $endBalance) {
if(!$convertToNative && 'native_balance' === $key) {
Log::debug(sprintf('[a] Will skip expense array "%s"', $key));
continue;
}
if($convertToNative && 'native_balance' !== $key) {
Log::debug(sprintf('[b] Will skip expense array "%s"', $key));
continue;
}
Log::debug(sprintf('Will process expense array "%s" with amount %s', $key, $endBalance));
$searchCode = $convertToNative ? $default->code: $key;
Log::debug(sprintf('Search code is %s', $searchCode));
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string) ($startBalances[$accountId][$currencyCode] ?? '0');
$diff = bcsub((string) $endAmount, $startAmount);
$currencies[$currencyCode] ??= $this->currencyRepository->findByCode($currencyCode);
$startBalance = ($startBalances[$account->id][$key] ?? '0');
Log::debug(sprintf('Start balance is %s', $startBalance));
$diff = bcsub($endBalance, $startBalance);
$currencies[$searchCode] ??= $this->currencyRepository->findByCode($searchCode);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'name' => $accountNames[$account->id],
'difference' => $diff,
'diff_float' => (float) $diff, // intentional float
'currency_id' => $currencies[$currencyCode]->id,
'currency_id' => $currencies[$searchCode]->id,
];
}
}
}
// recreate currencies, but on ID instead of code.
$newCurrencies = [];
foreach ($currencies as $currency) {