Fix account api for new features.

This commit is contained in:
James Cole
2025-01-15 18:54:49 +01:00
parent b13030420b
commit 9316ff3e51
5 changed files with 97 additions and 72 deletions

View File

@@ -119,7 +119,7 @@ class EditController extends Controller
} }
$request->session()->forget('accounts.edit.fromUpdate'); $request->session()->forget('accounts.edit.fromUpdate');
$openingBalanceAmount = (string) $repository->getOpeningBalanceAmount($account); $openingBalanceAmount = (string) $repository->getOpeningBalanceAmount($account, false);
if ('0' === $openingBalanceAmount) { if ('0' === $openingBalanceAmount) {
$openingBalanceAmount = ''; $openingBalanceAmount = '';
} }

View File

@@ -293,7 +293,7 @@ class AccountRepository implements AccountRepositoryInterface
/** /**
* Returns the amount of the opening balance for this account. * Returns the amount of the opening balance for this account.
*/ */
public function getOpeningBalanceAmount(Account $account): ?string public function getOpeningBalanceAmount(Account $account, bool $convertToNative): ?string
{ {
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') $journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id) ->where('transactions.account_id', $account->id)
@@ -307,6 +307,9 @@ class AccountRepository implements AccountRepositoryInterface
if (null === $transaction) { if (null === $transaction) {
return null; return null;
} }
if($convertToNative) {
return $transaction->native_amount ?? '0';
}
return $transaction->amount; return $transaction->amount;
} }

View File

@@ -106,7 +106,7 @@ interface AccountRepositoryInterface
/** /**
* Returns the amount of the opening balance for this account. * Returns the amount of the opening balance for this account.
*/ */
public function getOpeningBalanceAmount(Account $account): ?string; public function getOpeningBalanceAmount(Account $account, bool $convertToNative): ?string;
/** /**
* Return date of opening balance as string or null. * Return date of opening balance as string or null.

View File

@@ -170,7 +170,7 @@ class CreditRecalculateService
$this->validateOpeningBalance($account, $openingBalance); $this->validateOpeningBalance($account, $openingBalance);
} }
} }
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; $startOfDebt = $this->repository->getOpeningBalanceAmount($account, false) ?? '0';
$leftOfDebt = app('steam')->positive($startOfDebt); $leftOfDebt = app('steam')->positive($startOfDebt);
// 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))); // 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)));

View File

@@ -27,7 +27,9 @@ namespace FireflyIII\Transformers;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Facades\Steam;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@@ -62,17 +64,24 @@ class AccountTransformer extends AbstractTransformer
$liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType)); $liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType));
$liabilityType = '' === $liabilityType ? null : strtolower($liabilityType); $liabilityType = '' === $liabilityType ? null : strtolower($liabilityType);
$liabilityDirection = $this->repository->getMetaValue($account, 'liability_direction'); $liabilityDirection = $this->repository->getMetaValue($account, 'liability_direction');
$convertToNative = Amount::convertToNative();
// get account role (will only work if the type is asset. // get account role (will only work if the type is asset).
$default = Amount::getDefaultCurrency();
$accountRole = $this->getAccountRole($account, $accountType); $accountRole = $this->getAccountRole($account, $accountType);
$date = $this->getDate(); $date = $this->getDate();
$date->endOfDay(); $date->endOfDay();
[$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account); [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account, $default);
[$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType); [$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType);
[$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType); [$openingBalance, $nativeOpeningBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $convertToNative);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType); [$interest, $interestPeriod] = $this->getInterest($account, $accountType);
if (!$convertToNative) {
// reset default currency to NULL, not interesting.
$default = null;
}
$openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces); $openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces);
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth'); $includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth');
$longitude = null; $longitude = null;
@@ -90,6 +99,14 @@ class AccountTransformer extends AbstractTransformer
if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) { if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) {
$order = null; $order = null;
} }
// balance, native balance, virtual balance, native virtual balance?
$finalBalance = Steam::finalAccountBalance($account, $date);
if($convertToNative) {
$finalBalance['balance'] = $finalBalance[$currencyCode] ?? '0';
}
$currentBalance = app('steam')->bcround($finalBalance['balance'] ?? '0', $decimalPlaces);
$nativeCurrentBalance = $convertToNative ? app('steam')->bcround($finalBalance['native_balance'] ?? '0', $default->decimal_places) : null;
return [ return [
'id' => (string) $account->id, 'id' => (string) $account->id,
@@ -104,7 +121,12 @@ class AccountTransformer extends AbstractTransformer
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $currencySymbol,
'currency_decimal_places' => $decimalPlaces, 'currency_decimal_places' => $decimalPlaces,
'current_balance' => app('steam')->bcround(Steam::finalAccountBalance($account, $date)['balance'] ?? '0', $decimalPlaces), 'native_currency_id' => null === $default ? null : (string) $default->id,
'native_currency_code' => $default?->code,
'native_currency_symbol' => $default?->symbol,
'native_currency_decimal_places' => $default?->decimal_places,
'current_balance' => $currentBalance,
'native_current_balance' => $nativeCurrentBalance,
'current_balance_date' => $date->toAtomString(), 'current_balance_date' => $date->toAtomString(),
'notes' => $this->repository->getNoteText($account), 'notes' => $this->repository->getNoteText($account),
'monthly_payment_date' => $monthlyPaymentDate, 'monthly_payment_date' => $monthlyPaymentDate,
@@ -113,7 +135,9 @@ class AccountTransformer extends AbstractTransformer
'iban' => '' === $account->iban ? null : $account->iban, 'iban' => '' === $account->iban ? null : $account->iban,
'bic' => $this->repository->getMetaValue($account, 'BIC'), 'bic' => $this->repository->getMetaValue($account, 'BIC'),
'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces), 'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces),
'native_virtual_balance' => $convertToNative ? app('steam')->bcround($account->native_virtual_balance, $default->decimal_places) : null,
'opening_balance' => $openingBalance, 'opening_balance' => $openingBalance,
'native_opening_balance' => $nativeOpeningBalance,
'opening_balance_date' => $openingBalanceDate, 'opening_balance_date' => $openingBalanceDate,
'liability_type' => $liabilityType, 'liability_type' => $liabilityType,
'liability_direction' => $liabilityDirection, 'liability_direction' => $liabilityDirection,
@@ -156,16 +180,13 @@ class AccountTransformer extends AbstractTransformer
return $date; return $date;
} }
/** private function getCurrency(Account $account, TransactionCurrency $default): array
* @throws FireflyException
*/
private function getCurrency(Account $account): array
{ {
$currency = $this->repository->getAccountCurrency($account); $currency = $this->repository->getAccountCurrency($account);
// only grab default when result is null: // only grab default when result is null:
if (null === $currency) { if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup); $currency = $default;
} }
$currencyId = (string) $currency->id; $currencyId = (string) $currency->id;
$currencyCode = $currency->code; $currencyCode = $currency->code;
@@ -203,13 +224,14 @@ class AccountTransformer extends AbstractTransformer
/** /**
* TODO refactor call to get~OpeningBalanceAmount / Date because it is a lot of queries * TODO refactor call to get~OpeningBalanceAmount / Date because it is a lot of queries
*/ */
private function getOpeningBalance(Account $account, string $accountType): array private function getOpeningBalance(Account $account, string $accountType, bool $convertToNative): array
{ {
$openingBalance = null; $openingBalance = null;
$openingBalanceDate = null; $openingBalanceDate = null;
$nativeOpeningBalance = null;
if (in_array($accountType, ['asset', 'liabilities'], true)) { if (in_array($accountType, ['asset', 'liabilities'], true)) {
$amount = $this->repository->getOpeningBalanceAmount($account); $openingBalance = $this->repository->getOpeningBalanceAmount($account, false);
$openingBalance = $amount; $nativeOpeningBalance = $this->repository->getOpeningBalanceAmount($account, true);
$openingBalanceDate = $this->repository->getOpeningBalanceDate($account); $openingBalanceDate = $this->repository->getOpeningBalanceDate($account);
} }
if (null !== $openingBalanceDate) { if (null !== $openingBalanceDate) {
@@ -220,7 +242,7 @@ class AccountTransformer extends AbstractTransformer
$openingBalanceDate = $object->toAtomString(); $openingBalanceDate = $object->toAtomString();
} }
return [$openingBalance, $openingBalanceDate]; return [$openingBalance, $nativeOpeningBalance, $openingBalanceDate];
} }
private function getInterest(Account $account, string $accountType): array private function getInterest(Account $account, string $accountType): array