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;
@@ -57,28 +59,35 @@ class AccountTransformer extends AbstractTransformer
$this->repository->setUser($account->user); $this->repository->setUser($account->user);
// get account type: // get account type:
$fullType = $account->accountType->type; $fullType = $account->accountType->type;
$accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType)); $accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType));
$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).
$accountRole = $this->getAccountRole($account, $accountType); $default = Amount::getDefaultCurrency();
$date = $this->getDate(); $accountRole = $this->getAccountRole($account, $accountType);
$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);
$openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces); if (!$convertToNative) {
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth'); // reset default currency to NULL, not interesting.
$longitude = null; $default = null;
$latitude = null; }
$zoomLevel = null;
$location = $this->repository->getLocation($account); $openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces);
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth');
$longitude = null;
$latitude = null;
$zoomLevel = null;
$location = $this->repository->getLocation($account);
if (null !== $location) { if (null !== $location) {
$longitude = $location->longitude; $longitude = $location->longitude;
$latitude = $location->latitude; $latitude = $location->latitude;
@@ -86,48 +95,63 @@ class AccountTransformer extends AbstractTransformer
} }
// no order for some accounts: // no order for some accounts:
$order = $account->order; $order = $account->order;
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,
'created_at' => $account->created_at->toAtomString(), 'created_at' => $account->created_at->toAtomString(),
'updated_at' => $account->updated_at->toAtomString(), 'updated_at' => $account->updated_at->toAtomString(),
'active' => $account->active, 'active' => $account->active,
'order' => $order, 'order' => $order,
'name' => $account->name, 'name' => $account->name,
'type' => strtolower($accountType), 'type' => strtolower($accountType),
'account_role' => $accountRole, 'account_role' => $accountRole,
'currency_id' => $currencyId, 'currency_id' => $currencyId,
'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,
'current_balance_date' => $date->toAtomString(), 'native_currency_code' => $default?->code,
'notes' => $this->repository->getNoteText($account), 'native_currency_symbol' => $default?->symbol,
'monthly_payment_date' => $monthlyPaymentDate, 'native_currency_decimal_places' => $default?->decimal_places,
'credit_card_type' => $creditCardType, 'current_balance' => $currentBalance,
'account_number' => $this->repository->getMetaValue($account, 'account_number'), 'native_current_balance' => $nativeCurrentBalance,
'iban' => '' === $account->iban ? null : $account->iban, 'current_balance_date' => $date->toAtomString(),
'bic' => $this->repository->getMetaValue($account, 'BIC'), 'notes' => $this->repository->getNoteText($account),
'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces), 'monthly_payment_date' => $monthlyPaymentDate,
'opening_balance' => $openingBalance, 'credit_card_type' => $creditCardType,
'opening_balance_date' => $openingBalanceDate, 'account_number' => $this->repository->getMetaValue($account, 'account_number'),
'liability_type' => $liabilityType, 'iban' => '' === $account->iban ? null : $account->iban,
'liability_direction' => $liabilityDirection, 'bic' => $this->repository->getMetaValue($account, 'BIC'),
'interest' => $interest, 'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces),
'interest_period' => $interestPeriod, 'native_virtual_balance' => $convertToNative ? app('steam')->bcround($account->native_virtual_balance, $default->decimal_places) : null,
'current_debt' => $this->repository->getMetaValue($account, 'current_debt'), 'opening_balance' => $openingBalance,
'include_net_worth' => $includeNetWorth, 'native_opening_balance' => $nativeOpeningBalance,
'longitude' => $longitude, 'opening_balance_date' => $openingBalanceDate,
'latitude' => $latitude, 'liability_type' => $liabilityType,
'zoom_level' => $zoomLevel, 'liability_direction' => $liabilityDirection,
'links' => [ 'interest' => $interest,
'interest_period' => $interestPeriod,
'current_debt' => $this->repository->getMetaValue($account, 'current_debt'),
'include_net_worth' => $includeNetWorth,
'longitude' => $longitude,
'latitude' => $latitude,
'zoom_level' => $zoomLevel,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/accounts/'.$account->id, 'uri' => '/accounts/' . $account->id,
], ],
], ],
]; ];
@@ -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;
@@ -186,7 +207,7 @@ class AccountTransformer extends AbstractTransformer
if (null !== $monthlyPaymentDate) { if (null !== $monthlyPaymentDate) {
// try classic date: // try classic date:
if (10 === strlen($monthlyPaymentDate)) { if (10 === strlen($monthlyPaymentDate)) {
$object = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone')); $object = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone'));
if (null === $object) { if (null === $object) {
$object = today(config('app.timezone')); $object = today(config('app.timezone'));
} }
@@ -203,24 +224,25 @@ 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) {
$object = Carbon::createFromFormat('Y-m-d H:i:s', $openingBalanceDate, config('app.timezone')); $object = Carbon::createFromFormat('Y-m-d H:i:s', $openingBalanceDate, config('app.timezone'));
if (null === $object) { if (null === $object) {
$object = today(config('app.timezone')); $object = today(config('app.timezone'));
} }
$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