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,14 +39,18 @@ use Symfony\Component\HttpFoundation\ParameterBag;
class AccountTransformer extends AbstractTransformer class AccountTransformer extends AbstractTransformer
{ {
protected AccountRepositoryInterface $repository; protected AccountRepositoryInterface $repository;
protected bool $convertToNative;
protected TransactionCurrency $default;
/** /**
* AccountTransformer constructor. * AccountTransformer constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->parameters = new ParameterBag(); $this->parameters = new ParameterBag();
$this->repository = app(AccountRepositoryInterface::class); $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 = (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, $default); [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces] = $this->getCurrency($account);
[$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType); [$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); [$interest, $interestPeriod] = $this->getInterest($account, $accountType);
if (!$convertToNative) { $default = $this->default;
if (!$this->convertToNative) {
// reset default currency to NULL, not interesting. // reset default currency to NULL, not interesting.
$default = null; $default = null;
} }
@@ -100,13 +103,13 @@ class AccountTransformer extends AbstractTransformer
$order = null; $order = null;
} }
// balance, native balance, virtual balance, native virtual balance? // balance, native balance, virtual balance, native virtual balance?
$finalBalance = Steam::finalAccountBalance($account, $date); $finalBalance = Steam::finalAccountBalance($account, $date);
if($convertToNative) { if ($this->convertToNative) {
$finalBalance['balance'] = $finalBalance[$currencyCode] ?? '0'; $finalBalance['balance'] = $finalBalance[$currencyCode] ?? '0';
} }
$currentBalance = app('steam')->bcround($finalBalance['balance'] ?? '0', $decimalPlaces); $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 [ return [
'id' => (string) $account->id, 'id' => (string) $account->id,
@@ -135,7 +138,7 @@ 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, 'native_virtual_balance' => $this->convertToNative ? app('steam')->bcround($account->native_virtual_balance, $default->decimal_places) : null,
'opening_balance' => $openingBalance, 'opening_balance' => $openingBalance,
'native_opening_balance' => $nativeOpeningBalance, 'native_opening_balance' => $nativeOpeningBalance,
'opening_balance_date' => $openingBalanceDate, 'opening_balance_date' => $openingBalanceDate,
@@ -180,13 +183,13 @@ class AccountTransformer extends AbstractTransformer
return $date; return $date;
} }
private function getCurrency(Account $account, TransactionCurrency $default): array 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 = $default; $currency = $this->default;
} }
$currencyId = (string) $currency->id; $currencyId = (string) $currency->id;
$currencyCode = $currency->code; $currencyCode = $currency->code;

View File

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

View File

@@ -28,8 +28,10 @@ use Carbon\Carbon;
use Carbon\CarbonInterface; use Carbon\CarbonInterface;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\ObjectGroup; use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Models\BillDateCalculator; use FireflyIII\Support\Models\BillDateCalculator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -40,14 +42,18 @@ class BillTransformer extends AbstractTransformer
{ {
private BillDateCalculator $calculator; private BillDateCalculator $calculator;
private BillRepositoryInterface $repository; private BillRepositoryInterface $repository;
private TransactionCurrency $default;
private bool $convertToNative;
/** /**
* BillTransformer constructor. * BillTransformer constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->repository = app(BillRepositoryInterface::class); $this->repository = app(BillRepositoryInterface::class);
$this->calculator = app(BillDateCalculator::class); $this->calculator = app(BillDateCalculator::class);
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
} }
/** /**
@@ -58,23 +64,23 @@ class BillTransformer extends AbstractTransformer
*/ */
public function transform(Bill $bill): array 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); $paidData = $this->paidData($bill);
$lastPaidDate = $this->getLastPaidDate($paidData); $lastPaidDate = $this->getLastPaidDate($paidData);
$start = $this->parameters->get('start') ?? today()->subYears(10); $start = $this->parameters->get('start') ?? today()->subYears(10);
$end = $this->parameters->get('end') ?? today()->addYears(10); $end = $this->parameters->get('end') ?? today()->addYears(10);
$payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate); $payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate);
$currency = $bill->transactionCurrency; $currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill); $notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes; $notes = '' === $notes ? null : $notes;
$objectGroupId = null; $objectGroupId = null;
$objectGroupOrder = null; $objectGroupOrder = null;
$objectGroupTitle = null; $objectGroupTitle = null;
$this->repository->setUser($bill->user); $this->repository->setUser($bill->user);
/** @var null|ObjectGroup $objectGroup */ /** @var null|ObjectGroup $objectGroup */
$objectGroup = $bill->objectGroups->first(); $objectGroup = $bill->objectGroups->first();
if (null !== $objectGroup) { if (null !== $objectGroup) {
$objectGroupId = $objectGroup->id; $objectGroupId = $objectGroup->id;
$objectGroupOrder = $objectGroup->order; $objectGroupOrder = $objectGroup->order;
@@ -84,7 +90,7 @@ class BillTransformer extends AbstractTransformer
$paidDataFormatted = []; $paidDataFormatted = [];
$payDatesFormatted = []; $payDatesFormatted = [];
foreach ($paidData as $object) { foreach ($paidData as $object) {
$date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone')); $date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'));
if (null === $date) { if (null === $date) {
$date = today(config('app.timezone')); $date = today(config('app.timezone'));
} }
@@ -93,24 +99,24 @@ class BillTransformer extends AbstractTransformer
} }
foreach ($payDates as $string) { foreach ($payDates as $string) {
$date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone')); $date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'));
if (null === $date) { if (null === $date) {
$date = today(config('app.timezone')); $date = today(config('app.timezone'));
} }
$payDatesFormatted[] = $date->toAtomString(); $payDatesFormatted[] = $date->toAtomString();
} }
// next expected match // next expected match
$nem = null; $nem = null;
$nemDate = null; $nemDate = null;
$nemDiff = trans('firefly.not_expected_period'); $nemDiff = trans('firefly.not_expected_period');
$firstPayDate = $payDates[0] ?? null; $firstPayDate = $payDates[0] ?? null;
if (null !== $firstPayDate) { if (null !== $firstPayDate) {
$nemDate = Carbon::createFromFormat('!Y-m-d', $firstPayDate, config('app.timezone')); $nemDate = Carbon::createFromFormat('!Y-m-d', $firstPayDate, config('app.timezone'));
if (null === $nemDate) { if (null === $nemDate) {
$nemDate = today(config('app.timezone')); $nemDate = today(config('app.timezone'));
} }
$nem = $nemDate->toAtomString(); $nem = $nemDate->toAtomString();
// nullify again when it's outside the current view range. // nullify again when it's outside the current view range.
if ( if (
@@ -131,7 +137,7 @@ class BillTransformer extends AbstractTransformer
$current = $payDatesFormatted[0] ?? null; $current = $payDatesFormatted[0] ?? null;
if (null !== $current && !$nemDate->isToday()) { if (null !== $current && !$nemDate->isToday()) {
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current); $temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
if (null === $temp2) { if (null === $temp2) {
$temp2 = today(config('app.timezone')); $temp2 = today(config('app.timezone'));
} }
@@ -141,18 +147,22 @@ class BillTransformer extends AbstractTransformer
} }
return [ return [
'id' => $bill->id, 'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(), 'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(), 'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => (string) $bill->transaction_currency_id, 'currency_id' => (string) $bill->transaction_currency_id,
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, '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, 'name' => $bill->name,
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places), 'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $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_min' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_min, $default->decimal_places) : null,
'native_amount_max' => app('steam')->bcround($bill->native_amount_max, $defaultCurrency->decimal_places), 'native_amount_max' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_max, $default->decimal_places) : null,
'date' => $bill->date->toAtomString(), 'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(), 'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(), 'extension_date' => $bill->extension_date?->toAtomString(),
@@ -173,7 +183,7 @@ class BillTransformer extends AbstractTransformer
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/bills/'.$bill->id, 'uri' => '/bills/' . $bill->id,
], ],
], ],
]; ];
@@ -195,13 +205,13 @@ class BillTransformer extends AbstractTransformer
// 2023-07-18 this particular date is used to search for the last paid date. // 2023-07-18 this particular date is used to search for the last paid date.
// 2023-07-18 the cloned $searchDate is used to grab the correct transactions. // 2023-07-18 the cloned $searchDate is used to grab the correct transactions.
/** @var Carbon $start */ /** @var Carbon $start */
$start = clone $this->parameters->get('start'); $start = clone $this->parameters->get('start');
$searchStart = clone $start; $searchStart = clone $start;
$start->subDay(); $start->subDay();
/** @var Carbon $end */ /** @var Carbon $end */
$end = clone $this->parameters->get('end'); $end = clone $this->parameters->get('end');
$searchEnd = clone $end; $searchEnd = clone $end;
// move the search dates to the start of the day. // move the search dates to the start of the day.
$searchStart->startOfDay(); $searchStart->startOfDay();
@@ -211,7 +221,7 @@ class BillTransformer extends AbstractTransformer
app('log')->debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d'))); app('log')->debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d')));
// Get from database when bill was paid. // Get from database when bill was paid.
$set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd); $set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd);
app('log')->debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count())); app('log')->debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
// Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date. // Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
@@ -220,7 +230,7 @@ class BillTransformer extends AbstractTransformer
app('log')->debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d'))); app('log')->debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d')));
// At this point the "next match" is exactly after the last time the bill was paid. // At this point the "next match" is exactly after the last time the bill was paid.
$result = []; $result = [];
foreach ($set as $entry) { foreach ($set as $entry) {
$result[] = [ $result[] = [
'transaction_group_id' => (string) $entry->transaction_group_id, 'transaction_group_id' => (string) $entry->transaction_group_id,

View File

@@ -25,8 +25,10 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepository; use FireflyIII\Repositories\Budget\OperationsRepository;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
@@ -40,6 +42,15 @@ class BudgetLimitTransformer extends AbstractTransformer
'budget', 'budget',
]; ];
protected TransactionCurrency $default;
protected bool $convertToNative;
public function __construct()
{
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
}
/** /**
* Include Budget * Include Budget
* *
@@ -55,8 +66,8 @@ class BudgetLimitTransformer extends AbstractTransformer
*/ */
public function transform(BudgetLimit $budgetLimit): array public function transform(BudgetLimit $budgetLimit): array
{ {
$repository = app(OperationsRepository::class); $repository = app(OperationsRepository::class);
$limitRepos = app(BudgetLimitRepositoryInterface::class); $limitRepos = app(BudgetLimitRepositoryInterface::class);
$repository->setUser($budgetLimit->budget->user); $repository->setUser($budgetLimit->budget->user);
$limitRepos->setUser($budgetLimit->budget->user); $limitRepos->setUser($budgetLimit->budget->user);
$expenses = $repository->sumExpenses( $expenses = $repository->sumExpenses(
@@ -82,28 +93,38 @@ class BudgetLimitTransformer extends AbstractTransformer
$currencySymbol = $currency->symbol; $currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places; $currencyDecimalPlaces = $currency->decimal_places;
} }
$amount = app('steam')->bcround($amount, $currencyDecimalPlaces); $amount = app('steam')->bcround($amount, $currencyDecimalPlaces);
$default = $this->default;
if (!$this->convertToNative) {
$default = null;
}
return [ return [
'id' => (string) $budgetLimit->id, 'id' => (string) $budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(), 'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(), 'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start' => $budgetLimit->start_date->toAtomString(), 'start' => $budgetLimit->start_date->toAtomString(),
'end' => $budgetLimit->end_date->endOfDay()->toAtomString(), 'end' => $budgetLimit->end_date->endOfDay()->toAtomString(),
'budget_id' => (string) $budgetLimit->budget_id, 'budget_id' => (string) $budgetLimit->budget_id,
'currency_id' => (string) $currencyId, 'currency_id' => (string) $currencyId,
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'currency_name' => $currencyName, 'currency_name' => $currencyName,
'currency_decimal_places' => $currencyDecimalPlaces, 'currency_decimal_places' => $currencyDecimalPlaces,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $currencySymbol,
'amount' => $amount, 'native_currency_id' => null === $default ? null : (string) $default->id,
'period' => $budgetLimit->period, 'native_currency_code' => $default?->code,
'spent' => $expenses[$currencyId]['sum'] ?? '0', 'native_currency_symbol' => $default?->symbol,
'notes' => '' === $notes ? null : $notes, 'native_currency_decimal_places' => $default?->decimal_places,
'links' => [ 'amount' => $amount,
'native_amount' => $this->convertToNative ? app('steam')->bcround($budgetLimit->native_amount, $default->decimal_places) : null,
'period' => $budgetLimit->period,
'spent' => $expenses[$currencyId]['sum'] ?? '0', // will be in native if convertToNative.
'notes' => '' === $notes ? null : $notes,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/budgets/limits/'.$budgetLimit->id, 'uri' => '/budgets/limits/' . $budgetLimit->id,
], ],
], ],
]; ];

View File

@@ -26,8 +26,10 @@ namespace FireflyIII\Transformers;
use FireflyIII\Enums\AutoBudgetType; use FireflyIII\Enums\AutoBudgetType;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@@ -38,15 +40,19 @@ class BudgetTransformer extends AbstractTransformer
{ {
private OperationsRepositoryInterface $opsRepository; private OperationsRepositoryInterface $opsRepository;
private BudgetRepositoryInterface $repository; private BudgetRepositoryInterface $repository;
private bool $convertToNative;
private TransactionCurrency $default;
/** /**
* BudgetTransformer constructor. * BudgetTransformer constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(BudgetRepositoryInterface::class); $this->repository = app(BudgetRepositoryInterface::class);
$this->parameters = new ParameterBag(); $this->parameters = new ParameterBag();
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
} }
/** /**
@@ -55,53 +61,71 @@ class BudgetTransformer extends AbstractTransformer
public function transform(Budget $budget): array public function transform(Budget $budget): array
{ {
$this->opsRepository->setUser($budget->user); $this->opsRepository->setUser($budget->user);
$start = $this->parameters->get('start'); $start = $this->parameters->get('start');
$end = $this->parameters->get('end'); $end = $this->parameters->get('end');
$autoBudget = $this->repository->getAutoBudget($budget); $autoBudget = $this->repository->getAutoBudget($budget);
$spent = []; $spent = [];
if (null !== $start && null !== $end) { if (null !== $start && null !== $end) {
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]))); $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
} }
$abCurrencyId = null; // info for auto budget.
$abCurrencyCode = null; $abType = null;
$abType = null; $abAmount = null;
$abAmount = null; $abNative = null;
$abPeriod = null; $abPeriod = null;
$notes = $this->repository->getNoteText($budget); $notes = $this->repository->getNoteText($budget);
$types = [ $types = [
AutoBudgetType::AUTO_BUDGET_RESET->value => 'reset', AutoBudgetType::AUTO_BUDGET_RESET->value => 'reset',
AutoBudgetType::AUTO_BUDGET_ROLLOVER->value => 'rollover', AutoBudgetType::AUTO_BUDGET_ROLLOVER->value => 'rollover',
AutoBudgetType::AUTO_BUDGET_ADJUSTED->value => 'adjusted', 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) { if (null !== $autoBudget) {
$abCurrencyId = (string) $autoBudget->transactionCurrency->id; $abType = $types[$autoBudget->auto_budget_type];
$abCurrencyCode = $autoBudget->transactionCurrency->code; $abAmount = app('steam')->bcround($autoBudget->amount, $currency->decimal_places);
$abType = $types[$autoBudget->auto_budget_type]; $abNative = $this->convertToNative ? app('steam')->bcround($autoBudget->native_amount, $default->decimal_places) : null;
$abAmount = app('steam')->bcround($autoBudget->amount, $autoBudget->transactionCurrency->decimal_places); $abPeriod = $autoBudget->period;
$abPeriod = $autoBudget->period;
} }
return [ return [
'id' => (string) $budget->id, 'id' => (string) $budget->id,
'created_at' => $budget->created_at->toAtomString(), 'created_at' => $budget->created_at->toAtomString(),
'updated_at' => $budget->updated_at->toAtomString(), 'updated_at' => $budget->updated_at->toAtomString(),
'active' => $budget->active, 'active' => $budget->active,
'name' => $budget->name, 'name' => $budget->name,
'order' => $budget->order, 'order' => $budget->order,
'notes' => $notes, 'notes' => $notes,
'auto_budget_type' => $abType, 'auto_budget_type' => $abType,
'auto_budget_period' => $abPeriod, 'auto_budget_period' => $abPeriod,
'auto_budget_currency_id' => $abCurrencyId,
'auto_budget_currency_code' => $abCurrencyCode, 'currency_id' => null === $autoBudget ? null : (string) $autoBudget->transactionCurrency->id,
'auto_budget_amount' => $abAmount, 'currency_code' => $autoBudget?->transactionCurrency->code,
'spent' => $spent, 'currency_name' => $autoBudget?->transactionCurrency->name,
'links' => [ '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,
'native_auto_budget_amount' => $abNative,
'spent' => $spent, // always in native.
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/budgets/'.$budget->id, 'uri' => '/budgets/' . $budget->id,
], ],
], ],
]; ];

View File

@@ -25,8 +25,10 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface; use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** /**
@@ -36,14 +38,18 @@ class CategoryTransformer extends AbstractTransformer
{ {
private OperationsRepositoryInterface $opsRepository; private OperationsRepositoryInterface $opsRepository;
private CategoryRepositoryInterface $repository; private CategoryRepositoryInterface $repository;
private TransactionCurrency $default;
private bool $convertToNative;
/** /**
* CategoryTransformer constructor. * CategoryTransformer constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(CategoryRepositoryInterface::class); $this->repository = app(CategoryRepositoryInterface::class);
$this->default = Amount::getDefaultCurrency();
$this->convertToNative = Amount::convertToNative();
} }
/** /**
@@ -62,20 +68,28 @@ class CategoryTransformer extends AbstractTransformer
$earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category]))); $earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category])));
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category]))); $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category])));
} }
$notes = $this->repository->getNoteText($category); $default = $this->default;
if (!$this->convertToNative) {
$default = null;
}
$notes = $this->repository->getNoteText($category);
return [ return [
'id' => $category->id, 'id' => $category->id,
'created_at' => $category->created_at->toAtomString(), 'created_at' => $category->created_at->toAtomString(),
'updated_at' => $category->updated_at->toAtomString(), 'updated_at' => $category->updated_at->toAtomString(),
'name' => $category->name, 'name' => $category->name,
'notes' => $notes, 'notes' => $notes,
'spent' => $spent, 'native_currency_id' => null === $default ? null : (string) $default->id,
'earned' => $earned, 'native_currency_code' => $default?->code,
'links' => [ 'native_currency_symbol' => $default?->symbol,
'native_currency_decimal_places' => $default?->decimal_places,
'spent' => $spent,
'earned' => $earned,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/categories/'.$category->id, 'uri' => '/categories/' . $category->id,
], ],
], ],
]; ];