Clean up liability overview.

This commit is contained in:
James Cole
2022-12-27 07:01:13 +01:00
parent eff631276e
commit bb11e61034
9 changed files with 139 additions and 88 deletions

View File

@@ -51,7 +51,7 @@ trait AccountServiceTrait
protected AccountRepositoryInterface $accountRepository;
/**
* @param null|string $iban
* @param null|string $iban
*
* @return null|string
*/
@@ -76,7 +76,7 @@ trait AccountServiceTrait
/**
* Returns true if the data in the array is submitted but empty.
*
* @param array $data
* @param array $data
*
* @return bool
*/
@@ -104,8 +104,8 @@ trait AccountServiceTrait
*
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
*
* @param Account $account
* @param array $data
* @param Account $account
* @param array $data
*
*/
public function updateMetaData(Account $account, array $data): void
@@ -155,14 +155,14 @@ trait AccountServiceTrait
$data[$field] = 1;
}
$factory->crud($account, $field, (string) $data[$field]);
$factory->crud($account, $field, (string)$data[$field]);
}
}
}
/**
* @param Account $account
* @param string $note
* @param Account $account
* @param string $note
*
* @codeCoverageIgnore
* @return bool
@@ -195,13 +195,13 @@ trait AccountServiceTrait
/**
* Verify if array contains valid data to possibly store or update the opening balance.
*
* @param array $data
* @param array $data
*
* @return bool
*/
public function validOBData(array $data): bool
{
$data['opening_balance'] = (string) ($data['opening_balance'] ?? '');
$data['opening_balance'] = (string)($data['opening_balance'] ?? '');
if ('' !== $data['opening_balance'] && 0 === bccomp($data['opening_balance'], '0')) {
$data['opening_balance'] = '';
}
@@ -217,8 +217,8 @@ trait AccountServiceTrait
}
/**
* @param Account $account
* @param array $data
* @param Account $account
* @param array $data
*
* @return TransactionGroup
* @throws FireflyException
@@ -311,7 +311,7 @@ trait AccountServiceTrait
/**
* Delete TransactionGroup with liability credit in it.
*
* @param Account $account
* @param Account $account
*/
protected function deleteCreditTransaction(Account $account): void
{
@@ -329,7 +329,7 @@ trait AccountServiceTrait
/**
* Returns the credit transaction group, or NULL if it does not exist.
*
* @param Account $account
* @param Account $account
*
* @return TransactionGroup|null
*/
@@ -343,7 +343,7 @@ trait AccountServiceTrait
/**
* Delete TransactionGroup with opening balance in it.
*
* @param Account $account
* @param Account $account
*/
protected function deleteOBGroup(Account $account): void
{
@@ -362,7 +362,7 @@ trait AccountServiceTrait
/**
* Returns the opening balance group, or NULL if it does not exist.
*
* @param Account $account
* @param Account $account
*
* @return TransactionGroup|null
*/
@@ -372,8 +372,8 @@ trait AccountServiceTrait
}
/**
* @param int $currencyId
* @param string $currencyCode
* @param int $currencyId
* @param string $currencyCode
*
* @return TransactionCurrency
* @throws FireflyException
@@ -400,20 +400,29 @@ trait AccountServiceTrait
/**
* Create the opposing "credit liability" transaction for credit liabilities.
*
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
*
* @return TransactionGroup
* @throws FireflyException
*/
protected function updateCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
protected function updateCreditTransaction(Account $account, string $direction, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
{
Log::debug(sprintf('Now in %s', __METHOD__));
if (0 === bccomp($openingBalance, '0')) {
Log::debug('Amount is zero, so will not update liability credit group.');
throw new FireflyException('Amount for update liability credit was unexpectedly 0.');
Log::debug('Amount is zero, so will not update liability credit/debit group.');
throw new FireflyException('Amount for update liability credit/debit was unexpectedly 0.');
}
// if direction is "debit" (i owe this debt), amount is negative.
// which means the liability will have a negative balance which the user must fill.
$openingBalance = app('steam')->negative($openingBalance);
// if direction is "credit" (I am owed this debt), amount is positive.
// which means the liability will have a positive balance which is drained when its paid back into any asset.
if ('credit' === $direction) {
$openingBalance = app('steam')->positive($openingBalance);
}
// create if not exists:
@@ -451,9 +460,9 @@ trait AccountServiceTrait
}
/**
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
*
* @return TransactionGroup
* @throws FireflyException
@@ -468,7 +477,24 @@ trait AccountServiceTrait
}
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
$amount = app('steam')->positive($openingBalance);
// set source and/or destination based on whether the amount is positive or negative.
// first, assume the amount is positive and go from there:
// if amount is positive ("I am owed this debt"), source is special account, destination is the liability.
$sourceId = null;
$sourceName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
$destId = $account->id;
$destName = null;
if(-1 === bccomp($openingBalance, '0')) {
// amount is negative, reverse it
$sourceId = $account->id;
$sourceName = null;
$destId = null;
$destName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
}
// amount must be positive for the transaction to work.
$amount = app('steam')->positive($openingBalance);
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
@@ -484,10 +510,10 @@ trait AccountServiceTrait
[
'type' => 'Liability credit',
'date' => $openingBalanceDate,
'source_id' => null,
'source_name' => trans('firefly.liability_credit_description', ['account' => $account->name], $language),
'destination_id' => $account->id,
'destination_name' => null,
'source_id' => $sourceId,
'source_name' => $sourceName,
'destination_id' => $destId,
'destination_name' => $destName,
'user' => $account->user_id,
'currency_id' => $currency->id,
'order' => 0,
@@ -526,7 +552,7 @@ trait AccountServiceTrait
/**
* TODO refactor to "getfirstjournal"
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return TransactionJournal
* @throws FireflyException
@@ -545,8 +571,8 @@ trait AccountServiceTrait
/**
* TODO Rename to getOpposingTransaction
*
* @param TransactionJournal $journal
* @param Account $account
* @param TransactionJournal $journal
* @param Account $account
*
* @return Transaction
* @throws FireflyException
@@ -563,8 +589,8 @@ trait AccountServiceTrait
}
/**
* @param TransactionJournal $journal
* @param Account $account
* @param TransactionJournal $journal
* @param Account $account
*
* @return Transaction
* @throws FireflyException
@@ -584,9 +610,9 @@ trait AccountServiceTrait
* Update or create the opening balance group.
* Since opening balance and date can still be empty strings, it may fail.
*
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
*
* @return TransactionGroup
* @throws FireflyException
@@ -646,9 +672,9 @@ trait AccountServiceTrait
}
/**
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
* @param Account $account
* @param string $openingBalance
* @param Carbon $openingBalanceDate
*
* @return TransactionGroup
* @throws FireflyException

View File

@@ -62,9 +62,11 @@ class CreditRecalculateService
return;
}
if (null !== $this->group && null === $this->account) {
Log::debug('Have to handle a group.');
$this->processGroup();
}
if (null !== $this->account && null === $this->group) {
Log::debug('Have to handle an account.');
// work based on account.
$this->processAccount();
}
@@ -213,7 +215,6 @@ class CreditRecalculateService
}
$factory->crud($account, 'current_debt', $leftOfDebt);
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
}
@@ -252,16 +253,16 @@ class CreditRecalculateService
Log::debug(sprintf('Processing group #%d, journal #%d of type "%s"', $journal->id, $groupId, $type));
// it's a withdrawal into this liability (from asset).
// if it's a credit, we don't care, because sending more money
// to a credit-liability doesn't increase the amount (yet)
// if it's a credit ("I am owed"), this increases the amount due,
// because we're lending person X more money
if (
$type === TransactionType::WITHDRAWAL
&& (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));
$amount = bcadd($amount, app('steam')->positive($usedAmount));
Log::debug(sprintf('Is withdrawal (%s) into credit liability #%d, will increase amount due to %s.', $transaction->account_id, $usedAmount, $amount));
return $amount;
}

View File

@@ -324,7 +324,7 @@ class AccountUpdateService
$openingBalance = $data['opening_balance'];
$openingBalanceDate = $data['opening_balance_date'];
if ('credit' === $direction) {
$this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate);
$this->updateCreditTransaction($account, $direction, $openingBalance, $openingBalanceDate);
}
}