Skip to content

Firefly III: a personal finances manager

Final touches for the balance and transactions.

James Cole202facf43d82
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 0a976bd8d3..3f1642ff4c 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -331,17 +331,21 @@ class AccountFactory
*/
private function storeCreditLiability(Account $account, array $data)
{
+ Log::debug('storeCreditLiability');
$account->refresh();
$accountType = $account->accountType->type;
$direction = $this->accountRepository->getMetaValue($account, 'liability_direction');
$valid = config('firefly.valid_liabilities');
if (in_array($accountType, $valid, true) && 'credit' === $direction) {
+ Log::debug('Is a liability with credit direction.');
if ($this->validOBData($data)) {
+ Log::debug('Has valid CL data.');
$openingBalance = $data['opening_balance'];
$openingBalanceDate = $data['opening_balance_date'];
$this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate);
}
if (!$this->validOBData($data)) {
+ Log::debug('Has NOT valid CL data.');
$this->deleteCreditTransaction($account);
}
}
diff --git a/app/Http/Controllers/Account/EditController.php b/app/Http/Controllers/Account/EditController.php
index be6a0e1da8..3aaacbf4cf 100644
--- a/app/Http/Controllers/Account/EditController.php
+++ b/app/Http/Controllers/Account/EditController.php
@@ -45,10 +45,8 @@ class EditController extends Controller
use ModelInformation;
private AttachmentHelperInterface $attachments;
- /** @var CurrencyRepositoryInterface The currency repository */
- private $currencyRepos;
- /** @var AccountRepositoryInterface The account repository */
- private $repository;
+ private CurrencyRepositoryInterface $currencyRepos;
+ private AccountRepositoryInterface $repository;
/**
* EditController constructor.
@@ -106,6 +104,11 @@ class EditController extends Controller
],
];
+ $liabilityDirections = [
+ 'debit' => trans('firefly.liability_direction_debit'),
+ 'credit' => trans('firefly.liability_direction_credit'),
+ ];
+
// interest calculation periods:
$interestPeriods = [
'daily' => (string)trans('firefly.interest_calc_daily'),
@@ -119,7 +122,7 @@ class EditController extends Controller
}
$request->session()->forget('accounts.edit.fromUpdate');
- $openingBalanceAmount = (string)$repository->getOpeningBalanceAmount($account);
+ $openingBalanceAmount = app('steam')->positive((string)$repository->getOpeningBalanceAmount($account));
$openingBalanceDate = $repository->getOpeningBalanceDate($account);
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
@@ -138,6 +141,7 @@ class EditController extends Controller
'opening_balance_date' => $openingBalanceDate,
'liability_type_id' => $account->account_type_id,
'opening_balance' => $openingBalanceAmount,
+ 'liability_direction' => $this->repository->getMetaValue($account, 'liability_direction'),
'virtual_balance' => $account->virtual_balance,
'currency_id' => $currency->id,
'include_net_worth' => $includeNetWorth,
@@ -157,6 +161,7 @@ class EditController extends Controller
'subTitle',
'subTitleIcon',
'locations',
+ 'liabilityDirections',
'objectType',
'roles',
'preFilled',
diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php
index 028e2cfce6..ac0c95de4a 100644
--- a/app/Http/Controllers/Account/IndexController.php
+++ b/app/Http/Controllers/Account/IndexController.php
@@ -103,7 +103,7 @@ class IndexController extends Controller
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance);
- $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 6, '.', '');
+ $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', '');
$account->interestPeriod = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')));
$account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
}
@@ -164,7 +164,7 @@ class IndexController extends Controller
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance);
- $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 6, '.', '');
+ $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', '');
$account->interestPeriod = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')));
$account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
$account->location = $this->repository->getLocation($account);
diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php
index 2b35d1713a..68b4076e16 100644
--- a/app/Services/Internal/Support/AccountServiceTrait.php
+++ b/app/Services/Internal/Support/AccountServiceTrait.php
@@ -226,6 +226,24 @@ trait AccountServiceTrait
}
}
+ /**
+ * Delete TransactionGroup with liability credit in it.
+ *
+ * @param Account $account
+ */
+ protected function deleteCreditTransaction(Account $account): void
+ {
+ Log::debug(sprintf('deleteCreditTransaction() for account #%d', $account->id));
+ $creditGroup = $this->getCreditTransaction($account);
+
+ if (null !== $creditGroup) {
+ Log::debug('Credit journal found, delete journal.');
+ /** @var TransactionGroupDestroyService $service */
+ $service = app(TransactionGroupDestroyService::class);
+ $service->destroy($creditGroup);
+ }
+ }
+
/**
* Returns the opening balance group, or NULL if it does not exist.
*
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index e3b293899a..3219bde6ea 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -306,20 +306,25 @@ class AccountUpdateService
*/
private function updateCreditLiability(Account $account, array $data): void
{
- $type = $account->accountType;
+ $type = $account->accountType;
$valid = config('firefly.valid_liabilities');
if (in_array($type->type, $valid, true)) {
+ $direction = array_key_exists('liability_direction', $data) ? $data['liability_direction'] : 'empty';
// check if is submitted as empty, that makes it valid:
if ($this->validOBData($data) && !$this->isEmptyOBData($data)) {
$openingBalance = $data['opening_balance'];
$openingBalanceDate = $data['opening_balance_date'];
-
- $this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate);
+ if ('credit' === $data['liability_direction']) {
+ $this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate);
+ }
}
if (!$this->validOBData($data) && $this->isEmptyOBData($data)) {
$this->deleteCreditTransaction($account);
}
+ if ($this->validOBData($data) && !$this->isEmptyOBData($data) && 'credit' !== $direction) {
+ $this->deleteCreditTransaction($account);
+ }
}
}
diff --git a/resources/views/v1/accounts/edit.twig b/resources/views/v1/accounts/edit.twig
index 01013d7d72..a52a1e5f56 100644
--- a/resources/views/v1/accounts/edit.twig
+++ b/resources/views/v1/accounts/edit.twig
@@ -36,6 +36,7 @@
{% if objectType == 'liabilities' %}
{{ ExpandedForm.select('liability_type_id', liabilityTypes) }}
{{ ExpandedForm.amountNoCurrency('opening_balance', null, {label:'debt_start_amount'|_, helpText: 'debt_start_amount_help'|_}) }}
+ {{ ExpandedForm.select('liability_direction', liabilityDirections) }}
{{ ExpandedForm.date('opening_balance_date', null, {label:'debt_start_date'|_}) }}
{{ ExpandedForm.percentage('interest') }}
{{ ExpandedForm.select('interest_period', interestPeriods) }}