firefly-iii/app/Transformers/AvailableBudgetTransformer.php

125 lines
4.3 KiB
PHP
Raw Normal View History

2018-06-24 01:33:06 -05:00
<?php
/**
* AvailableBudgetTransformer.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-06-24 01:33:06 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 01:33:06 -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 01:33:06 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 01:33:06 -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 01:33:06 -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 01:33:06 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2019-08-30 02:13:10 -05:00
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
2019-08-30 01:19:55 -05:00
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
2018-06-24 01:33:06 -05:00
2018-06-24 06:20:29 -05:00
/**
* Class AvailableBudgetTransformer
*/
2018-12-16 06:55:19 -06:00
class AvailableBudgetTransformer extends AbstractTransformer
2018-06-24 01:33:06 -05:00
{
2019-08-30 02:13:10 -05:00
/** @var NoBudgetRepositoryInterface */
private $noBudgetRepository;
2019-08-30 01:19:55 -05:00
/** @var OperationsRepositoryInterface */
private $opsRepository;
/** @var BudgetRepositoryInterface */
private $repository;
2018-06-24 01:33:06 -05:00
/**
* CurrencyTransformer constructor.
*
* @codeCoverageIgnore
*/
2018-12-16 06:55:19 -06:00
public function __construct()
2018-06-24 01:33:06 -05:00
{
2019-08-30 02:13:10 -05:00
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noBudgetRepository = app(NoBudgetRepositoryInterface::class);
if ('testing' === config('app.env')) {
2019-06-07 11:20:15 -05:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
2018-06-24 01:33:06 -05:00
}
/**
* Transform the note.
*
* @param AvailableBudget $availableBudget
*
* @return array
*/
public function transform(AvailableBudget $availableBudget): array
{
$this->repository->setUser($availableBudget->user);
2018-12-12 13:30:25 -06:00
$currency = $availableBudget->transactionCurrency;
$data = [
2018-12-18 23:06:01 -06:00
'id' => (int)$availableBudget->id,
'created_at' => $availableBudget->created_at->toAtomString(),
'updated_at' => $availableBudget->updated_at->toAtomString(),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($availableBudget->amount, $currency->decimal_places),
'start' => $availableBudget->start_date->format('Y-m-d'),
'end' => $availableBudget->end_date->format('Y-m-d'),
'spent_in_budgets' => [],
'spent_no_budget' => [],
'links' => [
2018-06-24 01:33:06 -05:00
[
'rel' => 'self',
'uri' => '/available_budgets/' . $availableBudget->id,
],
],
];
$start = $this->parameters->get('start');
$end = $this->parameters->get('end');
if (null !== $start && null !== $end) {
$data['spent_in_budgets'] = $this->getSpentInBudgets();
$data['spent_no_budget'] = $this->spentOutsideBudgets();
}
2018-06-24 01:33:06 -05:00
return $data;
}
/**
* @return array
*/
private function getSpentInBudgets(): array
{
$allActive = $this->repository->getActiveBudgets();
2019-08-30 01:19:55 -05:00
return $this->opsRepository->spentInPeriodMc(
$allActive, new Collection, $this->parameters->get('start'), $this->parameters->get('end')
);
}
/**
* @return array
*/
private function spentOutsideBudgets(): array
{
2019-08-30 02:13:10 -05:00
return $this->noBudgetRepository->spentInPeriodWoBudgetMc(new Collection, $this->parameters->get('start'), $this->parameters->get('end'));
}
}