Commit updated transformers.

This commit is contained in:
James Cole
2025-01-18 17:20:39 +01:00
parent 23d70a2fac
commit f69b9ac9da
6 changed files with 220 additions and 133 deletions

View File

@@ -39,6 +39,8 @@ use Symfony\Component\HttpFoundation\ParameterBag;
class AccountTransformer extends AbstractTransformer
{
protected AccountRepositoryInterface $repository;
protected bool $convertToNative;
protected TransactionCurrency $default;
/**
* AccountTransformer constructor.
@@ -47,6 +49,8 @@ class AccountTransformer extends AbstractTransformer
{
$this->parameters = new ParameterBag();
$this->repository = app(AccountRepositoryInterface::class);
$this->convertToNative = Amount::convertToNative();
$this->default = Amount::getDefaultCurrency();
}
/**
@@ -64,20 +68,19 @@ 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).
$default = Amount::getDefaultCurrency();
$accountRole = $this->getAccountRole($account, $accountType);
$date = $this->getDate();
$date->endOfDay();
[$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account, $default);
[$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account);
[$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType);
[$openingBalance, $nativeOpeningBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $convertToNative);
[$openingBalance, $nativeOpeningBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $this->convertToNative);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType);
if (!$convertToNative) {
$default = $this->default;
if (!$this->convertToNative) {
// reset default currency to NULL, not interesting.
$default = null;
}
@@ -101,12 +104,12 @@ class AccountTransformer extends AbstractTransformer
}
// balance, native balance, virtual balance, native virtual balance?
$finalBalance = Steam::finalAccountBalance($account, $date);
if($convertToNative) {
if ($this->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;
$nativeCurrentBalance = $this->convertToNative ? app('steam')->bcround($finalBalance['native_balance'] ?? '0', $default->decimal_places) : null;
return [
'id' => (string) $account->id,
@@ -135,7 +138,7 @@ 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,
'native_virtual_balance' => $this->convertToNative ? app('steam')->bcround($account->native_virtual_balance, $default->decimal_places) : null,
'opening_balance' => $openingBalance,
'native_opening_balance' => $nativeOpeningBalance,
'opening_balance_date' => $openingBalanceDate,
@@ -180,13 +183,13 @@ class AccountTransformer extends AbstractTransformer
return $date;
}
private function getCurrency(Account $account, TransactionCurrency $default): array
private function getCurrency(Account $account): array
{
$currency = $this->repository->getAccountCurrency($account);
// only grab default when result is null:
if (null === $currency) {
$currency = $default;
$currency = $this->default;
}
$currencyId = (string) $currency->id;
$currencyCode = $currency->code;

View File

@@ -25,9 +25,11 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
/**
* Class AvailableBudgetTransformer
@@ -37,6 +39,8 @@ class AvailableBudgetTransformer extends AbstractTransformer
private NoBudgetRepositoryInterface $noBudgetRepository;
private OperationsRepositoryInterface $opsRepository;
private BudgetRepositoryInterface $repository;
private TransactionCurrency $default;
private bool $convertToNative;
/**
* CurrencyTransformer constructor.
@@ -46,6 +50,8 @@ class AvailableBudgetTransformer extends AbstractTransformer
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noBudgetRepository = app(NoBudgetRepositoryInterface::class);
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/**
@@ -56,6 +62,10 @@ class AvailableBudgetTransformer extends AbstractTransformer
$this->repository->setUser($availableBudget->user);
$currency = $availableBudget->transactionCurrency;
$default = $this->default;
if(!$this->convertToNative) {
$default = null;
}
$data = [
'id' => (string) $availableBudget->id,
'created_at' => $availableBudget->created_at->toAtomString(),
@@ -64,7 +74,12 @@ class AvailableBudgetTransformer extends AbstractTransformer
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'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,
'amount' => app('steam')->bcround($availableBudget->amount, $currency->decimal_places),
'native_amount' => $this->convertToNative ? app('steam')->bcround($availableBudget->native_amount, $currency->decimal_places) : null,
'start' => $availableBudget->start_date->toAtomString(),
'end' => $availableBudget->end_date->endOfDay()->toAtomString(),
'spent_in_budgets' => [],

View File

@@ -28,8 +28,10 @@ use Carbon\Carbon;
use Carbon\CarbonInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Models\BillDateCalculator;
use Illuminate\Support\Collection;
@@ -40,6 +42,8 @@ class BillTransformer extends AbstractTransformer
{
private BillDateCalculator $calculator;
private BillRepositoryInterface $repository;
private TransactionCurrency $default;
private bool $convertToNative;
/**
* BillTransformer constructor.
@@ -48,6 +52,8 @@ class BillTransformer extends AbstractTransformer
{
$this->repository = app(BillRepositoryInterface::class);
$this->calculator = app(BillDateCalculator::class);
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/**
@@ -58,7 +64,7 @@ class BillTransformer extends AbstractTransformer
*/
public function transform(Bill $bill): array
{
$defaultCurrency = $this->parameters->get('defaultCurrency') ?? app('amount')->getDefaultCurrency();
$default = $this->parameters->get('defaultCurrency') ?? $this->default;
$paidData = $this->paidData($bill);
$lastPaidDate = $this->getLastPaidDate($paidData);
@@ -148,11 +154,15 @@ class BillTransformer extends AbstractTransformer
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'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,
'name' => $bill->name,
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'native_amount_min' => app('steam')->bcround($bill->native_amount_min, $defaultCurrency->decimal_places),
'native_amount_max' => app('steam')->bcround($bill->native_amount_max, $defaultCurrency->decimal_places),
'native_amount_min' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_min, $default->decimal_places) : null,
'native_amount_max' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_max, $default->decimal_places) : null,
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),

View File

@@ -25,8 +25,10 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepository;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Item;
@@ -40,6 +42,15 @@ class BudgetLimitTransformer extends AbstractTransformer
'budget',
];
protected TransactionCurrency $default;
protected bool $convertToNative;
public function __construct()
{
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/**
* Include Budget
*
@@ -83,6 +94,11 @@ class BudgetLimitTransformer extends AbstractTransformer
$currencyDecimalPlaces = $currency->decimal_places;
}
$amount = app('steam')->bcround($amount, $currencyDecimalPlaces);
$default = $this->default;
if (!$this->convertToNative) {
$default = null;
}
return [
'id' => (string) $budgetLimit->id,
@@ -96,9 +112,14 @@ class BudgetLimitTransformer extends AbstractTransformer
'currency_name' => $currencyName,
'currency_decimal_places' => $currencyDecimalPlaces,
'currency_symbol' => $currencySymbol,
'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,
'amount' => $amount,
'native_amount' => $this->convertToNative ? app('steam')->bcround($budgetLimit->native_amount, $default->decimal_places) : null,
'period' => $budgetLimit->period,
'spent' => $expenses[$currencyId]['sum'] ?? '0',
'spent' => $expenses[$currencyId]['sum'] ?? '0', // will be in native if convertToNative.
'notes' => '' === $notes ? null : $notes,
'links' => [
[

View File

@@ -26,8 +26,10 @@ namespace FireflyIII\Transformers;
use FireflyIII\Enums\AutoBudgetType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -38,6 +40,8 @@ class BudgetTransformer extends AbstractTransformer
{
private OperationsRepositoryInterface $opsRepository;
private BudgetRepositoryInterface $repository;
private bool $convertToNative;
private TransactionCurrency $default;
/**
* BudgetTransformer constructor.
@@ -47,6 +51,8 @@ class BudgetTransformer extends AbstractTransformer
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(BudgetRepositoryInterface::class);
$this->parameters = new ParameterBag();
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/**
@@ -63,10 +69,10 @@ class BudgetTransformer extends AbstractTransformer
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
}
$abCurrencyId = null;
$abCurrencyCode = null;
// info for auto budget.
$abType = null;
$abAmount = null;
$abNative = null;
$abPeriod = null;
$notes = $this->repository->getNoteText($budget);
@@ -75,12 +81,18 @@ class BudgetTransformer extends AbstractTransformer
AutoBudgetType::AUTO_BUDGET_ROLLOVER->value => 'rollover',
AutoBudgetType::AUTO_BUDGET_ADJUSTED->value => 'adjusted',
];
$currency = $autoBudget?->transactionCurrency;
$default = $this->default;
if (!$this->convertToNative) {
$default = null;
}
if(null === $autoBudget) {
$currency = $default;
}
if (null !== $autoBudget) {
$abCurrencyId = (string) $autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type];
$abAmount = app('steam')->bcround($autoBudget->amount, $autoBudget->transactionCurrency->decimal_places);
$abAmount = app('steam')->bcround($autoBudget->amount, $currency->decimal_places);
$abNative = $this->convertToNative ? app('steam')->bcround($autoBudget->native_amount, $default->decimal_places) : null;
$abPeriod = $autoBudget->period;
}
@@ -94,10 +106,22 @@ class BudgetTransformer extends AbstractTransformer
'notes' => $notes,
'auto_budget_type' => $abType,
'auto_budget_period' => $abPeriod,
'auto_budget_currency_id' => $abCurrencyId,
'auto_budget_currency_code' => $abCurrencyCode,
'currency_id' => null === $autoBudget ? null : (string) $autoBudget->transactionCurrency->id,
'currency_code' => $autoBudget?->transactionCurrency->code,
'currency_name' => $autoBudget?->transactionCurrency->name,
'currency_decimal_places' => $autoBudget?->transactionCurrency->decimal_places,
'currency_symbol' => $autoBudget?->transactionCurrency->symbol,
'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,
// amount and native amount if present.
'auto_budget_amount' => $abAmount,
'spent' => $spent,
'native_auto_budget_amount' => $abNative,
'spent' => $spent, // always in native.
'links' => [
[
'rel' => 'self',

View File

@@ -25,8 +25,10 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection;
/**
@@ -36,6 +38,8 @@ class CategoryTransformer extends AbstractTransformer
{
private OperationsRepositoryInterface $opsRepository;
private CategoryRepositoryInterface $repository;
private TransactionCurrency $default;
private bool $convertToNative;
/**
* CategoryTransformer constructor.
@@ -44,6 +48,8 @@ class CategoryTransformer extends AbstractTransformer
{
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(CategoryRepositoryInterface::class);
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/**
@@ -62,6 +68,10 @@ class CategoryTransformer extends AbstractTransformer
$earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category])));
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category])));
}
$default = $this->default;
if (!$this->convertToNative) {
$default = null;
}
$notes = $this->repository->getNoteText($category);
return [
@@ -70,6 +80,10 @@ class CategoryTransformer extends AbstractTransformer
'updated_at' => $category->updated_at->toAtomString(),
'name' => $category->name,
'notes' => $notes,
'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,
'spent' => $spent,
'earned' => $earned,
'links' => [