2018-02-11 13:45:33 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PiggyBankEventTransformer.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Transformers;
|
|
|
|
|
|
|
|
|
2018-02-18 03:31:15 -06:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2018-02-11 13:45:33 -06:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2018-02-17 03:47:06 -06:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use League\Fractal\Resource\Item;
|
2018-02-11 13:45:33 -06:00
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankEventTransformer
|
|
|
|
*/
|
|
|
|
class PiggyBankEventTransformer extends TransformerAbstract
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of resources possible to include
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-02-17 03:47:06 -06:00
|
|
|
protected $availableIncludes = ['piggy_bank', 'transaction'];
|
2018-02-11 13:45:33 -06:00
|
|
|
/**
|
|
|
|
* List of resources to automatically include
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultIncludes = [];
|
|
|
|
|
|
|
|
/** @var ParameterBag */
|
|
|
|
protected $parameters;
|
|
|
|
|
|
|
|
/**
|
2018-02-17 03:47:06 -06:00
|
|
|
* PiggyBankEventTransformer constructor.
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2018-02-11 13:45:33 -06:00
|
|
|
*
|
|
|
|
* @param ParameterBag $parameters
|
|
|
|
*/
|
|
|
|
public function __construct(ParameterBag $parameters)
|
|
|
|
{
|
|
|
|
$this->parameters = $parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-17 03:47:06 -06:00
|
|
|
* Include piggy bank into end result.
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
|
|
|
* @param PiggyBankEvent $event
|
|
|
|
*
|
|
|
|
* @return Item
|
|
|
|
*/
|
|
|
|
public function includePiggyBank(PiggyBankEvent $event): Item
|
|
|
|
{
|
|
|
|
return $this->item($event->piggyBank, new PiggyBankTransformer($this->parameters), 'piggy_banks');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include transaction into end result.
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
|
|
|
* @param PiggyBankEvent $event
|
|
|
|
*
|
|
|
|
* @return Item
|
|
|
|
*/
|
|
|
|
public function includeTransaction(PiggyBankEvent $event): Item
|
|
|
|
{
|
|
|
|
$journal = $event->transactionJournal;
|
2018-04-02 07:50:17 -05:00
|
|
|
$pageSize = (int)app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data;
|
2018-02-17 03:47:06 -06:00
|
|
|
|
|
|
|
// journals always use collector and limited using URL parameters.
|
2018-02-18 03:31:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2018-02-17 03:47:06 -06:00
|
|
|
$collector->setUser($journal->user);
|
|
|
|
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
|
|
|
|
$collector->setAllAssetAccounts();
|
|
|
|
$collector->setJournals(new Collection([$journal]));
|
2018-04-02 07:50:17 -05:00
|
|
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
2018-02-17 03:47:06 -06:00
|
|
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
|
|
|
}
|
|
|
|
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
|
|
|
$journals = $collector->getJournals();
|
|
|
|
|
|
|
|
return $this->item($journals->first(), new TransactionTransformer($this->parameters), 'transactions');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert piggy bank event.
|
|
|
|
*
|
2018-02-11 13:45:33 -06:00
|
|
|
* @param PiggyBankEvent $event
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(PiggyBankEvent $event): array
|
|
|
|
{
|
2018-02-16 09:44:52 -06:00
|
|
|
$account = $event->piggyBank->account;
|
2018-04-02 07:50:17 -05:00
|
|
|
$currencyId = (int)$account->getMeta('currency_id');
|
2018-02-16 09:44:52 -06:00
|
|
|
$decimalPlaces = 2;
|
|
|
|
if ($currencyId > 0) {
|
2018-02-17 03:47:06 -06:00
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$repository->setUser($account->user);
|
|
|
|
$currency = $repository->findNull($currencyId);
|
2018-02-16 09:44:52 -06:00
|
|
|
$decimalPlaces = $currency->decimal_places;
|
|
|
|
}
|
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
$data = [
|
|
|
|
'id' => (int)$event->id,
|
|
|
|
'updated_at' => $event->updated_at->toAtomString(),
|
|
|
|
'created_at' => $event->created_at->toAtomString(),
|
2018-02-16 09:44:52 -06:00
|
|
|
'amount' => round($event->amount, $decimalPlaces),
|
2018-02-11 13:45:33 -06:00
|
|
|
'links' => [
|
|
|
|
[
|
|
|
|
'rel' => 'self',
|
|
|
|
'uri' => '/piggy_bank_events/' . $event->id,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|