diff --git a/app/Console/Commands/Correction/TriggerCreditCalculation.php b/app/Console/Commands/Correction/TriggerCreditCalculation.php index 2d799e8719..21be5aa4fe 100644 --- a/app/Console/Commands/Correction/TriggerCreditCalculation.php +++ b/app/Console/Commands/Correction/TriggerCreditCalculation.php @@ -31,7 +31,6 @@ use Illuminate\Console\Command; /** * Class CorrectionSkeleton - * TODO DONT FORGET TO ADD THIS TO THE DOCKER BUILD */ class TriggerCreditCalculation extends Command { diff --git a/app/Services/Internal/Support/CreditRecalculateService.php b/app/Services/Internal/Support/CreditRecalculateService.php index 0535f3ec99..2ca91024ab 100644 --- a/app/Services/Internal/Support/CreditRecalculateService.php +++ b/app/Services/Internal/Support/CreditRecalculateService.php @@ -185,6 +185,15 @@ class CreditRecalculateService app('log')->debug(sprintf('Now processing account #%d ("%s")', $account->id, $account->name)); // get opening balance (if present) $this->repository->setUser($account->user); + $direction = (string)$this->repository->getMetaValue($account, 'liability_direction'); + $openingBalance = $this->repository->getOpeningBalance($account); + if (null !== $openingBalance) { + app('log')->debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id)); + // if account direction is "debit" ("I owe this amount") the opening balance must always be AWAY from the account: + if ('debit' === $direction) { + $this->validateOpeningBalance($account, $openingBalance); + } + } $startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; $leftOfDebt = app('steam')->positive($startOfDebt); @@ -196,9 +205,6 @@ class CreditRecalculateService // amount is positive or negative, doesn't matter. $factory->crud($account, 'start_of_debt', $startOfDebt); - // get direction of liability: - $direction = (string)$this->repository->getMetaValue($account, 'liability_direction'); - app('log')->debug(sprintf('Debt direction is "%s"', $direction)); // now loop all transactions (except opening balance and credit thing) @@ -217,6 +223,42 @@ class CreditRecalculateService app('log')->debug(sprintf('Done processing account #%d ("%s")', $account->id, $account->name)); } + /** + * If account direction is "debit" ("I owe this amount") the opening balance must always be AWAY from the account: + * + * @param Account $account + * @param TransactionJournal $openingBalance + * + * @return void + */ + private function validateOpeningBalance(Account $account, TransactionJournal $openingBalance) + { + /** @var Transaction $source */ + $source = $openingBalance->transactions()->where('amount', '<', 0)->first(); + /** @var Transaction $dest */ + $dest = $openingBalance->transactions()->where('amount', '>', 0)->first(); + if ((int)$source->account_id !== $account->id) { + app('log')->info(sprintf('Liability #%d has a reversed opening balance. Will fix this now.', $account->id)); + app('log')->debug(sprintf('Source amount "%s" is now "%s"', $source->amount, app('steam')->positive($source->amount))); + app('log')->debug(sprintf('Destination amount "%s" is now "%s"', $dest->amount, app('steam')->negative($dest->amount))); + $source->amount = app('steam')->positive($source->amount); + $dest->amount = app('steam')->negative($source->amount); + var_dump($source->foreign_amount); + if (null !== $source->foreign_amount && '' !== $source->foreign_amount) { + $source->foreign_amount = app('steam')->positive($source->foreign_amount); + app('log')->debug(sprintf('Source foreign amount "%s" is now "%s"', $source->foreign_amount, app('steam')->positive($source->foreign_amount))); + } + if (null !== $dest->foreign_amount && '' !== $dest->foreign_amount) { + $dest->foreign_amount = app('steam')->negative($dest->foreign_amount); + app('log')->debug(sprintf('Destination amount "%s" is now "%s"', $dest->foreign_amount, app('steam')->negative($dest->foreign_amount))); + } + $source->save(); + $dest->save(); + return; + } + app('log')->debug('Opening balance is valid'); + } + /** * @param Account $account * @param string $direction