Files
firefly-iii/app/Transformers/BudgetLimitTransformer.php

124 lines
4.7 KiB
PHP
Raw Normal View History

2018-06-24 13:20:29 +02:00
<?php
2018-06-24 13:20:29 +02:00
/**
* BudgetLimitTransformer.php
2020-02-16 13:57:18 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 13:20:29 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 13:20:29 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-06-24 13:20:29 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 13:20:29 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-06-24 13:20:29 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-24 13:20:29 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-06-30 05:21:21 +02:00
2018-06-24 13:20:29 +02:00
use FireflyIII\Models\BudgetLimit;
2025-01-18 17:20:39 +01:00
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
2025-08-03 17:42:07 +02:00
use FireflyIII\Support\Facades\Steam;
2025-08-17 11:46:03 +02:00
use FireflyIII\Support\JsonApi\Enrichments\BudgetEnrichment;
2020-11-15 14:02:29 +01:00
use League\Fractal\Resource\Item;
2018-06-24 13:20:29 +02:00
/**
* Class BudgetLimitTransformer
*/
2018-12-16 13:55:19 +01:00
class BudgetLimitTransformer extends AbstractTransformer
2018-06-24 13:20:29 +02:00
{
2025-07-31 20:35:44 +02:00
protected array $availableIncludes
2020-11-15 14:02:29 +01:00
= [
'budget',
];
2025-07-31 20:35:44 +02:00
protected bool $convertToPrimary;
2025-08-03 17:42:07 +02:00
protected TransactionCurrency $primaryCurrency;
2025-01-18 17:20:39 +01:00
public function __construct()
{
2025-08-03 17:42:07 +02:00
$this->primaryCurrency = Amount::getPrimaryCurrency();
2025-07-31 20:35:44 +02:00
$this->convertToPrimary = Amount::convertToPrimary();
2025-01-18 17:20:39 +01:00
}
2020-11-15 14:02:29 +01:00
/**
* Include Budget
*
* @return Item
*/
public function includeBudget(BudgetLimit $limit)
{
2025-08-17 11:46:03 +02:00
// enrich budget
$budget = $limit->budget;
$enrichment = new BudgetEnrichment();
2025-08-17 11:46:03 +02:00
$enrichment->setStart($this->parameters->get('start'));
$enrichment->setEnd($this->parameters->get('end'));
$enrichment->setUser($budget->user);
$budget = $enrichment->enrichSingle($budget);
2025-08-17 11:46:03 +02:00
return $this->item($budget, new BudgetTransformer(), 'budgets');
2020-11-15 14:02:29 +01:00
}
2018-06-24 13:20:29 +02:00
/**
* Transform the note.
*/
public function transform(BudgetLimit $budgetLimit): array
{
2025-08-03 17:42:07 +02:00
2025-08-07 19:37:36 +02:00
$currency = $budgetLimit->meta['currency'];
2025-08-03 17:42:07 +02:00
$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);
2025-01-18 17:20:39 +01:00
}
2020-10-23 19:11:25 +02:00
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,
2025-08-03 17:42:07 +02:00
// currency settings according to 6.3.0
'object_has_currency_setting' => true,
2025-08-03 17:42:07 +02:00
'currency_id' => (string)$currency->id,
'currency_name' => $currency->name,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
2025-08-03 17:42:07 +02:00
2025-08-17 16:47:39 +02:00
'primary_currency_id' => (string) $this->primaryCurrency->id,
2025-08-03 17:42:07 +02:00
'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' => [
2018-06-24 13:20:29 +02:00
[
'rel' => 'self',
'uri' => '/budgets/limits/'.$budgetLimit->id,
2018-06-24 13:20:29 +02:00
],
],
];
}
}