|
|
|
@@ -27,7 +27,9 @@ namespace FireflyIII\Transformers;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
|
use FireflyIII\Support\Facades\Amount;
|
|
|
|
|
use FireflyIII\Support\Facades\Steam;
|
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
|
|
|
|
@@ -62,17 +64,24 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
$liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType));
|
|
|
|
|
$liabilityType = '' === $liabilityType ? null : strtolower($liabilityType);
|
|
|
|
|
$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);
|
|
|
|
|
$date = $this->getDate();
|
|
|
|
|
$date->endOfDay();
|
|
|
|
|
|
|
|
|
|
[$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account);
|
|
|
|
|
[$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account, $default);
|
|
|
|
|
[$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);
|
|
|
|
|
|
|
|
|
|
if (!$convertToNative) {
|
|
|
|
|
// reset default currency to NULL, not interesting.
|
|
|
|
|
$default = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces);
|
|
|
|
|
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth');
|
|
|
|
|
$longitude = null;
|
|
|
|
@@ -90,6 +99,14 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) {
|
|
|
|
|
$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 [
|
|
|
|
|
'id' => (string) $account->id,
|
|
|
|
@@ -104,7 +121,12 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
'currency_code' => $currencyCode,
|
|
|
|
|
'currency_symbol' => $currencySymbol,
|
|
|
|
|
'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(),
|
|
|
|
|
'notes' => $this->repository->getNoteText($account),
|
|
|
|
|
'monthly_payment_date' => $monthlyPaymentDate,
|
|
|
|
@@ -113,7 +135,9 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
'iban' => '' === $account->iban ? null : $account->iban,
|
|
|
|
|
'bic' => $this->repository->getMetaValue($account, 'BIC'),
|
|
|
|
|
'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,
|
|
|
|
|
'native_opening_balance' => $nativeOpeningBalance,
|
|
|
|
|
'opening_balance_date' => $openingBalanceDate,
|
|
|
|
|
'liability_type' => $liabilityType,
|
|
|
|
|
'liability_direction' => $liabilityDirection,
|
|
|
|
@@ -156,16 +180,13 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
return $date;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
|
|
|
|
private function getCurrency(Account $account): array
|
|
|
|
|
private function getCurrency(Account $account, TransactionCurrency $default): array
|
|
|
|
|
{
|
|
|
|
|
$currency = $this->repository->getAccountCurrency($account);
|
|
|
|
|
|
|
|
|
|
// only grab default when result is null:
|
|
|
|
|
if (null === $currency) {
|
|
|
|
|
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
|
|
|
|
$currency = $default;
|
|
|
|
|
}
|
|
|
|
|
$currencyId = (string) $currency->id;
|
|
|
|
|
$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
|
|
|
|
|
*/
|
|
|
|
|
private function getOpeningBalance(Account $account, string $accountType): array
|
|
|
|
|
private function getOpeningBalance(Account $account, string $accountType, bool $convertToNative): array
|
|
|
|
|
{
|
|
|
|
|
$openingBalance = null;
|
|
|
|
|
$openingBalanceDate = null;
|
|
|
|
|
$nativeOpeningBalance = null;
|
|
|
|
|
if (in_array($accountType, ['asset', 'liabilities'], true)) {
|
|
|
|
|
$amount = $this->repository->getOpeningBalanceAmount($account);
|
|
|
|
|
$openingBalance = $amount;
|
|
|
|
|
$openingBalance = $this->repository->getOpeningBalanceAmount($account, false);
|
|
|
|
|
$nativeOpeningBalance = $this->repository->getOpeningBalanceAmount($account, true);
|
|
|
|
|
$openingBalanceDate = $this->repository->getOpeningBalanceDate($account);
|
|
|
|
|
}
|
|
|
|
|
if (null !== $openingBalanceDate) {
|
|
|
|
@@ -220,7 +242,7 @@ class AccountTransformer extends AbstractTransformer
|
|
|
|
|
$openingBalanceDate = $object->toAtomString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [$openingBalance, $openingBalanceDate];
|
|
|
|
|
return [$openingBalance, $nativeOpeningBalance, $openingBalanceDate];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getInterest(Account $account, string $accountType): array
|
|
|
|
|