firefly-iii/app/Transformers/BudgetLimitTransformer.php

111 lines
3.9 KiB
PHP
Raw Normal View History

2018-06-24 06:20:29 -05:00
<?php
/**
* BudgetLimitTransformer.php
2020-02-16 06:57:18 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 06:20:29 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 06:20:29 -05: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 06:20:29 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 06:20:29 -05: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 06:20:29 -05: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 06:20:29 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-06-29 22:21:21 -05:00
2018-06-24 06:20:29 -05:00
use FireflyIII\Models\BudgetLimit;
2020-11-19 23:24:08 -06:00
use FireflyIII\Repositories\Budget\OperationsRepository;
use Illuminate\Support\Collection;
2020-11-15 07:02:29 -06:00
use League\Fractal\Resource\Item;
2018-06-24 06:20:29 -05:00
/**
* Class BudgetLimitTransformer
*/
2018-12-16 06:55:19 -06:00
class BudgetLimitTransformer extends AbstractTransformer
2018-06-24 06:20:29 -05:00
{
2020-11-15 07:02:29 -06:00
/** @var string[] */
protected $availableIncludes
= [
'budget',
];
/**
* Include Budget
*
* @param BudgetLimit $limit
*
* @return Item
*/
public function includeBudget(BudgetLimit $limit)
{
2020-11-19 23:24:08 -06:00
return $this->item($limit->budget, new BudgetTransformer, 'budgets');
2020-11-15 07:02:29 -06:00
}
2018-06-24 06:20:29 -05:00
/**
* Transform the note.
*
* @param BudgetLimit $budgetLimit
*
* @return array
*/
public function transform(BudgetLimit $budgetLimit): array
{
2020-11-19 23:24:08 -06:00
$repository = app(OperationsRepository::class);
$repository->setUser($budgetLimit->budget->user);
2021-03-21 03:15:40 -05:00
$expenses = $repository->sumExpenses(
$budgetLimit->start_date, $budgetLimit->end_date, null, new Collection([$budgetLimit->budget]), $budgetLimit->transactionCurrency
);
2020-11-19 23:24:08 -06:00
2020-07-12 10:32:48 -05:00
$currency = $budgetLimit->transactionCurrency;
$amount = $budgetLimit->amount;
$currencyDecimalPlaces = 2;
$currencyId = null;
$currencyName = null;
$currencyCode = null;
$currencySymbol = null;
2018-12-09 01:45:53 -06:00
if (null !== $currency) {
2020-07-12 10:32:48 -05:00
$amount = $budgetLimit->amount;
2020-11-15 07:02:29 -06:00
$currencyId = (int)$currency->id;
2020-07-12 10:32:48 -05:00
$currencyName = $currency->name;
$currencyCode = $currency->code;
$currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places;
2018-12-09 01:45:53 -06:00
}
2020-11-15 07:02:29 -06:00
$amount = number_format((float)$amount, $currencyDecimalPlaces, '.', '');
2020-10-23 12:11:25 -05:00
return [
2021-03-21 03:15:40 -05:00
'id' => (string)$budgetLimit->id,
2020-07-12 10:32:48 -05:00
'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start' => $budgetLimit->start_date->format('Y-m-d'),
'end' => $budgetLimit->end_date->format('Y-m-d'),
2021-03-06 01:40:51 -06:00
'budget_id' => (string)$budgetLimit->budget_id,
2021-03-21 03:15:40 -05:00
'currency_id' => (string)$currencyId,
2020-07-12 10:32:48 -05:00
'currency_code' => $currencyCode,
'currency_name' => $currencyName,
2021-03-06 01:40:51 -06:00
'currency_decimal_places' => $currencyDecimalPlaces,
2020-07-12 10:32:48 -05:00
'currency_symbol' => $currencySymbol,
'amount' => $amount,
2020-11-19 23:24:08 -06:00
'period' => $budgetLimit->period,
'spent' => $expenses[$currencyId]['sum'] ?? '0',
2020-07-12 10:32:48 -05:00
'links' => [
2018-06-24 06:20:29 -05:00
[
'rel' => 'self',
2018-12-09 01:45:53 -06:00
'uri' => '/budgets/limits/' . $budgetLimit->id,
2018-06-24 06:20:29 -05:00
],
],
];
}
}