mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-03 11:08:28 +00:00
Catch null
This commit is contained in:
@@ -182,8 +182,7 @@ class CreditRecalculateService
|
|||||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||||
$currency = $this->repository->getAccountCurrency($account);
|
$currency = $this->repository->getAccountCurrency($account);
|
||||||
$decimals = $currency?->decimal_places ?? 2;
|
app('log')->debug(sprintf('Start of debt is "%s", so initial left of debt is "%s"', app('steam')->bcround($startOfDebt, 2), app('steam')->bcround($leftOfDebt, 2)));
|
||||||
app('log')->debug(sprintf('Start of debt is "%s", so initial left of debt is "%s"', app('steam')->bcround($startOfDebt, $decimals), app('steam')->bcround($leftOfDebt, $decimals)));
|
|
||||||
|
|
||||||
/** @var AccountMetaFactory $factory */
|
/** @var AccountMetaFactory $factory */
|
||||||
$factory = app(AccountMetaFactory::class);
|
$factory = app(AccountMetaFactory::class);
|
||||||
@@ -249,15 +248,7 @@ class CreditRecalculateService
|
|||||||
$journal = $transaction->transactionJournal;
|
$journal = $transaction->transactionJournal;
|
||||||
$foreignCurrency = $transaction->foreignCurrency;
|
$foreignCurrency = $transaction->foreignCurrency;
|
||||||
$accountCurrency = $this->repository->getAccountCurrency($account);
|
$accountCurrency = $this->repository->getAccountCurrency($account);
|
||||||
$decimals = $accountCurrency->decimal_places;
|
|
||||||
$type = $journal->transactionType->type;
|
$type = $journal->transactionType->type;
|
||||||
|
|
||||||
/** @var Transaction $destTransaction */
|
|
||||||
$destTransaction = $journal->transactions()->where('amount', '>', '0')->first();
|
|
||||||
|
|
||||||
/** @var Transaction $sourceTransaction */
|
|
||||||
$sourceTransaction = $journal->transactions()->where('amount', '<', '0')->first();
|
|
||||||
|
|
||||||
app('log')->debug(sprintf('Left of debt is: %s', app('steam')->bcround($leftOfDebt, 2)));
|
app('log')->debug(sprintf('Left of debt is: %s', app('steam')->bcround($leftOfDebt, 2)));
|
||||||
|
|
||||||
if ('' === $direction) {
|
if ('' === $direction) {
|
||||||
@@ -280,7 +271,7 @@ class CreditRecalculateService
|
|||||||
if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1
|
if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 1 (withdrawal into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 1 (withdrawal into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -288,7 +279,7 @@ class CreditRecalculateService
|
|||||||
if ($isSameAccount && $isCredit && $this->isWithdrawalOut($usedAmount, $type)) { // case 2
|
if ($isSameAccount && $isCredit && $this->isWithdrawalOut($usedAmount, $type)) { // case 2
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcsub($leftOfDebt, $usedAmount);
|
$result = bcsub($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 2 (withdrawal away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 2 (withdrawal away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -296,7 +287,7 @@ class CreditRecalculateService
|
|||||||
if ($isSameAccount && $isCredit && $this->isDepositOut($usedAmount, $type)) { // case 3
|
if ($isSameAccount && $isCredit && $this->isDepositOut($usedAmount, $type)) { // case 3
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcsub($leftOfDebt, $usedAmount);
|
$result = bcsub($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 3 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 3 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -304,35 +295,35 @@ class CreditRecalculateService
|
|||||||
if ($isSameAccount && $isCredit && $this->isDepositIn($usedAmount, $type)) { // case 4
|
if ($isSameAccount && $isCredit && $this->isDepositIn($usedAmount, $type)) { // case 4
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 4 (deposit into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 4 (deposit into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if ($isSameAccount && $isCredit && $this->isTransferIn($usedAmount, $type)) { // case 5
|
if ($isSameAccount && $isCredit && $this->isTransferIn($usedAmount, $type)) { // case 5
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 5 (transfer into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 5 (transfer into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if ($isSameAccount && $isDebit && $this->isWithdrawalIn($usedAmount, $type)) { // case 6
|
if ($isSameAccount && $isDebit && $this->isWithdrawalIn($usedAmount, $type)) { // case 6
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcsub($leftOfDebt, $usedAmount);
|
$result = bcsub($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 6 (withdrawal into debit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 6 (withdrawal into debit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if($isSameAccount && $isDebit && $this->isDepositOut($usedAmount, $type)) { // case 7
|
if($isSameAccount && $isDebit && $this->isDepositOut($usedAmount, $type)) { // case 7
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 7 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 7 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if($isSameAccount && $isDebit && $this->isWithdrawalOut($usedAmount, $type)) { // case 8
|
if($isSameAccount && $isDebit && $this->isWithdrawalOut($usedAmount, $type)) { // case 8
|
||||||
$usedAmount = app('steam')->positive($usedAmount);
|
$usedAmount = app('steam')->positive($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case 8 (withdrawal away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case 8 (withdrawal away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -341,12 +332,12 @@ class CreditRecalculateService
|
|||||||
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
|
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
|
||||||
$usedAmount = app('steam')->negative($usedAmount);
|
$usedAmount = app('steam')->negative($usedAmount);
|
||||||
$result = bcadd($leftOfDebt, $usedAmount);
|
$result = bcadd($leftOfDebt, $usedAmount);
|
||||||
app('log')->debug(sprintf('Case X (all other cases): %s + %s = %s', app('steam')->bcround($leftOfDebt, $decimals), app('steam')->bcround($usedAmount, $decimals), app('steam')->bcround($result, $decimals)));
|
app('log')->debug(sprintf('Case X (all other cases): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
app('log')->warning(sprintf('[-1] Catch-all, should not happen. Left of debt = %s', app('steam')->bcround($leftOfDebt, $decimals)));
|
app('log')->warning(sprintf('[-1] Catch-all, should not happen. Left of debt = %s', app('steam')->bcround($leftOfDebt, 2)));
|
||||||
|
|
||||||
return $leftOfDebt;
|
return $leftOfDebt;
|
||||||
}
|
}
|
||||||
|
@@ -183,6 +183,14 @@ class ExchangeRateConverter
|
|||||||
;
|
;
|
||||||
++$this->queryCount;
|
++$this->queryCount;
|
||||||
$rate = (string) $result?->rate;
|
$rate = (string) $result?->rate;
|
||||||
|
|
||||||
|
if ('' === $rate) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if('0' === $rate) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$cache->store($rate);
|
$cache->store($rate);
|
||||||
|
|
||||||
// if the rate has not been cached during this particular run, save it
|
// if the rate has not been cached during this particular run, save it
|
||||||
@@ -198,10 +206,6 @@ class ExchangeRateConverter
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
if ('' === $rate) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $rate;
|
return $rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user