Fix math issue in summary controller.

This commit is contained in:
James Cole 2023-08-13 07:24:51 +02:00
parent 610bc108e7
commit 499720df46
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -354,6 +354,7 @@ class BasicController extends Controller
*/ */
private function getLeftToSpendInfo(Carbon $start, Carbon $end): array private function getLeftToSpendInfo(Carbon $start, Carbon $end): array
{ {
app('log')->debug('Now in getLeftToSpendInfo');
$return = []; $return = [];
$today = today(config('app.timezone')); $today = today(config('app.timezone'));
$available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end); $available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end);
@ -367,7 +368,7 @@ class BasicController extends Controller
$nativeLeft = [ $nativeLeft = [
'key' => 'left-to-spend-in-native', 'key' => 'left-to-spend-in-native',
'value' => '0', 'value' => '0',
'currency_id' => (int)$default->id, 'currency_id' => (string)$default->id,
'currency_code' => $default->code, 'currency_code' => $default->code,
'currency_symbol' => $default->symbol, 'currency_symbol' => $default->symbol,
'currency_decimal_places' => (int)$default->decimal_places, 'currency_decimal_places' => (int)$default->decimal_places,
@ -375,7 +376,7 @@ class BasicController extends Controller
$nativePerDay = [ $nativePerDay = [
'key' => 'left-per-day-to-spend-in-native', 'key' => 'left-per-day-to-spend-in-native',
'value' => '0', 'value' => '0',
'currency_id' => (int)$default->id, 'currency_id' => (string)$default->id,
'currency_code' => $default->code, 'currency_code' => $default->code,
'currency_symbol' => $default->symbol, 'currency_symbol' => $default->symbol,
'currency_decimal_places' => (int)$default->decimal_places, 'currency_decimal_places' => (int)$default->decimal_places,
@ -386,17 +387,20 @@ class BasicController extends Controller
* @var array $row * @var array $row
*/ */
foreach ($spent as $currencyId => $row) { foreach ($spent as $currencyId => $row) {
app('log')->debug(sprintf('Processing spent array in currency #%d', $currencyId));
$currencyId = (int)$currencyId;
$spent = '0'; $spent = '0';
$spentNative = '0'; $spentNative = '0';
// get the sum from the array of transactions (double loop but who cares) // get the sum from the array of transactions (double loop but who cares)
/** @var array $budget */ /** @var array $budget */
foreach ($row['budgets'] as $budget) { foreach ($row['budgets'] as $budget) {
app('log')->debug(sprintf('Processing expenses in budget "%s".', $budget['name']));
/** @var array $journal */ /** @var array $journal */
foreach ($budget['transaction_journals'] as $journal) { foreach ($budget['transaction_journals'] as $journal) {
$journalCurrencyId = $journal['currency_id']; $journalCurrencyId = $journal['currency_id'];
$currency = $currencies[$journalCurrencyId] ?? $this->currencyRepos->find($journalCurrencyId); $currency = $currencies[$journalCurrencyId] ?? $this->currencyRepos->find($journalCurrencyId);
$currencies[$currencyId] = $currency; $currencies[$currencyId] = $currency;
$amount = bcmul($journal['amount'], '-1'); $amount = app('steam')->negative($journal['amount']);
$amountNative = $converter->convert($default, $currency, $start, $amount); $amountNative = $converter->convert($default, $currency, $start, $amount);
if ((int)$journal['foreign_currency_id'] === (int)$default->id) { if ((int)$journal['foreign_currency_id'] === (int)$default->id) {
$amountNative = $journal['foreign_amount']; $amountNative = $journal['foreign_amount'];
@ -404,15 +408,18 @@ class BasicController extends Controller
$spent = bcadd($spent, $amount); $spent = bcadd($spent, $amount);
$spentNative = bcadd($spentNative, $amountNative); $spentNative = bcadd($spentNative, $amountNative);
} }
app('log')->debug(sprintf('Total spent in budget "%s" is %s', $budget['name'], $spent));
} }
// either an amount was budgeted or 0 is available. // either an amount was budgeted or 0 is available.
$currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId); $currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId);
$currencies[$currencyId] = $currency; $currencies[$currencyId] = $currency;
$amount = $available[$currencyId]['amount'] ?? '0'; $amount = $available[$currencyId]['amount'] ?? '0';
$amountNative = $converter->convert($default, $currency, $start, $amount); $amountNative = $available[$currencyId]['native_amount'] ?? '0';
$left = bcadd($amount, $spent); $left = bcadd($amount, $spent);
$leftNative = bcadd($amountNative, $spentNative); $leftNative = bcadd($amountNative, $spentNative);
app('log')->debug(sprintf('Available amount is %s', $amount));
app('log')->debug(sprintf('Amount left is %s', $left));
// how much left per day? // how much left per day?
$days = $today->diffInDays($end) + 1; $days = $today->diffInDays($end) + 1;
@ -429,10 +436,10 @@ class BasicController extends Controller
$return[] = [ $return[] = [
'key' => sprintf('left-to-spend-in-%s', $row['currency_code']), 'key' => sprintf('left-to-spend-in-%s', $row['currency_code']),
'value' => $left, 'value' => $left,
'currency_id' => $row['currency_id'], 'currency_id' => (string)$row['currency_id'],
'currency_code' => $row['currency_code'], 'currency_code' => $row['currency_code'],
'currency_symbol' => $row['currency_symbol'], 'currency_symbol' => $row['currency_symbol'],
'currency_decimal_places' => $row['currency_decimal_places'], 'currency_decimal_places' => (int)$row['currency_decimal_places'],
]; ];
// left (native) // left (native)
$nativeLeft['value'] = $leftNative; $nativeLeft['value'] = $leftNative;
@ -441,10 +448,10 @@ class BasicController extends Controller
$return[] = [ $return[] = [
'key' => sprintf('left-per-day-to-spend-in-%s', $row['currency_code']), 'key' => sprintf('left-per-day-to-spend-in-%s', $row['currency_code']),
'value' => $perDay, 'value' => $perDay,
'currency_id' => $row['currency_id'], 'currency_id' => (string)$row['currency_id'],
'currency_code' => $row['currency_code'], 'currency_code' => $row['currency_code'],
'currency_symbol' => $row['currency_symbol'], 'currency_symbol' => $row['currency_symbol'],
'currency_decimal_places' => $row['currency_decimal_places'], 'currency_decimal_places' => (int)$row['currency_decimal_places'],
]; ];
// left per day (native) // left per day (native)