2018-02-11 13:45:33 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PiggyBankTransformer.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;
|
2020-06-07 04:31:01 -05:00
|
|
|
use FireflyIII\Models\ObjectGroup;
|
2018-02-11 13:45:33 -06:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
2018-04-21 16:48:54 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2018-02-17 03:47:06 -06:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
2022-12-29 12:42:40 -06:00
|
|
|
use JsonException;
|
2018-02-11 13:45:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankTransformer
|
|
|
|
*/
|
2018-12-15 00:59:02 -06:00
|
|
|
class PiggyBankTransformer extends AbstractTransformer
|
2018-02-11 13:45:33 -06:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
private AccountRepositoryInterface $accountRepos;
|
|
|
|
private CurrencyRepositoryInterface $currencyRepos;
|
2021-03-06 05:45:49 -06:00
|
|
|
private PiggyBankRepositoryInterface $piggyRepos;
|
2018-12-19 22:46:05 -06:00
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
/**
|
2018-02-17 03:47:06 -06:00
|
|
|
* PiggyBankTransformer constructor.
|
|
|
|
*
|
2023-02-12 00:15:06 -06:00
|
|
|
|
2018-02-11 13:45:33 -06:00
|
|
|
*/
|
2018-12-15 00:59:02 -06:00
|
|
|
public function __construct()
|
2018-02-11 13:45:33 -06:00
|
|
|
{
|
2018-12-19 22:46:05 -06:00
|
|
|
$this->accountRepos = app(AccountRepositoryInterface::class);
|
|
|
|
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
|
2018-02-11 13:45:33 -06:00
|
|
|
}
|
2021-09-18 03:26:12 -05:00
|
|
|
|
2018-02-17 03:47:06 -06:00
|
|
|
/**
|
|
|
|
* Transform the piggy bank.
|
|
|
|
*
|
2022-12-27 14:13:18 -06:00
|
|
|
* @param PiggyBank $piggyBank
|
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(PiggyBank $piggyBank): array
|
|
|
|
{
|
2018-04-21 16:48:54 -05:00
|
|
|
$account = $piggyBank->account;
|
|
|
|
|
2018-12-19 22:46:05 -06:00
|
|
|
// set up repositories
|
|
|
|
$this->accountRepos->setUser($account->user);
|
|
|
|
$this->currencyRepos->setUser($account->user);
|
|
|
|
$this->piggyRepos->setUser($account->user);
|
|
|
|
|
|
|
|
// get currency from account, or use default.
|
2020-01-07 22:48:45 -06:00
|
|
|
$currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
|
2018-02-17 03:47:06 -06:00
|
|
|
|
2018-12-19 22:46:05 -06:00
|
|
|
// note
|
|
|
|
$notes = $this->piggyRepos->getNoteText($piggyBank);
|
|
|
|
$notes = '' === $notes ? null : $notes;
|
2018-04-21 16:48:54 -05:00
|
|
|
|
2020-06-07 04:31:01 -05:00
|
|
|
$objectGroupId = null;
|
2020-06-20 09:28:23 -05:00
|
|
|
$objectGroupOrder = null;
|
2020-06-07 04:31:01 -05:00
|
|
|
$objectGroupTitle = null;
|
|
|
|
/** @var ObjectGroup $objectGroup */
|
|
|
|
$objectGroup = $piggyBank->objectGroups->first();
|
|
|
|
if (null !== $objectGroup) {
|
2022-12-27 14:13:18 -06:00
|
|
|
$objectGroupId = (int)$objectGroup->id;
|
|
|
|
$objectGroupOrder = (int)$objectGroup->order;
|
2020-06-07 04:31:01 -05:00
|
|
|
$objectGroupTitle = $objectGroup->title;
|
|
|
|
}
|
|
|
|
|
2018-02-17 03:47:06 -06:00
|
|
|
// get currently saved amount:
|
2022-12-29 04:29:00 -06:00
|
|
|
$currentAmount = app('steam')->bcround($this->piggyRepos->getCurrentAmount($piggyBank), $currency->decimal_places);
|
2018-02-11 13:45:33 -06:00
|
|
|
|
2022-03-27 11:30:46 -05:00
|
|
|
// Amounts, depending on 0.0 state of target amount
|
2022-12-29 04:29:00 -06:00
|
|
|
$percentage = null;
|
|
|
|
$targetAmount = $piggyBank->targetamount;
|
|
|
|
$leftToSave = null;
|
|
|
|
$savePerMonth = null;
|
|
|
|
if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
|
|
|
|
$leftToSave = bcsub($piggyBank->targetamount, $currentAmount);
|
|
|
|
$percentage = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100');
|
|
|
|
$targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places);
|
|
|
|
$leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places);
|
|
|
|
$savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
|
2022-03-27 11:30:46 -05:00
|
|
|
}
|
2021-09-18 03:20:19 -05:00
|
|
|
$startDate = $piggyBank->startdate?->toAtomString();
|
|
|
|
$targetDate = $piggyBank->targetdate?->toAtomString();
|
2018-04-21 16:48:54 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
|
2020-10-23 12:11:25 -05:00
|
|
|
return [
|
2022-12-27 14:13:18 -06:00
|
|
|
'id' => (string)$piggyBank->id,
|
2018-12-19 22:46:05 -06:00
|
|
|
'created_at' => $piggyBank->created_at->toAtomString(),
|
|
|
|
'updated_at' => $piggyBank->updated_at->toAtomString(),
|
2022-12-27 14:13:18 -06:00
|
|
|
'account_id' => (string)$piggyBank->account_id,
|
2019-01-26 05:11:23 -06:00
|
|
|
'account_name' => $piggyBank->account->name,
|
2018-12-19 22:46:05 -06:00
|
|
|
'name' => $piggyBank->name,
|
2022-12-27 14:13:18 -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-27 14:13:18 -06:00
|
|
|
'currency_decimal_places' => (int)$currency->decimal_places,
|
2022-12-29 04:29:00 -06:00
|
|
|
'target_amount' => $targetAmount,
|
2018-12-19 22:46:05 -06:00
|
|
|
'percentage' => $percentage,
|
|
|
|
'current_amount' => $currentAmount,
|
2022-12-29 04:29:00 -06:00
|
|
|
'left_to_save' => $leftToSave,
|
2022-03-27 11:30:46 -05:00
|
|
|
'save_per_month' => $savePerMonth,
|
2018-12-19 22:46:05 -06:00
|
|
|
'start_date' => $startDate,
|
|
|
|
'target_date' => $targetDate,
|
2022-12-27 14:13:18 -06:00
|
|
|
'order' => (int)$piggyBank->order,
|
2018-12-19 22:46:05 -06:00
|
|
|
'active' => true,
|
|
|
|
'notes' => $notes,
|
2022-12-27 14:13:18 -06:00
|
|
|
'object_group_id' => $objectGroupId ? (string)$objectGroupId : null,
|
2020-06-07 04:31:01 -05:00
|
|
|
'object_group_order' => $objectGroupOrder,
|
|
|
|
'object_group_title' => $objectGroupTitle,
|
2018-12-19 22:46:05 -06:00
|
|
|
'links' => [
|
2018-02-11 13:45:33 -06:00
|
|
|
[
|
|
|
|
'rel' => 'self',
|
2022-12-27 14:13:18 -06:00
|
|
|
'uri' => '/piggy_banks/'.$piggyBank->id,
|
2018-02-11 13:45:33 -06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|