2018-02-11 13:45:33 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PiggyBankEventTransformer.php
|
2020-02-16 06:57:18 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-11 13:45:33 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-11 13:45:33 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -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-02-11 13:45:33 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-11 13:45:33 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-11 13:45:33 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -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-02-11 13:45:33 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Transformers;
|
2021-09-18 03:26:12 -05:00
|
|
|
|
2022-12-29 12:42:40 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-02-11 13:45:33 -06:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2018-07-22 09:35:46 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2018-02-17 03:47:06 -06:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2018-12-19 22:46:05 -06:00
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
2022-12-29 12:42:40 -06:00
|
|
|
use JsonException;
|
2018-02-11 13:45:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankEventTransformer
|
|
|
|
*/
|
2018-12-15 15:03:42 -06:00
|
|
|
class PiggyBankEventTransformer extends AbstractTransformer
|
2018-02-11 13:45:33 -06:00
|
|
|
{
|
2021-03-06 05:45:49 -06:00
|
|
|
private CurrencyRepositoryInterface $currencyRepos;
|
|
|
|
private PiggyBankRepositoryInterface $piggyRepos;
|
|
|
|
private AccountRepositoryInterface $repository;
|
2018-12-19 22:46:05 -06:00
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
/**
|
2018-02-17 03:47:06 -06:00
|
|
|
* PiggyBankEventTransformer constructor.
|
|
|
|
*
|
2023-02-12 00:15:06 -06:00
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
*/
|
2018-12-15 15:03:42 -06:00
|
|
|
public function __construct()
|
2018-02-11 13:45:33 -06:00
|
|
|
{
|
2018-12-19 22:46:05 -06:00
|
|
|
$this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
|
2018-02-11 13:45:33 -06:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:47:06 -06:00
|
|
|
/**
|
|
|
|
* Convert piggy bank event.
|
|
|
|
*
|
2022-12-29 12:42:40 -06:00
|
|
|
* @param PiggyBankEvent $event
|
2018-02-11 13:45:33 -06:00
|
|
|
*
|
|
|
|
* @return array
|
2022-12-29 12:42:40 -06:00
|
|
|
* @throws FireflyException
|
|
|
|
* @throws JsonException
|
2018-02-11 13:45:33 -06:00
|
|
|
*/
|
|
|
|
public function transform(PiggyBankEvent $event): array
|
|
|
|
{
|
2018-12-19 22:46:05 -06:00
|
|
|
// get account linked to piggy bank
|
2018-07-22 09:35:46 -05:00
|
|
|
$account = $event->piggyBank->account;
|
|
|
|
|
2018-12-19 22:46:05 -06:00
|
|
|
// set up repositories.
|
|
|
|
$this->repository->setUser($account->user);
|
|
|
|
$this->currencyRepos->setUser($account->user);
|
|
|
|
$this->piggyRepos->setUser($account->user);
|
|
|
|
|
|
|
|
// get associated currency or fall back to the default:
|
2020-07-12 10:32:48 -05:00
|
|
|
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
|
2018-12-19 22:46:05 -06:00
|
|
|
|
|
|
|
// get associated journal and transaction, if any:
|
2020-07-12 10:54:51 -05:00
|
|
|
$journalId = $event->transaction_journal_id;
|
2019-09-22 15:10:39 -05:00
|
|
|
$groupId = null;
|
2022-12-29 12:42:40 -06:00
|
|
|
if (0 !== (int)$journalId) {
|
|
|
|
$groupId = (int)$event->transactionJournal->transaction_group_id;
|
|
|
|
$journalId = (int)$journalId;
|
2019-09-22 15:10:39 -05:00
|
|
|
}
|
2021-03-06 05:45:49 -06:00
|
|
|
|
2020-10-23 12:11:25 -05:00
|
|
|
return [
|
2022-12-29 12:42:40 -06:00
|
|
|
'id' => (string)$event->id,
|
2018-12-19 22:46:05 -06:00
|
|
|
'created_at' => $event->created_at->toAtomString(),
|
|
|
|
'updated_at' => $event->updated_at->toAtomString(),
|
2022-12-27 14:13:18 -06:00
|
|
|
'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
|
2022-12-29 12:42:40 -06:00
|
|
|
'currency_id' => (string)$currency->id,
|
2018-12-19 22:46:05 -06:00
|
|
|
'currency_code' => $currency->code,
|
|
|
|
'currency_symbol' => $currency->symbol,
|
2022-12-29 12:42:40 -06:00
|
|
|
'currency_decimal_places' => (int)$currency->decimal_places,
|
|
|
|
'transaction_journal_id' => $journalId ? (string)$journalId : null,
|
|
|
|
'transaction_group_id' => $groupId ? (string)$groupId : null,
|
2018-12-19 22:46:05 -06:00
|
|
|
'links' => [
|
2018-02-11 13:45:33 -06:00
|
|
|
[
|
|
|
|
'rel' => 'self',
|
2022-12-29 12:42:40 -06:00
|
|
|
'uri' => '/piggy_bank_events/'.$event->id,
|
2018-02-11 13:45:33 -06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|