Remove null exceptions

This commit is contained in:
James Cole
2022-12-24 05:48:04 +01:00
parent 22a237d316
commit 5f6772260d
5 changed files with 45 additions and 24 deletions

View File

@@ -135,6 +135,7 @@ class EditController extends Controller
// code to handle active-checkboxes // code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token'); $hasOldInput = null !== $request->old('_token');
$virtualBalance = null === $account->virtual_balance ? '0' : $account->virtual_balance;
$preFilled = [ $preFilled = [
'account_number' => $repository->getMetaValue($account, 'account_number'), 'account_number' => $repository->getMetaValue($account, 'account_number'),
'account_role' => $repository->getMetaValue($account, 'account_role'), 'account_role' => $repository->getMetaValue($account, 'account_role'),
@@ -145,7 +146,7 @@ class EditController extends Controller
'liability_type_id' => $account->account_type_id, 'liability_type_id' => $account->account_type_id,
'opening_balance' => app('steam')->bcround($openingBalanceAmount, $currency->decimal_places), 'opening_balance' => app('steam')->bcround($openingBalanceAmount, $currency->decimal_places),
'liability_direction' => $this->repository->getMetaValue($account, 'liability_direction'), 'liability_direction' => $this->repository->getMetaValue($account, 'liability_direction'),
'virtual_balance' => app('steam')->bcround($account->virtual_balance, $currency->decimal_places), 'virtual_balance' => app('steam')->bcround($virtualBalance, $currency->decimal_places),
'currency_id' => $currency->id, 'currency_id' => $currency->id,
'include_net_worth' => $includeNetWorth, 'include_net_worth' => $includeNetWorth,
'interest' => $repository->getMetaValue($account, 'interest'), 'interest' => $repository->getMetaValue($account, 'interest'),

View File

@@ -167,14 +167,19 @@ class IndexController extends Controller
$endBalances = app('steam')->balancesByAccounts($accounts, $end); $endBalances = app('steam')->balancesByAccounts($accounts, $end);
$activities = app('steam')->getLastActivities($ids); $activities = app('steam')->getLastActivities($ids);
$accounts->each( $accounts->each(
function (Account $account) use ($activities, $startBalances, $endBalances) { function (Account $account) use ($activities, $startBalances, $endBalances) {
$interest = (string)$this->repository->getMetaValue($account, 'interest');
$interest = '' === $interest ? '0' : $interest;
// See reference nr. 68 // See reference nr. 68
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id); $account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
$account->startBalance = $this->isInArray($startBalances, $account->id); $account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id); $account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance); $account->difference = bcsub($account->endBalance, $account->startBalance);
$account->interest = app('steam')->bcround($this->repository->getMetaValue($account, 'interest'), 4); $account->interest = app('steam')->bcround($interest, 4);
$account->interestPeriod = (string) trans( $account->interestPeriod = (string) trans(
sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')) sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))
); );

View File

@@ -45,7 +45,7 @@ class PiggyBankUpdateRequest extends FormRequest
'name' => $this->convertString('name'), 'name' => $this->convertString('name'),
'startdate' => $this->getCarbonDate('startdate'), 'startdate' => $this->getCarbonDate('startdate'),
'account_id' => $this->convertInteger('account_id'), 'account_id' => $this->convertInteger('account_id'),
'targetamount' => $this->convertString('targetamount'), 'targetamount' => trim($this->convertString('targetamount')),
'targetdate' => $this->getCarbonDate('targetdate'), 'targetdate' => $this->getCarbonDate('targetdate'),
'notes' => $this->stringWithNewlines('notes'), 'notes' => $this->stringWithNewlines('notes'),
'object_group_title' => $this->convertString('object_group'), 'object_group_title' => $this->convertString('object_group'),

View File

@@ -446,6 +446,9 @@ trait ModifiesPiggyBanks
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) { if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
$piggyBank->targetamount = $data['targetamount']; $piggyBank->targetamount = $data['targetamount'];
} }
if (array_key_exists('targetamount', $data) && '' === $data['targetamount']) {
$piggyBank->targetamount = '0';
}
if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) { if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) {
$piggyBank->targetdate = $data['targetdate']; $piggyBank->targetdate = $data['targetdate'];
} }

View File

@@ -231,13 +231,25 @@ class CreditRecalculateService
{ {
Log::debug(sprintf('Now in %s(#%d, %s)', __METHOD__, $transaction->id, $amount)); Log::debug(sprintf('Now in %s(#%d, %s)', __METHOD__, $transaction->id, $amount));
$journal = $transaction->transactionJournal; $journal = $transaction->transactionJournal;
$foreignCurrency = $transaction->foreignCurrency;
$accountCurrency = $this->repository->getAccountCurrency($account);
$groupId = $journal->transaction_group_id; $groupId = $journal->transaction_group_id;
$type = $journal->transactionType->type; $type = $journal->transactionType->type;
Log::debug(sprintf('Account currency is #%d (%s)', $accountCurrency->id, $accountCurrency->code));
if ('' === $direction) { if ('' === $direction) {
Log::debug('Since direction is "", do nothing.'); Log::debug('Since direction is "", do nothing.');
return $amount; return $amount;
} }
// amount to use depends on the currency:
$usedAmount = $transaction->amount;
if (null !== $foreignCurrency && $foreignCurrency->id === $accountCurrency->id) {
$usedAmount = $transaction->foreign_amount;
Log::debug('Will now use foreign amount!');
}
Log::debug(sprintf('Processing group #%d, journal #%d of type "%s"', $journal->id, $groupId, $type)); Log::debug(sprintf('Processing group #%d, journal #%d of type "%s"', $journal->id, $groupId, $type));
@@ -247,7 +259,7 @@ class CreditRecalculateService
if ( if (
$type === TransactionType::WITHDRAWAL $type === TransactionType::WITHDRAWAL
&& (int)$account->id === (int)$transaction->account_id && (int)$account->id === (int)$transaction->account_id
&& 1 === bccomp($transaction->amount, '0') && 1 === bccomp($usedAmount, '0')
&& 'credit' === $direction && 'credit' === $direction
) { ) {
Log::debug(sprintf('Is withdrawal into credit liability #%d, does not influence the amount due.', $transaction->account_id)); Log::debug(sprintf('Is withdrawal into credit liability #%d, does not influence the amount due.', $transaction->account_id));
@@ -259,7 +271,7 @@ class CreditRecalculateService
if ( if (
$type === TransactionType::DEPOSIT $type === TransactionType::DEPOSIT
&& (int)$account->id === (int)$transaction->account_id && (int)$account->id === (int)$transaction->account_id
&& -1 === bccomp($transaction->amount, '0') && -1 === bccomp($usedAmount, '0')
&& 'credit' === $direction && 'credit' === $direction
) { ) {
Log::debug(sprintf('Is deposit from liability #%d,does not influence the amount left.', $transaction->account_id)); Log::debug(sprintf('Is deposit from liability #%d,does not influence the amount left.', $transaction->account_id));
@@ -268,7 +280,7 @@ class CreditRecalculateService
} }
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) { if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
$amount = bcadd($amount, bcmul($transaction->amount, '-1')); $amount = bcadd($amount, bcmul($usedAmount, '-1'));
} }
Log::debug(sprintf('Amount is now %s', $amount)); Log::debug(sprintf('Amount is now %s', $amount));