Skip to content

Firefly III: a personal finances manager

Remove null exceptions

James Cole5f6772260d03
diff --git a/app/Http/Controllers/Account/EditController.php b/app/Http/Controllers/Account/EditController.php
index 49848992a0..363896bfe8 100644
--- a/app/Http/Controllers/Account/EditController.php
+++ b/app/Http/Controllers/Account/EditController.php
@@ -135,6 +135,7 @@ class EditController extends Controller
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
+ $virtualBalance = null === $account->virtual_balance ? '0' : $account->virtual_balance;
$preFilled = [
'account_number' => $repository->getMetaValue($account, 'account_number'),
'account_role' => $repository->getMetaValue($account, 'account_role'),
@@ -145,7 +146,7 @@ class EditController extends Controller
'liability_type_id' => $account->account_type_id,
'opening_balance' => app('steam')->bcround($openingBalanceAmount, $currency->decimal_places),
'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,
'include_net_worth' => $includeNetWorth,
'interest' => $repository->getMetaValue($account, 'interest'),
diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php
index c8d275aa74..31cbad77ac 100644
--- a/app/Http/Controllers/Account/IndexController.php
+++ b/app/Http/Controllers/Account/IndexController.php
@@ -167,14 +167,19 @@ class IndexController extends Controller
$endBalances = app('steam')->balancesByAccounts($accounts, $end);
$activities = app('steam')->getLastActivities($ids);
+
$accounts->each(
function (Account $account) use ($activities, $startBalances, $endBalances) {
+
+ $interest = (string)$this->repository->getMetaValue($account, 'interest');
+ $interest = '' === $interest ? '0' : $interest;
+
// See reference nr. 68
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
$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(
sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))
);
diff --git a/app/Http/Requests/PiggyBankUpdateRequest.php b/app/Http/Requests/PiggyBankUpdateRequest.php
index d48268a5de..9655612d3f 100644
--- a/app/Http/Requests/PiggyBankUpdateRequest.php
+++ b/app/Http/Requests/PiggyBankUpdateRequest.php
@@ -45,7 +45,7 @@ class PiggyBankUpdateRequest extends FormRequest
'name' => $this->convertString('name'),
'startdate' => $this->getCarbonDate('startdate'),
'account_id' => $this->convertInteger('account_id'),
- 'targetamount' => $this->convertString('targetamount'),
+ 'targetamount' => trim($this->convertString('targetamount')),
'targetdate' => $this->getCarbonDate('targetdate'),
'notes' => $this->stringWithNewlines('notes'),
'object_group_title' => $this->convertString('object_group'),
diff --git a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
index 3b6d41e9c4..d43ff1def1 100644
--- a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
+++ b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
@@ -446,6 +446,9 @@ trait ModifiesPiggyBanks
if (array_key_exists('targetamount', $data) && '' !== $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']) {
$piggyBank->targetdate = $data['targetdate'];
}
diff --git a/app/Services/Internal/Support/CreditRecalculateService.php b/app/Services/Internal/Support/CreditRecalculateService.php
index e67346304d..4147714aa6 100644
--- a/app/Services/Internal/Support/CreditRecalculateService.php
+++ b/app/Services/Internal/Support/CreditRecalculateService.php
@@ -99,7 +99,7 @@ class CreditRecalculateService
}
/**
- * @param TransactionJournal $journal
+ * @param TransactionJournal $journal
*
* @throws FireflyException
*/
@@ -121,7 +121,7 @@ class CreditRecalculateService
}
/**
- * @param TransactionJournal $journal
+ * @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -132,8 +132,8 @@ class CreditRecalculateService
}
/**
- * @param TransactionJournal $journal
- * @param string $direction
+ * @param TransactionJournal $journal
+ * @param string $direction
*
* @return Account
* @throws FireflyException
@@ -154,7 +154,7 @@ class CreditRecalculateService
}
/**
- * @param TransactionJournal $journal
+ * @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -189,7 +189,7 @@ class CreditRecalculateService
}
/**
- * @param Account $account
+ * @param Account $account
*/
private function processWorkAccount(Account $account): void
{
@@ -205,7 +205,7 @@ class CreditRecalculateService
$factory->crud($account, 'start_of_debt', $startOfDebt);
// get direction of liability:
- $direction = (string) $this->repository->getMetaValue($account, 'liability_direction');
+ $direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
// now loop all transactions (except opening balance and credit thing)
$transactions = $account->transactions()->get();
@@ -220,24 +220,36 @@ class CreditRecalculateService
}
/**
- * @param Account $account
- * @param string $direction
- * @param Transaction $transaction
- * @param string $amount
+ * @param Account $account
+ * @param string $direction
+ * @param Transaction $transaction
+ * @param string $amount
*
* @return string
*/
private function processTransaction(Account $account, string $direction, Transaction $transaction, string $amount): string
{
Log::debug(sprintf('Now in %s(#%d, %s)', __METHOD__, $transaction->id, $amount));
- $journal = $transaction->transactionJournal;
- $groupId = $journal->transaction_group_id;
- $type = $journal->transactionType->type;
+ $journal = $transaction->transactionJournal;
+ $foreignCurrency = $transaction->foreignCurrency;
+ $accountCurrency = $this->repository->getAccountCurrency($account);
+ $groupId = $journal->transaction_group_id;
+ $type = $journal->transactionType->type;
+
+ Log::debug(sprintf('Account currency is #%d (%s)', $accountCurrency->id, $accountCurrency->code));
+
if ('' === $direction) {
Log::debug('Since direction is "", do nothing.');
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));
@@ -246,8 +258,8 @@ class CreditRecalculateService
// to a credit-liability doesn't increase the amount (yet)
if (
$type === TransactionType::WITHDRAWAL
- && (int) $account->id === (int) $transaction->account_id
- && 1 === bccomp($transaction->amount, '0')
+ && (int)$account->id === (int)$transaction->account_id
+ && 1 === bccomp($usedAmount, '0')
&& 'credit' === $direction
) {
Log::debug(sprintf('Is withdrawal into credit liability #%d, does not influence the amount due.', $transaction->account_id));
@@ -258,8 +270,8 @@ class CreditRecalculateService
// likewise deposit into a credit debt does not change the amount
if (
$type === TransactionType::DEPOSIT
- && (int) $account->id === (int) $transaction->account_id
- && -1 === bccomp($transaction->amount, '0')
+ && (int)$account->id === (int)$transaction->account_id
+ && -1 === bccomp($usedAmount, '0')
&& 'credit' === $direction
) {
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)) {
- $amount = bcadd($amount, bcmul($transaction->amount, '-1'));
+ $amount = bcadd($amount, bcmul($usedAmount, '-1'));
}
Log::debug(sprintf('Amount is now %s', $amount));
@@ -276,7 +288,7 @@ class CreditRecalculateService
}
/**
- * @param Account|null $account
+ * @param Account|null $account
*/
public function setAccount(?Account $account): void
{
@@ -284,7 +296,7 @@ class CreditRecalculateService
}
/**
- * @param TransactionGroup $group
+ * @param TransactionGroup $group
*/
public function setGroup(TransactionGroup $group): void
{