Convert more of the budget view to native currency.

This commit is contained in:
James Cole 2024-12-27 13:42:48 +01:00
parent 419975285c
commit 96493425d1
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 82 additions and 38 deletions

View File

@ -92,11 +92,12 @@ class BudgetController extends Controller
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($this->convertToNative);
$cache->addProperty('chart.budget.budget');
$cache->addProperty($budget->id);
if ($cache->has()) {
return response()->json($cache->get());
return response()->json($cache->get());
}
$step = $this->calculateStep($start, $end); // depending on diff, do something with range of chart.
$collection = new Collection([$budget]);
@ -108,7 +109,7 @@ class BudgetController extends Controller
while ($end >= $loopStart) {
/** @var Carbon $loopEnd */
$loopEnd = app('navigation')->endOfPeriod($loopStart, $step);
$spent = $this->opsRepository->sumExpenses($loopStart, $loopEnd, null, $collection);
$spent = $this->opsRepository->sumExpenses($loopStart, $loopEnd, null, $collection); // this method already converts to native.
$label = trim(app('navigation')->periodShow($loopStart, $step));
foreach ($spent as $row) {
@ -198,6 +199,7 @@ class BudgetController extends Controller
$budgetLimitId = null === $budgetLimit ? 0 : $budgetLimit->id;
$cache = new CacheProperties();
$cache->addProperty($budget->id);
$cache->addProperty($this->convertToNative);
$cache->addProperty($budgetLimitId);
$cache->addProperty('chart.budget.expense-asset');
$start = session('first', today(config('app.timezone'))->startOfYear());
@ -212,7 +214,7 @@ class BudgetController extends Controller
$cache->addProperty($end);
if ($cache->has()) {
return response()->json($cache->get());
// return response()->json($cache->get());
}
$collector->setRange($start, $end);
$collector->setBudget($budget);
@ -222,14 +224,33 @@ class BudgetController extends Controller
// group by asset account ID:
foreach ($journals as $journal) {
$key = sprintf('%d-%d', (int) $journal['source_account_id'], $journal['currency_id']);
$key = sprintf('%d-%d', $journal['source_account_id'], $journal['currency_id']);
$amount = $journal['amount'];
// if convert to native, use the native things, unless it's the foreign amount which is in the native currency.
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] !== $this->defaultCurrency->id) {
$key = sprintf('%d-%d', $journal['source_account_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['native_amount'];
}
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] === $this->defaultCurrency->id) {
$key = sprintf('%d-%d', $journal['source_account_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['foreign_amount'];
}
$result[$key] ??= [
'amount' => '0',
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $symbol,
'currency_code' => $code,
'currency_name' => $name,
];
$result[$key]['amount'] = bcadd($journal['amount'], $result[$key]['amount']);
$result[$key]['amount'] = bcadd($amount, $result[$key]['amount']);
}
$names = $this->getAccountNames(array_keys($result));
@ -261,6 +282,7 @@ class BudgetController extends Controller
$budgetLimitId = null === $budgetLimit ? 0 : $budgetLimit->id;
$cache = new CacheProperties();
$cache->addProperty($budget->id);
$cache->addProperty($this->convertToNative);
$cache->addProperty($budgetLimitId);
$cache->addProperty('chart.budget.expense-category');
$start = session('first', today(config('app.timezone'))->startOfYear());
@ -283,13 +305,36 @@ class BudgetController extends Controller
$chartData = [];
foreach ($journals as $journal) {
$key = sprintf('%d-%d', $journal['category_id'], $journal['currency_id']);
$symbol = $journal['currency_symbol'];
$code = $journal['currency_code'];
$name = $journal['currency_name'];
$amount = $journal['amount'];
// if convert to native, use the native things, unless it's the foreign amount which is in the native currency.
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] !== $this->defaultCurrency->id
) {
$key = sprintf('%d-%d', $journal['category_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['native_amount'];
}
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] === $this->defaultCurrency->id
) {
$key = sprintf('%d-%d', $journal['category_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['foreign_amount'];
}
$result[$key] ??= [
'amount' => '0',
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $symbol,
'currency_code' => $code,
'currency_name' => $name,
];
$result[$key]['amount'] = bcadd($journal['amount'], $result[$key]['amount']);
$result[$key]['amount'] = bcadd($amount, $result[$key]['amount']);
}
$names = $this->getCategoryNames(array_keys($result));
@ -320,6 +365,7 @@ class BudgetController extends Controller
$cache = new CacheProperties();
$cache->addProperty($budget->id);
$cache->addProperty($budgetLimitId);
$cache->addProperty($this->convertToNative);
$cache->addProperty('chart.budget.expense-expense');
$start = session('first', today(config('app.timezone'))->startOfYear());
$end = today();
@ -332,7 +378,7 @@ class BudgetController extends Controller
$cache->addProperty($end);
if ($cache->has()) {
return response()->json($cache->get());
// return response()->json($cache->get());
}
$collector->setRange($start, $end);
$collector->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget)->withAccountInformation();
@ -343,13 +389,32 @@ class BudgetController extends Controller
/** @var array $journal */
foreach ($journals as $journal) {
$key = sprintf('%d-%d', $journal['destination_account_id'], $journal['currency_id']);
$amount = $journal['amount'];
// if convert to native, use the native things, unless it's the foreign amount which is in the native currency.
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] !== $this->defaultCurrency->id) {
$key = sprintf('%d-%d', $journal['destination_account_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['native_amount'];
}
if($this->convertToNative && $journal['currency_id'] !== $this->defaultCurrency->id && $journal['foreign_currency_id'] === $this->defaultCurrency->id) {
$key = sprintf('%d-%d', $journal['destination_account_id'], $this->defaultCurrency->id);
$symbol = $this->defaultCurrency->symbol;
$code = $this->defaultCurrency->code;
$name = $this->defaultCurrency->name;
$amount = $journal['foreign_amount'];
}
$result[$key] ??= [
'amount' => '0',
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $symbol,
'currency_code' => $code,
'currency_name' => $name
];
$result[$key]['amount'] = bcadd($journal['amount'], $result[$key]['amount']);
$result[$key]['amount'] = bcadd($amount, $result[$key]['amount']);
}
$names = $this->getAccountNames(array_keys($result));

View File

@ -214,7 +214,6 @@ class OperationsRepository implements OperationsRepositoryInterface
Log::debug('Start of sumExpenses.');
// this collector excludes all transfers TO liabilities (which are also withdrawals)
// because those expenses only become expenses once they move from the liability to the friend.
// TODO this filter must be somewhere in AccountRepositoryInterface because I suspect its needed more often (A113)
// 2024-12-24 disable the exclusion for now.
@ -258,30 +257,10 @@ class OperationsRepository implements OperationsRepositoryInterface
// same but for transactions in the foreign currency:
if (null !== $currency) {
Log::debug('STOP looking for transactions in the foreign currency.');
// Log::debug(sprintf('Look for transactions with foreign currency %s', $currency->code));
// // app('log')->debug(sprintf('Currency is "%s".', $currency->name));
// /** @var GroupCollectorInterface $collector */
// $collector = app(GroupCollectorInterface::class);
// $collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setForeignCurrency($currency)->setBudgets($budgets);
//
// if (null !== $accounts) {
// $collector->setAccounts($accounts);
// }
// $result = $collector->getExtractedJournals();
// // app('log')->debug(sprintf('Found %d journals with currency %s.', count($result), $currency->code));
// // do not use array_merge because you want keys to overwrite (otherwise you get double results):
// Log::debug(sprintf('Found %d extra journals in foreign currency.', count($result)));
// $journals = $result + $journals;
}
$array = [];
foreach ($journals as $journal) {
// Log::debug(sprintf('Journal #%d.', $journal['transaction_journal_id']));
// Log::debug(sprintf('Amounts: %1$s %2$s (amount), %3$s %4$s (foreign_amount), %5$s %6$s (native_amount) %5$s %7$s (foreign native amount)',
// $journal['currency_code'], $journal['amount'], $journal['foreign_currency_code'], $journal['foreign_amount'],
// $default->code, $journal['native_amount'], $journal['native_foreign_amount'])
// );
// TODO same as in category::sumexpenses
$amount = '0';
$currencyId = (int) $journal['currency_id'];