Improve budget limit.

This commit is contained in:
James Cole
2025-08-03 17:42:07 +02:00
parent 56f1eb03e0
commit 4ff5f5883d
13 changed files with 244 additions and 71 deletions

View File

@@ -51,11 +51,11 @@ class AvailableBudgetTransformer extends AbstractTransformer
public function transform(AvailableBudget $availableBudget): array
{
$currency = $availableBudget->transactionCurrency;
$amount = app('steam')->bcround($availableBudget->amount, $currency->decimal_places);
$amount = Steam::bcround($availableBudget->amount, $currency->decimal_places);
$pcAmount = null;
if ($this->convertToPrimary) {
$pcAmount = app('steam')->bcround($availableBudget->native_amount, $this->primary->decimal_places);
$pcAmount = Steam::bcround($availableBudget->native_amount, $this->primary->decimal_places);
}
$data = [

View File

@@ -26,10 +26,8 @@ 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 FireflyIII\Support\Facades\Steam;
use League\Fractal\Resource\Item;
/**
@@ -42,11 +40,11 @@ class BudgetLimitTransformer extends AbstractTransformer
'budget',
];
protected bool $convertToPrimary;
protected TransactionCurrency $primary;
protected TransactionCurrency $primaryCurrency;
public function __construct()
{
$this->primary = Amount::getPrimaryCurrency();
$this->primaryCurrency = Amount::getPrimaryCurrency();
$this->convertToPrimary = Amount::convertToPrimary();
}
@@ -65,65 +63,53 @@ class BudgetLimitTransformer extends AbstractTransformer
*/
public function transform(BudgetLimit $budgetLimit): array
{
$repository = app(OperationsRepository::class);
$limitRepos = app(BudgetLimitRepositoryInterface::class);
$repository->setUser($budgetLimit->budget->user);
$limitRepos->setUser($budgetLimit->budget->user);
$expenses = $repository->sumExpenses(
$budgetLimit->start_date,
$budgetLimit->end_date,
null,
new Collection([$budgetLimit->budget]),
$budgetLimit->transactionCurrency
);
$currency = $budgetLimit->transactionCurrency;
$amount = $budgetLimit->amount;
$notes = $limitRepos->getNoteText($budgetLimit);
$currencyDecimalPlaces = 2;
$currencyId = null;
$currencyName = null;
$currencyCode = null;
$currencySymbol = null;
if (null !== $currency) {
$amount = $budgetLimit->amount;
$currencyId = $currency->id;
$currencyName = $currency->name;
$currencyCode = $currency->code;
$currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places;
}
$amount = app('steam')->bcround($amount, $currencyDecimalPlaces);
$primary = $this->primary;
if (!$this->convertToPrimary) {
$primary = null;
}
$currency = $budgetLimit->transactionCurrency;
if (null === $currency) {
$currency = $this->primaryCurrency;
}
$amount = Steam::bcround($budgetLimit->amount, $currency->decimal_places);
$pcAmount = null;
if ($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) {
$pcAmount = $amount;
}
if ($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) {
$pcAmount = Steam::bcround($budgetLimit->native_amount, $this->primaryCurrency->decimal_places);
}
return [
'id' => (string)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start' => $budgetLimit->start_date->toAtomString(),
'end' => $budgetLimit->end_date->endOfDay()->toAtomString(),
'budget_id' => (string)$budgetLimit->budget_id,
'currency_id' => (string)$currencyId,
'currency_code' => $currencyCode,
'currency_name' => $currencyName,
'currency_decimal_places' => $currencyDecimalPlaces,
'currency_symbol' => $currencySymbol,
'primary_currency_id' => $primary instanceof TransactionCurrency ? (string)$primary->id : null,
'primary_currency_code' => $primary?->code,
'primary_currency_symbol' => $primary?->symbol,
'primary_currency_decimal_places' => $primary?->decimal_places,
'amount' => $amount,
'pc_amount' => $this->convertToPrimary ? app('steam')->bcround($budgetLimit->native_amount, $primary->decimal_places) : null,
'period' => $budgetLimit->period,
'spent' => $expenses[$currencyId]['sum'] ?? '0', // will be in primary currency if convertToPrimary.
'notes' => '' === $notes ? null : $notes,
'links' => [
'id' => (string)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start' => $budgetLimit->start_date->toAtomString(),
'end' => $budgetLimit->end_date->endOfDay()->toAtomString(),
'budget_id' => (string)$budgetLimit->budget_id,
// currency settings according to 6.3.0
'object_has_currency_setting' => true,
'currency_id' => (string)$currency->id,
'currency_name' => $currency->name,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'primary_currency_id' => (int)$this->primaryCurrency->id,
'primary_currency_name' => $this->primaryCurrency->name,
'primary_currency_code' => $this->primaryCurrency->code,
'primary_currency_symbol' => $this->primaryCurrency->symbol,
'primary_currency_decimal_places' => $this->primaryCurrency->decimal_places,
'amount' => $amount,
'pc_amount' => $pcAmount,
'period' => $budgetLimit->period,
'spent' => $budgetLimit->meta['spent'],
'pc_spent' => $budgetLimit->meta['pc_spent'],
'notes' => $budgetLimit->meta['notes'],
'links' => [
[
'rel' => 'self',
'uri' => '/budgets/limits/'.$budgetLimit->id,
'uri' => '/budgets/limits/' . $budgetLimit->id,
],
],
];

View File

@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -91,8 +92,8 @@ class BudgetTransformer extends AbstractTransformer
}
if (null !== $autoBudget) {
$abType = $types[$autoBudget->auto_budget_type];
$abAmount = app('steam')->bcround($autoBudget->amount, $currency->decimal_places);
$abPrimary = $this->convertToPrimary ? app('steam')->bcround($autoBudget->native_amount, $primary->decimal_places) : null;
$abAmount = Steam::bcround($autoBudget->amount, $currency->decimal_places);
$abPrimary = $this->convertToPrimary ? Steam::bcround($autoBudget->native_amount, $primary->decimal_places) : null;
$abPeriod = $autoBudget->period;
}
@@ -136,7 +137,7 @@ class BudgetTransformer extends AbstractTransformer
{
$return = [];
foreach ($array as $data) {
$data['sum'] = app('steam')->bcround($data['sum'], (int) $data['currency_decimal_places']);
$data['sum'] = Steam::bcround($data['sum'], (int) $data['currency_decimal_places']);
$return[] = $data;
}

View File

@@ -99,7 +99,7 @@ class CategoryTransformer extends AbstractTransformer
{
$return = [];
foreach ($array as $data) {
$data['sum'] = app('steam')->bcround($data['sum'], (int)$data['currency_decimal_places']);
$data['sum'] = Steam::bcround($data['sum'], (int)$data['currency_decimal_places']);
$return[] = $data;
}

View File

@@ -75,7 +75,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
'id' => (string) $event->id,
'created_at' => $event->created_at?->toAtomString(),
'updated_at' => $event->updated_at?->toAtomString(),
'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
'amount' => Steam::bcround($event->amount, $currency->decimal_places),
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,

View File

@@ -88,9 +88,9 @@ class PiggyBankTransformer extends AbstractTransformer
if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
$leftToSave = bcsub($piggyBank->target_amount, $currentAmount);
$percentage = (int) bcmul(bcdiv($currentAmount, $targetAmount), '100');
$targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places);
$leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places);
$savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
$targetAmount = Steam::bcround($targetAmount, $currency->decimal_places);
$leftToSave = Steam::bcround($leftToSave, $currency->decimal_places);
$savePerMonth = Steam::bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
}
$startDate = $piggyBank->start_date?->format('Y-m-d');
$targetDate = $piggyBank->target_date?->format('Y-m-d');

View File

@@ -193,10 +193,10 @@ class RecurrenceTransformer extends AbstractTransformer
$destinationType = $destinationAccount->accountType->type;
$destinationIban = $destinationAccount->iban;
}
$amount = app('steam')->bcround($transaction->amount, $transaction->transactionCurrency->decimal_places);
$amount = Steam::bcround($transaction->amount, $transaction->transactionCurrency->decimal_places);
$foreignAmount = null;
if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) {
$foreignAmount = app('steam')->bcround($transaction->foreign_amount, $foreignCurrencyDp);
$foreignAmount = Steam::bcround($transaction->foreign_amount, $foreignCurrencyDp);
}
$transactionArray = [
'id' => (string) $transaction->id,